Wrapper fpr .NET

Hey there.

I am trying to write my own Wrapper-DLL for .NET.
As far I have some first results, but I am not quite happy with it, because it looks a bit messy.
Can anyone, who has already written a Wrapper DLL E-Mail me a bit Sample-Source-Code, so that I can get an Idea of how you've done it.
That would be very kind.

E-Mail: marc.baumberger@leyboldoptics.com

Gerhard Hofmann's picture

Hello,

for example
[DllImport("OCasToDotNet.dll")] [SuppressUnmanagedCodeSecurity]
private static extern IntPtr BRepMesh_ShapeTool_FirstVertex_17(IntPtr E);
///
///if there is not first vertex (semi-infinite edge)
///
public static TopoDS_Vertex FirstVertex(TopoDS_Edge E)
{
try
{

TopoDS_Vertex _res = Handle.Create(BRepMesh_ShapeTool_FirstVertex_17(E)) as TopoDS_Vertex;

return _res;
}
catch (SEHException)
{
throw new OpenCascade.Exception();
}
catch (AccessViolationException)
{
throw new OpenCascade.Exception("Access violation in OpenCascade");
}
}

[DllImport("OCasToDotNet.dll")] [SuppressUnmanagedCodeSecurity]
private static extern IntPtr BRepMesh_ShapeTool_LastVertex_18(IntPtr E);
///
///if there is not last vertex (semi-infinite edge)
///
public static TopoDS_Vertex LastVertex(TopoDS_Edge E)
{
try
{

TopoDS_Vertex _res = Handle.Create(BRepMesh_ShapeTool_LastVertex_18(E)) as TopoDS_Vertex;

return _res;
}
catch (SEHException)
{
throw new OpenCascade.Exception();
}
catch (AccessViolationException)
{
throw new OpenCascade.Exception("Access violation in OpenCascade");
}
}

and the corresponding C++ Code is:

OCASTOCSHARP_API CTopoDS_Vertex *CALLING_CONVENTION BRepMesh_ShapeTool_FirstVertex_17(CTopoDS_Edge * E)
{

CTopoDS_Vertex *_res = CTopoDS_Vertex::Create(BRepMesh_ShapeTool::FirstVertex(E->GetData()),(CHandle *)NULL);

return _res;
}
OCASTOCSHARP_API CTopoDS_Vertex *CALLING_CONVENTION BRepMesh_ShapeTool_LastVertex_18(CTopoDS_Edge * E)
{

CTopoDS_Vertex *_res = CTopoDS_Vertex::Create(BRepMesh_ShapeTool::LastVertex(E->GetData()),(CHandle *)NULL);

return _res;
}

I hope everything appears well formatted in this message. But it is ofcourse alot more that you need to know. I generated everything automatically from the cdl files and you may use the wrapper for free (look at the open source community)

best regards
Gerhard

Claudio Benghi's picture

Gerhard,

I've been looking at OpenCASCADE for a couple of weeks now but still couldn't figure out how to use your wrapper to display the model.
I couldn't find any implementation for TKV3D or any other package in the visualization module (OCCTDocumentation6.2.0/Visualization/html/packages.html)

I've not much familiarity with c++ [I probably wouldn't use the wrapper if I did ;-) ] but to improve my skills I'd like to try to implement the module (or the required foundations).

If I understood correctly I shold work on 2 VS projects:
- a c# prj referencing your c# wrapper and implementing the missing classes in a compatible namespace p/invoking the other.
- a c++ project defining the methods and... here I enter the deepest fog ... should I be calling existing occ dlls? Would this be an extension of the existing c++ program? Would I need to reference your headers? No clue.

I know that your company sells licenses for CADability, so forgive me if you find this questions inappropriate, but somewhere I also read you asking which module would be needed for visualisation. Apparently they are many I'd say that samples\standard\mfc\06_AISSelect may give you an idea of the (long term) useful functions.

Thank you,
Claudio

Naehfix's picture

Hi there.

I finally found my way through making a wrapper. Unfortunally you have to create two DLL's.
The first one that encapsulates the Data-Types an Classes from the OCC-Framework. This DLL has to be compiled without direct connection to the .NET Framework. It has to be compiled with the /clr command. This makes the DLL usable for a Managed C++ Dll, which is the second DLL you have to create. This second DLL are like Proxy-Servers, which connect the First DLL and its Classes with the .NET Framework.

Managed C++ is the only language which can combine native and managed Code. Thats the reason why you HAVE to write your Wrapper-DLL in Managed C++ if you don't want to you P/Invoke which, in my opinion, is a messy way to do the job.

I am now as far, as I can use my Wrapper-DLL to load an IGES-File an Display it in a C# Project. I am going to implement some more functionality, but I have a project running which only needs some certain OCC-Functions. I will make my DLLs public and maybe open source when I am finished with the implementation i need for my Project.

But as I see it is a very common Problem for many people around here, not being able to use OCC from a .NET language other then Managed C++. If I find some more time, I will add a lot more OCC-Functionality to my Wrapper-DLL.

When I am back at work (Thursday) I will post a short Version of how exactly I implemented my Wrapper-DLL. May be this will help some of you, to get the idea how to accomplish that.

Best Regards
Marc

Naehfix's picture

By the way. Microsoft added some workaround for using existing C++ Code in .NET.
Therefore you have to create a new C++ Project and add your existing Code. This Code has to be compiled as an DLL-File with the /CLR Option enabled.

As far as I have read articles about that, it SHOULD Work this way. But it might come to some complications if you have some...hmm...lets call it "messy code". So in Theory it SHOULD be possible to take the OCC-Code (yeah its Open Source ;) ), pack it into a new C++ Project and compile it with the /CLR option enabled. This SHOULD result in a brand new OCC-DLL which should be usable in any .NET Project. As a matter of time preasure in my Project, I don't have much time to play around on this suggestion. But may be someone else wants to try this. I'd be happy to hear from any results.

Stephane Routelous's picture

fyi, at work, we are using OpenCASCADE as backend and a GUI and applicative code in C#/.Net.

I'm not exposing OpenCASCADE to C#. I defined a pure C++ library encapsulating the OpenCASCADE calls.
with swig (http://www.swig.org/) I'm generating C# objects from this API. and the front end only uses this API. (some 3d display is also done with OCC).

so, I have OpenCASCADE -> Applicative backend objects (C++) -> SWIG -> Applicative backend objects (C#) -> Front End (where we usually inherit from the applicative backend objects to add front end specific code).

as an example, I have a MyAPI_Model C++ class containing a TopoDS_Shape and an AIS_InteractiveObject.
The access to the shape is only done with c++ datatypes ( ex : MyAPI_Model::GetBoundingBox(double& x,double& x,double& z,double& X,double& Y,double& Z) ) and the display is done with OCC wrapping methods (MyAPI_Model::Show() MyAPI_Model::Hide() which will call the AIS_InteractiveContext ).

The good part is that the front end is totally independant of OpenCASCADE, so in the future we will certainly need to replace the 3d viewer from OpenCASCADE with something more appropriate. This will be done completly in the backend, without changing one line of code in the front end.

It's more work for the API definition, but once it's done, the code is usable by other user who have zero knowledge of OpenCASCADE, and as the OCC code is self contained, it's easier to modify/debug/replace (we can completly remplace OCC without changing one line of code in the front end)

Stephane

Naehfix's picture

Hi there.
As promised, here is a link to the Code I've written so far.
www.jay-code.de/lo/OpenCascadeWrapper.zip

Its an Visual Studio .Net 2008 Project which includes 3 Projects.
One is a pure C++ Project that encapsulates the OCC-Classes and is compiled with the /CLR:OldSyntax switch.

The Next Project is an Managed C++ DLL wich encapsulates the C++ Project Classes an publicates it to the .NET Framework.

The third Project is a C# Sharp Window-Form which uses the Managed C++ Classes to create a 3D Window which loads an shows an IGES-File.

There is not much code in it yet. But its my first attempt wo write a Wrapper-DLL and it seems to work well. More code is to be added.

Hopefully it gives you an Idea of how to accomplich writing a in .NET usable Wrapper for OCC.

If you've got any questions about the code, feel free to ask here or write me an E-mail to marc.baumberger@leyboldoptics.com

Marc

Naehfix's picture

Hi there.
As promised, here is a link to the Code I've written so far.
www.jay-code.de/lo/OpenCascadeWrapper.zip

Its an Visual Studio .Net 2008 Project which includes 3 Projects.
One is a pure C++ Project that encapsulates the OCC-Classes and is compiled with the /CLR:OldSyntax switch.

The Next Project is an Managed C++ DLL wich encapsulates the C++ Project Classes an publicates it to the .NET Framework.

The third Project is a C# Sharp Window-Form which uses the Managed C++ Classes to create a 3D Window which loads an shows an IGES-File.

There is not much code in it yet. But its my first attempt wo write a Wrapper-DLL and it seems to work well. More code is to be added.

Hopefully it gives you an Idea of how to accomplich writing a in .NET usable Wrapper for OCC.

If you've got any questions about the code, feel free to ask here or write me an E-mail to marc.baumberger@leyboldoptics.com

Marc

Naehfix's picture

Hi there.
As promised, here is a link to the Code I've written so far.
www.jay-code.de/lo/OpenCascadeWrapper.zip

Its an Visual Studio .Net 2008 Project which includes 3 Projects.
One is a pure C++ Project that encapsulates the OCC-Classes and is compiled with the /CLR:OldSyntax switch.

The Next Project is an Managed C++ DLL wich encapsulates the C++ Project Classes an publicates it to the .NET Framework.

The third Project is a C# Sharp Window-Form which uses the Managed C++ Classes to create a 3D Window which loads an shows an IGES-File.

There is not much code in it yet. But its my first attempt wo write a Wrapper-DLL and it seems to work well. More code is to be added.

Hopefully it gives you an Idea of how to accomplich writing a in .NET usable Wrapper for OCC.

If you've got any questions about the code, feel free to ask here or write me an E-mail to marc.baumberger@leyboldoptics.com

Marc

Timo Roth's picture

Hello Marc,

I am also interested in a open-source C#-Wrapper for OCC. Did you do some further work on it since February?

Regards,
Timo

Naehfix's picture

Hi Timo.

Actually I have written a small C++/C# Wrapper. But I only implemented the Classes and Functions I needed.
If you give me your E-Mail Address, I could send you my code, so you can get an Idea of how I wrote the Wrapper. It's pretty easy, but it is a lot of writing. That's why I only wrapped the Classes and Functions I needed.

Greetings
Marc

Adrian's picture

Hello Marc,

I´m also interested in this wrapper. It´s possible i´ll have to write something similar. Thanks for your help

Regards,
Adrián

Gerhard Hofmann's picture

Hello Timo,

did you test my wrapper? http://www.opencascade.org/org/community/projects/?project_id=218

regards,
Gerhard

Stephen Leary's picture

the IntTools package seem to be missing from your wrapper Gerhard. (Although i could be wrong)

Are there any plans to add it?

Stephen

Guido van Hilst not specified's picture

For developing opencascade with c#/.net have a look at: https://www.occwrapper.com/