Skip to content

Commit

Permalink
Merge pull request #472 from DomCR/Viewport-select-entities
Browse files Browse the repository at this point in the history
Viewport select entities
  • Loading branch information
DomCR authored Oct 15, 2024
2 parents 27801e5 + ea7a3c2 commit ed1325c
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/Common/AssertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ACadSharp.Tests.Common
{
public static class AssertUtils
{
public static void AreEqual<T>(T expected, T actual, string varname)
public static void AreEqual<T>(T expected, T actual, string varname = null)
{
switch (expected, actual)
{
Expand Down
94 changes: 94 additions & 0 deletions src/ACadSharp.Tests/Entities/ViewportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using CSMath;
using System.Collections.Generic;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class ViewportTests
{
[Fact]
public void GetBoundingBoxTest()
{
Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.Center = new XYZ(10, 10, 0);

BoundingBox boundingBox = viewport.GetBoundingBox();

AssertUtils.AreEqual(viewport.Center, boundingBox.Center);
AssertUtils.AreEqual(new XYZ(-40, -15, 0), boundingBox.Min);
AssertUtils.AreEqual(new XYZ(60, 35, 0), boundingBox.Max);
}

[Fact]
public void GetModelBoundingBoxTest()
{
Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.ViewHeight = 50;
viewport.ViewCenter = new XY(10, 10);

BoundingBox boundingBox = viewport.GetModelBoundingBox();

Assert.Equal(100, viewport.ViewWidth);
AssertUtils.AreEqual(viewport.ViewCenter, ((XY)boundingBox.Center));
AssertUtils.AreEqual(new XYZ(-40, -15, 0), boundingBox.Min);
AssertUtils.AreEqual(new XYZ(60, 35, 0), boundingBox.Max);
}

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

Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.ViewHeight = 50;
viewport.ViewCenter = new XY(10, 10);

//Viewbox
//min: -40, -15
//max: 60, 35

doc.PaperSpace.Entities.Add(viewport);

//Entities in the view
Point ptIn = new Point(new XYZ(0, 0, 0));
Line lineIn = new Line(new XYZ(), new XYZ(100, 100, 0));

List<Entity> inView = new List<Entity>
{
ptIn,
lineIn
};
doc.Entities.AddRange(inView);

//Entities out the view
Point ptOut = new Point(new XYZ(100, 100, 0));

List<Entity> outView = new List<Entity>
{
ptOut
};
doc.Entities.AddRange(outView);

var selected = viewport.SelectEntities();

Assert.NotEmpty(selected);

foreach (Entity e in selected)
{
Assert.Contains(e, inView);
Assert.DoesNotContain(e, outView);
}

var selectedPartial = viewport.SelectEntities(false);
Assert.DoesNotContain(lineIn, selectedPartial);
}
}
}
30 changes: 28 additions & 2 deletions src/ACadSharp/Entities/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ACadSharp.Objects;
using ACadSharp.Tables;
using CSMath;
using System;
using System.Collections.Generic;

namespace ACadSharp.Entities
Expand Down Expand Up @@ -334,8 +335,8 @@ public override CadObject Clone()
/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
XYZ min = new XYZ(this.Center.X - this.Width, this.Center.Y - this.Height, this.Center.Z);
XYZ max = new XYZ(this.Center.X + this.Width, this.Center.Y + this.Height, this.Center.Z);
XYZ min = new XYZ(this.Center.X - this.Width / 2, this.Center.Y - this.Height / 2, this.Center.Z);
XYZ max = new XYZ(this.Center.X + this.Width / 2, this.Center.Y + this.Height / 2, this.Center.Z);
return new BoundingBox(min, max);
}

Expand All @@ -346,5 +347,30 @@ public BoundingBox GetModelBoundingBox()
XYZ max = new XYZ(this.ViewCenter.X + this.ViewWidth / 2, this.ViewCenter.Y + this.ViewHeight / 2, 0);
return new BoundingBox(min, max);
}

/// <summary>
/// Gets all the entities from the model that are in the view of the viewport.
/// </summary>
/// <returns></returns>
public List<Entity> SelectEntities(bool includePartial = true)
{
if (this.Document == null)
{
throw new InvalidOperationException($"Viewport needs to be assigned to a document.");
}

List<Entity> entities = new List<Entity>();

BoundingBox box = this.GetModelBoundingBox();
foreach (Entity e in this.Document.Entities)
{
if (box.IsIn(e.GetBoundingBox(), out bool partialIn) || (partialIn && includePartial))
{
entities.Add(e);
}
}

return entities;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private bool readDimension(CadEntityTemplate template, DxfMap map, string subcla
case 50:
var dim = new DimensionLinear();
tmp.SetDimensionObject(dim);
dim.Rotation = CSMath.Utilities.DegToRad(this._reader.ValueAsDouble);
dim.Rotation = CSMath.MathHelper.DegToRad(this._reader.ValueAsDouble);
map.SubClasses.Add(DxfSubclassMarker.LinearDimension, DxfClassMap.Create<DimensionLinear>());
return true;
case 70:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private bool readDimensionStyle(CadTableEntryTemplate<DimensionStyle> template,
template.CadObject.FixedExtensionLineLength = this._reader.ValueAsDouble;
return true;
case 50:
template.CadObject.JoggedRadiusDimensionTransverseSegmentAngle = CSMath.Utilities.DegToRad(this._reader.ValueAsDouble);
template.CadObject.JoggedRadiusDimensionTransverseSegmentAngle = CSMath.MathHelper.DegToRad(this._reader.ValueAsDouble);
return true;
case 69:
template.CadObject.TextBackgroundFillMode = (DimensionTextBackgroundFillMode)this._reader.ValueAsShort;
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static XY GetCenter(XY start, XY end, double bulge, out double radius)

double gamma = (Math.PI - theta) / 2;
double phi = (end - start).GetAngle() + Math.Sign(bulge) * gamma;
return new XY(start.X + radius * CSMath.Utilities.Cos(phi), start.Y + radius * CSMath.Utilities.Sin(phi));
return new XY(start.X + radius * CSMath.MathHelper.Cos(phi), start.Y + radius * CSMath.MathHelper.Sin(phi));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/Tables/DimensionStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public double JoggedRadiusDimensionTransverseSegmentAngle
set
{
//5 - 90
if (value < CSMath.Utilities.DegToRad(5) || value > Math.PI / 2)
if (value < CSMath.MathHelper.DegToRad(5) || value > Math.PI / 2)
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"The {nameof(JoggedRadiusDimensionTransverseSegmentAngle)} must be in range of 5 to 90 degrees.");
}
Expand Down

0 comments on commit ed1325c

Please sign in to comment.