Skip to content

Commit

Permalink
Merge pull request #428 from DomCR/UnknownNonGraphicalObject
Browse files Browse the repository at this point in the history
Unknown None Graphical Object
  • Loading branch information
DomCR authored Aug 25, 2024
2 parents 9c57548 + 361d376 commit c3d924b
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 43 deletions.
18 changes: 0 additions & 18 deletions src/ACadSharp.Tests/CadObjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Xunit;
using ACadSharp.Tests.Common;
using ACadSharp.Tables.Collections;
using ACadSharp.Entities;

namespace ACadSharp.Tests
Expand All @@ -28,23 +27,6 @@ static CadObjectTests()
}
}

[Theory]
[MemberData(nameof(ACadTypes))]
public void DefaultConstructor(Type t)
{
CadObject cadObject = Factory.CreateObject(t, false);

Assert.NotNull(cadObject);
Assert.True(0 == cadObject.Handle);

Assert.NotEqual(ObjectType.UNDEFINED, cadObject.ObjectType);

Assert.False(string.IsNullOrEmpty(cadObject.ObjectName));
Assert.False(string.IsNullOrEmpty(cadObject.SubclassMarker));

Assert.Null(cadObject.XDictionary);
}

[Fact]
public void CreateExtendedDictionaryTest()
{
Expand Down
17 changes: 17 additions & 0 deletions src/ACadSharp.Tests/Entities/EntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ static EntityTests()
}
}

[Theory]
[MemberData(nameof(EntityTypes))]
public void DefaultConstructor(Type t)
{
CadObject cadObject = Factory.CreateObject(t, false);

Assert.NotNull(cadObject);
Assert.True(0 == cadObject.Handle);

Assert.NotEqual(ObjectType.UNDEFINED, cadObject.ObjectType);

Assert.False(string.IsNullOrEmpty(cadObject.ObjectName));
Assert.False(string.IsNullOrEmpty(cadObject.SubclassMarker));

Assert.Null(cadObject.XDictionary);
}

[Theory]
[MemberData(nameof(EntityTypes))]
public void BoundingBoxTest(Type entityType)
Expand Down
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
8 changes: 4 additions & 4 deletions src/ACadSharp/Entities/UnknownEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace ACadSharp.Entities
{
/// <summary>
/// Class that holds the basic information for an unknown entity
/// Class that holds the basic information for an unknown <see cref="Entity"/>.
/// </summary>
/// <remarks>
/// Unknown entities may appear in the <see cref="CadDocument"/> if the dwg file contains proxies or entities not yet supported by ACadSharp
/// Unknown entities may appear in the <see cref="CadDocument"/> if the cad file contains proxies or entities not yet supported by ACadSharp.
/// </remarks>
public class UnknownEntity : Entity
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public override string SubclassMarker
}

/// <summary>
/// Dxf class linked to this entity
/// Dxf class linked to this entity.
/// </summary>
public DxfClass DxfClass { get; }

Expand All @@ -62,7 +62,7 @@ internal UnknownEntity(DxfClass dxfClass)
/// </remarks>
public override BoundingBox GetBoundingBox()
{
return default;
return BoundingBox.Null;
}
}
}
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;
}
}
6 changes: 4 additions & 2 deletions src/ACadSharp/IO/DWG/DwgDocumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ACadSharp.Entities;
using ACadSharp.IO.Templates;
using ACadSharp.Objects;
using System;
using System.Collections.Generic;

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

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

public List<UnknownEntity> UnknownEntities { 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
25 changes: 18 additions & 7 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,6 @@ private CadTemplate readUnlistedType(short classNumber)
case "LWPOLYLINE":
template = this.readLWPolyline();
break;
case "MATERIAL":
break;
case "MESH":
template = this.readMesh();
break;
Expand All @@ -1029,9 +1027,9 @@ private CadTemplate readUnlistedType(short classNumber)
case "SORTENTSTABLE":
template = this.readSortentsTable();
break;
case "VISUALSTYLE":
template = this.readVisualStyle();
break;
//case "VISUALSTYLE":
// template = this.readVisualStyle();
// break;
case "WIPEOUT":
template = this.readCadImage(new Wipeout());
break;
Expand All @@ -1047,6 +1045,11 @@ private CadTemplate readUnlistedType(short classNumber)
template = this.readUnknownEntity(c);
this._builder.Notify($"Unlisted object with DXF name {c.DxfName} has been read as an UnknownEntity", NotificationType.Warning);
}
else if (template == null && !c.IsAnEntity)
{
template = this.readUnknownNonGraphicalObject(c);
this._builder.Notify($"Unlisted object with DXF name {c.DxfName} has been read as an UnknownNonGraphicalObject", NotificationType.Warning);
}

if (template == null)
{
Expand All @@ -1063,13 +1066,21 @@ 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;
}

private CadTemplate readUnknownNonGraphicalObject(DxfClass dxfClass)
{
UnknownNonGraphicalObject obj = new UnknownNonGraphicalObject(dxfClass);
CadUnknownNonGraphicalObjectTemplate template = new CadUnknownNonGraphicalObjectTemplate(obj);

this.readCommonNonEntityData(template);

return template;
}

private CadTemplate readText()
{
TextEntity text = new TextEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private void writeObject(CadObject obj)
case MultiLeaderAnnotContext:
case MultiLeaderStyle:
case SortEntitiesTable:
case UnknownNonGraphicalObject:
case VisualStyle:
this.notify($"Object type not implemented {obj.GetType().FullName}", NotificationType.NotImplemented);
return;
Expand Down Expand Up @@ -132,6 +133,11 @@ private void writeDictionary(CadDictionary dictionary)
return;
}

if (item is UnknownNonGraphicalObject)
{
return;
}

this._writer.WriteVariableText(item.Name);
this._writer.HandleReference(DwgReferenceType.SoftOwnership, item.Handle);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected void writeObject<T>(T co)
case MultiLeaderAnnotContext:
case VisualStyle:
case ImageDefinitionReactor:
case UnknownNonGraphicalObject:
case XRecord:
this.notify($"Object not implemented : {co.GetType().FullName}");
return;
Expand Down
6 changes: 6 additions & 0 deletions src/ACadSharp/IO/Templates/CadUnknownEntityTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using ACadSharp.Entities;
using ACadSharp.Objects;

namespace ACadSharp.IO.Templates
{
internal class CadUnknownNonGraphicalObjectTemplate : CadNonGraphicalObjectTemplate
{
public CadUnknownNonGraphicalObjectTemplate(UnknownNonGraphicalObject obj) : base(obj) { }
}

internal class CadUnknownEntityTemplate : CadEntityTemplate
{
public CadUnknownEntityTemplate(UnknownEntity entity) : base(entity) { }
Expand Down
Loading

0 comments on commit c3d924b

Please sign in to comment.