Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 272 proxies #335

Merged
merged 9 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion ACadSharp.Tests/CadDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ static CadDocumentTests()

foreach (var item in DataFactory.GetTypes<Entity>())
{
if (item == typeof(Block) || item == typeof(BlockEnd))
if (item == typeof(Block)
|| item == typeof(BlockEnd)
|| item == typeof(UnknownEntity))
continue;

EntityTypes.Add(item);
Expand Down
5 changes: 5 additions & 0 deletions ACadSharp.Tests/Entities/EntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ static EntityTests()
{
foreach (var item in DataFactory.GetTypes<Entity>())
{
if(item == typeof(UnknownEntity))
{
continue;
}

EntityTypes.Add(item);
}
}
Expand Down
2 changes: 0 additions & 2 deletions ACadSharp.Tests/IO/CadReaderTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using ACadSharp.Header;
using ACadSharp.IO;
using ACadSharp.IO.DWG;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using Xunit.Abstractions;

Expand Down
8 changes: 7 additions & 1 deletion ACadSharp.Tests/Internal/DxfMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ static DxfMapTests()

foreach (var item in d.GetTypes().Where(i => !i.IsAbstract && i.IsPublic))
{
if (item.IsSubclassOf(typeof(Entity)) || item.IsSubclassOf(typeof(TableEntry)))
if (item.IsSubclassOf(typeof(Entity))
|| item.IsSubclassOf(typeof(TableEntry)))
{
if(item == typeof(UnknownEntity))
{
continue;
}

Types.Add(item);
}
}
Expand Down
10 changes: 6 additions & 4 deletions ACadSharp/Classes/DxfClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class DxfClass
public int InstanceCount { get; set; }

/// <summary>
/// Was-a-proxy flag.Set to 1 if class was not loaded when this DXF file was created, and 0 otherwise
/// Was-a-proxy flag. Set to 1 if class was not loaded when this DXF file was created, and 0 otherwise
/// </summary>
[DxfCodeValue(280)]
public bool WasZombie { get; set; }

/// <summary>
/// Is-an-entity flag.Set to 1 if class was derived from the AcDbEntity class and can reside in the BLOCKS or ENTITIES section.If 0, instances may appear only in the OBJECTS section
/// Is-an-entity flag.
/// Set to 1 if class was derived from the AcDbEntity class and can reside in the BLOCKS or ENTITIES section.
/// If 0, instances may appear only in the OBJECTS section
/// </summary>
[DxfCodeValue(281)]
public bool IsAnEntity { get; set; }
Expand All @@ -54,11 +56,11 @@ public class DxfClass
/// <summary>
/// Item class id
/// </summary>
public short ItemClassId { get; set; }
internal short ItemClassId { get; set; }

public ACadVersion DwgVersion { get; set; }

public short MaintenanceVersion { get; set; }
internal short MaintenanceVersion { get; set; }

/// <inheritdoc/>
public override string ToString()
Expand Down
32 changes: 32 additions & 0 deletions ACadSharp/Entities/UnknownEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ACadSharp.Classes;

namespace ACadSharp.Entities
{
/// <summary>
/// Class that holds the basic information for an unknown 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
/// </remarks>
public class UnknownEntity : Entity
{
/// <inheritdoc/>
public override ObjectType ObjectType => ObjectType.UNDEFINED;

/// <inheritdoc/>
public override string ObjectName => this.DxfClass.DxfName;

/// <inheritdoc/>
public override string SubclassMarker => this.DxfClass.CppClassName;

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

internal UnknownEntity(DxfClass dxfClass)
{
this.DxfClass = dxfClass;
}
}
}
3 changes: 2 additions & 1 deletion ACadSharp/IO/CadDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public T GetCadObject<T>(ulong? handle) where T : CadObject
return null;
}

public bool TryGetCadObject<T>(ulong? handle, out T value) where T : CadObject
public virtual bool TryGetCadObject<T>(ulong? handle, out T value) where T : CadObject
{
if (!handle.HasValue)
{
Expand Down Expand Up @@ -120,6 +120,7 @@ public bool TryGetTableEntry<T>(string name, out T entry)
return entry != null;
}

[Obsolete]
public T GetObjectTemplate<T>(ulong handle) where T : CadTemplate
{
if (this.templates.TryGetValue(handle, out CadTemplate builder))
Expand Down
19 changes: 18 additions & 1 deletion ACadSharp/IO/DWG/DwgDocumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ACadSharp.IO.Templates;
using ACadSharp.Entities;
using ACadSharp.IO.Templates;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using System;
using System.Collections.Generic;

namespace ACadSharp.IO.DWG
Expand All @@ -13,6 +15,8 @@ internal class DwgDocumentBuilder : CadDocumentBuilder

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

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

public DwgDocumentBuilder(CadDocument document, DwgReaderConfiguration configuration)
: base(document)
{
Expand Down Expand Up @@ -41,5 +45,18 @@ public override void BuildDocument()

base.BuildDocument();
}

public override bool TryGetCadObject<T>(ulong? handle, out T value)
{
bool result = base.TryGetCadObject(handle, out value);

if (value is UnknownEntity && !this.Configuration.KeepUnknownEntities)
{
value = null;
result = false;
}

return result;
}
}
}
11 changes: 10 additions & 1 deletion ACadSharp/IO/DWG/DwgReaderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
public class DwgReaderConfiguration : CadReaderConfiguration
{
/// <summary>
/// Use the Standard Cycling Redundancy Check to verify the integrity of the file.
/// Use the Standard Cycling Redundancy Check to verify the integrity of the file, default value is set to false.
/// </summary>
/// <remarks>
/// DWG file format uses a modification of a standard Cyclic Redundancy Check as an error detecting mechanism,
/// if this flag is enabled the reader will perform this verification to detect any possible error, but it will greatly increase the reading time.
/// </remarks>
public bool CrcCheck { 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 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
/// </remarks>
public bool KeepUnknownEntities { get; set; } = false;
}
}
12 changes: 12 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamReaders/DwgClassesReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public DxfClassCollection Read(IDwgStreamReader sreader)
dxfClass.WasZombie = sreader.ReadBit();
//BS : itemclassid -- 0x1F2 for classes which produce entities, 0x1F3 for classes which produce objects.
dxfClass.ItemClassId = sreader.ReadBitShort();
if (dxfClass.ItemClassId == 0x1F2)
{
dxfClass.IsAnEntity = true;
}
else if (dxfClass.ItemClassId == 0x1F3)
{
dxfClass.IsAnEntity = false;
}
else
{
this.notify($"Invalid DxfClass id value: {dxfClass.ItemClassId} for {dxfClass.CppClassName}", NotificationType.Error);
}

if (this._fileHeader.AcadVersion >= ACadVersion.AC1018)
{
Expand Down
22 changes: 21 additions & 1 deletion ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ private CadTemplate readObject(ObjectType type)

switch (type)
{
case ObjectType.UNUSED:
case ObjectType.UNDEFINED:
break;
case ObjectType.TEXT:
template = this.readText();
Expand Down Expand Up @@ -1041,14 +1041,34 @@ private CadTemplate readUnlistedType(short classNumber)
break;
}

if (template == null && c.IsAnEntity)
{
template = this.readUnknownEntity(c);
this._builder.Notify($"Unlisted object with DXF name {c.DxfName} has been read as an UnknownEntity", NotificationType.Warning);
}

if (template == null)
{
this._builder.Notify($"Unlisted object not implemented, DXF name: {c.DxfName}", NotificationType.NotImplemented);
}

return template;
}

#region Text entities

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 readText()
{
TextEntity text = new TextEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void writeCommonData(CadObject cadObject)
}
break;
case ObjectType.INVALID:
case ObjectType.UNUSED:
case ObjectType.UNDEFINED:
this.notify($"CadObject type: {cadObject.ObjectType} fullname: {cadObject.GetType().FullName}", NotificationType.NotImplemented);
return;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private void writeEntity(Entity entity)
//Ignored Entities
switch (entity)
{
case UnknownEntity:
case AttributeEntity:
case Shape:
case Solid3D:
Expand Down Expand Up @@ -859,7 +860,7 @@ private void writeHatch(Hatch hatch)
for (var i = 0; i < pline.Vertices.Count; ++i)
{
var vertex = pline.Vertices[i];
var bulge = pline.Bulges[i];
var bulge = pline.Bulges[i];

this._writer.Write2RawDouble(new XY(vertex.X, vertex.Y));
if (pline.HasBulge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected void writeEntity<T>(T entity)
case Solid3D:
case MultiLeader:
case Wipeout:
case UnknownEntity:
this.notify($"Entity type not implemented : {entity.GetType().FullName}", NotificationType.NotImplemented);
return;
}
Expand Down
30 changes: 18 additions & 12 deletions ACadSharp/IO/Templates/CadBlockRecordTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,18 @@ public override void Build(CadDocumentBuilder builder)

if (this.FirstEntityHandle.HasValue)
{
var entities = this.getEntitiesCollection<Entity>(builder, this.FirstEntityHandle.Value, this.LastEntityHandle.Value);
this.CadObject.Entities.AddRange(entities);
foreach (Entity e in this.getEntitiesCollection<Entity>(builder, this.FirstEntityHandle.Value, this.LastEntityHandle.Value))
{
this.addEntity(e);
}
}
else
{
foreach (ulong handle in this.OwnedObjectsHandlers)
{
if (builder.TryGetCadObject<Entity>(handle, out Entity child))
{
switch (child)
{
case Viewport viewport:
this.CadObject.Viewports.Add(viewport);
break;
default:
this.CadObject.Entities.Add(child);
break;
}

this.addEntity(child);
}
}
}
Expand Down Expand Up @@ -87,5 +80,18 @@ public void SetBlockToRecord(CadDocumentBuilder builder)
this.CadObject.BlockEnd = blockEnd;
}
}

private void addEntity(Entity entity)
{
switch (entity)
{
case Viewport viewport:
this.CadObject.Viewports.Add(viewport);
break;
default:
this.CadObject.Entities.Add(entity);
break;
}
}
}
}
2 changes: 1 addition & 1 deletion ACadSharp/IO/Templates/CadTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ protected IEnumerable<T> getEntitiesCollection<T>(CadDocumentBuilder builder, ul
template = builder.GetObjectTemplate<CadEntityTemplate>(template.NextEntity.Value);
else
template = builder.GetObjectTemplate<CadEntityTemplate>(template.CadObject.Handle + 1);

}

return collection;
Expand All @@ -99,6 +98,7 @@ protected bool getTableReference<T>(CadDocumentBuilder builder, ulong? handle, s
{
builder.Notify($"{typeof(T).FullName} table reference with handle: {handle} | name: {name} not found for {this.CadObject.GetType().FullName} with handle {this.CadObject.Handle}", NotificationType.Warning);
}

return false;
}
}
Expand Down
9 changes: 9 additions & 0 deletions ACadSharp/IO/Templates/CadUnknownEntityTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ACadSharp.Entities;

namespace ACadSharp.IO.Templates
{
internal class CadUnknownEntityTemplate : CadEntityTemplate
{
public CadUnknownEntityTemplate(UnknownEntity entity) : base(entity) { }
}
}
2 changes: 1 addition & 1 deletion ACadSharp/Types/ObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum ObjectType : short
{
UNLISTED = -999,
INVALID = -1,
UNUSED = 0,
UNDEFINED = 0,

TEXT = 1,
ATTRIB = 2,
Expand Down
Loading