Skip to content

Commit

Permalink
Merge pull request #527 from DomCR/dwgWriter-xrecord-config
Browse files Browse the repository at this point in the history
Dwg writer xdata fix
  • Loading branch information
DomCR authored Jan 13, 2025
2 parents eafaf7e + 06b8a51 commit 639b4e7
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 60 deletions.
6 changes: 6 additions & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,12 @@ public void XData()
records.Add(new ExtendedDataInteger32(33));
records.Add(new ExtendedDataString("my extended data string"));
records.Add(new ExtendedDataHandle(5));
records.Add(new ExtendedDataReal(25.35));
records.Add(new ExtendedDataScale(0.66));
records.Add(new ExtendedDataDistance(481.48));
records.Add(new ExtendedDataDirection(new XYZ(4, 3, 2)));
records.Add(new ExtendedDataCoordinate(new XYZ(8, 7, 4)));
records.Add(new ExtendedDataWorldCoordinate(new XYZ(85, 74, 47)));
records.Add(new ExtendedDataLayer(layer.Handle));
records.Add(new ExtendedDataBinaryChunk(new byte[] { 1, 2, 3, 4 }));
records.Add(new ExtendedDataControlString(true));
Expand Down
6 changes: 6 additions & 0 deletions src/ACadSharp/Header/CadHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3287,5 +3287,11 @@ public Dictionary<DxfCode, object> GetValues(string systemvar)

return value;
}

/// <inheritdoc/>
public override string ToString()
{
return $"{this.Version}";
}
}
}
8 changes: 8 additions & 0 deletions src/ACadSharp/IO/CadWriterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ public class CadWriterConfiguration
/// default: false
/// </value>
public bool WriteXRecords { get; set; } = false;

/// <summary>
/// Will not ignore the <see cref="ACadSharp.XData.ExtendedData"/> collection in the <see cref="CadObject"/>.
/// </summary>
/// <value>
/// default: true
/// </value>
public bool WriteXData { get; set; } = true;
}
}
12 changes: 9 additions & 3 deletions src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,13 @@ private void writeEntityMode(Entity entity)

private void writeExtendedData(ExtendedDataDictionary data)
{
//EED size BS size of extended entity data, if any
foreach (var item in data.Entries)
if (this.WriteXData)
{
writeExtendedDataEntry(item.Key, item.Value);
//EED size BS size of extended entity data, if any
foreach (var item in data.Entries)
{
writeExtendedDataEntry(item.Key, item.Value);
}
}

this._writer.WriteBitShort(0);
Expand Down Expand Up @@ -364,6 +367,9 @@ private void writeExtendedDataEntry(AppId app, ExtendedData entry)
case ExtendedDataInteger32 s32:
mstream.Write(LittleEndianConverter.Instance.GetBytes(s32.Value), 0, 4);
break;
case ExtendedDataReal real:
mstream.Write(LittleEndianConverter.Instance.GetBytes(real.Value), 0, 8);
break;
case ExtendedDataScale scale:
mstream.Write(LittleEndianConverter.Instance.GetBytes(scale.Value), 0, 8);
break;
Expand Down
5 changes: 4 additions & 1 deletion src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal partial class DwgObjectWriter : DwgSectionIO

public bool WriteXRecords { get; }

public bool WriteXData { get; }

private Dictionary<ulong, CadDictionary> _dictionaries = new();

private Queue<CadObject> _objects = new();
Expand All @@ -40,14 +42,15 @@ internal partial class DwgObjectWriter : DwgSectionIO

private Entity _next;

public DwgObjectWriter(Stream stream, CadDocument document, Encoding encoding, bool writeXRecords = true) : base(document.Header.Version)
public DwgObjectWriter(Stream stream, CadDocument document, Encoding encoding, bool writeXRecords = true, bool writeXData = true) : base(document.Header.Version)
{
this._stream = stream;
this._document = document;

this._msmain = new MemoryStream();
this._writer = DwgStreamWriterBase.GetMergedWriter(document.Header.Version, this._msmain, encoding);
this.WriteXRecords = writeXRecords;
this.WriteXData = writeXData;
}

public void Write()
Expand Down
11 changes: 8 additions & 3 deletions src/ACadSharp/IO/DWG/DwgWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ACadSharp.IO
/// <summary>
/// Class for writing a DWG from a <see cref="CadDocument"/>.
/// </summary>
public class DwgWriter : CadWriterBase<CadWriterConfiguration>
public class DwgWriter : CadWriterBase<DwgWriterConfiguration>
{
private ACadVersion _version { get { return this._document.Header.Version; } }

Expand Down Expand Up @@ -91,7 +91,7 @@ public override void Dispose()
/// <param name="document"></param>
/// <param name="configuration"></param>
/// <param name="notification"></param>
public static void Write(string filename, CadDocument document, CadWriterConfiguration configuration = null, NotificationEventHandler notification = null)
public static void Write(string filename, CadDocument document, DwgWriterConfiguration configuration = null, NotificationEventHandler notification = null)
{
using (DwgWriter writer = new DwgWriter(filename, document))
{
Expand Down Expand Up @@ -291,7 +291,12 @@ private void writeRevHistory()
private void writeObjects()
{
MemoryStream stream = new MemoryStream();
DwgObjectWriter writer = new DwgObjectWriter(stream, this._document, this._encoding, this.Configuration.WriteXRecords);
DwgObjectWriter writer = new DwgObjectWriter(
stream,
this._document,
this._encoding,
this.Configuration.WriteXRecords,
this.Configuration.WriteXData);
writer.OnNotification += this.triggerNotification;
writer.Write();

Expand Down
6 changes: 6 additions & 0 deletions src/ACadSharp/IO/DWG/DwgWriterConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ACadSharp.IO.DWG
{
public class DwgWriterConfiguration : CadWriterConfiguration
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal class DxfBlocksSectionWriter : DxfSectionWriterBase
{
public override string SectionName { get { return DxfFileToken.BlocksSection; } }

public DxfBlocksSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder objectHolder) : base(writer, document, objectHolder) { }
public DxfBlocksSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder objectHolder, DxfWriterConfiguration configuration)
: base(writer, document, objectHolder, configuration) { }

protected override void writeSection()
{
Expand Down Expand Up @@ -41,7 +42,7 @@ private void writeBlock(Block block)
this._writer.Write(70, (short)block.Flags, map);

this._writer.Write(10, block.BasePoint, map);

this._writer.Write(3, block.Name, map);
this._writer.Write(4, block.Comments, map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ internal class DxfClassesSectionWriter : DxfSectionWriterBase
{
public override string SectionName { get { return DxfFileToken.ClassesSection; } }

public DxfClassesSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder) : base(writer, document, holder)
{
}
public DxfClassesSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder objectHolder, DxfWriterConfiguration configuration)
: base(writer, document, objectHolder, configuration) { }

protected override void writeSection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ internal class DxfEntitiesSectionWriter : DxfSectionWriterBase
{
public override string SectionName { get { return DxfFileToken.EntitiesSection; } }

public DxfEntitiesSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder) : base(writer, document, holder)
{
}
public DxfEntitiesSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder objectHolder, DxfWriterConfiguration configuration)
: base(writer, document, objectHolder, configuration) { }

protected override void writeSection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ internal class DxfHeaderSectionWriter : DxfSectionWriterBase

public CadHeader Header { get { return this._document.Header; } }

public DxfWriterConfiguration Options { get; }

public DxfHeaderSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder, DxfWriterConfiguration options) : base(writer, document, holder)
public DxfHeaderSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder, DxfWriterConfiguration configuration)
: base(writer, document, holder, configuration)
{
this.Options = options;
}

protected override void writeSection()
Expand All @@ -23,7 +21,7 @@ protected override void writeSection()

foreach (KeyValuePair<string, CadSystemVariable> item in map)
{
if (!this.Options.WriteAllHeaderVariables && !this.Options.HeaderVariables.Contains(item.Key))
if (!this.Configuration.WriteAllHeaderVariables && !this.Configuration.HeaderVariables.Contains(item.Key))
continue;

if (item.Value.ReferenceType.HasFlag(DxfReferenceType.Ignored))
Expand Down
11 changes: 5 additions & 6 deletions src/ACadSharp/IO/DXF/DxfStreamWriter/DxfObjectsSectionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ internal class DxfObjectsSectionWriter : DxfSectionWriterBase
{
public override string SectionName { get { return DxfFileToken.ObjectsSection; } }

public bool WriteXRecords { get; set; } = false;

public DxfObjectsSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder) : base(writer, document, holder)
public DxfObjectsSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder, DxfWriterConfiguration configuration)
: base(writer, document, holder, configuration)
{
}

Expand Down Expand Up @@ -45,7 +44,7 @@ protected void writeObject<T>(T co)
}


if (co is XRecord && !this.WriteXRecords)
if (co is XRecord && !this.Configuration.WriteXRecords)
{
return;
}
Expand Down Expand Up @@ -96,7 +95,7 @@ protected void writeObject<T>(T co)
throw new NotImplementedException($"Object not implemented : {co.GetType().FullName}");
}

this.writeExtendedData(co);
this.writeExtendedData(co.ExtendedData);
}

protected void writeBookColor(BookColor color)
Expand All @@ -117,7 +116,7 @@ protected void writeDictionary(CadDictionary e)

foreach (NonGraphicalObject item in e)
{
if (item is XRecord && !this.WriteXRecords)
if (item is XRecord && !this.Configuration.WriteXRecords)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected void writeEntity<T>(T entity)
throw new NotImplementedException($"Entity not implemented {entity.GetType().FullName}");
}

this.writeExtendedData(entity);
this.writeExtendedData(entity.ExtendedData);
}

private void writeArc(Arc arc)
Expand Down
74 changes: 70 additions & 4 deletions src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using ACadSharp.Entities;
using ACadSharp.Tables;
using ACadSharp.XData;
using CSMath;
using CSUtilities.Converters;
using System;
using System.Drawing;
using System.IO;
using System.Text;

namespace ACadSharp.IO.DXF
{
Expand All @@ -16,17 +20,21 @@ internal abstract partial class DxfSectionWriterBase

public CadObjectHolder Holder { get; }

public DxfWriterConfiguration Configuration { get; }

protected IDxfStreamWriter _writer;
protected CadDocument _document;

public DxfSectionWriterBase(
IDxfStreamWriter writer,
CadDocument document,
CadObjectHolder holder)
CadObjectHolder holder,
DxfWriterConfiguration configuration)
{
this._writer = writer;
this._document = document;
this.Holder = holder;
this.Configuration = configuration;
}

public void Write()
Expand Down Expand Up @@ -72,17 +80,75 @@ protected void writeCommonObjectData(CadObject cadObject)

this._writer.Write(DxfCode.SoftPointerId, cadObject.Owner.Handle);

//TODO: Write exended data
if (cadObject.ExtendedData != null)
if (cadObject.ExtendedData != null && this.Configuration.WriteXData)
{
//this._writer.Write(DxfCode.ControlString,DxfFileToken.ReactorsToken);
//this._writer.Write(DxfCode.HardOwnershipId, cadObject.ExtendedData);
//this._writer.Write(DxfCode.ControlString, "}");
}
}

protected void writeExtendedData(CadObject cadObject)
protected void writeExtendedData(ExtendedDataDictionary xdata)
{
if (xdata == null || !this.Configuration.WriteXData)
{
return;
}

foreach (var entry in xdata.Entries)
{
this._writer.Write(DxfCode.ExtendedDataRegAppName, entry.Key.Name);

foreach (ExtendedDataRecord record in entry.Value.Records)
{
switch (record)
{
case ExtendedDataBinaryChunk binaryChunk:
this._writer.Write(binaryChunk.Code, binaryChunk.Value);
break;
case ExtendedDataControlString control:
this._writer.Write(control.Code, control.Value);
break;
case ExtendedDataInteger16 s16:
this._writer.Write(s16.Code, s16.Value);
break;
case ExtendedDataInteger32 s32:
this._writer.Write(s32.Code, s32.Value);
break;
case ExtendedDataReal real:
this._writer.Write(real.Code, real.Value);
break;
case ExtendedDataScale scale:
this._writer.Write(scale.Code, scale.Value);
break;
case ExtendedDataDistance dist:
this._writer.Write(dist.Code, dist.Value);
break;
case ExtendedDataDirection dir:
this._writer.Write(dir.Code, (IVector)dir.Value);
break;
case ExtendedDataCoordinate coord:
this._writer.Write(coord.Code, (IVector)coord.Value);
break;
case ExtendedDataWorldCoordinate wcoord:
this._writer.Write(wcoord.Code, (IVector)wcoord.Value);
break;
case IExtendedDataHandleReference handle:
ulong h = handle.Value;
if (handle.ResolveReference(this._document) == null)
{
h = 0;
}
this._writer.Write(DxfCode.ExtendedDataHandle, h);
break;
case ExtendedDataString str:
this._writer.Write(str.Code, str.Value);
break;
default:
throw new System.NotSupportedException($"ExtendedDataRecord of type {record.GetType().FullName} not supported.");
}
}
}
}

protected void writeCommonEntityData(Entity entity)
Expand Down
Loading

0 comments on commit 639b4e7

Please sign in to comment.