Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding box #213

Merged
merged 26 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ACadSharp.Tests/Entities/EntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ static EntityTests()
}
}

[Theory]
[MemberData(nameof(EntityTypes))]
public void BoundingBoxTest(Type entityType)
{
Entity entity = EntityFactory.Create(entityType);

entity.GetBoundingBox();
}

[Theory]
[MemberData(nameof(EntityTypes))]
public void Clone(Type entityType)
Expand Down
11 changes: 11 additions & 0 deletions ACadSharp/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,16 @@ public override CadObject Clone()

return clone;
}

public override BoundingBox GetBoundingBox()
{
BoundingBox box = BoundingBox.Null;
foreach (var item in this.BlockOwner.Entities)
{
box = box.Merge(item.GetBoundingBox());
}

return box;
}
}
}
7 changes: 7 additions & 0 deletions ACadSharp/Blocks/BlockEnd.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Attributes;
using ACadSharp.Entities;
using ACadSharp.Tables;
using CSMath;

namespace ACadSharp.Blocks
{
Expand Down Expand Up @@ -36,5 +37,11 @@ public override CadObject Clone()
clone.Owner = new BlockRecord((this.Owner as BlockRecord).Name);
return clone;
}

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return BoundingBox.Null;
}
}
}
43 changes: 43 additions & 0 deletions ACadSharp/Entities/Arc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class Arc : Circle
[DxfCodeValue(DxfReferenceType.IsAngle, 51)]
public double EndAngle { get; set; } = Math.PI;

/// <summary>
/// Default constructor
/// </summary>
public Arc() : base() { }

/// <summary>
Expand Down Expand Up @@ -73,5 +76,45 @@ public static Arc CreateFromBulge(XY p1, XY p2, double bulge)
EndAngle = endAngle,
};
}

/// <summary>
/// Process the 2 points limiting the arc segment
/// </summary>
/// <param name="start">Start point of the arc segment</param>
/// <param name="end">End point of the arc segment</param>
public void GetEndVertices(out XYZ start, out XYZ end)
{
if (this.Normal != XYZ.AxisZ)
{
throw new NotImplementedException("GetBoundPoints box for not aligned Normal is not implemented");
}

double tmpEndAngle = this.EndAngle;

if (this.EndAngle < this.StartAngle)
{
tmpEndAngle += 2 * Math.PI;
}

double delta = tmpEndAngle - this.StartAngle;

double angle = this.StartAngle + delta;
double startX = this.Radius * Math.Sin(angle);
double startY = this.Radius * Math.Cos(angle);

startX = MathUtils.IsZero(startX) ? 0 : startX;
startY = MathUtils.IsZero(startY) ? 0 : startY;

start = new XYZ(startX, startY, 0);

double angle2 = this.StartAngle + delta * 2;
double endX = (this.Radius * Math.Sin(angle2));
double endY = (this.Radius * Math.Cos(angle2));

endX = MathUtils.IsZero(endX) ? 0 : endX;
endY = MathUtils.IsZero(endY) ? 0 : endY;

end = new XYZ(endX, endY, 0);
}
}
}
26 changes: 26 additions & 0 deletions ACadSharp/Entities/CadImageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
using System.Collections.Generic;
using System;
using ACadSharp.Objects;
using System.Linq;

namespace ACadSharp.Entities
{
/// <summary>
/// Common base class for <see cref="RasterImage" /> and <see cref="Wipeout" />.
/// </summary>
[DxfSubClass(null, true)]
public abstract class CadImageBase : Entity
{
Expand Down Expand Up @@ -185,6 +189,28 @@ internal ImageDefinitionReactor DefinitionReactor
private ImageDefinition _definition;
private ImageDefinitionReactor _definitionReactor;

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
if (!this.ClipBoundaryVertices.Any())
{
return BoundingBox.Null;
}

double minX = this.ClipBoundaryVertices.Select(v => v.X).Min();
double minY = this.ClipBoundaryVertices.Select(v => v.Y).Min();
XYZ min = new XYZ(minX, minY, 0) + this.InsertPoint;

double maxX = this.ClipBoundaryVertices.Select(v => v.X).Max();
double maxY = this.ClipBoundaryVertices.Select(v => v.Y).Max();
XYZ max = new XYZ(maxX, maxY, 0) + this.InsertPoint;

BoundingBox box = new BoundingBox(min, max);

return box;
}

/// <inheritdoc/>
public override CadObject Clone()
{
CadImageBase clone = (CadImageBase)base.Clone();
Expand Down
27 changes: 25 additions & 2 deletions ACadSharp/Entities/Circle.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ACadSharp.Attributes;
using CSMath;
using System;

namespace ACadSharp.Entities
{
Expand All @@ -22,7 +23,7 @@ public class Circle : Entity

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

/// <summary>
/// Specifies the three-dimensional normal unit vector for the object.
/// </summary>
Expand All @@ -45,11 +46,33 @@ public class Circle : Entity
/// Specifies the radius of an arc, circle, or position marker.
/// </summary>
[DxfCodeValue(40)]
public double Radius { get; set; } = 1.0;
public double Radius
{
get { return this._radius; }
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The radius must be greater than 0.");
}
this._radius = value;
}
}

private double _radius = 1.0;

/// <summary>
/// Default constructor
/// </summary>
public Circle() : base() { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
XYZ min = new XYZ(Math.Min(this.Center.X - this.Radius, this.Center.X + this.Radius), Math.Min(this.Center.Y - this.Radius, this.Center.Y + this.Radius), Math.Min(this.Center.Z - this.Radius, this.Center.Z + this.Radius));
XYZ max = new XYZ(Math.Max(this.Center.X - this.Radius, this.Center.X + this.Radius), Math.Max(this.Center.Y - this.Radius, this.Center.Y + this.Radius), Math.Max(this.Center.Z - this.Radius, this.Center.Z + this.Radius));

return new BoundingBox(min, max);
}
}
}
1 change: 1 addition & 0 deletions ACadSharp/Entities/Dimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ protected Dimension(DimensionType type)
this._flags |= DimensionType.BlockReference;
}

/// <inheritdoc/>
public override CadObject Clone()
{
Dimension clone = (Dimension)base.Clone();
Expand Down
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionAligned.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public override double Measurement

protected DimensionAligned(DimensionType type) : base(type) { }

/// <summary>
/// Default constructor.
/// </summary>
public DimensionAligned() : base(DimensionType.Aligned) { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.FirstPoint, this.SecondPoint);
}
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionAngular2Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ public override double Measurement
}
}

/// <summary>
/// Default constructor.
/// </summary>
public DimensionAngular2Line() : base(DimensionType.Angular)
{
}

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.FirstPoint, this.SecondPoint);
}
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionAngular3Pt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public override double Measurement
}
}

/// <summary>
/// Default constructor.
/// </summary>
public DimensionAngular3Pt() : base(DimensionType.Angular3Point) { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.FirstPoint, this.SecondPoint);
}
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionDiameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public override double Measurement
}
}

/// <summary>
/// Default constructor.
/// </summary>
public DimensionDiameter() : base(DimensionType.Diameter) { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.InsertionPoint - this.AngleVertex, this.InsertionPoint + this.AngleVertex);
}
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionOrdinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public bool IsOrdinateTypeX
}
}

/// <summary>
/// Default constructor.
/// </summary>
public DimensionOrdinate() : base(DimensionType.Ordinate) { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.FeatureLocation, this.LeaderEndpoint);
}
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionRadius.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public override double Measurement
}
}

/// <summary>
/// Default constructor.
/// </summary>
public DimensionRadius() : base(DimensionType.Radius) { }

/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return new BoundingBox(this.InsertionPoint - this.AngleVertex, this.InsertionPoint + this.AngleVertex);
}
}
}
22 changes: 11 additions & 11 deletions ACadSharp/Entities/Ellipse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ACadSharp.Attributes;
using ACadSharp.IO.Templates;
using CSMath;
using System;

Expand Down Expand Up @@ -32,31 +31,31 @@ public class Ellipse : Entity
public double Thickness { get; set; } = 0.0;

/// <summary>
/// Extrusion direction
/// Extrusion direction.
/// </summary>
[DxfCodeValue(210, 220, 230)]
public XYZ Normal { get; set; } = XYZ.AxisZ;

/// <summary>
/// Center point (in WCS)
/// Center point (in WCS).
/// </summary>
[DxfCodeValue(10, 20, 30)]
public XYZ Center { get; set; } = XYZ.Zero;

/// <summary>
/// Endpoint of major axis, relative to the center (in WCS)
/// Endpoint of major axis, relative to the center (in WCS).
/// </summary>
[DxfCodeValue(11, 21, 31)]
public XYZ EndPoint { get; set; } = XYZ.Zero;

/// <summary>
/// Ratio of minor axis to major axis
/// Ratio of minor axis to major axis.
/// </summary>
[DxfCodeValue(40)]
public double RadiusRatio { get; set; } = 0.0;

/// <summary>
/// Start parameter
/// Start parameter.
/// </summary>
/// <value>
/// The valid range is 0 to 2 * PI.
Expand All @@ -65,17 +64,18 @@ public class Ellipse : Entity
public double StartParameter { get; set; } = 0.0;

/// <summary>
/// End parameter
/// End parameter.
/// </summary>
/// <value>
/// The valid range is 0 to 2 * PI.
/// </value>
[DxfCodeValue(42)]
public double EndParameter { get; set; } = Math.PI * 2;

/// <summary>
/// Default constructor
/// </summary>
public Ellipse() : base() { }
/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
return BoundingBox.Null;
}
}
}
8 changes: 7 additions & 1 deletion ACadSharp/Entities/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ACadSharp.Attributes;
using ACadSharp.Objects;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using CSMath;
using System;

namespace ACadSharp.Entities
Expand Down Expand Up @@ -94,6 +94,12 @@ public LineType LineType
/// </summary>
public Entity() : base() { }

/// <summary>
/// Gets the bounding box aligned with the axis XYZ that ocupies this entity
/// </summary>
/// <returns></returns>
public abstract BoundingBox GetBoundingBox();

/// <inheritdoc/>
public void MatchProperties(IEntity entity)
{
Expand Down
Loading
Loading