New user: "simple" code not working

Hello,

I'm new to both OCC and programming on VS (2013 Express for WD), and I have the following code which is not working (copied from the user's guide, with some modifications):

=================================================
// Initial test

#include
#include
#include
#include
#include
#include
#include

int main(void){

cout

try{
OCC_CATCH_SIGNALS

Standard_Real dx = 10; //Parameters
Standard_Real dy = 10; //to build a wedge
Standard_Real dz = 10;
Handle(V3d_Viewer)aViewer;
Handle(AIS_InteractiveContext)aContext;
aContext = new AIS_InteractiveContext(aViewer);
BRepPrimAPI_MakeBox w(dx, dy, dz);
TopoDS_Solid S = w.Solid();
Handle(AIS_Shape) anAis = new AIS_Shape(S);
//creation of the presentable object
aContext->Display(anAis);
//Display the presentable object in the 3d viewer.
}
catch (Standard_Failure) {
Handle(Standard_Failure) error = Standard_Failure::Caught();
cout }

return 0;
}
=======================================================================

The project builds without problems, but when I run the built program I get an error. Using VS's Local Window Debugger I was met with the following message:

"Unhandled exception at 0x00007FFD91453F3E (TKV3d.dll) in Software de Fatiamento.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

Any clues as to what is causing this? I use a x64 system, and I believe I correctly built the OCC libraries for such a system (using the developers' guide with help from forums).

Thank you,
Rodrigo

Rodrigo Castro Andrade's picture

Hello again,

After some research on these forums, I noticed the obvious mistake of not instantiating V3d_Viewer. So here are some alterations to the previous code:

===============
//Create a Graphic Driver from the default Aspect_DisplayConnection
Handle(Aspect_DisplayConnection) aDisplayConnection;
Handle(Graphic3d_GraphicDriver) GD = Graphic3d::InitGraphicDriver(aDisplayConnection);

//
Handle(V3d_Viewer)aViewer = new V3d_Viewer(GD, (short const*)"Viewer");
Handle(V3d_View)aView = aViewer->CreateView();
aViewer->SetViewOn(aView);
======================

The program no longer crashes, but it does not show a view window. Any help?

P.S.: Notice above that Aspect_DisplayConnection is not instantiated. I'm using it like that because I had some issues with this class. My compiler did not recognize the existance of a default constructor for it, or any other constructor for that matter.

zbaekhk's picture

You need to create a window and then map viewer to window.
you can find a example in OCC installed folder.

Rodrigo Castro Andrade's picture

Thank you for your helping.

After a long while trying to figure out how to create a window and map it to a view (and not a viewER), I am now faced with the following problem: the compiler does not recognize the SetWindow() method:

===================================================================
Handle(V3d_View) aView = aViewer->CreateView();

// Creates a window
Handle(WNT_WClass) myWClass = new WNT_WClass((char*)L"some_class_fdfdddd", NULL, 0);
Handle(WNT_Window) aWindow = new WNT_Window("Teste Inicial", myWClass, 0, 1, 1, 400, 400, Quantity_NOC_MATRAGRAY, 0, 0, 0);

// Maps the view to the previously created window
aViewer->SetViewOn(aView);
aView->SetWindow(aWindow);
===================================================================

The compiler shows the following errors, all on the last line of the code above (aView->SetWindow(aWindow)):

Error 1 error C2027: use of undefined type 'V3d_View'
Error 2 error C2039: 'SetWindow' : is not a member of 'Handle_V3d_View'
Error 3 IntelliSense: pointer to incomplete class type is not allowed

I am most certainly doing something wrong, but what? Any tips are appreciated.

P.S.: I searched the examples on the OCC folder, but those did not work for my case. Still, they and the forums were really helpful.

Rodrigo Castro Andrade's picture

As I suspected, the problem was solved by simply adding: #include .
The problem now compiles and runs, but the window with the object is not displayed.

Any clues? The current code is as follows. Thanks in advance.

===========================================
// Initial test

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

int main(void){

cout << " First test with OCC Technology! " << endl;

try{
OCC_CATCH_SIGNALS

//Create a Graphic Driver from the default Aspect_DisplayConnection
Handle(Aspect_DisplayConnection) aDisplayConnection;
Handle(Graphic3d_GraphicDriver) myGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);

// Creates a viewer
Handle(V3d_Viewer)aViewer = new V3d_Viewer(myGraphicDriver, (short const*)"Viewer");
aViewer->SetDefaultLights();
aViewer->SetLightOn();

// Creates an interactive context
Handle(AIS_InteractiveContext)aContext = new AIS_InteractiveContext(aViewer);

// Creates a window
Handle(WNT_WClass) myWClass = new WNT_WClass((char*)L"some_class_fdfdddd", NULL, 0);
Handle(WNT_Window) aWindow = new WNT_Window("Teste Inicial", myWClass, 0, 1, 1, 400, 400, Quantity_NOC_MATRAGRAY, 0, 0, 0);

// Creates a view from the viewer, and maps the view to the previously created window
Handle(V3d_View) aView = aViewer->CreateView();
aViewer->SetViewOn(aView);
aView->SetWindow(aWindow);

// Creation of the presentable object, a box in this case
Standard_Real dx = 10; //Parameters
Standard_Real dy = 10; //to build a box
Standard_Real dz = 10;
BRepPrimAPI_MakeBox w(dx, dy, dz);
TopoDS_Solid S = w.Solid();
Handle(AIS_Shape) anAis = new AIS_Shape(S);

//Display the presentable object in the 3D viewer.
aContext->Display(anAis);
}
catch (Standard_Failure) {
Handle(Standard_Failure) error = Standard_Failure::Caught();
cout << error << endl;
}

return 0;
}
====================================================================================

Rodrigo Castro Andrade's picture

Just a quick corretion (that did not fix the problem):

instead of
Handle(WNT_Window) aWindow = new WNT_Window("Teste Inicial", myWClass, 0, 1, 1, 400, 400, Quantity_NOC_MATRAGRAY, 0, 0, 0);

consider
Handle(WNT_Window) aWindow = new WNT_Window("Teste Inicial", myWClass, 1, 1, 1, 400, 400, Quantity_NOC_MATRAGRAY, 0, 0, 0);

The difference is on the argument '1' after the argument myWClass.

Thanks

Rodrigo Castro Andrade's picture

After taking a look at the WNT_Window class methods, I have solved the problem. The act of creating a window does not open it, which is done by the Map() method.
So the final form of the window creation block looks like this:

======================================================
// Creates and opens a window
Handle(WNT_WClass) myWClass = new WNT_WClass((char*)L"some_class_fdfdddd", NULL, 0);
Handle(WNT_Window) aWindow = new WNT_Window("Teste Inicial", myWClass, 1, 1, 1, 400, 400, Quantity_NOC_MATRAGRAY, 0, 0, 0);
aWindow->Map();
====================================================

Although I already expected the solution to be something simple, I'd like to point out that I found no sample, documentation or discussion on the forums pointing this issue to people who are new to this.
In any case, I am (finally) enjoying this tool, and would like to thank those who keep it up.

Rodrigo

Andrey Pozdeev's picture

Hi Rodrigo,

I sympathize with you, OCC is not a very friendly library to use for beginners.

and a beginner could be many things, someone who knows programming but however does not know the api, or someone who is learning to program in c++ and also trying to understand the api, I was in the latter not too long ago.

Welcome to the love-hate relationship we have with OCC.

hang in there... a good starting point for getting into occ is the QTOCC harness by Peter Dolbey.
I pretty much used his code to start my project openshapefactory.

some suggestions for "learning how to learn the library" (this is because as you will sadly find out the documentation of OCC is only the tip of the iceberg... OCC us under documented.. every day you will find a new function or feature that was not documented but was available in OCC, dont be surprised if that happens)

0- First of all grab the documentation, however under-documented it is... it is actually quite good and sets-out the philosophical background of the library. when i fist started using occ, I printed those pdf's and binded them together and had them next to me physically when ever i had free time, I just read about the foundation classes, topology, visualization and the many features available...

1- Learn by Example: open the occ samples particularly the geometry sample.... when you execute a function it normally opens a window showing sample code... find a word or something in it that you could search for within the visual studio solution that will find the actual function behind that button.

2-use text searching tools within the occ folder such as grep, if in windows there is wingrep.

3- Roman Lygin has a great blog that has articles that range from beginning concepts, all the way to advanced topics, search him on google, he also occasionally posts in this forum.

4- if you are not that good in programming with c++, use the python binding PythonOCC by Thomas Paviot and Jelle Feringa, you will get everything in OCC but using the more lenient python. There is also c# and Java bindings, if you already know java you could jump into that and will also be more lenient...

5- search the forum for past posts... you would be surprised how many gems are hidden in this forum from many years ago.

now after telling you how to "learn to learn the library"... let me give you some field notes about OCC....

OCC as geometry kernel... is a competitive product with Acis, Parasolids, CGM....

at the moment OCC is the only opensource geometry kernel that can give you a high level of quality geometry and based on industry standards within the "solids modelling" group of libraries, that is tolerance modelling, topology and surfacing like Nurbs, Bezier and Analytic geometries like Conics.

so making the decision about thinking to use OCC boils down to the purpose you intend for the library.

if you want to learn how to program a geometry kernel as an educational exercise... OCC is perfect for that. Also great for doing in-house tools which solve a very specific geometrical problem and were you will be the sole user of the code, it doesn't need to be robust and you don't mind bugs, because all you really care about is the data that it shoots out or the diagrams it generates for a paper you may be writing.

if you want to use OCC to develop a commercial product... there are many factors to consider:

1- OCC off the shelf is not a very robust product, however it is a very robust service, the base source code may not be robust, but you can hire the occ developers as a service and these two combined will create a robust product. For commercial products I cant see how you would have one without the other. (look at side note).

2- ACIS, Parasolids and CGM are more robust products off the shelf. you can create a robust product from these libraries with minimal interaction with the main developers... and many third-party solutions which plug to these systems exist, so there is a more supportive ecosystem around these tools to develop commercial softwares say for specific industries like Architecture because the programmer or developer doesnt have to worry too much about the library working or not working but rather at how the GUI is going to look and how to map the GUI to the domain problem the software is addressing. that's why Autodesk, Bentley and Dassault use these libraries or were using them when they were small companies and then developed their own once they had the capital, Autodesk for example recently replaced ACIS with their own internal solution, after many years of using ACISas the geometry kernel of their main products.

In conclusion, Congrats in joining the community of users interested in OCC, but consider why you would want to use OCC in light of the points I have mentioned, so that you could have a much more fit-for-purpose geometry kernel that is selected based on your needs.

Regards,

a side note on occ as a service and not a product: At the moment whether by design or accident, the relationship between the occ parent company and the product is very interesting. The main revenue stream for the OCC developers is providing services. So while OCC is a product, it is their secondary product, their primary product is the service, and they need this in order to profit and therefore have the motive to provide to us for free OCC the secondary product (occ the library). If the OCC developers don't get paid, they will fade-out and OCC source code will be left on a limb. So we don't want them to not profit, so that they still have the business imperative to provide us with OCC as benefit to the opensource community, which is also an integral component to their primary product "the a-la-carte services".

Rodrigo Castro Andrade's picture

Thanks for the kind reply. I have indeed once considered using other geometric kernels, but at this point OCC is my choice.
I will certainly (if not already doing that) follow your tips on "learning the library".

Regards,
Rodrigo