PolyLine.ToList(); in visual studio


#1

Hi

I want to get the points of a polyline like in the c# component in gh, but in visual studio, but I dosent work.

Anybody could tell me how to fix this?
Plline

Thanks!


(Vicente Soler) #2

.ToList() is an extension method part of LINQ. My guess is you just need to add using System.Linq; at the top.


(David Rutten) #3

Polyline is already a List<Point3d> type. It directly inherits from that class.


#4

HI, @visose and @DavidRutten,

thank you both.

I have a new question: when I feed the Polylines in my vs made component it fails cause of " Invalid cast:Curve Polyline.

I dont understand why? Is it cause of the pManager.AddCurveParameter ?
What should it be or do I need to cast it, even when its already a Polyline coming in?

Thanks!


(David Rutten) #5

The C# component has a type-hint for Polyline, which does not inherit from Rhino.Geometry.Curve.

The regular GH_Component only has a curve parameter. If you plug something which looks like a polyline into standard curve parameter you’ll either get a PolylineCurve, a NurbsCurve with degree=1, a NurbsCurve with higher degree but a load of stacked control-points or knots (somebody really screwed up if this happens), or a PolyCurve consisting of segments that are either LineCurve, PolylineCurve, NurbsCurve, or more deeply nested PolyCurve.

Trying to interpret and deal directly with this plethora of possibilities would be a waste of effort, so instead inside a GH_Component what you need to do it:

Curve curve = null;
if (!DA.GetData(n, ref curve)) return;
if (!curve.TryGetPolyline(out Polyline polyline))
  throw new ArgumentException("Only polylines are allowed as inputs");

(David Rutten) #6

As a general note, there exist a fair number of curve-like types in RhinoCommon that do not in fact inherit from the Curve base class. Examples are Line, Circle, Arc, and Polyline. These are completely separate types that do not carry around the additional baggage required by Rhino.Geometry.Curve. To create curves out of these simple types, you have to put them inside classes like LineCurve, ArcCurve and PolylineCurve.


#7

Thanks @DavidRutten for the explanation and the solution!