How to cast RhinoObject to Curve


#1

Hi, I’m writing c# plugin for Rhino 5. I need to select one or more surfaces , draw one line and find intersection points between them if there are any. To satisfy object types in “CurveSurface()” the line must be “Curve”. I can’t cast created line as curve object. Could you help me please!

Rhino.DocObjects.ObjRef[] surfs;
            Rhino.Input.RhinoGet.GetMultipleObjects("Select Polysurfaces & surfaces", true, 
                Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter, out surfs);
            Point3d Start = new Point3d(0, 0, 100);
            Point3d End = new Point3d(0, 0, -100);
            Guid line = doc.Objects.AddLine(Start, End);
            Rhino.DocObjects.RhinoObject ObjLine = doc.Objects.Find(line);
// CAST  line to Curve - error
            **Curve cLine =ObjLine.Curve();**

            Rhino.Geometry.Surface surf = surfs[0].Surface();
            double tl = 2.1 * doc.ModelAbsoluteTolerance;
            Rhino.Geometry.Intersect.CurveIntersections Cross = Rhino.Geometry.Intersect.Intersection.CurveSurface(cLine, surf, 0.001, tl);

(Menno Deij - van Rijswijk) #2

You could make a LineCurve object directly, without adding it to the document.

LineCurve lc = new LineCurve(new Point3d(0,0,100), new Point3d(0,0,-100));

Or, create a curve from the Line:

Line l = new Line(new Point3d(0,0,100), new Point3d(0,0,-100));
Curve crv = l.ToNurbsCurve();

#3

Thank you!
But how could be converted existing objects to specific types(surfaces, curvs …). after using Find command?


(Menno Deij - van Rijswijk) #4

To me, your code looks good (it should work), but you don’t check for errors.
Like so, you could find the error:

Guid line = doc.Objects.AddLine(Start, End);
if (line == Guid.Empty) 
   RhinoApp.WriteLine("The line could not be added.");
RhinoObject ObjLine = doc.Objects.Find(line);
if (ObjLine == null)
  RhinoApp.WriteLine("The object could not be found");
Curve cLine = ObjLine.Curve();

#5

Yes, I skip the error checks for better readability.
The line
Curve cLine = ObjLine.Curve();
could not be compiled but I think that the solution is:
Curve cLine =ObjLine.Geometry as Curve;

May be this is the way for converting from RhinoObject to specific type.
Thank you for the support


(Menno Deij - van Rijswijk) #6

Yes, I skip the error checks for better readability.

I hope only on the forum and not in your code :wink:

I think the best way in this case is:

Guid line = doc.Objects.AddLine(Start, End);
if (line == Guid.Empty) 
   RhinoApp.WriteLine("The line could not be added.");

ObjRef oRef = new ObjRef(line)
Curve cLine = oRef.Curve();

#7

Thank you! :slight_smile:
This is exactly what I’m looking for!