Skip to content

Commit 43fab9e

Browse files
authored
Merge pull request #189 from DomCR/DxfReader-sortensTable-fix
SortensTable Fix
2 parents 7cbc513 + d9f94b2 commit 43fab9e

File tree

13 files changed

+234
-56
lines changed

13 files changed

+234
-56
lines changed

ACadSharp.Tests/IO/LocalSampleTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using ACadSharp.Entities;
2-
using ACadSharp.IO;
1+
using ACadSharp.IO;
32
using System.IO;
4-
using System.Linq;
53
using Xunit;
64
using Xunit.Abstractions;
75

ACadSharp.Tests/Tables/Collections/BlockRecordTests.cs renamed to ACadSharp.Tests/Tables/BlockRecordTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Linq;
66
using Xunit;
77

8-
namespace ACadSharp.Tests.Tables.Collections
8+
namespace ACadSharp.Tests.Tables
99
{
1010
public class BlockRecordTests
1111
{
@@ -57,6 +57,24 @@ public void NotAllowDuplicatesTest()
5757
Assert.Throws<ArgumentException>(() => record.Entities.Add(l1));
5858
}
5959

60+
[Fact()]
61+
public void CreateSortensTableTest()
62+
{
63+
string name = "my_block";
64+
BlockRecord record = new BlockRecord(name);
65+
66+
record.Entities.Add(new Line());
67+
record.Entities.Add(new Line());
68+
record.Entities.Add(new Line());
69+
record.Entities.Add(new Line());
70+
71+
record.CreateSortEntitiesTable();
72+
73+
Assert.NotNull(record.SortEntitiesTable);
74+
Assert.NotNull(record.SortEntitiesTable.Sorters);
75+
Assert.Empty(record.SortEntitiesTable.Sorters);
76+
}
77+
6078
[Fact()]
6179
public void CloneTest()
6280
{

ACadSharp/CadObject.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public abstract class CadObject : IHandledCadObject
4141
public IHandledCadObject Owner { get; internal set; }
4242

4343
/// <summary>
44-
/// Object dictionary
44+
/// Extended Dictionary object.
4545
/// </summary>
46+
/// <remarks>
47+
/// An extended dictionary can be created using <see cref="CreateExtendedDictionary"/>
48+
/// </remarks>
4649
public CadDictionary XDictionary
4750
{
4851
get { return this._xdictionary; }
@@ -85,6 +88,20 @@ public CadDocument Document
8588
/// </summary>
8689
public CadObject() { }
8790

91+
/// <summary>
92+
/// Creates the extended dictionary if null.
93+
/// </summary>
94+
/// <returns>The <see cref="CadDictionary"/> attached to this <see cref="CadObject"/></returns>
95+
public CadDictionary CreateExtendedDictionary()
96+
{
97+
if (this._xdictionary == null)
98+
{
99+
this.XDictionary = new CadDictionary();
100+
}
101+
102+
return this._xdictionary;
103+
}
104+
88105
/// <summary>
89106
/// Creates a new object that is a copy of the current instance.
90107
/// </summary>

ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5130,6 +5130,9 @@ private CadTemplate readSortentsTable()
51305130

51315131
this.readCommonNonEntityData(template);
51325132

5133+
//parenthandle (soft pointer)
5134+
template.BlockOwnerHandle = this.handleReference();
5135+
51335136
//Common:
51345137
//Numentries BL number of entries
51355138
int numentries = this._mergedReaders.ReadBitLong();
@@ -5147,9 +5150,6 @@ private CadTemplate readSortentsTable()
51475150
template.Values.Add((sortHandle, entityHandle));
51485151
}
51495152

5150-
//owner handle (soft pointer)
5151-
template.BlockOwnerHandle = this.handleReference();
5152-
51535153
return template;
51545154
}
51555155

ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Objects.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CSUtilities.Converters;
33
using CSUtilities.IO;
44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78

@@ -70,6 +71,9 @@ private void writeObject(CadObject obj)
7071
case Scale scale:
7172
this.writeScale(scale);
7273
break;
74+
case SortEntitiesTable sorttables:
75+
this.writeSortEntitiesTable(sorttables);
76+
break;
7377
case XRecord record:
7478
this.writeXRecord(record);
7579
break;
@@ -431,6 +435,27 @@ private void writeScale(Scale scale)
431435
this._writer.WriteBit(scale.IsUnitScale);
432436
}
433437

438+
private void writeSortEntitiesTable(SortEntitiesTable sortEntitiesTable)
439+
{
440+
//parenthandle (soft pointer)
441+
this._writer.HandleReference(DwgReferenceType.SoftPointer, sortEntitiesTable.BlockOwner);
442+
443+
//Common:
444+
//Numentries BL number of entries
445+
this._writer.WriteBitLong(sortEntitiesTable.Sorters.Count());
446+
447+
foreach (var item in sortEntitiesTable.Sorters)
448+
{
449+
//Sort handle(numentries of these, CODE 0, i.e.part of the main bit stream, not of the handle bit stream!).
450+
//The sort handle does not have to point to an entity (but it can).
451+
//This is just the handle used for determining the drawing order of the entity specified by the entity handle in the handle bit stream.
452+
//When the sortentstable doesn’t have a
453+
//mapping from entity handle to sort handle, then the entity’s own handle is used for sorting.
454+
this._writer.HandleReference(item.Handle);
455+
this._writer.HandleReference(DwgReferenceType.SoftPointer, item.Entity);
456+
}
457+
}
458+
434459
private void writeXRecord(XRecord xrecord)
435460
{
436461
MemoryStream stream = new MemoryStream();

ACadSharp/IO/DWG/FileHeaders/DwgLocalSectionMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ACadSharp.IO.DWG
22
{
3-
public class DwgLocalSectionMap
3+
internal class DwgLocalSectionMap
44
{
55
public int Compression { get; set; } = 2;
66

ACadSharp/IO/DXF/DxfReader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ public override CadHeader ReadHeader()
211211
this.triggerNotification($"Invalid value for header variable {currVar} | {parameters.FirstOrDefault()}", NotificationType.Warning, ex);
212212
}
213213

214-
215214
this._reader.ReadNext();
216215
}
217216

ACadSharp/IO/DXF/DxfStreamWriter/DxfObjectsSectionWriter.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ protected void writeObject<T>(T co)
3131
case AcdbPlaceHolder:
3232
case Material:
3333
case MultiLeaderStyle:
34-
case SortEntitiesTable:
3534
case VisualStyle:
3635
case ImageDefinitionReactor:
3736
this.notify($"Object not implemented : {co.GetType().FullName}");
@@ -69,7 +68,7 @@ protected void writeObject<T>(T co)
6968
this.writeScale(scale);
7069
break;
7170
case SortEntitiesTable sortensTable:
72-
//this.writeSortentsTable(sortensTable);
71+
this.writeSortentsTable(sortensTable);
7372
break;
7473
case XRecord record:
7574
this.writeXRecord(record);
@@ -254,20 +253,15 @@ protected void writeMLineStyle(MLineStyle style)
254253

255254
private void writeSortentsTable(SortEntitiesTable e)
256255
{
257-
if (e.BlockOwner == null)
258-
{
259-
//In some cases the block onwer is null in the files, this has to be checked
260-
this.notify("SortEntitiesTable with handle {e.Handle} has no block owner", NotificationType.Warning);
261-
return;
262-
}
256+
this._writer.Write(DxfCode.Subclass, DxfSubclassMarker.SortentsTable);
263257

264-
this._writer.Write(DxfCode.Start, e.ObjectName);
258+
this._writer.WriteHandle(330, e.BlockOwner);
265259

266-
this.writeCommonObjectData(e);
267-
268-
this._writer.Write(DxfCode.Subclass, DxfSubclassMarker.XRecord);
269-
270-
this._writer.Write(330, e.BlockOwner.Handle);
260+
foreach (SortEntitiesTable.Sorter item in e.Sorters)
261+
{
262+
this._writer.WriteHandle(331, item.Entity);
263+
this._writer.Write(5, item.Handle);
264+
}
271265
}
272266

273267
protected void writeXRecord(XRecord e)

ACadSharp/IO/Templates/CadSortensTableTemplate.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using ACadSharp.Blocks;
2-
using ACadSharp.Entities;
1+
using ACadSharp.Entities;
32
using ACadSharp.Objects;
43
using ACadSharp.Tables;
54
using System.Collections.Generic;
@@ -20,7 +19,7 @@ public override void Build(CadDocumentBuilder builder)
2019
{
2120
base.Build(builder);
2221

23-
if (builder.TryGetCadObject(BlockOwnerHandle, out CadObject owner))
22+
if (builder.TryGetCadObject(this.BlockOwnerHandle, out CadObject owner))
2423
{
2524
// Not always a block
2625
if (owner is BlockRecord record)
@@ -30,23 +29,20 @@ public override void Build(CadDocumentBuilder builder)
3029
else if (owner is null)
3130
{
3231
builder.Notify($"Block owner for SortEntitiesTable {this.CadObject.Handle} not found", NotificationType.Warning);
32+
return;
3333
}
3434
else
3535
{
3636
builder.Notify($"Block owner for SortEntitiesTable {this.CadObject.Handle} is not a block {owner.GetType().FullName} | {owner.Handle}", NotificationType.Warning);
37+
return;
3738
}
3839
}
3940

40-
foreach ((ulong?, ulong?) pair in Values)
41+
foreach ((ulong?, ulong?) pair in this.Values)
4142
{
4243
if (builder.TryGetCadObject(pair.Item2, out Entity entity))
4344
{
44-
SortEntitiesTable.Sorter sorter = new SortEntitiesTable.Sorter
45-
{
46-
SortHandle = pair.Item1.Value,
47-
Entity = entity
48-
};
49-
this.CadObject.Sorters.Add(sorter);
45+
this.CadObject.AddEntity(entity, pair.Item1.Value);
5046
}
5147
else
5248
{

ACadSharp/Objects/CadDictionary.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,15 @@ public static CadDictionary CreateRoot()
198198
return root;
199199
}
200200

201-
internal CadDictionary() { }
201+
/// <summary>
202+
/// Default constructor.
203+
/// </summary>
204+
public CadDictionary() { }
202205

206+
/// <summary>
207+
/// Constructor for a named dictionary.
208+
/// </summary>
209+
/// <param name="name">Dictionary name.</param>
203210
public CadDictionary(string name)
204211
{
205212
this.Name = name;
@@ -276,11 +283,13 @@ public bool TryGetEntry<T>(string name, out T value)
276283
return false;
277284
}
278285

286+
/// <inheritdoc/>
279287
public IEnumerator<NonGraphicalObject> GetEnumerator()
280288
{
281289
return this._entries.Values.GetEnumerator();
282290
}
283291

292+
/// <inheritdoc/>
284293
IEnumerator IEnumerable.GetEnumerator()
285294
{
286295
return this._entries.Values.GetEnumerator();

0 commit comments

Comments
 (0)