Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XRecord DwgWriter #190

Merged
merged 8 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
writer configuration
  • Loading branch information
DomCR committed Jul 24, 2024
commit 6c5af588db9fe781341f7ec705c7f3ea462aa5eb
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);
}
}
}
38 changes: 32 additions & 6 deletions ACadSharp/IO/CadWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,36 @@

namespace ACadSharp.IO
{
public abstract class CadWriterBase : ICadWriter
/// <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;
}

/// <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 +46,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
4 changes: 2 additions & 2 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void writeObject(CadObject obj)
return;
}

if (obj is XRecord && this.IgnoreXRecords)
if (obj is XRecord && !this.WriteXRecords)
{
return;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ private void writeDictionary(CadDictionary dictionary)
//Common:
foreach (var item in dictionary)
{
if (item is XRecord && this.IgnoreXRecords)
if (item is XRecord && !this.WriteXRecords)
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal partial class DwgObjectWriter : DwgSectionIO
/// </summary>
public Dictionary<ulong, long> Map { get; } = new Dictionary<ulong, long>();

public bool IgnoreXRecords { get; }
public bool WriteXRecords { get; }

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

Expand All @@ -39,14 +39,14 @@ internal partial class DwgObjectWriter : DwgSectionIO

private Entity _next;

public DwgObjectWriter(Stream stream, CadDocument document, bool ignoreXRecords = true) : 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.IgnoreXRecords = ignoreXRecords;
this.WriteXRecords = writeXRecords;
}

public void Write()
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
Loading