diff --git a/src/ACadSharp.Tests/IO/DWG/DwgReaderTests.cs b/src/ACadSharp.Tests/IO/DWG/DwgReaderTests.cs index 280ea827..06de6dca 100644 --- a/src/ACadSharp.Tests/IO/DWG/DwgReaderTests.cs +++ b/src/ACadSharp.Tests/IO/DWG/DwgReaderTests.cs @@ -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); diff --git a/src/ACadSharp.Tests/IO/DXF/DxfReaderTests.cs b/src/ACadSharp.Tests/IO/DXF/DxfReaderTests.cs index 766a0406..0ce31e18 100644 --- a/src/ACadSharp.Tests/IO/DXF/DxfReaderTests.cs +++ b/src/ACadSharp.Tests/IO/DXF/DxfReaderTests.cs @@ -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; diff --git a/src/ACadSharp/IO/CadDocumentBuilder.cs b/src/ACadSharp/IO/CadDocumentBuilder.cs index 11a3d788..02a8c84b 100644 --- a/src/ACadSharp/IO/CadDocumentBuilder.cs +++ b/src/ACadSharp/IO/CadDocumentBuilder.cs @@ -1,5 +1,6 @@ using ACadSharp.Entities; using ACadSharp.IO.Templates; +using ACadSharp.Objects; using ACadSharp.Tables; using ACadSharp.Tables.Collections; using System; @@ -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 cadObjectsTemplates = new(); @@ -105,6 +108,12 @@ public bool TryGetCadObject(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; diff --git a/src/ACadSharp/IO/CadReaderConfiguration.cs b/src/ACadSharp/IO/CadReaderConfiguration.cs index eaea2a22..401682d8 100644 --- a/src/ACadSharp/IO/CadReaderConfiguration.cs +++ b/src/ACadSharp/IO/CadReaderConfiguration.cs @@ -14,12 +14,21 @@ public abstract class CadReaderConfiguration public bool Failsafe { get; set; } = true; /// - /// The reader will try to read and add to the document all that are linked to a + /// The reader will try to read and add to the document all that are linked to a /// which may be a proxy or an entity that is not yet supported by ACadSharp, default value is set to false. /// /// - /// 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. /// public bool KeepUnknownEntities { get; set; } = false; + + /// + /// The reader will try to read and add to the document all that are linked to a + /// which may be a proxy or an NonGraphicalObject that is not yet supported by ACadSharp, default value is set to false. + /// + /// + /// These NonGraphicalObjects do not contain any information and will be ignored by the writers. + /// + public bool KeepUnknownNonGraphicalObjects { get; set; } = false; } } diff --git a/src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs b/src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs index f8a6076d..0bfe5323 100644 --- a/src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs +++ b/src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs @@ -1,6 +1,7 @@ using ACadSharp.Entities; using ACadSharp.IO.Templates; using ACadSharp.Objects; +using System; using System.Collections.Generic; namespace ACadSharp.IO.DWG @@ -13,16 +14,14 @@ internal class DwgDocumentBuilder : CadDocumentBuilder public List BlockRecordTemplates { get; set; } = new(); - public List UnknownEntities { get; } = new(); - - public List UnknownNonGraphicalObjects { get; } = new(); - public List PaperSpaceEntities { get; } = new(); public List 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) { diff --git a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs index f8fa7ae6..dac09e25 100644 --- a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs +++ b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs @@ -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; @@ -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; diff --git a/src/ACadSharp/IO/DXF/DxfDocumentBuilder.cs b/src/ACadSharp/IO/DXF/DxfDocumentBuilder.cs index 67aa54a3..431d95dc 100644 --- a/src/ACadSharp/IO/DXF/DxfDocumentBuilder.cs +++ b/src/ACadSharp/IO/DXF/DxfDocumentBuilder.cs @@ -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; diff --git a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs index 2bd588df..f261d9c2 100644 --- a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs +++ b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs @@ -70,13 +70,34 @@ private CadTemplate readObject() case DxfFileToken.ObjectXRecord: return this.readObjectCodes(new CadXRecordTemplate(), this.readXRecord); default: - this._builder.Notify($"Object not implemented: {this._reader.ValueAsString}", NotificationType.NotImplemented); + DxfMap map = DxfMap.Create(); + 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; } } diff --git a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs index b6748803..26030894 100644 --- a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs +++ b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs @@ -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; } }