Skip to content

Commit

Permalink
Merge pull request #238 from DomCR/Dimension-measurement
Browse files Browse the repository at this point in the history
Dimension Measurement
  • Loading branch information
DomCR authored Feb 12, 2024
2 parents cdb606f + 4b83738 commit 1e6cade
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 20 deletions.
5 changes: 4 additions & 1 deletion ACadSharp.Tests/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public void DwgEntitiesToDxfFile(string test)

protected virtual void writeDwgFile(string file, CadDocument doc)
{
if (doc.Header.Version < ACadVersion.AC1014 || doc.Header.Version > ACadVersion.AC1018)
if (!TestVariables.LocalEnv)
return;

if (!isSupportedVersion(doc.Header.Version))
return;

using (DwgWriter writer = new DwgWriter(file, doc))
Expand Down
22 changes: 8 additions & 14 deletions ACadSharp/Entities/Dimension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ACadSharp.Attributes;
using ACadSharp.Blocks;
using ACadSharp.Tables;
using CSMath;
using CSUtilities.Extensions;
using System;

namespace ACadSharp.Entities
Expand Down Expand Up @@ -81,11 +81,11 @@ public bool IsTextUserDefinedLocation
{
if (value)
{
this._flags |= DimensionType.TextUserDefinedLocation;
this._flags = this._flags.AddFlag(DimensionType.TextUserDefinedLocation);
}
else
{
this._flags &= ~DimensionType.TextUserDefinedLocation;
this._flags = this._flags.RemoveFlag(DimensionType.TextUserDefinedLocation);
}
}
}
Expand All @@ -99,32 +99,26 @@ public bool IsTextUserDefinedLocation
/// <summary>
/// Dimension text line-spacing style
/// </summary>
/// <remarks>
/// optional
/// </remarks>
[DxfCodeValue(72)]
[DxfCodeValue(DxfReferenceType.Optional, 72)]
public LineSpacingStyleType LineSpacingStyle { get; set; }

/// <summary>
/// Dimension text-line spacing factor
/// </summary>
/// <remarks>
/// (optional) Percentage of default (3-on-5) line spacing to be applied.
/// Percentage of default (3-on-5) line spacing to be applied.
/// </remarks>
/// <value>
/// Valid values range from 0.25 to 4.00
/// </value>
[DxfCodeValue(41)]
[DxfCodeValue(DxfReferenceType.Optional, 41)]
public double LineSpacingFactor { get; set; }

/// <summary>
/// Actual measurement
/// </summary>
/// <remarks>
/// optional; read-only value
/// </remarks>
[DxfCodeValue(42)]
public double Measurement { get; internal set; }
[DxfCodeValue(DxfReferenceType.Optional, 42)]
public abstract double Measurement { get; }

/// <summary>
/// Gets or sets a value indicating whether the first arrow
Expand Down
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionAligned.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public class DimensionAligned : Dimension
[DxfCodeValue(DxfReferenceType.Optional, 52)]
public double ExtLineRotation { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
return this.FirstPoint.DistanceFrom(this.SecondPoint);
}
}

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

public DimensionAligned() : base(DimensionType.Aligned) { }
Expand Down
12 changes: 12 additions & 0 deletions ACadSharp/Entities/DimensionAngular2Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public class DimensionAngular2Line : Dimension
[DxfCodeValue(16, 26, 36)]
public XYZ DimensionArc { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
XY v1 = (XY)(this.FirstPoint - this.SecondPoint);
XY v2 = (XY)(this.AngleVertex - this.DimensionArc);

return v1.AngleFrom(v2);
}
}

public DimensionAngular2Line() : base(DimensionType.Angular)
{
}
Expand Down
23 changes: 23 additions & 0 deletions ACadSharp/Entities/DimensionAngular3Pt.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 Down Expand Up @@ -41,6 +42,28 @@ public class DimensionAngular3Pt : Dimension
[DxfCodeValue(15, 25, 35)]
public XYZ AngleVertex { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
XY v1 = (XY)(this.FirstPoint - this.AngleVertex);
XY v2 = (XY)(this.SecondPoint - this.AngleVertex);

if (v1.Equals(v2))
{
return 0.0;
}

if (v1.IsParallel(v2))
{
return Math.PI;
}

return (double)v1.AngleFrom(v2);
}
}

public DimensionAngular3Pt() : base(DimensionType.Angular3Point) { }
}
}
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionDiameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public class DimensionDiameter : Dimension
[DxfCodeValue(40)]
public double LeaderLength { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
return 2 * this.InsertionPoint.DistanceFrom(this.AngleVertex);
}
}

public DimensionDiameter() : base(DimensionType.Diameter) { }
}
}
15 changes: 15 additions & 0 deletions ACadSharp/Entities/DimensionLinear.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ACadSharp.Attributes;
using CSMath;
using System;

namespace ACadSharp.Entities
{
Expand All @@ -25,9 +27,22 @@ public class DimensionLinear : DimensionAligned
/// <summary>
/// Angle of rotated, horizontal, or vertical dimensions
/// </summary>
/// <value>
/// Value in radians
/// </value>
[DxfCodeValue(DxfReferenceType.IsAngle, 50)]
public double Rotation { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
double rot = FirstPoint.AngleFrom(this.SecondPoint);
return Math.Abs(FirstPoint.DistanceFrom(this.SecondPoint) * Math.Cos(this.Rotation - rot));
}
}

public DimensionLinear() : base(DimensionType.Linear) { }
}
}
26 changes: 23 additions & 3 deletions ACadSharp/Entities/DimensionOrdinate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ACadSharp.Attributes;
using CSMath;
using CSUtilities.Extensions;
using System;

namespace ACadSharp.Entities
{
Expand All @@ -21,7 +23,7 @@ public class DimensionOrdinate : Dimension
public override string ObjectName => DxfFileToken.EntityDimension;

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

/// <summary>
/// Definition point for linear and angular dimensions (in WCS)
Expand All @@ -35,6 +37,24 @@ public class DimensionOrdinate : Dimension
[DxfCodeValue(14, 24, 34)]
public XYZ LeaderEndpoint { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
XY dir = this.IsOrdinateTypeX ? XY.AxisY : XY.AxisX;
double sin = Math.Sin(this.HorizontalDirection);
double cos = Math.Cos(this.HorizontalDirection);
dir = new XY(dir.X * cos - dir.Y * sin, dir.X * sin + dir.Y * cos);

double t = dir.Dot(this.InsertionPoint.Convert<XY>() - this.FeatureLocation.Convert<XY>());
XY pr = this.FeatureLocation.Convert<XY>() + t * dir;
XY v = this.InsertionPoint.Convert<XY>() - pr;
double distSqrt = v.Dot(v);
return Math.Sqrt(distSqrt);
}
}

/// <summary>
/// Ordinate type. If true, ordinate is X-type else is ordinate is Y-type
/// </summary>
Expand All @@ -48,11 +68,11 @@ public bool IsOrdinateTypeX
{
if (value)
{
this._flags |= DimensionType.OrdinateTypeX;
this._flags = this._flags.AddFlag(DimensionType.OrdinateTypeX);
}
else
{
this._flags &= ~DimensionType.OrdinateTypeX;
this._flags = this._flags.RemoveFlag(DimensionType.OrdinateTypeX);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions ACadSharp/Entities/DimensionRadius.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public class DimensionRadius : Dimension
[DxfCodeValue(40)]
public double LeaderLength { get; set; }

/// <inheritdoc/>
public override double Measurement
{
get
{
return this.InsertionPoint.DistanceFrom(this.AngleVertex);
}
}

public DimensionRadius() : base(DimensionType.Radius) { }
}
}
2 changes: 1 addition & 1 deletion ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ private void readCommonDimensionData(CadDimensionTemplate template)
//Linespacing Factor BD 41
dimension.LineSpacingFactor = this._objectReader.ReadBitDouble();
//Actual Measurement BD 42
dimension.Measurement = this._objectReader.ReadBitDouble();
this._objectReader.ReadBitDouble();
}

//R2007 +:
Expand Down
3 changes: 3 additions & 0 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ private bool readDimension(CadEntityTemplate template, DxfMap map, string subcla
//Flags do not have set
tmp.SetDimensionFlags((DimensionType)this._reader.ValueAsShort);
return true;
//Measurement - read only
case 42:
return true;
//Undocumented codes
case 73:
case 74:
Expand Down
4 changes: 3 additions & 1 deletion ACadSharp/IO/Templates/CadDimensionTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class DimensionPlaceholder : Dimension
{
public override ObjectType ObjectType { get { return ObjectType.INVALID; } }

public override double Measurement { get; }

public DimensionPlaceholder() : base(DimensionType.Linear) { }
}

Expand Down Expand Up @@ -79,7 +81,7 @@ public void SetDimensionObject(Dimension dimensionAligned)
dimensionAligned.AttachmentPoint = dimension.AttachmentPoint;
dimensionAligned.LineSpacingStyle = dimension.LineSpacingStyle;
dimensionAligned.LineSpacingFactor = dimension.LineSpacingFactor;
dimensionAligned.Measurement = dimension.Measurement;
//dimensionAligned.Measurement = dimension.Measurement;
dimensionAligned.Text = dimension.Text;
dimensionAligned.TextRotation = dimension.TextRotation;
dimensionAligned.HorizontalDirection = dimension.HorizontalDirection;
Expand Down

0 comments on commit 1e6cade

Please sign in to comment.