Embedding FreeCADGui/cs

Introduction
It is possible to import the FreeCAD module into a Python application, and use all its tools from the host application, but the graphical user interface (GUI) can be imported as a Python module as well. Normally you can only import the complete interface as a whole, not pieces of it; this is because the FreeCAD interface system is not only made of independent widgets and toolbars, but is a complex construction where several invisible components (such as the selection system, etc) are needed for the main 3D view to be able to function.

But, with a bit of hacking, it is possible to import the whole FreeCAD interface, then move the 3D view from it to your own Qt application. We show here 3 different methods.

Using the FreeCAD 3D view widget directly
Be aware that there are a lot of problems with this approach. The Qt event handling doesn't seem to work (no idea why) and if you use the 3d view's context-menu the application crashes. A better way could be to create your own 3d view SoQtExaminerViewer or SoQtViewer and "push" the content of FreeCAD's 3d view to your view, as shown in the other sections below.

First, get the main window via PySide:

Then get the View3DInventor view the same way:

The following code is generated automatically, by creating a Ui-file with QtDesigner, and converting it to python code with the pyuic tool:

Then, create a main window that should be your application's main window, apply the UI setup above to it in order to add an MDI area and "move" our 3D view to it.

Creating a soGui Examiner Viewer
Alternatively, you can also use the FreeCADGui module to extract a OpenInventor (Coin) representation of the objects of your scene, then use that data in an external viewer (your application). Here is an easy way to get the 3D representation of an object.

Then, create a standalone viewer with Pivy:

Then you just need to run your viewer:

Using the quarter module
Instead of using the sogui viewer, you can also use the more modern quarter module. This is generally the best of the three options.

Without even firing up the FreeCAD Gui
Starting from FreeCAD rev2760 (2010, 1), it is now possible to obtain the coin representation of any FreeCAD object without opening the main window. This makes it extremely easy to implement your own viewer and transparently have FreeCAD updating it. After importing the module, you need to fire it up with the  method, after which you can use all of FreeCAD's view providers to obtain OpenInventor (Coin) nodes.

Or, if using pivy's sogui module doesn't work for you (the sogui module is becoming obsoleted and the coin developers are now favoring the new quarter library, which has much better interaction with qt), this is the same script but with using quarter:

Additional information

 * Embedding a view to another (QT) application?
 * Using Gui functions without Gui.showMainWindow in python script
 * Acess Scenegraph through python API in without Gui mode
 * FreeCAD Hangs When Called from Python
 * Problem with FreeCADGui

In the source code there are examples of embedding FreeCAD with various graphical toolkits:
 * src/Tools/embedded