Skip to content

Commit

Permalink
Merge pull request #383 from DomCR/Issue-353_writer-options-close-str…
Browse files Browse the repository at this point in the history
…eam-fix

Writer options
  • Loading branch information
DomCR authored Jul 18, 2024
2 parents 006bbe5 + ab9d3f2 commit 5bc59d7
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 34 deletions.
6 changes: 2 additions & 4 deletions ACadSharp.Tests/IO/CadReaderTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using ACadSharp.Entities;
using ACadSharp.Header;
using ACadSharp.Header;
using ACadSharp.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace ACadSharp.Tests.IO
{
public abstract class CadReaderTestsBase<T> : IOTestsBase, IDisposable
where T : CadReaderBase
where T : ICadReader
{
protected readonly Dictionary<string, CadDocument> _documents = new Dictionary<string, CadDocument>(); //TODO: this does not store the document readed

Expand Down
13 changes: 12 additions & 1 deletion ACadSharp/IO/CadReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@

namespace ACadSharp.IO
{
public abstract class CadReaderBase : ICadReader
/// <summary>
/// Base class for the DWG and DXF readers.
/// </summary>
/// <typeparam name="T">Configuration type for the reader.</typeparam>
public abstract class CadReaderBase<T> : ICadReader
where T : CadReaderConfiguration, new()
{
/// <inheritdoc/>
public event NotificationEventHandler OnNotification;

/// <summary>
/// Reader configuration.
/// </summary>
public T Configuration { get; set; } = new();

protected CadDocument _document = new CadDocument(false);

protected Encoding _encoding = Encoding.Default;
Expand Down
7 changes: 4 additions & 3 deletions ACadSharp/IO/CadReaderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using ACadSharp.IO;

namespace ACadSharp.IO
namespace ACadSharp.IO
{
/// <summary>
/// Base configuration for all the <see cref="CadReaderBase{T}"/> classes.
/// </summary>
public abstract class CadReaderConfiguration
{
/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions ACadSharp/IO/CadWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ namespace ACadSharp.IO
{
public abstract class CadWriterBase : ICadWriter
{
/// <summary>
/// Notification event to get information about the writing process.
/// </summary>
/// <remarks>
/// The notification system informs about any issue or non critical errors during the writing.
/// </remarks>
public event NotificationEventHandler OnNotification;

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

Expand Down
12 changes: 6 additions & 6 deletions ACadSharp/IO/DWG/DwgReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

namespace ACadSharp.IO
{
public class DwgReader : CadReaderBase
/// <summary>
/// Class for reading a DWG file into a <see cref="CadDocument"></see>.
/// </summary>
public class DwgReader : CadReaderBase<DwgReaderConfiguration>
{
public DwgReaderConfiguration Configuration { get; set; } = new DwgReaderConfiguration();

private DwgDocumentBuilder _builder;

private DwgFileHeader _fileHeader;

/// <summary>
Expand Down Expand Up @@ -68,7 +68,7 @@ public static CadDocument Read(Stream stream, DwgReaderConfiguration configurati
}

/// <summary>
/// Read a dwg document from a file
/// Read a dwg document from a file.
/// </summary>
/// <param name="filename"></param>
/// <param name="notification">Notification handler, sends any message or notification about the reading process.</param>
Expand All @@ -79,7 +79,7 @@ public static CadDocument Read(string filename, NotificationEventHandler notific
}

/// <summary>
/// Read a dwg document from a file
/// Read a dwg document from a file.
/// </summary>
/// <param name="filename"></param>
/// <param name="configuration"></param>
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/IO/DWG/DwgReaderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace ACadSharp.IO
{
/// <summary>
/// Configuration for reading DWG files.
/// </summary>
public class DwgReaderConfiguration : CadReaderConfiguration
{
/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions ACadSharp/IO/DWG/DwgWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace ACadSharp.IO
{
/// <summary>
/// Class for writing a DWG from a <see cref="CadDocument"/>.
/// </summary>
public class DwgWriter : CadWriterBase
{
private ACadVersion _version { get { return this._document.Header.Version; } }
Expand All @@ -20,7 +23,7 @@ public class DwgWriter : CadWriterBase
private Dictionary<ulong, long> _handlesMap = new Dictionary<ulong, long>();

/// <summary>
///
/// Initializes a new instance of the <see cref="DwgWriter"/> class.
/// </summary>
/// <param name="filename"></param>
/// <param name="document"></param>
Expand All @@ -30,7 +33,7 @@ public DwgWriter(string filename, CadDocument document)
}

/// <summary>
///
/// Initializes a new instance of the <see cref="DwgWriter"/> class.
/// </summary>
/// <param name="stream"></param>
/// <param name="document"></param>
Expand Down
7 changes: 4 additions & 3 deletions ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

namespace ACadSharp.IO
{
public class DxfReader : CadReaderBase
/// <summary>
/// Class for reading a DXF file into a <see cref="CadDocument"></see>.
/// </summary>
public class DxfReader : CadReaderBase<DxfReaderConfiguration>
{
public DxfReaderConfiguration Configuration { get; set; } = new DxfReaderConfiguration();

private ACadVersion _version;
private DxfDocumentBuilder _builder;
private IDxfStreamReader _reader;
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/IO/DXF/DxfReaderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace ACadSharp.IO
{
/// <summary>
/// Configuration for reading DXF files.
/// </summary>
public class DxfReaderConfiguration : CadReaderConfiguration
{
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ACadSharp/IO/DXF/DxfStreamWriter/DxfHeaderSectionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ internal class DxfHeaderSectionWriter : DxfSectionWriterBase

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

public DxfWriterOptions Options { get; }
public DxfWriterConfiguration Options { get; }

public DxfHeaderSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder, DxfWriterOptions options) : base(writer, document, holder)
public DxfHeaderSectionWriter(IDxfStreamWriter writer, CadDocument document, CadObjectHolder holder, DxfWriterConfiguration options) : base(writer, document, holder)
{
this.Options = options;
}
Expand Down
10 changes: 8 additions & 2 deletions ACadSharp/IO/DXF/DxfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace ACadSharp.IO
{
/// <summary>
/// Class for writing a DXF from a <see cref="CadDocument"/>.
/// </summary>
public class DxfWriter : CadWriterBase
{
/// <summary>
/// Flag indicating if the dxf will be writen as a binary file
/// </summary>
public bool IsBinary { get; }

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

private IDxfStreamWriter _writer;
private CadObjectHolder _objectHolder = new CadObjectHolder();
Expand Down Expand Up @@ -124,7 +130,7 @@ private void createStreamWriter()

private void writeHeader()
{
var writer = new DxfHeaderSectionWriter(this._writer, this._document, this._objectHolder, this.Options);
var writer = new DxfHeaderSectionWriter(this._writer, this._document, this._objectHolder, this.Configuration);
writer.OnNotification += this.triggerNotification;

writer.Write();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace ACadSharp.IO
{
public class DxfWriterOptions
/// <summary>
/// Configuration for writing DWG files.
/// </summary>
public class DxfWriterConfiguration
{
/// <summary>
/// Variables that must be writen in a dxf file
Expand Down Expand Up @@ -72,7 +75,7 @@ public class DxfWriterOptions

private HashSet<string> _headerVariables;

public DxfWriterOptions()
public DxfWriterConfiguration()
{
this._headerVariables = new HashSet<string>(Variables);
}
Expand Down
20 changes: 13 additions & 7 deletions ACadSharp/IO/ICadReader.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using ACadSharp.Header;
using CSUtilities.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACadSharp.IO
{
/// <summary>
/// Common interface for the different Cad readers.
/// </summary>
public interface ICadReader : IDisposable
{
/// <summary>
/// Read the Cad header section of the file
/// Notification event to get information about the reading process.
/// </summary>
/// <remarks>
/// The notification system informs about any issue or non critical errors during the reading.
/// </remarks>
event NotificationEventHandler OnNotification;

/// <summary>
/// Read the Cad header section of the file.
/// </summary>
CadHeader ReadHeader();

/// <summary>
/// Read the cad document
/// Read the cad document.
/// </summary>
CadDocument Read();
}
Expand Down

0 comments on commit 5bc59d7

Please sign in to comment.