Skip to content

Commit

Permalink
added dxfreader feat
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Aug 25, 2024
1 parent e282831 commit 361d376
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/ACadSharp.Tests/IO/DWG/DwgReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public override void AssertDocumentContent(FileModel test)
public override void AssertDocumentTree(FileModel test)
{
DwgReaderConfiguration configuration = new DwgReaderConfiguration();
configuration.KeepUnknownNonGraphicalObjects = true;
configuration.KeepUnknownEntities = true;

CadDocument doc = DwgReader.Read(test.Path, configuration);
Expand Down
1 change: 1 addition & 0 deletions src/ACadSharp.Tests/IO/DXF/DxfReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public override void AssertDocumentContent(FileModel test)
public override void AssertDocumentTree(FileModel test)
{
DxfReaderConfiguration configuration = new DxfReaderConfiguration();
configuration.KeepUnknownNonGraphicalObjects = true;
configuration.KeepUnknownEntities = true;

CadDocument doc;
Expand Down
9 changes: 9 additions & 0 deletions src/ACadSharp/IO/CadDocumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ACadSharp.Entities;
using ACadSharp.IO.Templates;
using ACadSharp.Objects;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using System;
Expand Down Expand Up @@ -35,6 +36,8 @@ internal abstract class CadDocumentBuilder

public abstract bool KeepUnknownEntities { get; }

public abstract bool KeepUnknownNonGraphicalObjects { get; }

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

protected Dictionary<ulong, CadTemplate> cadObjectsTemplates = new();
Expand Down Expand Up @@ -105,6 +108,12 @@ public bool TryGetCadObject<T>(ulong? handle, out T value) where T : CadObject
return false;
}

if (obj is UnknownNonGraphicalObject && !this.KeepUnknownNonGraphicalObjects)
{
value = null;
return false;
}

if (obj is T)
{
value = (T)obj;
Expand Down
13 changes: 11 additions & 2 deletions src/ACadSharp/IO/CadReaderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ public abstract class CadReaderConfiguration
public bool Failsafe { get; set; } = true;

/// <summary>
/// The reader will try to read and add to the document all <see cref="ObjectType.UNLISTED"/> that are linked to a <see cref="Classes. DxfClass"/>
/// The reader will try to read and add to the document all <see cref="ObjectType.UNLISTED"/> that are linked to a <see cref="Classes.DxfClass"/>
/// which may be a proxy or an entity that is not yet supported by ACadSharp, default value is set to false.
/// </summary>
/// <remarks>
/// These entities do not contain any geometric information and will be ignored by the writers
/// These entities do not contain any geometric information and will be ignored by the writers.
/// </remarks>
public bool KeepUnknownEntities { get; set; } = false;

/// <summary>
/// The reader will try to read and add to the document all <see cref="ObjectType.UNLISTED"/> that are linked to a <see cref="Classes.DxfClass"/>
/// which may be a proxy or an NonGraphicalObject that is not yet supported by ACadSharp, default value is set to false.
/// </summary>
/// <remarks>
/// These NonGraphicalObjects do not contain any information and will be ignored by the writers.
/// </remarks>
public bool KeepUnknownNonGraphicalObjects { get; set; } = false;
}
}
7 changes: 3 additions & 4 deletions src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Entities;
using ACadSharp.IO.Templates;
using ACadSharp.Objects;
using System;
using System.Collections.Generic;

namespace ACadSharp.IO.DWG
Expand All @@ -13,16 +14,14 @@ internal class DwgDocumentBuilder : CadDocumentBuilder

public List<CadBlockRecordTemplate> BlockRecordTemplates { get; set; } = new();

public List<UnknownEntity> UnknownEntities { get; } = new();

public List<UnknownNonGraphicalObject> UnknownNonGraphicalObjects { get; } = new();

public List<Entity> PaperSpaceEntities { get; } = new();

public List<Entity> ModelSpaceEntities { get; } = new();

public override bool KeepUnknownEntities => this.Configuration.KeepUnknownEntities;

public override bool KeepUnknownNonGraphicalObjects => this.Configuration.KeepUnknownNonGraphicalObjects;

public DwgDocumentBuilder(ACadVersion version, CadDocument document, DwgReaderConfiguration configuration)
: base(version, document)
{
Expand Down
4 changes: 0 additions & 4 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,6 @@ private CadTemplate readUnknownEntity(DxfClass dxfClass)
UnknownEntity entity = new UnknownEntity(dxfClass);
CadUnknownEntityTemplate template = new CadUnknownEntityTemplate(entity);

this._builder.UnknownEntities.Add(entity);

this.readCommonEntityData(template);

return template;
Expand All @@ -1078,8 +1076,6 @@ private CadTemplate readUnknownNonGraphicalObject(DxfClass dxfClass)
UnknownNonGraphicalObject obj = new UnknownNonGraphicalObject(dxfClass);
CadUnknownNonGraphicalObjectTemplate template = new CadUnknownNonGraphicalObjectTemplate(obj);

this._builder.UnknownNonGraphicalObjects.Add(obj);

this.readCommonNonEntityData(template);

return template;
Expand Down
2 changes: 2 additions & 0 deletions src/ACadSharp/IO/DXF/DxfDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal class DxfDocumentBuilder : CadDocumentBuilder

public override bool KeepUnknownEntities => this.Configuration.KeepUnknownEntities;

public override bool KeepUnknownNonGraphicalObjects => this.Configuration.KeepUnknownNonGraphicalObjects;

public DxfDocumentBuilder(ACadVersion version, CadDocument document, DxfReaderConfiguration configuration) : base(version, document)
{
this.Configuration = configuration;
Expand Down
25 changes: 23 additions & 2 deletions src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,34 @@ private CadTemplate readObject()
case DxfFileToken.ObjectXRecord:
return this.readObjectCodes<XRecord>(new CadXRecordTemplate(), this.readXRecord);
default:
this._builder.Notify($"Object not implemented: {this._reader.ValueAsString}", NotificationType.NotImplemented);
DxfMap map = DxfMap.Create<CadObject>();
CadUnknownNonGraphicalObjectTemplate unknownEntityTemplate = null;
if (this._builder.DocumentToBuild.Classes.TryGetByName(this._reader.ValueAsString, out Classes.DxfClass dxfClass))
{
this._builder.Notify($"NonGraphicalObject not supported read as an UnknownNonGraphicalObject: {this._reader.ValueAsString}", NotificationType.NotImplemented);
unknownEntityTemplate = new CadUnknownNonGraphicalObjectTemplate(new UnknownNonGraphicalObject(dxfClass));
}
else
{
this._builder.Notify($"UnknownNonGraphicalObject not supported: {this._reader.ValueAsString}", NotificationType.NotImplemented);
}

this._reader.ReadNext();

do
{
if (unknownEntityTemplate != null && this._builder.KeepUnknownEntities)
{
this.readCommonCodes(unknownEntityTemplate, out bool isExtendedData, map);
if (isExtendedData)
continue;
}

this._reader.ReadNext();
}
while (this._reader.DxfCode != DxfCode.Start);
return null;

return unknownEntityTemplate;
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,7 @@ protected CadEntityTemplate readEntity()
}
while (this._reader.DxfCode != DxfCode.Start);

if (this._builder.Configuration.KeepUnknownEntities)
{
return unknownEntityTemplate;
}
else
{
return null;
}
return unknownEntityTemplate;
}
}

Expand Down

0 comments on commit 361d376

Please sign in to comment.