Skip to content

Commit

Permalink
HatchTests
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Aug 19, 2024
1 parent d7aac2f commit 9281c34
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 9 deletions.
117 changes: 117 additions & 0 deletions src/ACadSharp.Tests/Entities/HatchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using ACadSharp.Entities;
using CSMath;
using CSUtilities.Extensions;
using System;
using System.Collections.Generic;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class HatchTests
{
[Fact]
public void CreateHatch()
{
Hatch hatch = new Hatch();
hatch.IsSolid = true;

hatch.SeedPoints.Add(new XY());

List<Hatch.BoundaryPath.Line> edges = new List<Hatch.BoundaryPath.Line>();

//edges
Hatch.BoundaryPath.Line edge1 = new Hatch.BoundaryPath.Line
{
Start = new CSMath.XY(0, 0),
End = new CSMath.XY(1, 0)
};
edges.Add(edge1);

Hatch.BoundaryPath.Line edge2 = new Hatch.BoundaryPath.Line
{
Start = new CSMath.XY(1, 0),
End = new CSMath.XY(1, 1)
};
edges.Add(edge2);

Hatch.BoundaryPath.Line edge3 = new Hatch.BoundaryPath.Line
{
Start = new CSMath.XY(1, 1),
End = new CSMath.XY(0, 1)
};
edges.Add(edge3);

Hatch.BoundaryPath.Line edge4 = new Hatch.BoundaryPath.Line
{
Start = new CSMath.XY(0, 1),
End = new CSMath.XY(0, 0)
};
edges.Add(edge4);


Hatch.BoundaryPath path = new Hatch.BoundaryPath();
foreach (var item in edges)
{
path.Edges.Add(item);
}

hatch.Paths.Add(path);

Assert.NotEmpty(hatch.Paths);
Assert.NotEmpty(path.Edges);
Assert.False(path.IsPolyline);
}

[Fact]
public void CreatePolylineHatch()
{
Hatch hatch = new Hatch();
hatch.IsSolid = true;

Hatch.BoundaryPath path = new Hatch.BoundaryPath();

Hatch.BoundaryPath.Polyline pline = new Hatch.BoundaryPath.Polyline();
pline.Vertices.Add(new XYZ(0, 0, 0));
pline.Vertices.Add(new XYZ(1, 0, 0));
pline.Vertices.Add(new XYZ(1, 1, 0));
pline.Vertices.Add(new XYZ(0, 1, 0));
pline.Vertices.Add(new XYZ(0, 0, 0));

path.Edges.Add(pline);
path.Flags = path.Flags.AddFlag(BoundaryPathFlags.Polyline);
hatch.Paths.Add(path);

Assert.True(path.IsPolyline);
}

[Fact]
public void PolylineHatchNotAllowMoreEdges()
{
Hatch hatch = new Hatch();
hatch.IsSolid = true;

Hatch.BoundaryPath path = new Hatch.BoundaryPath();

Hatch.BoundaryPath.Polyline pline = new Hatch.BoundaryPath.Polyline();
pline.Vertices.Add(new XYZ(0, 0, 0));
pline.Vertices.Add(new XYZ(1, 0, 0));
pline.Vertices.Add(new XYZ(1, 1, 0));
pline.Vertices.Add(new XYZ(0, 1, 0));
pline.Vertices.Add(new XYZ(0, 0, 0));

path.Edges.Add(pline);

Assert.Throws<InvalidOperationException>(() =>
{
path.Edges.Add(new Hatch.BoundaryPath.Line());
}
);

Assert.Throws<InvalidOperationException>(() =>
{
path.Edges.Add(new Hatch.BoundaryPath.Polyline());
}
);
}
}
}
6 changes: 5 additions & 1 deletion src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,11 @@ public void CreateHatch()


Hatch.BoundaryPath path = new Hatch.BoundaryPath();
path.Edges.AddRange(edges);
foreach (var item in edges)
{
path.Edges.Add(item);
}

hatch.Paths.Add(path);

this.Document.Entities.Add(hatch);
Expand Down
4 changes: 2 additions & 2 deletions src/ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private void onRemove(object sender, CollectionChangedEventArgs e)
}
}

internal void RegisterCollection<T>(IObservableCollection<T> collection)
internal void RegisterCollection<T>(IObservableCadCollection<T> collection)
where T : CadObject
{
switch (collection)
Expand Down Expand Up @@ -498,7 +498,7 @@ internal void RegisterCollection<T>(IObservableCollection<T> collection)
}
}

internal void UnregisterCollection<T>(IObservableCollection<T> collection)
internal void UnregisterCollection<T>(IObservableCadCollection<T> collection)
where T : CadObject
{
switch (collection)
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/CadObjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ACadSharp
{
public class CadObjectCollection<T> : IObservableCollection<T>
public class CadObjectCollection<T> : IObservableCadCollection<T>
where T : CadObject
{
public event EventHandler<CollectionChangedEventArgs> OnAdd;
Expand Down
65 changes: 63 additions & 2 deletions src/ACadSharp/Entities/Hatch.BoundaryPath.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
using ACadSharp.Attributes;
using CSUtilities.Extensions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace ACadSharp.Entities
{
public partial class Hatch
{
public partial class BoundaryPath
{
public bool IsPolyline { get { return this.Edges.OfType<Polyline>().Any(); } }

/// <summary>
/// Boundary path type flag
/// </summary>
[DxfCodeValue(92)]
public BoundaryPathFlags Flags { get; set; }
public BoundaryPathFlags Flags
{
get
{
if (this.IsPolyline)
{
this._flags = this._flags.AddFlag(BoundaryPathFlags.Polyline);
}
else
{
this._flags = this._flags.RemoveFlag(BoundaryPathFlags.Polyline);
}

return this._flags;
}
set
{
this._flags = value;
}
}

/// <summary>
/// Number of edges in this boundary path
Expand All @@ -20,18 +45,54 @@ public partial class BoundaryPath
/// only if boundary is not a polyline
/// </remarks>
[DxfCodeValue(DxfReferenceType.Count, 93)]
public List<Edge> Edges { get; set; } = new List<Edge>();
public ObservableCollection<Edge> Edges { get; } = new();

/// <summary>
/// Source boundary objects
/// </summary>
[DxfCodeValue(DxfReferenceType.Count, 97)]
public List<Entity> Entities { get; set; } = new List<Entity>();

private BoundaryPathFlags _flags;

public BoundaryPath()
{
Edges.CollectionChanged += this.onEdgesCollectionChanged;
}

public BoundaryPath Clone()
{
throw new System.NotImplementedException();
}

private void onEdgesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
onAdd(e);
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
break;
}
}

private void onAdd(NotifyCollectionChangedEventArgs e)
{
foreach (Edge edge in e.NewItems)
{
if (this.Edges.Count > 1 && this.IsPolyline)
{
throw new System.InvalidOperationException();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ACadSharp
{
public interface IObservableCollection<T> : IEnumerable<T>
public interface IObservableCadCollection<T> : IEnumerable<T>
where T : CadObject
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/Objects/CadDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ACadSharp.Objects
/// </remarks>
[DxfName(DxfFileToken.ObjectDictionary)]
[DxfSubClass(DxfSubclassMarker.Dictionary)]
public class CadDictionary : NonGraphicalObject, IObservableCollection<NonGraphicalObject>
public class CadDictionary : NonGraphicalObject, IObservableCadCollection<NonGraphicalObject>
{
public event EventHandler<CollectionChangedEventArgs> OnAdd;
public event EventHandler<CollectionChangedEventArgs> OnRemove;
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/Tables/Collections/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace ACadSharp.Tables.Collections
{
[DxfSubClass(DxfSubclassMarker.Table)]
public abstract class Table<T> : CadObject, ITable, IObservableCollection<T>
public abstract class Table<T> : CadObject, ITable, IObservableCadCollection<T>
where T : TableEntry
{
public event EventHandler<CollectionChangedEventArgs> OnAdd;
Expand Down

0 comments on commit 9281c34

Please sign in to comment.