Question about GeometryBase

Could someone help me understand how to move from a GeometryBase data type into its actual object type. I am kinda new to C# so bear with me. How do I go from geometrybase type to a polycurve type? I guess I am asking after getting to the geometrybase type how do I pivot to its intended class to access methods of interest. Is there something I am missing conceptually ?

here is some basic code I was noodling with. In this case I would like to convert any polycurves that have the name Test to nurbs curves. But I cant access the ToNurbsCurve method because its actually a geometry base not a polycurve despite reading out like a polycurve when calling .tostring().

Thanks !

            ObjectTable doc_objs = doc.Objects;

            LayerTable l_table = doc.Layers;         

            //RhinoApp.WriteLine(l_table.GetType().ToString());
            
            foreach (var ott in doc_objs)
            {
                //RhinoApp.WriteLine(ott.ToString());
                //RhinoApp.WriteLine(ott.Name);


                if (ott.Name == "Test")
                {
                    var geom = ott.Geometry;

                    RhinoApp.WriteLine(geom.ToString());
                    
                    if (geom.ToString() == "Rhino.Geometry.PolyCurve")
                    {
                        var targ_c = geom.ToNurbsCurve();
           
                    }
                    

                    //targ_geom.Add(ott);
                }
            



            }

Good Morning, there is a good guide to getting started here
Rhino - Your First Plugin (Windows) (rhino3d.com)

Once you have made your geometry you need to add it to the document, you should have a reference to a document but if not you access RhinoDoc.ActiveDoc

Then you can access the ObjectTable which provides many Add methods.
doc.Objects.Add(mygeometry);

You can also pass in the Object Attributes as a parameter which will allow you to define which layer the geometry is on.

More info in the API Doc’s https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.tables.objecttable/add

@david.birch.uk Thanks for the reply !

I am just a little confused because when I loop through all the objects within the object table and print them I see that they are Rhino.Geometry.PolylineCurve but I cant seem to access any methods from that class ? What could I be missing ?

            ObjectTable doc_objs = doc.Objects;         

            foreach (var ott in doc_objs)
            {
                var test_obj = ott.Geometry;


                RhinoApp.WriteLine(test_obj.ToString());
                
            }

The result is

Command: OBJtesting
Rhino.Geometry.PolyCurve
Rhino.Geometry.NurbsCurve
Rhino.Geometry.NurbsCurve
Rhino.Geometry.PolyCurve
Rhino.Geometry.PolyCurve
Rhino.Geometry.PolylineCurve

But I cant seem to access the methods within these classes.

test_obj is a Rhino.Geometry.GeometryBase type. How would I convert its respective type (PolyCurve, NurbsCurve,ect)

try playing with this

foreach (var obj in doc.Objects)
{
    var curveObject = obj as Rhino.DocObjects.CurveObject;
    if (curveObject != null)
    {
        RhinoApp.WriteLine("obj is curveObject: {0}", curveObject);
    }
    else
    {
        RhinoApp.WriteLine("obj is: {0}", obj);
    }

    var geometry = obj.Geometry;
    RhinoApp.WriteLine("geometry: {0}", geometry);

    if (geometry.ObjectType == Rhino.DocObjects.ObjectType.Curve)
    {
        var nurbsCurve = geometry as Rhino.Geometry.Curve;
        if (nurbsCurve != null)
        {
            RhinoApp.WriteLine("nurbsCurve: {0}", nurbsCurve);
        }
    }
}

as jdhills sample shows

the actual conversion from geometry base to say Curve is done using a Cast in C#

var nurbsCurve = geometry as Rhino.Geometry.Curve;

 if (nurbsCurve != null)

in this case the as operator is performing the cast from geometrybase to curve

C# Type Casting (w3schools.com)
Casting and type conversions - C# Programming Guide | Microsoft Learn

@jdhill Thanks for this I was playing with it last night, I see now.

@david.birch.uk Thanks for following up ! Ill get back to studying C# !

1 Like