Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
fixes issues with nurbs curves and arcs, namely #170 and a sideline c…
Browse files Browse the repository at this point in the history
…ause for #176
  • Loading branch information
didimitrie committed May 21, 2018
1 parent 1a51d1a commit 1ee6988
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion SpeckleCore
113 changes: 76 additions & 37 deletions SpeckleRhinoConverter/src/SpeckleRhGhConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static SpeckleInterval ToSpeckle( this Interval interval )

public static Interval ToNative( this SpeckleInterval interval )
{
return new Interval( ( double ) interval.Start, ( double ) interval.End ); ;
return new Interval( ( double ) interval.Start, ( double ) interval.End );
}

// Interval2d
Expand Down Expand Up @@ -270,22 +270,24 @@ public static SpeckleLine ToSpeckle( this Line line )
// Rh Line capture
public static SpeckleLine ToSpeckle( this LineCurve line )
{
return new SpeckleLine( ( new Point3d[ ] { line.PointAtStart, line.PointAtEnd } ).ToFlatArray(), properties: line.UserDictionary.ToSpeckle() );
return new SpeckleLine( ( new Point3d[ ] { line.PointAtStart, line.PointAtEnd } ).ToFlatArray(), properties: line.UserDictionary.ToSpeckle() ) { Domain = line.Domain.ToSpeckle() };
}

// Back again only to LINECURVES because we hate rhinocommon
// Back again only to LINECURVES because we hate grasshopper and its dealings with rhinocommon
public static LineCurve ToNative( this SpeckleLine line )
{
var pts = line.Value.ToPoints();
var myLine = new LineCurve( pts[ 0 ], pts[ 1 ] );
if ( line.Domain != null )
myLine.Domain = line.Domain.ToNative();
myLine.UserDictionary.ReplaceContentsWith( line.Properties.ToNative() );
return myLine;
}

// Rectangles now and forever forward will become polylines
public static SpecklePolyline ToSpeckle( this Rectangle3d rect )
{
return new SpecklePolyline( ( new Point3d[ ] { rect.Corner( 0 ), rect.Corner( 1 ), rect.Corner( 2 ), rect.Corner( 3 ) } ).ToFlatArray() ) { closed = true };
return new SpecklePolyline( ( new Point3d[ ] { rect.Corner( 0 ), rect.Corner( 1 ), rect.Corner( 2 ), rect.Corner( 3 ) } ).ToFlatArray() ) { Closed = true };
}

// Circle
Expand All @@ -299,6 +301,7 @@ public static ArcCurve ToNative( this SpeckleCircle circ )
{
Circle circle = new Circle( new Plane( circ.Center.ToNative().Location, circ.Normal.ToNative() ), ( double ) circ.Radius );
var myCircle = new ArcCurve( circle );
myCircle.Domain = circ.Domain.ToNative();
myCircle.UserDictionary.ReplaceContentsWith( circ.Properties.ToNative() );
return myCircle;
}
Expand All @@ -312,6 +315,7 @@ public static SpeckleObject ToSpeckle( this ArcCurve a )
Circle preCircle;
a.TryGetCircle( out preCircle );
SpeckleCircle myCircle = preCircle.ToSpeckle();
myCircle.Domain = a.Domain.ToSpeckle();
myCircle.Properties = a.UserDictionary.ToSpeckle();
return myCircle;
}
Expand All @@ -320,6 +324,7 @@ public static SpeckleObject ToSpeckle( this ArcCurve a )
Arc preArc;
a.TryGetArc( out preArc );
SpeckleArc myArc = preArc.ToSpeckle();
myArc.Domain = a.Domain.ToSpeckle();
myArc.Properties = a.UserDictionary.ToSpeckle();
return myArc;
}
Expand All @@ -335,7 +340,11 @@ public static SpeckleArc ToSpeckle( this Arc a )
public static ArcCurve ToNative( this SpeckleArc a )
{
Arc arc = new Arc( a.Plane.ToNative(), ( double ) a.Radius, ( double ) a.AngleRadians );
arc.StartAngle = ( double ) a.StartAngle;
arc.EndAngle = ( double ) a.EndAngle;
var myArc = new ArcCurve( arc );
if ( a.Domain != null )
myArc.Domain = a.Domain.ToNative();
myArc.UserDictionary.ReplaceContentsWith( a.Properties.ToNative() );
return myArc;
}
Expand All @@ -350,6 +359,8 @@ public static NurbsCurve ToNative( this SpeckleEllipse e )
{
Ellipse elp = new Ellipse( e.Plane.ToNative(), ( double ) e.FirstRadius, ( double ) e.SecondRadius );
var myEllp = elp.ToNurbsCurve();
if ( e.Domain != null )
myEllp.Domain = e.Domain.ToNative();
myEllp.UserDictionary.ReplaceContentsWith( e.Properties.ToNative() );
return myEllp;
}
Expand All @@ -363,9 +374,9 @@ public static SpeckleObject ToSpeckle( this Polyline poly )
return new SpeckleLine( poly.ToFlatArray() );

var myPoly = new SpecklePolyline( poly.ToFlatArray() );
myPoly.closed = poly.IsClosed;
myPoly.Closed = poly.IsClosed;

if ( myPoly.closed )
if ( myPoly.Closed )
myPoly.Value.RemoveRange( myPoly.Value.Count - 3, 3 );

return myPoly;
Expand All @@ -382,11 +393,12 @@ public static SpeckleObject ToSpeckle( this PolylineCurve poly )
return new SpeckleLine( polyline.ToFlatArray(), null, poly.UserDictionary.ToSpeckle() );

var myPoly = new SpecklePolyline( polyline.ToFlatArray() );
myPoly.closed = polyline.IsClosed;
myPoly.Closed = polyline.IsClosed;

if ( myPoly.closed )
if ( myPoly.Closed )
myPoly.Value.RemoveRange( myPoly.Value.Count - 3, 3 );

myPoly.Domain = poly.Domain.ToSpeckle();
myPoly.Properties = poly.UserDictionary.ToSpeckle();
return myPoly;
}
Expand All @@ -397,9 +409,11 @@ public static SpeckleObject ToSpeckle( this PolylineCurve poly )
public static PolylineCurve ToNative( this SpecklePolyline poly )
{
var points = poly.Value.ToPoints().ToList();
if ( poly.closed ) points.Add( points[ 0 ] );
if ( poly.Closed ) points.Add( points[ 0 ] );

var myPoly = new PolylineCurve( points );
if ( poly.Domain != null )
myPoly.Domain = poly.Domain.ToNative();
myPoly.UserDictionary.ReplaceContentsWith( poly.Properties.ToNative() );
return myPoly;
}
Expand All @@ -409,23 +423,25 @@ public static PolylineCurve ToNative( this SpecklePolyline poly )
public static SpecklePolycurve ToSpeckle( this PolyCurve p )
{
SpecklePolycurve myPoly = new SpecklePolycurve();
myPoly.Closed = p.IsClosed;
myPoly.Domain = p.Domain.ToSpeckle();

p.RemoveNesting();
var segments = p.Explode();

myPoly.Segments = segments.Select( s => { return s.ToSpeckle(); } ).ToList();
myPoly.Properties = p.UserDictionary.ToSpeckle();
myPoly.SetHashes( myPoly.Segments.Select( obj => obj.Hash ).ToArray() );
myPoly.SetHashes( myPoly.Segments.Select( obj => obj.Hash ).ToArray() );

return myPoly;
}

public static PolyCurve ToNative( this SpecklePolycurve p )
{

PolyCurve myPolyc = new PolyCurve();
foreach ( var segment in p.Segments )
{
switch(segment)
switch ( segment )
{
case SpeckleCore.SpeckleCurve crv:
myPolyc.Append( crv.ToNative() );
Expand All @@ -442,6 +458,8 @@ public static PolyCurve ToNative( this SpecklePolycurve p )
}
}
myPolyc.UserDictionary.ReplaceContentsWith( p.Properties.ToNative() );
if ( p.Domain != null )
myPolyc.Domain = p.Domain.ToNative();
return myPolyc;
}

Expand Down Expand Up @@ -495,6 +513,7 @@ public static SpeckleObject ToSpeckle( this Curve curve )
myCurve.Periodic = nurbsCurve.IsPeriodic;
myCurve.Rational = nurbsCurve.IsRational;
myCurve.Domain = nurbsCurve.Domain.ToSpeckle();
myCurve.Closed = nurbsCurve.IsClosed;

myCurve.Properties = properties;
return myCurve;
Expand Down Expand Up @@ -537,35 +556,56 @@ public static SpeckleObject ToSpeckle( this NurbsCurve curve )
curve.ToPolyline( 0, 1, 0, 0, 0, 0.1, 0, 0, true ).TryGetPolyline( out poly );

SpeckleCurve myCurve = new SpeckleCurve( poly: ( SpecklePolyline ) poly.ToSpeckle(), properties: curve.UserDictionary.ToSpeckle() );

myCurve.Weights = curve.Points.Select( ctp => ctp.Weight ).ToList();
myCurve.Points = curve.Points.Select( ctp => ctp.Location ).ToFlatArray().ToList();
myCurve.Knots = curve.Knots.ToList();
myCurve.Degree = curve.Degree;
myCurve.Periodic = curve.IsPeriodic;
myCurve.Rational = curve.IsRational;
myCurve.Domain = curve.Domain.ToSpeckle();
myCurve.Closed = curve.IsClosed;

myCurve.Properties = properties;
return myCurve;
}

public static NurbsCurve ToNative( this SpeckleCurve curve )
{
var ptsList = curve.Points.ToPoints();

// Bug/feature in Rhino sdk: creating a periodic curve adds two extra stupid points?
var myCurve = NurbsCurve.Create( curve.Periodic, curve.Degree, new Point3d[ curve.Periodic ? ptsList.Length - 2 : ptsList.Length ] );
myCurve.Domain = curve.Domain.ToNative();

for ( int i = 0; i < ptsList.Length; i++ )
myCurve.Points.SetPoint( i, ptsList[ i ].X, ptsList[ i ].Y, ptsList[ i ].Z, curve.Weights[ i ] );

for ( int i = 0; i < curve.Knots.Count; i++ )
myCurve.Knots[ i ] = curve.Knots[ i ];
var ptsList = curve.Points.ToPoints();

myCurve.UserDictionary.ReplaceContentsWith( curve.Properties.ToNative() );
return myCurve;
if ( !curve.Periodic )
{
// Bug/feature in Rhino sdk: creating a periodic curve adds two extra stupid points?
var myCurve = NurbsCurve.Create( curve.Periodic, curve.Degree, ptsList );
myCurve.Domain = curve.Domain.ToNative();

// set weights
for ( int i = 0; i < ptsList.Length; i++ )
myCurve.Points.SetPoint( i, ptsList[ i ].X, ptsList[ i ].Y, ptsList[ i ].Z, curve.Weights[ i ] );

// set knots
for ( int i = 0; i < curve.Knots.Count; i++ )
myCurve.Knots[ i ] = curve.Knots[ i ];

myCurve.UserDictionary.ReplaceContentsWith( curve.Properties.ToNative() );
return myCurve;
}
else
{
var thePts = ptsList.Take( ptsList.Length - 3 ).ToArray();
var myCurve = NurbsCurve.Create( curve.Periodic, curve.Degree, thePts );

// set weights
for ( int i = 0; i < ptsList.Length; i++ )
myCurve.Points.SetPoint( i, ptsList[ i ].X, ptsList[ i ].Y, ptsList[ i ].Z, curve.Weights[ i ] );

// set knots
for ( int i = 0; i < curve.Knots.Count; i++ )
myCurve.Knots[ i ] = curve.Knots[ i ];

myCurve.Domain = curve.Domain.ToNative();
return myCurve;
}
}

#endregion
Expand Down Expand Up @@ -649,7 +689,7 @@ public static SpeckleBrep ToSpeckle( this Brep brep )
{
MeshingParameters mySettings;
#if R6
mySettings = new MeshingParameters(0);
mySettings = new MeshingParameters( 0 );
#else
mySettings = MeshingParameters.Coarse;

Expand Down Expand Up @@ -789,15 +829,15 @@ public static object ToNative( this SpeckleAnnotation annot )
TextHeight = ( double ) annot.TextHeight
};
#if R6
var dimStyleIndex = Rhino.RhinoDoc.ActiveDoc.DimStyles.Add("Speckle");
var dimStyle = new Rhino.DocObjects.DimensionStyle
{
TextHeight = (double)annot.TextHeight,
Font = new Rhino.DocObjects.Font(annot.FontName, Rhino.DocObjects.Font.FontWeight.Bold, Rhino.DocObjects.Font.FontStyle.Italic, false, false)
};
Rhino.RhinoDoc.ActiveDoc.DimStyles.Modify(dimStyle, dimStyleIndex, true);

textEntity.DimensionStyleId = Rhino.RhinoDoc.ActiveDoc.DimStyles[dimStyleIndex].Id;
var dimStyleIndex = Rhino.RhinoDoc.ActiveDoc.DimStyles.Add( "Speckle" );
var dimStyle = new Rhino.DocObjects.DimensionStyle
{
TextHeight = ( double ) annot.TextHeight,
Font = new Rhino.DocObjects.Font( annot.FontName, Rhino.DocObjects.Font.FontWeight.Bold, Rhino.DocObjects.Font.FontStyle.Italic, false, false )
};
Rhino.RhinoDoc.ActiveDoc.DimStyles.Modify( dimStyle, dimStyleIndex, true );

textEntity.DimensionStyleId = Rhino.RhinoDoc.ActiveDoc.DimStyles[ dimStyleIndex ].Id;

#endif

Expand All @@ -812,7 +852,6 @@ public static object ToNative( this SpeckleAnnotation annot )
}
}


// Blocks and groups
// TODO

Expand Down

0 comments on commit 1ee6988

Please sign in to comment.