Skip to content

Commit 7d5174d

Browse files
authored
Merge pull request #525 from DomCR/issue-524_explode-hatch
Issue-524 Explode Hatch
2 parents 04434c5 + f586b60 commit 7d5174d

22 files changed

+223
-35
lines changed

src/ACadSharp.Tests/Entities/HatchTests.cs

+36-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public void CreateHatch()
4848
};
4949
edges.Add(edge4);
5050

51-
5251
Hatch.BoundaryPath path = new Hatch.BoundaryPath();
5352
foreach (var item in edges)
5453
{
@@ -85,7 +84,7 @@ public void CreatePolylineHatch()
8584
}
8685

8786
[Fact]
88-
public void PolylineHatchNotAllowMoreEdges()
87+
public void ExplodeTest()
8988
{
9089
Hatch hatch = new Hatch();
9190
hatch.IsSolid = true;
@@ -100,18 +99,12 @@ public void PolylineHatchNotAllowMoreEdges()
10099
pline.Vertices.Add(new XYZ(0, 0, 0));
101100

102101
path.Edges.Add(pline);
102+
path.Flags = path.Flags.AddFlag(BoundaryPathFlags.Polyline);
103+
hatch.Paths.Add(path);
103104

104-
Assert.Throws<InvalidOperationException>(() =>
105-
{
106-
path.Edges.Add(new Hatch.BoundaryPath.Line());
107-
}
108-
);
105+
var entities = hatch.Explode();
109106

110-
Assert.Throws<InvalidOperationException>(() =>
111-
{
112-
path.Edges.Add(new Hatch.BoundaryPath.Polyline());
113-
}
114-
);
107+
Assert.NotEmpty(entities);
115108
}
116109

117110
[Fact]
@@ -138,5 +131,35 @@ public void GetBoundingBoxTest()
138131
Assert.Equal(new XYZ(0, 0, 0), box.Min);
139132
Assert.Equal(new XYZ(1, 1, 0), box.Max);
140133
}
134+
135+
[Fact]
136+
public void PolylineHatchNotAllowMoreEdges()
137+
{
138+
Hatch hatch = new Hatch();
139+
hatch.IsSolid = true;
140+
141+
Hatch.BoundaryPath path = new Hatch.BoundaryPath();
142+
143+
Hatch.BoundaryPath.Polyline pline = new Hatch.BoundaryPath.Polyline();
144+
pline.Vertices.Add(new XYZ(0, 0, 0));
145+
pline.Vertices.Add(new XYZ(1, 0, 0));
146+
pline.Vertices.Add(new XYZ(1, 1, 0));
147+
pline.Vertices.Add(new XYZ(0, 1, 0));
148+
pline.Vertices.Add(new XYZ(0, 0, 0));
149+
150+
path.Edges.Add(pline);
151+
152+
Assert.Throws<InvalidOperationException>(() =>
153+
{
154+
path.Edges.Add(new Hatch.BoundaryPath.Line());
155+
}
156+
);
157+
158+
Assert.Throws<InvalidOperationException>(() =>
159+
{
160+
path.Edges.Add(new Hatch.BoundaryPath.Polyline());
161+
}
162+
);
163+
}
141164
}
142-
}
165+
}

src/ACadSharp/ACadSharp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PropertyGroup>
1818
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
20-
<Version>1.1.4</Version>
20+
<Version>1.1.5</Version>
2121
<PackageOutputPath>../nupkg</PackageOutputPath>
2222
</PropertyGroup>
2323

src/ACadSharp/Entities/Hatch.BoundaryPath.Arc.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using ACadSharp.Attributes;
2+
using ACadSharp.IO.DXF;
23
using CSMath;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
37

48
namespace ACadSharp.Entities
59
{
@@ -48,7 +52,40 @@ public class Arc : Edge
4852
/// <inheritdoc/>
4953
public override BoundingBox GetBoundingBox()
5054
{
51-
throw new System.NotImplementedException();
55+
return ((Entities.Arc)this.ToEntity()).GetBoundingBox();
56+
}
57+
58+
/// <summary>
59+
/// Converts the arc in a list of vertexes.
60+
/// </summary>
61+
/// <param name="precision">Number of vertexes generated.</param>
62+
/// <returns>A list vertexes that represents the arc expressed in object coordinate system.</returns>
63+
public List<XY> PolygonalVertexes(int precision)
64+
{
65+
return ((Entities.Arc)this.ToEntity()).PolygonalVertexes(precision);
66+
}
67+
68+
/// <inheritdoc/>
69+
public override Entity ToEntity()
70+
{
71+
if (this.CounterClockWise)
72+
{
73+
return new ACadSharp.Entities.Arc
74+
{
75+
Center = (XYZ)this.Center,
76+
Radius = this.Radius,
77+
StartAngle = this.StartAngle,
78+
EndAngle = this.EndAngle
79+
};
80+
}
81+
82+
return new ACadSharp.Entities.Arc
83+
{
84+
Center = (XYZ)this.Center,
85+
Radius = this.Radius,
86+
StartAngle = 2 * Math.PI - this.EndAngle,
87+
EndAngle = 2 * Math.PI - this.StartAngle
88+
};
5289
}
5390
}
5491
}

src/ACadSharp/Entities/Hatch.BoundaryPath.Edge.cs

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public Edge Clone()
3939

4040
/// <inheritdoc/>
4141
public abstract BoundingBox GetBoundingBox();
42+
43+
/// <summary>
44+
/// Create the equivalent entity for this Edge.
45+
/// </summary>
46+
/// <returns></returns>
47+
public abstract Entity ToEntity();
4248
}
4349
}
4450
}

src/ACadSharp/Entities/Hatch.BoundaryPath.Ellipse.cs

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ACadSharp.Attributes;
2+
using ACadSharp.IO.DXF;
23
using CSMath;
34

45
namespace ACadSharp.Entities
@@ -13,45 +14,62 @@ public class Ellipse : Edge
1314
public override EdgeType Type => EdgeType.EllipticArc;
1415

1516
/// <summary>
16-
/// Center point (in OCS)
17+
/// Center point (in OCS).
1718
/// </summary>
1819
[DxfCodeValue(10, 20)]
1920
public XY Center { get; set; }
2021

2122
/// <summary>
22-
/// Endpoint of major axis relative to center point (in OCS)
23+
/// Endpoint of major axis relative to center point (in OCS).
2324
/// </summary>
2425
[DxfCodeValue(11, 21)]
2526
public XY MajorAxisEndPoint { get; set; }
2627

2728
/// <summary>
28-
/// Length of minor axis (percentage of major axis length)
29+
/// Length of minor axis (percentage of major axis length).
2930
/// </summary>
3031
[DxfCodeValue(40)]
3132
public double MinorToMajorRatio { get; set; }
3233

3334
/// <summary>
34-
/// Start angle
35+
/// Start angle.
3536
/// </summary>
3637
[DxfCodeValue(50)]
3738
public double StartAngle { get; set; }
3839

3940
/// <summary>
40-
/// End angle
41+
/// End angle.
4142
/// </summary>
4243
[DxfCodeValue(51)]
4344
public double EndAngle { get; set; }
4445

4546
/// <summary>
46-
/// Is counterclockwise flag
47+
/// Is counterclockwise flag.
4748
/// </summary>
4849
[DxfCodeValue(73)]
49-
public bool CounterClockWise { get; set; }
50+
public bool IsCounterclockwise { get; set; }
51+
52+
/// <inheritdoc/>
53+
public override Entity ToEntity()
54+
{
55+
XYZ center = new XYZ(this.Center.X, this.Center.Y, 0.0);
56+
XYZ axisPoint = new XYZ(this.MajorAxisEndPoint.X, this.MajorAxisEndPoint.Y, 0.0);
57+
58+
double rotation = axisPoint.Convert<XY>().GetAngle();
59+
double majorAxis = 2 * axisPoint.GetLength();
60+
61+
Entities.Ellipse ellipse = new();
62+
ellipse.Center = center;
63+
ellipse.StartParameter = this.IsCounterclockwise ? this.StartAngle : 360 - this.EndAngle;
64+
ellipse.EndParameter = this.IsCounterclockwise ? this.EndAngle : 360 - this.StartAngle;
65+
66+
return ellipse;
67+
}
5068

5169
/// <inheritdoc/>
5270
public override BoundingBox GetBoundingBox()
5371
{
54-
throw new System.NotImplementedException();
72+
return this.ToEntity().GetBoundingBox();
5573
}
5674
}
5775
}

src/ACadSharp/Entities/Hatch.BoundaryPath.Line.cs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class Line : Edge
2323
[DxfCodeValue(11, 21)]
2424
public XY End { get; set; }
2525

26+
/// <inheritdoc/>
27+
public override Entity ToEntity()
28+
{
29+
return new Entities.Line(this.Start, this.End);
30+
}
2631

2732
/// <inheritdoc/>
2833
public override BoundingBox GetBoundingBox()

src/ACadSharp/Entities/Hatch.BoundaryPath.Polyline.cs

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public class Polyline : Edge
4444
[DxfCodeValue(DxfReferenceType.Count, 93)]
4545
public List<XYZ> Vertices { get; set; } = new();
4646

47+
/// <inheritdoc/>
48+
public override Entity ToEntity()
49+
{
50+
List<Vertex> vertices = new();
51+
foreach (XYZ v in this.Vertices)
52+
{
53+
vertices.Add(new Vertex2D(v));
54+
}
55+
56+
return new Polyline2D(vertices.Cast<Vertex2D>(), this.IsClosed);
57+
}
58+
4759
/// <inheritdoc/>
4860
public override BoundingBox GetBoundingBox()
4961
{

src/ACadSharp/Entities/Hatch.BoundaryPath.Spline.cs

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using ACadSharp.Attributes;
22
using CSMath;
3+
using CSUtilities.Extensions;
34
using System.Collections.Generic;
5+
using System.Linq;
46

57
namespace ACadSharp.Entities
68
{
@@ -62,6 +64,25 @@ public class Spline : Edge
6264
[DxfCodeValue(13, 23)]
6365
public XY EndTangent { get; set; }
6466

67+
/// <inheritdoc/>
68+
public override Entity ToEntity()
69+
{
70+
Entities.Spline spline = new();
71+
72+
spline.Degree = this.Degree;
73+
spline.Flags = this.Periodic ? spline.Flags.AddFlag(SplineFlags.Periodic) : spline.Flags;
74+
spline.Flags = this.Rational ? spline.Flags.AddFlag(SplineFlags.Rational) : spline.Flags;
75+
76+
spline.StartTangent = this.StartTangent.Convert<XYZ>();
77+
spline.EndTangent = this.EndTangent.Convert<XYZ>();
78+
79+
spline.ControlPoints.AddRange(this.ControlPoints);
80+
spline.Weights.AddRange(this.ControlPoints.Select(x => x.Z));
81+
spline.FitPoints.AddRange(this.FitPoints.Select(x => x.Convert<XYZ>()));
82+
83+
return spline;
84+
}
85+
6586
/// <inheritdoc/>
6687
public override BoundingBox GetBoundingBox()
6788
{

src/ACadSharp/Entities/Hatch.cs

+19
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ public partial class Hatch : Entity
130130
/// <inheritdoc/>
131131
public Hatch() : base() { }
132132

133+
/// <summary>
134+
/// Explode the hatch edges into the equivalent entities.
135+
/// </summary>
136+
/// <returns></returns>
137+
public IEnumerable<Entity> Explode()
138+
{
139+
List<Entity> entities = new List<Entity>();
140+
141+
foreach (BoundaryPath b in Paths)
142+
{
143+
foreach (BoundaryPath.Edge e in b.Edges)
144+
{
145+
entities.Add(e.ToEntity());
146+
}
147+
}
148+
149+
return entities;
150+
}
151+
133152
/// <inheritdoc/>
134153
public override BoundingBox GetBoundingBox()
135154
{

src/ACadSharp/Entities/Line.cs

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public class Line : Entity
5252
/// </summary>
5353
public Line() : base() { }
5454

55+
/// <summary>
56+
/// Constructor with the start and end.
57+
/// </summary>
58+
/// <param name="start"></param>
59+
/// <param name="end"></param>
60+
public Line(IVector start, IVector end) : base()
61+
{
62+
this.StartPoint = start.Convert<XYZ>();
63+
this.EndPoint = end.Convert<XYZ>();
64+
}
65+
5566
/// <summary>
5667
/// Constructor with the start and end.
5768
/// </summary>

src/ACadSharp/Entities/PolyLine.cs

+9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public bool IsClosed
9393
public Polyline() : base()
9494
{
9595
this.Vertices = new SeqendCollection<Vertex>(this);
96+
this.Vertices.OnAdd += this.verticesOnAdd;
97+
}
98+
99+
public Polyline(IEnumerable<Vertex> vertices, bool isColsed) : this()
100+
{
101+
this.Vertices.AddRange(vertices);
102+
this.IsClosed = isColsed;
96103
}
97104

98105
/// <inheritdoc/>
@@ -124,6 +131,8 @@ public override BoundingBox GetBoundingBox()
124131
return new BoundingBox(min, max);
125132
}
126133

134+
protected abstract void verticesOnAdd(object sender, CollectionChangedEventArgs e);
135+
127136
internal static IEnumerable<Entity> Explode(IPolyline polyline)
128137
{
129138
//Generic explode method for Polyline2D and LwPolyline

src/ACadSharp/Entities/PolyLine2D.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ public class Polyline2D : Polyline
2222
/// <inheritdoc/>
2323
public override string SubclassMarker => DxfSubclassMarker.Polyline;
2424

25+
/// <inheritdoc/>
2526
public Polyline2D() : base()
2627
{
27-
this.Vertices.OnAdd += this.verticesOnAdd;
2828
}
2929

30+
public Polyline2D(IEnumerable<Vertex2D> vertices, bool isColsed) : base(vertices, isColsed)
31+
{
32+
}
33+
34+
/// <inheritdoc/>
3035
public override IEnumerable<Entity> Explode()
3136
{
3237
return Polyline.Explode(this);
3338
}
3439

35-
private void verticesOnAdd(object sender, CollectionChangedEventArgs e)
40+
protected override void verticesOnAdd(object sender, CollectionChangedEventArgs e)
3641
{
3742
if (e.Item is not Vertex2D)
3843
{

0 commit comments

Comments
 (0)