Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/ACadSharp.Tests/CadDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
54 changes: 54 additions & 0 deletions src/ACadSharp.Tests/Internal/CadDocumentBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this one is needed, the functionality is tested in the local validation and the other reading tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in bd79b04.

{
[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<NotificationEventArgs> notifications = new List<NotificationEventArgs>();
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;
}
}
4 changes: 2 additions & 2 deletions src/ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,9 @@ public bool TryGetCadObject<T>(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;
}

Expand Down
Loading