From fed6458048bdd7c910dc9bc8e73255d1e1285bd8 Mon Sep 17 00:00:00 2001 From: ilCosmico Date: Wed, 7 Oct 2026 10:42:45 +0200 Subject: [PATCH 1/2] Return false from TryGetCadObject when the object is not of the requested type --- src/ACadSharp.Tests/CadDocumentTests.cs | 15 ++++++ .../Internal/CadDocumentBuilderTests.cs | 54 +++++++++++++++++++ src/ACadSharp/CadDocument.cs | 4 +- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs diff --git a/src/ACadSharp.Tests/CadDocumentTests.cs b/src/ACadSharp.Tests/CadDocumentTests.cs index c01c4b720..54ff8a9b7 100644 --- a/src/ACadSharp.Tests/CadDocumentTests.cs +++ b/src/ACadSharp.Tests/CadDocumentTests.cs @@ -443,4 +443,19 @@ public void SetCurrentTest() doc.SetCurrent(new MultiLeaderStyle(multiLeaderStyleName)); Assert.True(doc.MLeaderStyles.ContainsKey(multiLeaderStyleName)); } + + [Fact] + public void TryGetCadObjectTypeMismatch() + { + CadDocument doc = new CadDocument(); + + Line line = new Line(); + doc.Entities.Add(line); + + Assert.True(doc.TryGetCadObject(line.Handle, out Line found)); + Assert.Equal(line, found); + + Assert.False(doc.TryGetCadObject(line.Handle, out Circle circle)); + Assert.Null(circle); + } } \ No newline at end of file diff --git a/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs b/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs new file mode 100644 index 000000000..4c3635429 --- /dev/null +++ b/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs @@ -0,0 +1,54 @@ +using ACadSharp.Classes; +using ACadSharp.DataStorage; +using ACadSharp.Entities; +using ACadSharp.IO; +using ACadSharp.IO.DWG; +using System.Collections.Generic; +using System.IO; +using Xunit; + +namespace ACadSharp.Tests.Internal; + +public class CadDocumentBuilderTests +{ + [Fact] + public void BuildDataStorageSkipsOtherEntities() + { + CadDocument doc = new CadDocument(); + + Solid3D solid = new Solid3D(); + doc.Entities.Add(solid); + + UnknownEntity surface = new UnknownEntity(new DxfClass { DxfName = "REVOLVEDSURFACE", CppClassName = "AcDbRevolvedSurface" }); + doc.Entities.Add(surface); + + byte[] solidData = new byte[] { 1, 2, 3 }; + + DwgDocumentBuilder builder = new DwgDocumentBuilder(ACadVersion.AC1032, doc, new DwgReaderConfiguration { KeepUnknownEntities = true }); + builder.DataStorage = new CadFileDataStorage(); + builder.DataStorage.Records.Add(this.createAsmRecord(solid.Handle, solidData)); + builder.DataStorage.Records.Add(this.createAsmRecord(surface.Handle, new byte[] { 4, 5, 6 })); + + List notifications = new List(); + builder.OnNotification += (sender, e) => notifications.Add(e); + + builder.BuildDataStorage(); + + Assert.Equal(solidData, solid.AcisData); + NotificationEventArgs warning = Assert.Single(notifications); + Assert.Equal(NotificationType.Warning, warning.NotificationType); + } + + private AcdsRecord createAsmRecord(ulong handle, byte[] data) + { + AcdsRecord record = new AcdsRecord(); + + record.Columns.Add(CadFileDataStorage.Id, new AcdsRecordColumn { Name = CadFileDataStorage.Id, Handle = handle }); + + MemoryStream stream = new MemoryStream(); + stream.Write(data, 0, data.Length); + record.Columns.Add(CadFileDataStorage.AsmData, new AcdsRecordColumn { Name = CadFileDataStorage.AsmData, Data = stream }); + + return record; + } +} diff --git a/src/ACadSharp/CadDocument.cs b/src/ACadSharp/CadDocument.cs index f9b639cff..9cd5e4330 100644 --- a/src/ACadSharp/CadDocument.cs +++ b/src/ACadSharp/CadDocument.cs @@ -490,9 +490,9 @@ public bool TryGetCadObject(ulong handle, out T cadObject) if (handle == this.Handle) return false; - if (this._cadObjects.TryGetValue(handle, out IHandledCadObject obj)) + if (this._cadObjects.TryGetValue(handle, out IHandledCadObject obj) && obj is T) { - cadObject = obj as T; + cadObject = (T)obj; return true; } From bd79b042235695087ea7bae4800687951f6f417f Mon Sep 17 00:00:00 2001 From: ilCosmico Date: Thu, 8 Oct 2026 10:22:32 +0200 Subject: [PATCH 2/2] Remove the CadDocumentBuilder test --- .../Internal/CadDocumentBuilderTests.cs | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs diff --git a/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs b/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs deleted file mode 100644 index 4c3635429..000000000 --- a/src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs +++ /dev/null @@ -1,54 +0,0 @@ -using ACadSharp.Classes; -using ACadSharp.DataStorage; -using ACadSharp.Entities; -using ACadSharp.IO; -using ACadSharp.IO.DWG; -using System.Collections.Generic; -using System.IO; -using Xunit; - -namespace ACadSharp.Tests.Internal; - -public class CadDocumentBuilderTests -{ - [Fact] - public void BuildDataStorageSkipsOtherEntities() - { - CadDocument doc = new CadDocument(); - - Solid3D solid = new Solid3D(); - doc.Entities.Add(solid); - - UnknownEntity surface = new UnknownEntity(new DxfClass { DxfName = "REVOLVEDSURFACE", CppClassName = "AcDbRevolvedSurface" }); - doc.Entities.Add(surface); - - byte[] solidData = new byte[] { 1, 2, 3 }; - - DwgDocumentBuilder builder = new DwgDocumentBuilder(ACadVersion.AC1032, doc, new DwgReaderConfiguration { KeepUnknownEntities = true }); - builder.DataStorage = new CadFileDataStorage(); - builder.DataStorage.Records.Add(this.createAsmRecord(solid.Handle, solidData)); - builder.DataStorage.Records.Add(this.createAsmRecord(surface.Handle, new byte[] { 4, 5, 6 })); - - List notifications = new List(); - builder.OnNotification += (sender, e) => notifications.Add(e); - - builder.BuildDataStorage(); - - Assert.Equal(solidData, solid.AcisData); - NotificationEventArgs warning = Assert.Single(notifications); - Assert.Equal(NotificationType.Warning, warning.NotificationType); - } - - private AcdsRecord createAsmRecord(ulong handle, byte[] data) - { - AcdsRecord record = new AcdsRecord(); - - record.Columns.Add(CadFileDataStorage.Id, new AcdsRecordColumn { Name = CadFileDataStorage.Id, Handle = handle }); - - MemoryStream stream = new MemoryStream(); - stream.Write(data, 0, data.Length); - record.Columns.Add(CadFileDataStorage.AsmData, new AcdsRecordColumn { Name = CadFileDataStorage.AsmData, Data = stream }); - - return record; - } -}