How to ON_Brep in C#?

Hi

I try to get ON_Brep elements from c++ in c#, But it doesnt show any brep element in Rhino, Code has no error, but in console there are some brep Elements in list(I check with using gettype and count), but not show in Rhino. If anyone can help or give me some tipps for that?

My Code is something like
getting IntPtr from Interop.NativeGeometryNonConstPointer
and then getting Brep from Interop.FromOnBrep

I also dont know how to use this both Methods in the right way.
with lines, polylines or normal calculate from Native Methods works fine but with Element like Brep, Mesh ect., I can not do it.

Regards,
Jira

Dear Jira,

Can you share the Visual Studio project?

Hi Petras,

I can show some part of my codes:

In Library C++

extern "C" __declspec(dllexport) void MooseFunction(ON_Brep* pBrep)
{
    ON_3dPoint center_point1(0, 0, 0);
    ON_3dPoint height_poin1(0.0, 0.0, 10);
    ON_3dVector zaxis1 = height_poin1 - center_point1;
    ON_Plane plane1(center_point1, zaxis1);
    ON_Circle circle1(plane1, 10);
    ON_Cylinder cylinder1(circle1, zaxis1.Length());
    ON_Brep* brep = ON_BrepCylinder(cylinder1, TRUE, TRUE);

    pBrep = brep;
}

Class Native call in C#

[DllImport("example_brep.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int  MooseFunction(ref IntPtr pBrep);

Class for convert method from C++ in C#

public static void getBrep(ref Brep brepElement)
{
var ptr_brep = Interop.NativeGeometryNonConstPointer(brepElement);
NativeMethods.MooseFunction(ref ptr_brep);
brepElement = Interop.CreateFromNativePointer(const_ptr_brep) as Brep;
}

Test in Rhino C#

var sphere = new Sphere(new Point3d(10,10,10), 5);
var brep = sphere.ToBrep();
Convert.getBrep(ref brep);

I think it should actually show a cylinder in Rhino but it was just sphere even I used ref (>_< !)
I also dont know how to use the Syntax in the right way, I am still new with Rhino and also C++,C#

But with Lines, Points works fine without ref, I try both with ref and without ref.

Hi @mimysuna,

I just updated the Moose solution to create a Brep cylinder in C++ and pass it to .NET. If you’ve cloned the repo, just pull.

https://github.com/dalefugier/Moose

– Dale

1 Like

Thank you so much Dale <3

I just test but still doesnt work, return Nothing. (T_T)

Running the MooseNet command from the MooseNet plug-in make a cylinder. So, yeah, it works.

– Dale

Here is my result, lines(polylines), Points has no Problem. So I dont know if I must set up something else for brep Elements. I test in normal Rhino.Inside(Console), then test with RhinoPlugin.
Cylinder Brep still disapear (T_T), The sphere was created in Plugin not from native method, and yeah I got only 4 Elements (3 Polylines and 1 sphere) but my cylinder doesnt come.

I test Code C++ and there is a cylinder, but can not see it in C#.

But Thank you so much for adding example Code.

-mimy

This is also an impressive example for wrapping C++ .dll to C# .dll !! Thank you Dale!! I managed to wrap a C++.dll into C# dll… Now the grasshopper plugin can reference the c# dll rather than an absolute path c++ Dll import…

Super thanks!!!