GeometryBase to OnObject and Back

Hi,
So in our goal to get our code ready for Rhino 6, we are doing a bunch of RhinoCommon conversion, and along the way a lot of Interop’s are happening between converted RhinoCommon code and older .NET sdk code.

As part of this I have the need to convert from a GeometryBase Object to a OnObject (And back again)
I have some code that starts out like this:

        File3dm model=File3dm.Read(fileName);

        List<OnObjects> objects = new List<OnObject>();

        foreach(File3dmObject fObject in model.Objects)
        {
            GeometryBase gb =fObject.Geometry;

            OnObject obj = *****Need converter here**** from gb;

            if(obj==null) continue;

            Objects.Add(obj);
       }

I started writing a function to do this per object type, like this:

    private static OnObject toObj(GeometryBase gb)
    {
        Brep b = gb as Brep;
        if (b!=null)
            return (OnObject)Interop.ToOnBrep(b);

        Curve nc=gb as Curve;
        if (nc != null)
            return (OnCurve) Interop.ToOnCurve(nc);

        return null;
    }

But was hoping maybe there is a built in way to do this with some intptr or something?

(I also have code elsewhere that needs to take an OnObject to return a GeometryBase)

Thanks

Gordon J.

I would go with the following

private static OnObject ToOnObject(GeometryBase gb)
{
  IntPtr ptr = Rhino.Runtime.Interop.NativeGeometryNonConstPointer(gb);
  if( IntPtr.Zero == ptr )
    return null;
  return OnObject.WrapNativePointer(ptr, false, false, gb);
}

private static GeometryBase ToGeometryBase(OnObject obj)
{
  OnGeometry geom = OnGeometry.Cast(obj);
  if( geom==null )
    return null;
  geom = geom.Duplicate();
  IntPtr ptr = geom.InternalPointer();
  geom.SetDoDeleteFlag(false);
  return Rhino.Runtime.Interop.CreateFromNativePointer(ptr);
}

I can write some cleaner implementations of this in the future, but the above should work for now. Once I have something else in place, we can just tweak the contents of those two functions. This is on my todo list at
http://mcneel.myjetbrains.com/youtrack/issue/RH-35239

I have a similar problem, could you suggest how do I get back the Rhino_DotNet OnCurve object from
Rhino.Runtime.Interop.ToOnCurve(Curve source)

since a System.Object is returned by the above method, how should I extract the OnCurve object from here.

Thanks
Sunayana

Hi Sunayana,

If you have a Rhino.Geometry.Curve from RhinoCommon and you need a ``OnCurveobject for Rhino .NET, the use Steve'sToOnObject``` static function listed above.

Does this help?

– Dale

perform a cast using the ‘as’ keyword in C#

Thank you @dale @stevebaer for the pointers. Since I use C++/CLI currently, I did the following:

Summary

This text will be hidden

RMA::OpenNURBS::OnCurve^ intersectionCurve = dynamic_cast<RMA::OpenNURBS::OnCurve^>(Rhino::Runtime::Interop::ToOnCurve(commonIntersectionCurve));

here commonIntersectionCurve is of type Rhino::Geometry::Curve^

and it works well for me.

Sunayana