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

issue 163 Implement version AC1009 for DxfReader #165

Merged
merged 18 commits into from
Jul 2, 2024
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
30,695 changes: 30,695 additions & 0 deletions ACadSharp.Tests/Data/AC1009_tree.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions ACadSharp.Tests/IO/CadReaderTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public virtual void AssertDocumentDefaults(string test)
{
CadDocument doc = this.getDocument(test);

if (doc.Header.Version < ACadVersion.AC1012)
{
//Older version do not keep the handles for tables and other objects like block_records
//This can be fixed if the document creates the default entries manually
return;
}

this._docIntegrity.AssertDocumentDefaults(doc);
}

Expand All @@ -59,6 +66,12 @@ public virtual void AssertDocumentContent(string test)
{
CadDocument doc = this.getDocument(test, false);

if (doc.Header.Version < ACadVersion.AC1012)
{
//Older version do not keep the handles for tables and other objects like block_records
return;
}

this._docIntegrity.AssertDocumentContent(doc);
}

Expand Down
6 changes: 6 additions & 0 deletions ACadSharp.Tests/IO/DXF/DxfReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ public override void AssertDocumentTree(string test)
doc = reader.Read();
}

if(doc.Header.Version < ACadVersion.AC1012)
{
//Older version do not keep the handles for tables and other objects like block_records
return;
}

this._docIntegrity.AssertDocumentTree(doc);
}

Expand Down
5 changes: 5 additions & 0 deletions ACadSharp.Tests/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void DxfToDxf(string test)
{
CadDocument doc = DxfReader.Read(test);

if(doc.Header.Version < ACadVersion.AC1012)
{
return;
}

string file = Path.GetFileNameWithoutExtension(test);
string pathOut = Path.Combine(samplesOutFolder, $"{file}_rewrite_out.dxf");
this.writeDxfFile(pathOut, doc);
Expand Down
8 changes: 4 additions & 4 deletions ACadSharp/IO/CadDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal abstract class CadDocumentBuilder

public abstract bool KeepUnknownEntities { get; }

protected ulong maxHandle = 0;
public ulong InitialHandSeed { get; set; } = 0;

protected Dictionary<ulong, CadTemplate> cadObjectsTemplates = new();

Expand Down Expand Up @@ -276,12 +276,12 @@ private void addToMap(ICadObjectTemplate template)
{
if (template.CadObject.Handle == 0)
{
template.CadObject.Handle = this.maxHandle + 1;
template.CadObject.Handle = this.InitialHandSeed + 1;
}

if (template.CadObject.Handle > this.maxHandle)
if (template.CadObject.Handle > this.InitialHandSeed)
{
this.maxHandle = template.CadObject.Handle;
this.InitialHandSeed = template.CadObject.Handle;
}

this.templatesMap.Add(template.CadObject.Handle, template);
Expand Down
57 changes: 39 additions & 18 deletions ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ public static bool IsBinary(Stream stream, bool resetPos = false)
sio.Position = 0;
string sn = sio.ReadString(DxfBinaryReader.Sentinel.Length);

bool isBinary = sn == DxfBinaryReader.Sentinel;

if (resetPos)
{
stream.Position = 0;
}

return sn == DxfBinaryReader.Sentinel;
return isBinary;
}

/// <summary>
Expand Down Expand Up @@ -121,7 +125,7 @@ public override CadDocument Read()

this._reader = this._reader ?? this.getReader();

this._builder = new DxfDocumentBuilder(this._version,this._document, this.Configuration);
this._builder = new DxfDocumentBuilder(this._version, this._document, this.Configuration);
this._builder.OnNotification += this.onNotificationEvent;

while (this._reader.ValueAsString != DxfFileToken.EndOfFile)
Expand All @@ -140,6 +144,7 @@ public override CadDocument Read()
{
case DxfFileToken.HeaderSection:
this._document.Header = this.ReadHeader();
this._builder.InitialHandSeed = this._document.Header.HandleSeed;
break;
case DxfFileToken.ClassesSection:
this._document.Classes = this.readClasses();
Expand Down Expand Up @@ -450,16 +455,20 @@ private IDxfStreamReader getReader()
IDxfStreamReader tmpReader = null;
this._version = ACadVersion.Unknown;

bool isBinary = this.IsBinary();
if (isBinary)
{
tmpReader = new DxfBinaryReader(this._fileStream.Stream, Encoding.ASCII);
}
else
bool isBinary = IsBinary(this._fileStream.Stream, false);
bool isAC1009Format = false;

if (isBinary && this._fileStream.Stream.ReadByte() != -1)
{
tmpReader = new DxfTextReader(this._fileStream.Stream);
int flag = this._fileStream.ReadByte();
if (flag != -1 && flag != 0)
{
isAC1009Format = true;
}
}

tmpReader = this.createReader(isBinary, isAC1009Format);

tmpReader.Find(DxfFileToken.HeaderSection);

while (tmpReader.ValueAsString != DxfFileToken.EndSection)
Expand All @@ -474,7 +483,7 @@ private IDxfStreamReader getReader()
break;
}

if (this._version < ACadVersion.AC1012)
if (this._version < ACadVersion.AC1002)
{
if (this._version == ACadVersion.Unknown)
{
Expand All @@ -500,14 +509,7 @@ private IDxfStreamReader getReader()
tmpReader.ReadNext();
}

if (isBinary)
{
return new DxfBinaryReader(this._fileStream.Stream, this._encoding);
}
else
{
return new DxfTextReader(this._fileStream.Stream, this._encoding);
}
return this.createReader(isBinary, isAC1009Format);
}

private IDxfStreamReader goToSection(string sectionName)
Expand All @@ -523,5 +525,24 @@ private IDxfStreamReader goToSection(string sectionName)

return this._reader;
}

private IDxfStreamReader createReader(bool isBinary, bool isAC1009Format)
{
if (isBinary)
{
if (isAC1009Format)
{
return new DxfBinaryReaderAC1009(this._fileStream.Stream, Encoding.ASCII);
}
else
{
return new DxfBinaryReader(this._fileStream.Stream, Encoding.ASCII);
}
}
else
{
return new DxfTextReader(this._fileStream.Stream);
}
}
}
}
7 changes: 4 additions & 3 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace ACadSharp.IO.DXF
{
internal class DxfBinaryReader : DxfStreamReaderBase, IDxfStreamReader
{
public const string Sentinel = "AutoCAD Binary DXF";
public const string Sentinel = "AutoCAD Binary DXF\r\n\u001a\0";

public override int Position { get { return (int)this.baseStream.Position; } }

protected override Stream baseStream { get { return this._stream.BaseStream; } }

private Encoding _encoding;
protected BinaryReader _stream;

private BinaryReader _stream;
private Encoding _encoding;

public DxfBinaryReader(Stream stream) : this(stream, Encoding.ASCII) { }

Expand All @@ -32,6 +32,7 @@ protected override void start()
base.start();

byte[] sentinel = this._stream.ReadBytes(22);
//AutoCAD Binary DXF\r\n\u001a\0
string s = Encoding.ASCII.GetString(sentinel);
}

Expand Down
23 changes: 23 additions & 0 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfBinaryReaderAC1009.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.IO;
using System.Text;

namespace ACadSharp.IO.DXF
{
internal class DxfBinaryReaderAC1009 : DxfBinaryReader
{
public DxfBinaryReaderAC1009(Stream stream, Encoding encoding) : base(stream, encoding)
{
}

protected override DxfCode readCode()
{
int code = this._stream.ReadByte();
if (code == byte.MaxValue)
{
code = this._stream.ReadInt16();
}

return (DxfCode)code;
}
}
}
22 changes: 18 additions & 4 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ private void readBlock()
case 2:
case 3:
name = this._reader.ValueAsString;
if (name.Equals("$MODEL_SPACE", StringComparison.OrdinalIgnoreCase))
{
name = BlockRecord.ModelSpaceName;
}
else if (name.Equals("$PAPER_SPACE", StringComparison.OrdinalIgnoreCase))
{
name = BlockRecord.PaperSpaceName;
}

if (record == null && this._builder.TryGetTableEntry(name, out record))
{
record.BlockEntity = blckEntity;
Expand Down Expand Up @@ -101,11 +110,16 @@ private void readBlock()

if (record == null)
{
//record = new BlockRecord(name);
//record.BlockEntity = blckEntity;
record = new BlockRecord(name);
record.BlockEntity = blckEntity;
CadBlockRecordTemplate recordTemplate = new CadBlockRecordTemplate(record);

this._builder.BlockRecords.Add(record);

//this._builder.DocumentToBuild.BlockRecords.Add(record);
throw new DxfException($"Could not find the block record for {name} and handle {blckEntity.Handle}");
if (recordTemplate.CadObject.Name.Equals(BlockRecord.ModelSpaceName, StringComparison.OrdinalIgnoreCase))
{
this._builder.ModelSpaceTemplate = recordTemplate;
}
}

while (this._reader.ValueAsString != DxfFileToken.EndBlock)
Expand Down
2 changes: 1 addition & 1 deletion ACadSharp/IO/DXF/DxfStreamReader/DxfTablesSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void readTable()
this.readExtendedData(edata);
break;
default:
this._builder.Notify($"Unhandeled dxf code {this._reader.Code} at line {this._reader.Position}.");
this._builder.Notify($"[AcDbSymbolTable] Unhandeled dxf code {this._reader.Code} at line {this._reader.Position}.");
break;
}

Expand Down
4 changes: 1 addition & 3 deletions ACadSharp/Tables/Collections/BlockRecordsTable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ACadSharp.Blocks;

namespace ACadSharp.Tables.Collections
namespace ACadSharp.Tables.Collections
{
public class BlockRecordsTable : Table<BlockRecord>
{
Expand Down
Loading
Loading