Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected virtual void writeDwgFile(SingleCaseGenerator data, ACadVersion versio
string path = this.getPath(data.Name, "dwg", version);

data.Document.Header.Version = version;
DwgWriter.Write(path, data.Document, this.onNotification);
DwgWriter.Write(path, data.Document, notification: this.onNotification);
}
}
}
13 changes: 7 additions & 6 deletions ACadSharp/IO/CadWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

namespace ACadSharp.IO
{
public abstract class CadWriterBase : ICadWriter
/// <summary>
/// Base class for the CAD writers.
/// </summary>
public abstract class CadWriterBase<T> : ICadWriter
where T : CadWriterConfiguration, new()
{
/// <summary>
/// Notification event to get information about the writing process.
Expand All @@ -17,12 +21,9 @@ public abstract class CadWriterBase : ICadWriter
public event NotificationEventHandler OnNotification;

/// <summary>
/// Notifies the writer to close the stream once the operation is completed.
/// Configuration for the writer.
/// </summary>
/// <value>
/// default: true
/// </value>
public bool CloseStream { get; set; } = true;
public T Configuration { get; set; } = new T();

protected Stream _stream;

Expand Down
27 changes: 27 additions & 0 deletions ACadSharp/IO/CadWriterConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ACadSharp.IO
{
/// <summary>
/// Configuration for the <see cref="CadWriterBase{T}"/> class.
/// </summary>
public class CadWriterConfiguration
{
/// <summary>
/// The writer will close the stream once the operation is completed.
/// </summary>
/// <value>
/// default: true
/// </value>
public bool CloseStream { get; set; } = true;

/// <summary>
/// Will not ignore the <see cref="ACadSharp.Objects.XRecord"/> objects in the document.
/// </summary>
/// <remarks>
/// Due the complexity of XRecords, if this flag is set to true, it may cause a corruption of the file if the records have been modified manually.
/// </remarks>
/// <value>
/// default: false
/// </value>
public bool WriteXRecords { get; set; } = false;
}
}
3 changes: 3 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgMergedStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using CSMath;
using System;
using System.IO;
using System.Text;

namespace ACadSharp.IO.DWG
{
internal class DwgMergedStreamWriter : IDwgStreamWriter
{
public Encoding Encoding { get { return this.Main.Encoding; } }

public IDwgStreamWriter Main { get; }

public IDwgStreamWriter TextWriter { get; }
Expand Down
96 changes: 65 additions & 31 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Objects.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ACadSharp.Objects;
using CSMath;
using CSUtilities.Converters;
using CSUtilities.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand All @@ -29,11 +29,15 @@ private void writeObject(CadObject obj)
case MultiLeaderStyle:
case SortEntitiesTable:
case VisualStyle:
case XRecord:
this.notify($"Object type not implemented {obj.GetType().FullName}", NotificationType.NotImplemented);
return;
}

if (obj is XRecord && !this.WriteXRecords)
{
return;
}

this.writeCommonNonEntityData(obj);

switch (obj)
Expand Down Expand Up @@ -121,14 +125,15 @@ private void writeDictionary(CadDictionary dictionary)
}

//Common:
foreach (var name in dictionary.EntryNames)
foreach (var item in dictionary)
{
this._writer.WriteVariableText(name);
}
if (item is XRecord && !this.WriteXRecords)
{
return;
}

foreach (var handle in dictionary.EntryHandles)
{
this._writer.HandleReference(DwgReferenceType.SoftOwnership, handle);
this._writer.WriteVariableText(item.Name);
this._writer.HandleReference(DwgReferenceType.SoftOwnership, item.Handle);
}

this.addEntriesToWriter(dictionary);
Expand Down Expand Up @@ -555,7 +560,7 @@ private void writeSortEntitiesTable(SortEntitiesTable sortEntitiesTable)
{
//parenthandle (soft pointer)
this._writer.HandleReference(DwgReferenceType.SoftPointer, sortEntitiesTable.BlockOwner);

//Common:
//Numentries BL number of entries
this._writer.WriteBitLong(sortEntitiesTable.Sorters.Count());
Expand Down Expand Up @@ -590,44 +595,73 @@ private void writeXRecord(XRecord xrecord)

switch (groupValueType)
{
case GroupCodeValueType.None:
break;
case GroupCodeValueType.String:
break;
case GroupCodeValueType.Point3D:
break;
case GroupCodeValueType.Double:
case GroupCodeValueType.Byte:
case GroupCodeValueType.Bool:
ms.Write(Convert.ToByte(entry.Value, System.Globalization.CultureInfo.InvariantCulture));
break;
case GroupCodeValueType.Int16:
case GroupCodeValueType.ExtendedDataInt16:
ms.Write(Convert.ToInt16(entry.Value, System.Globalization.CultureInfo.InvariantCulture));
break;
case GroupCodeValueType.Int32:
case GroupCodeValueType.ExtendedDataInt32:
ms.Write(Convert.ToInt32(entry.Value, System.Globalization.CultureInfo.InvariantCulture));
break;
case GroupCodeValueType.Int64:
ms.Write(Convert.ToInt64(entry.Value, System.Globalization.CultureInfo.InvariantCulture));
break;
case GroupCodeValueType.Handle:
break;
case GroupCodeValueType.ObjectId:
case GroupCodeValueType.Double:
case GroupCodeValueType.ExtendedDataDouble:
double d = (entry.Value as double?).Value;
ms.Write<double, LittleEndianConverter>(d);
break;
case GroupCodeValueType.Bool:
case GroupCodeValueType.Point3D:
XYZ xyz = (entry.Value as XYZ?).Value;
ms.Write<double, LittleEndianConverter>(xyz.X);
ms.Write<double, LittleEndianConverter>(xyz.Y);
ms.Write<double, LittleEndianConverter>(xyz.Z);
break;
case GroupCodeValueType.Chunk:
case GroupCodeValueType.ExtendedDataChunk:
byte[] array = (byte[])entry.Value;
ms.Write((byte)array.Length);
ms.WriteBytes(array);
break;
case GroupCodeValueType.Comment:
break;
case GroupCodeValueType.String:
case GroupCodeValueType.ExtendedDataString:
case GroupCodeValueType.Handle:
string text = (string)entry.Value;

if (this.R2007Plus)
{
if (string.IsNullOrEmpty(text))
{
ms.Write<short, LittleEndianConverter>(0);
return;
}

ms.Write<short, LittleEndianConverter>((short)text.Length);
ms.Write(text, System.Text.Encoding.Unicode);
}
else if (string.IsNullOrEmpty(text))
{
ms.Write<short, LittleEndianConverter>(0);
ms.Write((byte)this._writer.Encoding.CodePage);
}
else
{
ms.Write<short, LittleEndianConverter>((short)text.Length);
ms.Write((byte)this._writer.Encoding.CodePage);
ms.Write(text, this._writer.Encoding);
}
break;
case GroupCodeValueType.ExtendedDataChunk:
break;
case GroupCodeValueType.ObjectId:
case GroupCodeValueType.ExtendedDataHandle:
break;
case GroupCodeValueType.ExtendedDataDouble:
break;
case GroupCodeValueType.ExtendedDataInt16:
break;
case GroupCodeValueType.ExtendedDataInt32:
ulong u = (entry.Value as ulong?).Value;
ms.Write<ulong, LittleEndianConverter>(u);
break;
default:
break;
throw new NotSupportedException();
}
}

Expand Down
5 changes: 4 additions & 1 deletion ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal partial class DwgObjectWriter : DwgSectionIO
/// </summary>
public Dictionary<ulong, long> Map { get; } = new Dictionary<ulong, long>();

public bool WriteXRecords { get; }

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

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

private Entity _next;

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

this._msmain = new MemoryStream();
this._writer = DwgStreamWriterBase.GetMergedWriter(document.Header.Version, this._msmain, TextEncoding.Windows1252());
this.WriteXRecords = writeXRecords;
}

public void Write()
Expand Down
2 changes: 0 additions & 2 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ internal abstract class DwgStreamWriterBase : StreamIO, IDwgStreamWriter

public long SavedPositionInBits { get; } = 0;

public Encoding Encoding { get; }

public int BitShift { get; private set; } = 0;

private byte _lastByte;
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CSMath;
using System;
using System.IO;
using System.Text;

namespace ACadSharp.IO.DWG
{
Expand All @@ -9,6 +10,8 @@ namespace ACadSharp.IO.DWG
/// </summary>
internal interface IDwgStreamWriter
{
Encoding Encoding { get; }

IDwgStreamWriter Main { get; }

Stream Stream { get; }
Expand Down
14 changes: 10 additions & 4 deletions 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
public class DwgWriter : CadWriterBase<CadWriterConfiguration>
{
private ACadVersion _version { get { return this._document.Header.Version; } }

Expand Down Expand Up @@ -72,7 +72,7 @@ public override void Write()

this._stream.Flush();

if (this.CloseStream)
if (this.Configuration.CloseStream)
{
this._stream.Close();
}
Expand All @@ -89,11 +89,17 @@ public override void Dispose()
/// </summary>
/// <param name="filename"></param>
/// <param name="document"></param>
/// <param name="configuration"></param>
/// <param name="notification"></param>
public static void Write(string filename, CadDocument document, NotificationEventHandler notification = null)
public static void Write(string filename, CadDocument document, CadWriterConfiguration configuration = null, NotificationEventHandler notification = null)
{
using (DwgWriter writer = new DwgWriter(filename, document))
{
if(configuration != null)
{
writer.Configuration = configuration;
}

writer.OnNotification += notification;
writer.Write();
}
Expand Down Expand Up @@ -287,7 +293,7 @@ private void writeRevHistory()
private void writeObjects()
{
MemoryStream stream = new MemoryStream();
DwgObjectWriter writer = new DwgObjectWriter(stream, this._document);
DwgObjectWriter writer = new DwgObjectWriter(stream, this._document, this.Configuration.WriteXRecords);
writer.OnNotification += this.triggerNotification;
writer.Write();

Expand Down
9 changes: 2 additions & 7 deletions ACadSharp/IO/DXF/DxfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ namespace ACadSharp.IO
/// <summary>
/// Class for writing a DXF from a <see cref="CadDocument"/>.
/// </summary>
public class DxfWriter : CadWriterBase
public class DxfWriter : CadWriterBase<DxfWriterConfiguration>
{
/// <summary>
/// Flag indicating if the dxf will be writen as a binary file
/// </summary>
public bool IsBinary { get; }

/// <summary>
/// DXF writer configuration.
/// </summary>
public DxfWriterConfiguration Configuration { get; set; } = new DxfWriterConfiguration();

private IDxfStreamWriter _writer;
private CadObjectHolder _objectHolder = new CadObjectHolder();

Expand Down Expand Up @@ -71,7 +66,7 @@ public override void Write()

this._writer.Flush();

if (this.CloseStream)
if (this.Configuration.CloseStream)
{
this._writer.Close();
}
Expand Down
2 changes: 1 addition & 1 deletion ACadSharp/IO/DXF/DxfWriterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ACadSharp.IO
/// <summary>
/// Configuration for writing DWG files.
/// </summary>
public class DxfWriterConfiguration
public class DxfWriterConfiguration : CadWriterConfiguration
{
/// <summary>
/// Variables that must be writen in a dxf file
Expand Down