Passing list of curves from c# to c++

Hi guys,
I have searched the web, I didn’t find the solution yet. My problem is that I have a list of curves, which are the selected curves in rhino grasshopper, I want to pass this list of curves from C# to C++ codes. Is there a simple way to do it?
Thank you.
Regards
John

Hi, what you want is something called “pinvoke”.
Marshalling to unmanaged code is not easy, especially for whole class instances. But essentially this is was Rhinocommon does. Probably its easier to extract the relevant curve properties (points, knots and weights) and recreate the curve from scratch in C++. I saw you are referring to rhino3dm. Are you aware that there is also .NET version of it? Marshalling works better if you just pass arrays of doubles.

Hi Tom,

Thank you for reply. I use .Net. I think it should be a simple way to pass an array of ON_Curve pointer from C# to C++. In my codes, I pass brep pointer and single curve pointer to the c++ from c#.
Regards
John

Wow, here is the chatgpt result, I will use it.

class Program
{
    [DllImport("YourCppLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void ProcessCurves(IntPtr[] curves, int count);

    public void PassCurvesToCpp(ON_Curve[] curveList)
    {
        IntPtr[] curvePointers = new IntPtr[curveList.Length];
        for (int i = 0; i < curveList.Length; ++i)
        {
            curvePointers[i] = curveList[i].ConstPointer();
        }

        ProcessCurves(curvePointers, curveList.Length);
    }
}

I’m pretty sure this code is garbage and will not work at all.
Dale has written an example plugin which might be helpful instead:

1 Like

Hi @smartunfold,

Clone and/or pull the Moose project (above) and search for ExampleSetCurves.

– Dale