Skip to content

Commit

Permalink
Merge pull request #168 from DomCR/issue-166_seqend-management
Browse files Browse the repository at this point in the history
Seqend management
  • Loading branch information
DomCR authored Sep 17, 2023
2 parents de39194 + 6292481 commit 5f271f1
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 32 deletions.
10 changes: 10 additions & 0 deletions ACadSharp.Tests/CadDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public void RemoveCadObject()
Assert.Null(l.LineType.Document);
}

[Fact]
public void Get0HandleObject()
{
CadDocument doc = new CadDocument();

Assert.Null(doc.GetCadObject(0));
Assert.False(doc.TryGetCadObject(0, out CadObject cadObject));
Assert.Null(cadObject);
}

[Fact(Skip = "Implementation in branch : table-operations")]
public void RemoveLayer()
{
Expand Down
35 changes: 35 additions & 0 deletions ACadSharp.Tests/Entities/PolylineTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ACadSharp.Entities;
using CSMath;
using System.Collections.Generic;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class PolylineTest
{
[Fact]
public void ClearVertrticesTest()
{
CadDocument doc = new CadDocument();

Polyline2D polyline = new Polyline2D();
List<Vertex2D> vertices = new List<Vertex2D>
{
new Vertex2D(),
new Vertex2D(new XY(1,1)),
new Vertex2D(new XY(2,2))
};
polyline.Vertices.AddRange(vertices);

doc.Entities.Add(polyline);

polyline.Vertices.Clear();

foreach (var item in vertices)
{
Assert.True(item.Handle == 0);
Assert.False(doc.TryGetCadObject(item.Handle, out Vertex2D _));
}
}
}
}
91 changes: 91 additions & 0 deletions ACadSharp.Tests/Entities/SeqendTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using ACadSharp.Entities;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class SeqendTests
{
[Fact]
public void NullTest()
{
Polyline2D polyline = new Polyline2D();

Assert.Null(polyline.Vertices.Seqend);
}

[Fact]
public void CreatedTest()
{
Polyline2D polyline = new Polyline2D();
polyline.Vertices.Add(new Vertex2D());

Assert.NotNull(polyline.Vertices.Seqend);
Assert.True(polyline.Vertices.Seqend.Handle == 0);
Assert.NotEmpty(polyline.Vertices);
}

[Fact]
public void RemovedTest()
{
Polyline2D polyline = new Polyline2D();
polyline.Vertices.Add(new Vertex2D());

Assert.NotNull(polyline.Vertices.Seqend);
Assert.True(polyline.Vertices.Seqend.Handle == 0);
Assert.NotEmpty(polyline.Vertices);

polyline.Vertices.Clear(); // Empty collection

Assert.Null(polyline.Vertices.Seqend);
}

[Fact]
public void AddToDocumentTest()
{
CadDocument doc = new CadDocument();

Polyline2D polyline = new Polyline2D();
polyline.Vertices.Add(new Vertex2D());

doc.Entities.Add(polyline);

Assert.False(polyline.Vertices.Seqend.Handle == 0);
Seqend inner = doc.GetCadObject<Seqend>(polyline.Vertices.Seqend.Handle);
Assert.NotNull(inner);
}

[Fact]
public void OnSeqendAddedTest()
{
CadDocument doc = new CadDocument();

Polyline2D polyline = new Polyline2D();

doc.Entities.Add(polyline);

polyline.Vertices.Add(new Vertex2D());

Assert.False(polyline.Vertices.Seqend.Handle == 0);
Assert.True(doc.TryGetCadObject(polyline.Vertices.Seqend.Handle, out Seqend inner));
Assert.NotNull(inner);
}

[Fact]
public void OnSeqendRemovedTest()
{
CadDocument doc = new CadDocument();

Polyline2D polyline = new Polyline2D();
polyline.Vertices.Add(new Vertex2D());
Seqend holder = polyline.Vertices.Seqend;

doc.Entities.Add(polyline);

polyline.Vertices.Clear();

Assert.True(holder.Handle == 0);
Assert.False(doc.TryGetCadObject(holder.Handle, out Seqend inner));
Assert.Null(inner);
}
}
}
24 changes: 20 additions & 4 deletions ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public bool TryGetCadObject<T>(ulong handle, out T cadObject)
where T : CadObject
{
cadObject = null;

if (handle == this.Handle)
return false;

if (this._cadObjects.TryGetValue(handle, out IHandledCadObject obj))
{
cadObject = obj as T;
Expand Down Expand Up @@ -429,9 +433,15 @@ internal void RegisterCollection<T>(IObservableCollection<T> collection, bool ad
this.addCadObject(cadObject);
}

if (collection is ISeqendColleciton seqendColleciton)
if (collection is ISeqendCollection seqendColleciton)
{
this.addCadObject(seqendColleciton.Seqend);
seqendColleciton.OnSeqendAdded += this.onAdd;
seqendColleciton.OnSeqendRemoved += this.onRemove;

if (seqendColleciton.Seqend != null)
{
this.addCadObject(seqendColleciton.Seqend);
}
}

if (addElements)
Expand Down Expand Up @@ -475,9 +485,15 @@ internal void UnregisterCollection<T>(IObservableCollection<T> collection, bool
this.removeCadObject(cadObject);
}

if (collection is ISeqendColleciton seqendColleciton)
if (collection is ISeqendCollection seqendColleciton)
{
this.removeCadObject(seqendColleciton.Seqend);
seqendColleciton.OnSeqendAdded -= this.onAdd;
seqendColleciton.OnSeqendRemoved -= this.onRemove;

if (seqendColleciton.Seqend != null)
{
this.removeCadObject(seqendColleciton.Seqend);
}
}

if (removeElements)
Expand Down
16 changes: 11 additions & 5 deletions ACadSharp/CadObjectCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using CSUtilities.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp
{
Expand All @@ -21,7 +23,7 @@ public class CadObjectCollection<T> : IObservableCollection<T>
/// </summary>
public int Count { get { return this._entries.Count; } }

private readonly HashSet<T> _entries = new HashSet<T>();
protected readonly HashSet<T> _entries = new HashSet<T>();

public CadObjectCollection(CadObject owner)
{
Expand All @@ -34,7 +36,7 @@ public CadObjectCollection(CadObject owner)
/// <param name="item"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public void Add(T item)
public virtual void Add(T item)
{
if (item is null) throw new ArgumentNullException(nameof(item));

Expand Down Expand Up @@ -67,15 +69,19 @@ public void AddRange(IEnumerable<T> items)
/// </summary>
public void Clear()
{
this._entries.Clear();
Queue<T> q = new(this._entries.ToList());
while (q.TryDequeue(out T entry))
{
this.Remove(entry);
}
}

/// <summary>
/// Removes a <see cref="CadObject"/> from the collection, this method triggers <see cref="OnRemove"/>
/// </summary>
/// <param name="item"></param>
/// <returns>The removed <see cref="CadObject"/></returns>
public T Remove(T item)
public virtual T Remove(T item)
{
if (!this._entries.Remove(item))
return null;
Expand Down
8 changes: 8 additions & 0 deletions ACadSharp/Entities/Vertex2D.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ACadSharp.Attributes;
using CSMath;

namespace ACadSharp.Entities
{
Expand All @@ -18,5 +19,12 @@ public class Vertex2D : Vertex

/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.PolylineVertex;

public Vertex2D() { }

public Vertex2D(XY location)
{
this.Location = (XYZ)location;
}
}
}
8 changes: 4 additions & 4 deletions ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ protected void writeCollection(IEnumerable arr, DxfCode[] codes = null)
}
}

if (arr is ISeqendColleciton colleciton)
if (arr is ISeqendCollection collection && collection.Seqend != null)
{
this.writeMappedObject(colleciton.Seqend);
this.writeMappedObject(collection.Seqend);
}
}

Expand Down Expand Up @@ -239,12 +239,12 @@ private void writeSegment(LineType.Segment segment)
return;

this._writer.Write(75, segment.ShapeNumber);

if (segment.Style != null)
{
this._writer.Write(340, segment.Style.Handle);
}

this._writer.Write(46, segment.Scale);
this._writer.Write(50, segment.Rotation);
this._writer.Write(44, segment.Offset.X);
Expand Down
10 changes: 5 additions & 5 deletions ACadSharp/IO/Templates/CadInsertTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public override void Build(CadDocumentBuilder builder)
insert.Block = block;
}

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
insert.Attributes.Seqend = seqend;
}

if (this.FirstAttributeHandle.HasValue)
{
var attributes = getEntitiesCollection<AttributeEntity>(builder, FirstAttributeHandle.Value, EndAttributeHandle.Value);
Expand All @@ -59,11 +64,6 @@ public override void Build(CadDocumentBuilder builder)
}
}
}

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
insert.Attributes.Seqend = seqend;
}
}
}
}
10 changes: 5 additions & 5 deletions ACadSharp/IO/Templates/CadPolyLineTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public override void Build(CadDocumentBuilder builder)

Polyline polyLine = this.CadObject as Polyline;

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
polyLine.Vertices.Seqend = seqend;
}

if (this.FirstVertexHandle.HasValue)
{
IEnumerable<Vertex> vertices = this.getEntitiesCollection<Vertex>(builder, this.FirstVertexHandle.Value, this.LastVertexHandle.Value);
Expand All @@ -43,11 +48,6 @@ public override void Build(CadDocumentBuilder builder)
}
}
}

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
polyLine.Vertices.Seqend = seqend;
}
}

public void SetPolyLineObject(Polyline polyLine)
Expand Down
10 changes: 5 additions & 5 deletions ACadSharp/IO/Templates/CadPolyfaceMeshTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public override void Build(CadDocumentBuilder builder)

PolyfaceMesh polyfaceMesh = (PolyfaceMesh)this.CadObject;

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
polyfaceMesh.Vertices.Seqend = seqend;
}

if (this.FirstVerticeHandle.HasValue)
{
IEnumerable<Entity> vertices = this.getEntitiesCollection<Entity>(builder, this.FirstVerticeHandle.Value, this.LastVerticeHandle.Value);
Expand All @@ -39,11 +44,6 @@ public override void Build(CadDocumentBuilder builder)
}
}
}

if (builder.TryGetCadObject<Seqend>(this.SeqendHandle, out Seqend seqend))
{
polyfaceMesh.Vertices.Seqend = seqend;
}
}

private void addItemToPolyface(CadObject item, CadDocumentBuilder builder)
Expand Down
8 changes: 7 additions & 1 deletion ACadSharp/ISeqendColleciton.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using ACadSharp.Entities;
using System;
using System.Collections;

namespace ACadSharp
{
public interface ISeqendColleciton
public interface ISeqendCollection : IEnumerable
{
public event EventHandler<CollectionChangedEventArgs> OnSeqendAdded;

public event EventHandler<CollectionChangedEventArgs> OnSeqendRemoved;

Seqend Seqend { get; }
}
}
Loading

0 comments on commit 5f271f1

Please sign in to comment.