OCC Tutorial 02 – Embedding OpenCascade into wxWidgets

Now that we have our system configure, the test beds working and our IDE project set up, we are now ready to embed our OpenCascade Window into our wxWidget Window.

There’s a handful of tutorials on ways to do this, but they’re either out of date by over a decade or are missing some important points that cause segmentation faults, X11 Bad Window errors, or simply just weird crashes.

Creating the Control

The initial tests kept OpenCascade as a separate entity being parented into a wxPanel during creation of the window at runtime. This was great for a first try, but it becomes tricky too get automatic refreshing and resizing working.

Includes

There’s a large list of includes needed even for a simple file. Instead of adding dozens of lines to this post, you can go over too here and see the required #includes.

Global Variables

The set of global variables needed are shown and described below:

</pre>
<pre>

Constructor

We then need to fill in the constructor. Note the Gist shown below is only an exert of a the full constructor.

The line which we need too dive into now is the Viewer(…) method. This is where the X11 window needs grabs the wxPanel Widget handle and creates a wrapper window around it.

Creating a OCC Window

But before we wrap any windows, first we want to make sure we can create one to make sure everything’s working.

This first gist shows creating the OCC window separate from wxWindow.

Embeding the OCC Window in wxWidgets

Too embed the OCC window into awxPanel control,  First the Handle is needed of the wxPanel.

GtkWidget* widget = panel->GetHandle();

A major issue is when the OCC window is created, the GTK window hasn’t always been fully realized yet and this causes random Segment Faults and crashes. To get around this, we run the next two lines.

gtk_widget_realize( widget );
gtk_widget_set_double_buffered(widget, 0);

The next line is too access the window from the Handle that was retrieved from the wxPanel.

Window wid = GDK_WINDOW_XWINDOW( widget->window );

Note that the Window object is a X11 window.

From there, we can now use the second Xw_Window constructor which creates the OCC window wrapped around a previous control.

wind = new Xw_Window(aDisplayConnection, wid);

The final thing too do is too tell OCC which Xw_Window too use and too map the window.

 //Set Window

mView->SetWindow(wind);

if(!wind->IsMapped()) wind->Map();

When it’s all put together, the Viewer(..) method looks like it does below.

Calling Draw and OnSize

With all that down, all we need to do now is handle resizing and drawing. It’s simply down by setting up the following methods.

And that’s that.

Actually Rendering something!

I’ll cover opening model files in another post, but for now, It’s simple too Create the ‘Demo Bottle’ from the DRAWEXE test bed. Simply add the following method to your class and call it at the end of the constructor after everything else.

Putting everything together, you should get something looking like the following:

Screenshot from 2016-03-07 15-35-01

But what’s that DoZoomFit(); method in that last Gist??? I’ll show that and more Input controls and orientation controls in the next Tutorial.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s