Skip to content
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
168 changes: 164 additions & 4 deletions src/Formplot/FileFormat/FormplotConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public static bool TryConvert<TPlot>( Formplot source, out TPlot? target )
{
return source switch
{
CurveDistancePlot curve => ConvertToCurveProfile( curve ),
BorePatternPlot typed => ConvertToCurveProfile( typed ),
CurveDistancePlot typed => ConvertToCurveProfile( typed ),
CylindricityPlot typed => ConvertToCurveProfile( typed ),
StraightnessPlot typed => ConvertToCurveProfile( typed ),
RoundnessPlot typed => ConvertToCurveProfile( typed ),
FlatnessPlot typed => ConvertToCurveProfile( typed ),
_ => null
};
}
Expand Down Expand Up @@ -120,13 +125,14 @@ private static void CopyDefaults( Formplot source, Formplot target )

private static void CopyDefaults( Point source, Point target )
{
target.Properties.Set( source.Properties );
target.Tolerance = source.Tolerance;
if( source.HasProperties )
target.Properties.Set( source.Properties );
if( source.HasTolerance )
target.Tolerance = source.Tolerance;
target.State = source.State;
target.Index = source.Index;
}


private static double AdjustAngle( double angle )
{
var factor = (int)( angle / 360 );
Expand Down Expand Up @@ -315,6 +321,160 @@ private static RoundnessPlot ConvertToRoundness( CylindricityPlot source )
return result;
}

private static CurveProfilePlot ConvertToCurveProfile( BorePatternPlot source )
{
var result = new CurveProfilePlot();

CopyDefaults( source, result );

result.Actual.CoordinateSystem = source.Actual.CoordinateSystem;
result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem;

//Segments and points have a reference to the plot, so we must copy them, despite being of the same type.
foreach( var segment in source.Segments )
{
var resultSegment = new Segment<CurvePoint, CurveGeometry>( segment.Name, segment.SegmentType );

foreach( var point in segment.Points )
{
var resultPoint = new CurvePoint( point.Position, point.Direction, point.Deviation );
CopyDefaults( point, resultPoint );
resultSegment.Points.Add( resultPoint );
}

result.Segments.Add( resultSegment );
}

return result;
}

private static CurveProfilePlot ConvertToCurveProfile( CylindricityPlot source )
{
var result = new CurveProfilePlot();

CopyDefaults( source, result );

result.Actual.CoordinateSystem = source.Actual.CoordinateSystem;
result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem;

foreach( var segment in source.Segments )
{
var resultSegment = new Segment<CurvePoint, CurveGeometry>( segment.Name, segment.SegmentType );
foreach( var point in segment.Points )
{
var x = Math.Cos( point.Angle ) * source.Nominal.Radius;
var y = Math.Sin( point.Angle ) * source.Nominal.Radius;

var curvePoint = new CurvePoint(
new Vector( x, y, point.Height * source.Nominal.Height ),
new Vector( x, y ).Normalized(),
point.Deviation );

CopyDefaults( point, curvePoint );

resultSegment.Points.Add( curvePoint );
}

result.Segments.Add( resultSegment );
}

return result;
}

private static CurveProfilePlot ConvertToCurveProfile( StraightnessPlot source )
{
var result = new CurveProfilePlot();

CopyDefaults( source, result );

result.Actual.CoordinateSystem = source.Actual.CoordinateSystem;
result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem;

foreach( var segment in source.Segments )
{
var resultSegment = new Segment<CurvePoint, CurveGeometry>( segment.Name, segment.SegmentType );
foreach( var point in segment.Points )
{
var curvePoint = new CurvePoint(
source.Nominal.Position + ( source.Nominal.Direction * point.Position * source.Nominal.Length ),
source.Nominal.Deviation,
point.Deviation );

CopyDefaults( point, curvePoint );

resultSegment.Points.Add( curvePoint );
}

result.Segments.Add( resultSegment );
}

return result;
}

private static CurveProfilePlot ConvertToCurveProfile( RoundnessPlot source )
{
var result = new CurveProfilePlot();

CopyDefaults( source, result );

result.Actual.CoordinateSystem = source.Actual.CoordinateSystem;
result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem;

foreach( var segment in source.Segments )
{
var resultSegment = new Segment<CurvePoint, CurveGeometry>( segment.Name, segment.SegmentType );
foreach( var point in segment.Points )
{
var x = Math.Cos( point.Angle ) * source.Nominal.Radius;
var y = Math.Sin( point.Angle ) * source.Nominal.Radius;

var curvePoint = new CurvePoint(
new Vector( x, y ),
new Vector( x, y ).Normalized(),
point.Deviation );

CopyDefaults( point, curvePoint );

resultSegment.Points.Add( curvePoint );
}

result.Segments.Add( resultSegment );
}

return result;
}

private static CurveProfilePlot ConvertToCurveProfile( FlatnessPlot source )
{
var result = new CurveProfilePlot();

CopyDefaults( source, result );

result.Actual.CoordinateSystem = source.Actual.CoordinateSystem;
result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem;


foreach( var segment in source.Segments )
{
var resultSegment = new Segment<CurvePoint, CurveGeometry>( segment.Name, segment.SegmentType );
foreach( var point in segment.Points )
{
var curvePoint = new CurvePoint(
new Vector( point.Coordinate1 * source.Nominal.Length1, point.Coordinate2 * source.Nominal.Length2 ),
new Vector( 0, 0, 1 ),
point.Deviation );

CopyDefaults( point, curvePoint );

resultSegment.Points.Add( curvePoint );
}

result.Segments.Add( resultSegment );
}

return result;
}

private static CurveProfilePlot ConvertToCurveProfile( CurveDistancePlot source )
{
var result = new CurveProfilePlot();
Expand Down
5 changes: 5 additions & 0 deletions src/Formplot/FileFormat/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public PropertyCollection Properties
set => _Properties = value;
}

/// <summary>
/// Returns <code>true</code> if this point has a tolerance and the tolerance is not empty.
/// </summary>
public bool HasProperties => _Properties is { Count: > 0 };

/// <summary>
/// Gets the current state.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Formplot/FileFormat/Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Segment<TPoint, TGeometry> : Segment, IEquatable<Segment<TPo
/// <summary>Constructor.</summary>
/// <param name="name">The name.</param>
/// <param name="segmentType">Type of the segment.</param>
public Segment( string name, SegmentTypes segmentType ) : base( name, segmentType )
public Segment( string name = "", SegmentTypes segmentType = SegmentTypes.None ) : base( name, segmentType )
{
Points = new PointCollection<TPoint, TGeometry>( this );
}
Expand Down
26 changes: 26 additions & 0 deletions src/Formplot/FileFormat/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ internal static Vector Deserialize( XmlReader reader )
return new Vector( x, y, z );
}

/// <summary>
/// Returns the vector with normalized length.
/// </summary>
public Vector Normalized()
{
var l = Math.Sqrt( X * X + Y * Y + Z * Z );
return l == 0.0
? new Vector( 0 )
: this / l;
}

public static Vector operator +( Vector a, Vector b )
{
return new Vector( a.X + b.X, a.Y + b.Y, a.Z + b.Z );
Expand All @@ -105,6 +116,21 @@ internal static Vector Deserialize( XmlReader reader )
return new Vector( a.X - b.X, a.Y - b.Y, a.Z - b.Z );
}

public static Vector operator *( Vector a, double b )
{
return new Vector( a.X * b, a.Y * b, a.Z * b );
}

public static Vector operator *( double b, Vector a )
{
return a * b;
}

public static Vector operator /( Vector a, double b )
{
return new Vector( a.X / b, a.Y / b, a.Z / b );
}

public static bool operator ==( Vector a, Vector b )
{
return Equals( a, b );
Expand Down
Loading
Loading