Skip to content

Commit

Permalink
Merge pull request #189 from DomCR/DxfReader-sortensTable-fix
Browse files Browse the repository at this point in the history
SortensTable Fix
  • Loading branch information
DomCR committed Jun 9, 2024
2 parents 7cbc513 + d9f94b2 commit 43fab9e
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 56 deletions.
4 changes: 1 addition & 3 deletions ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.IO;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using Xunit;

namespace ACadSharp.Tests.Tables.Collections
namespace ACadSharp.Tests.Tables
{
public class BlockRecordTests
{
Expand Down Expand Up @@ -57,6 +57,24 @@ public void NotAllowDuplicatesTest()
Assert.Throws<ArgumentException>(() => record.Entities.Add(l1));
}

[Fact()]
public void CreateSortensTableTest()
{
string name = "my_block";
BlockRecord record = new BlockRecord(name);

record.Entities.Add(new Line());
record.Entities.Add(new Line());
record.Entities.Add(new Line());
record.Entities.Add(new Line());

record.CreateSortEntitiesTable();

Assert.NotNull(record.SortEntitiesTable);
Assert.NotNull(record.SortEntitiesTable.Sorters);
Assert.Empty(record.SortEntitiesTable.Sorters);
}

[Fact()]
public void CloneTest()
{
Expand Down
19 changes: 18 additions & 1 deletion ACadSharp/CadObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public abstract class CadObject : IHandledCadObject
public IHandledCadObject Owner { get; internal set; }

/// <summary>
/// Object dictionary
/// Extended Dictionary object.
/// </summary>
/// <remarks>
/// An extended dictionary can be created using <see cref="CreateExtendedDictionary"/>
/// </remarks>
public CadDictionary XDictionary
{
get { return this._xdictionary; }
Expand Down Expand Up @@ -85,6 +88,20 @@ public CadDocument Document
/// </summary>
public CadObject() { }

/// <summary>
/// Creates the extended dictionary if null.
/// </summary>
/// <returns>The <see cref="CadDictionary"/> attached to this <see cref="CadObject"/></returns>
public CadDictionary CreateExtendedDictionary()
{
if (this._xdictionary == null)
{
this.XDictionary = new CadDictionary();
}

return this._xdictionary;
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5130,6 +5130,9 @@ private CadTemplate readSortentsTable()

this.readCommonNonEntityData(template);

//parenthandle (soft pointer)
template.BlockOwnerHandle = this.handleReference();

//Common:
//Numentries BL number of entries
int numentries = this._mergedReaders.ReadBitLong();
Expand All @@ -5147,9 +5150,6 @@ private CadTemplate readSortentsTable()
template.Values.Add((sortHandle, entityHandle));
}

//owner handle (soft pointer)
template.BlockOwnerHandle = this.handleReference();

return template;
}

Expand Down
25 changes: 25 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CSUtilities.Converters;
using CSUtilities.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -70,6 +71,9 @@ private void writeObject(CadObject obj)
case Scale scale:
this.writeScale(scale);
break;
case SortEntitiesTable sorttables:
this.writeSortEntitiesTable(sorttables);
break;
case XRecord record:
this.writeXRecord(record);
break;
Expand Down Expand Up @@ -431,6 +435,27 @@ private void writeScale(Scale scale)
this._writer.WriteBit(scale.IsUnitScale);
}

private void writeSortEntitiesTable(SortEntitiesTable sortEntitiesTable)
{
//parenthandle (soft pointer)
this._writer.HandleReference(DwgReferenceType.SoftPointer, sortEntitiesTable.BlockOwner);

//Common:
//Numentries BL number of entries
this._writer.WriteBitLong(sortEntitiesTable.Sorters.Count());

foreach (var item in sortEntitiesTable.Sorters)
{
//Sort handle(numentries of these, CODE 0, i.e.part of the main bit stream, not of the handle bit stream!).
//The sort handle does not have to point to an entity (but it can).
//This is just the handle used for determining the drawing order of the entity specified by the entity handle in the handle bit stream.
//When the sortentstable doesn’t have a
//mapping from entity handle to sort handle, then the entity’s own handle is used for sorting.
this._writer.HandleReference(item.Handle);
this._writer.HandleReference(DwgReferenceType.SoftPointer, item.Entity);
}
}

private void writeXRecord(XRecord xrecord)
{
MemoryStream stream = new MemoryStream();
Expand Down
2 changes: 1 addition & 1 deletion ACadSharp/IO/DWG/FileHeaders/DwgLocalSectionMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ACadSharp.IO.DWG
{
public class DwgLocalSectionMap
internal class DwgLocalSectionMap
{
public int Compression { get; set; } = 2;

Expand Down
1 change: 0 additions & 1 deletion ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public override CadHeader ReadHeader()
this.triggerNotification($"Invalid value for header variable {currVar} | {parameters.FirstOrDefault()}", NotificationType.Warning, ex);
}


this._reader.ReadNext();
}

Expand Down
22 changes: 8 additions & 14 deletions ACadSharp/IO/DXF/DxfStreamWriter/DxfObjectsSectionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected void writeObject<T>(T co)
case AcdbPlaceHolder:
case Material:
case MultiLeaderStyle:
case SortEntitiesTable:
case VisualStyle:
case ImageDefinitionReactor:
this.notify($"Object not implemented : {co.GetType().FullName}");
Expand Down Expand Up @@ -69,7 +68,7 @@ protected void writeObject<T>(T co)
this.writeScale(scale);
break;
case SortEntitiesTable sortensTable:
//this.writeSortentsTable(sortensTable);
this.writeSortentsTable(sortensTable);
break;
case XRecord record:
this.writeXRecord(record);
Expand Down Expand Up @@ -254,20 +253,15 @@ protected void writeMLineStyle(MLineStyle style)

private void writeSortentsTable(SortEntitiesTable e)
{
if (e.BlockOwner == null)
{
//In some cases the block onwer is null in the files, this has to be checked
this.notify("SortEntitiesTable with handle {e.Handle} has no block owner", NotificationType.Warning);
return;
}
this._writer.Write(DxfCode.Subclass, DxfSubclassMarker.SortentsTable);

this._writer.Write(DxfCode.Start, e.ObjectName);
this._writer.WriteHandle(330, e.BlockOwner);

this.writeCommonObjectData(e);

this._writer.Write(DxfCode.Subclass, DxfSubclassMarker.XRecord);

this._writer.Write(330, e.BlockOwner.Handle);
foreach (SortEntitiesTable.Sorter item in e.Sorters)
{
this._writer.WriteHandle(331, item.Entity);
this._writer.Write(5, item.Handle);
}
}

protected void writeXRecord(XRecord e)
Expand Down
16 changes: 6 additions & 10 deletions ACadSharp/IO/Templates/CadSortensTableTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ACadSharp.Blocks;
using ACadSharp.Entities;
using ACadSharp.Entities;
using ACadSharp.Objects;
using ACadSharp.Tables;
using System.Collections.Generic;
Expand All @@ -20,7 +19,7 @@ public override void Build(CadDocumentBuilder builder)
{
base.Build(builder);

if (builder.TryGetCadObject(BlockOwnerHandle, out CadObject owner))
if (builder.TryGetCadObject(this.BlockOwnerHandle, out CadObject owner))
{
// Not always a block
if (owner is BlockRecord record)
Expand All @@ -30,23 +29,20 @@ public override void Build(CadDocumentBuilder builder)
else if (owner is null)
{
builder.Notify($"Block owner for SortEntitiesTable {this.CadObject.Handle} not found", NotificationType.Warning);
return;
}
else
{
builder.Notify($"Block owner for SortEntitiesTable {this.CadObject.Handle} is not a block {owner.GetType().FullName} | {owner.Handle}", NotificationType.Warning);
return;
}
}

foreach ((ulong?, ulong?) pair in Values)
foreach ((ulong?, ulong?) pair in this.Values)
{
if (builder.TryGetCadObject(pair.Item2, out Entity entity))
{
SortEntitiesTable.Sorter sorter = new SortEntitiesTable.Sorter
{
SortHandle = pair.Item1.Value,
Entity = entity
};
this.CadObject.Sorters.Add(sorter);
this.CadObject.AddEntity(entity, pair.Item1.Value);
}
else
{
Expand Down
11 changes: 10 additions & 1 deletion ACadSharp/Objects/CadDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,15 @@ public static CadDictionary CreateRoot()
return root;
}

internal CadDictionary() { }
/// <summary>
/// Default constructor.
/// </summary>
public CadDictionary() { }

/// <summary>
/// Constructor for a named dictionary.
/// </summary>
/// <param name="name">Dictionary name.</param>
public CadDictionary(string name)
{
this.Name = name;
Expand Down Expand Up @@ -276,11 +283,13 @@ public bool TryGetEntry<T>(string name, out T value)
return false;
}

/// <inheritdoc/>
public IEnumerator<NonGraphicalObject> GetEnumerator()
{
return this._entries.Values.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return this._entries.Values.GetEnumerator();
Expand Down
51 changes: 51 additions & 0 deletions ACadSharp/Objects/SortEntitiesTable.Sorter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using ACadSharp.Attributes;
using ACadSharp.Entities;

namespace ACadSharp.Objects
{
public partial class SortEntitiesTable
{
/// <summary>
/// Entity sorter based in their position in the collection.
/// </summary>
public class Sorter
{
/// <inheritdoc/>
[DxfCodeValue(5)]
public ulong Handle
{
get
{
if (this._handle.HasValue)
{
return this._handle.Value;
}
else
{
return this.Entity.Handle;
}
}
internal set { this._handle = value; }
}

/// <summary>
/// Soft-pointer ID/handle to an entity
/// </summary>
[DxfCodeValue(331)]
public Entity Entity { get; set; }

private ulong? _handle;

/// <summary>
/// Sorter constructor with the entity and handle.
/// </summary>
/// <param name="entity">Enity in the block to be sorted.</param>
/// <param name="handle">Sorter handle, will use the entity handle if null.</param>
public Sorter(Entity entity, ulong? handle = null)
{
this.Entity = entity;
this._handle = handle;
}
}
}
}
Loading

0 comments on commit 43fab9e

Please sign in to comment.