Embedding FreeCADGui/fr: Difference between revisions

From FreeCAD Documentation
(Created page with "Alternativement, vous pouvez également utiliser le module '''FreeCADGui''' pour extraire une représentation '''coin/openInventor''' des objets de votre scène, puis utilisez...")
mNo edit summary
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
Vous savez déjà que vous pouvez [[Embedding FreeCAD/fr|importer le module FreeCAD]] dans une application Python, et d'utiliser tous les outils de l'application hôte. Mais l'interface utilisateur FreeCAD (GUI) peut également être importée en tant que module Python.
Vous savez déjà que vous pouvez [[Embedding FreeCAD/fr|importer le module FreeCAD]] dans une application Python, et d'utiliser tous les outils de l'application hôte. Mais l'interface utilisateur FreeCAD (GUI) peut également être importée en tant que module Python.
Normalement, vous pouvez importer l'interface complète dans son ensemble, et non pas des portions de celui-ci. C'est parce que le système d'interface FreeCAD n'est pas seulement faite de widgets indépendants, et, de barres d'outils, mais, cette interface est une construction complexe, où plusieurs composantes invisibles (tels que le système de sélection, etc) sont nécessaires à la vue 3D, pour pouvoir fonctionner.
Normalement, vous pouvez importer l'interface complète dans son ensemble, et non pas des portions de celui-ci. C'est parce que le système d'interface FreeCAD n'est pas seulement faite de widgets indépendants, et, de barres d'outils, mais, cette interface est une construction complexe, où plusieurs composantes invisibles (tels que le système de sélection, etc) sont nécessaires à la vue 3D, pour pouvoir fonctionner.
Line 12: Line 13:
Tout d'abord, obtenir la fenêtre principale via '''PyQt''' :
Tout d'abord, obtenir la fenêtre principale via '''PyQt''' :
<syntaxhighlight>
<syntaxhighlight>
from PyQt4 import QtGui
from PySide import QtGui
from PyQt4 import QtCore
from PySide import QtCore
def getMainWindow():
def getMainWindow():
toplevel = QtGui.qApp.topLevelWidgets()
toplevel = QtGui.qApp.topLevelWidgets()
for i in toplevel:
for i in toplevel:
if i.metaObject().className() == "Gui::MainWindow":
if i.metaObject().className() == "Gui::MainWindow":
return i
return i
raise Exception("No main window found")
raise Exception("No main window found")

mw=getMainWindow()
mw=getMainWindow()
</syntaxhighlight>
</syntaxhighlight>
Ensuite, récupérez les '''View3DInventor''' de la même façon :
Ensuite, récupérez les '''View3DInventor''' de la même façon :
<syntaxhighlight>
<syntaxhighlight>
def get3dview(mw):
def get3dview(mw):
childs=mw.findChildren(QtGui.QMainWindow)
childs=mw.findChildren(QtGui.QMainWindow)
for i in childs:
for i in childs:
if i.metaObject().className()=="Gui::View3DInventor":
if i.metaObject().className()=="Gui::View3DInventor":
return i
return i
return None
return None

v=get3dview(mw)
v=get3dview(mw)
</syntaxhighlight>
</syntaxhighlight>
Le code suivant est généré automatiquement, par la création d'un fichier [[Dialog_creation/fr|QtDesigner]], et de le convertir en code Python avec l'outil '''[[Dialog_creation/fr#Conversion_de_notre_bo.C3.AEte_de_dialogue_en_code_Python_avec_.22pyuic.22|pyuic]]''' :
Le code suivant est généré automatiquement, par la création d'un fichier [[Dialog_creation/fr|création d'un fichier UI avec QtDesigner]], et de le convertir en code Python avec l'outil pyuic:
<syntaxhighlight>
# -*- coding: utf-8 -*-


# Form implementation generated from reading ui file 'mainwindow.ui'
== Conversion de notre boîte de dialogue en code Python avec "pyuic" ==
#
# Created: Sun Dec 27 11:18:56 2009
# by: PySide UI code generator 4.6
#
# Modify for PySide 11/02/2015
# Python version: 2.7.8
# Qt version: 4.8.6
#
# WARNING! All changes made in this file will be lost!


from PySide import QtCore, QtGui
Maintenant, nous allons sauver notre widget quelque part. Il sera sauvegardé dans un fichier '''.Ui''', que nous allons facilement convertir en script Python avec '''pyuic'''.<br />
Dans windows, le programme est livré avec '''pyuic pyqt''' (à vérifier), sur Linux, vous aurez probablement besoin de l'installer séparément à partir de votre gestionnaire de paquets (sur debian-systèmes basés sur, il fait partie du paquet pyqt4-dev-tools).<br />
Pour faire la conversion, vous aurez besoin d'ouvrir une fenêtre de terminal (ou une fenêtre d'invite de commandes), accédez à l'endroit où vous avez enregistré votre fichier '''ui''' :
* '''pyuic.py''' est l'outil qui convertit les fichiers qt-designer '''.ui''' (Interface Utilisateur) en fichier '''.py''' (code Python), la ligne de commande dans la console DOS est :<br />
pyuic -x fichier.ui > fichier.py


class Ui_MainWindow(object):
* vous pouvez créer un fichier '''.bat''' pour automatiser la commande:
def setupUi(self, MainWindow):
* copiez cette ligne dans un fichier texte et sauvez le sous le nom '''"compile.bat"'''
MainWindow.setObjectName("MainWindow")
MainWindow.resize(508, 436)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.mdiArea = QtGui.QMdiArea(self.centralwidget)
self.mdiArea.setViewMode(QtGui.QMdiArea.TabbedView)
self.mdiArea.setTabPosition(QtGui.QTabWidget.South)
self.mdiArea.setObjectName("mdiArea")
self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 508, 27))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)


self.retranslateUi(MainWindow)
@"C:\Python27\python" "C:\Python27\Lib\site-packages\PyQt4\uic\pyuic.py" -x %1.ui > %1.py
QtCore.QMetaObject.connectSlotsByName(MainWindow)


def retranslateUi(self, MainWindow):
* puis tapez à la ligne de commande " '''compile fichier''' " sans extension, le nom "'''fichier'''" entré '''.ui''', sera le nom sortant avec extension '''.py'''
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
* '''ATTENTION: il faut que les fichiers soient présents et accessibles, vérifiez que les fichiers sont présents et que les chemins sont justes !'''
* pour cet exemple entièrement automatique et simplifié, '''"compile.bat"''' est au même endroit que le '''fichier.ui''' à convertir en '''fichier.py'''<br />
Autres liens de documentation [http://www.qtrac.eu/pyqtbook.html "Python and Qt"] , sur [http://ogirardot.developpez.com/introduction-pyqt/ Développez.com] et bien d'autres.<br />
Sur certains systèmes, le programme est appelé '''pyuic4''' au lieu de '''pyuic''' (attention à la compatibilité). Il sert simplement de convertisseur de fichier '''.Ui''' en un script python '''.py'''.<br /><br />
<syntaxhighlight>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Sun Dec 27 11:18:56 2009
# by: PyQt4 UI code generator 4.6
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(508, 436)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.mdiArea = QtGui.QMdiArea(self.centralwidget)
self.mdiArea.setViewMode(QtGui.QMdiArea.TabbedView)
self.mdiArea.setTabPosition(QtGui.QTabWidget.South)
self.mdiArea.setObjectName("mdiArea")
self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 508, 27))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
</syntaxhighlight>
</syntaxhighlight>
Ensuite, créez la fenêtre principale, qui devrait être la fenêtre principale de votre application, et appliquez la configuration UI (UI setup) ci-dessus, dans le but d'ajouter une zone MDI et "bouger" notre point de vue 3d pour elle.
Ensuite, créez la fenêtre principale, qui devrait être la fenêtre principale de votre application, et appliquez la configuration UI (UI setup) ci-dessus, dans le but d'ajouter une zone MDI et "bouger" notre point de vue 3d pour elle.
<syntaxhighlight>
<syntaxhighlight>
ui=Ui_MainWindow()
ui=Ui_MainWindow()
my_mw=QtGui.QMainWindow()
my_mw=QtGui.QMainWindow()
ui.setupUi(my_mw)
ui.setupUi(my_mw)
ui.mdiArea.addSubWindow(v)
ui.mdiArea.addSubWindow(v)
my_mw.show()
my_mw.show()
</syntaxhighlight>
</syntaxhighlight>
==Création d'un examinateur "soGui Examiner Viewer"==
==Création d'un examinateur "soGui Examiner Viewer"==
Line 115: Line 101:
myNode=coin.SoDB.readAll(inp) # restore from string
myNode=coin.SoDB.readAll(inp) # restore from string
</syntaxhighlight>
</syntaxhighlight>
Then, create a standalone viewer with pivy:
Ensuite, créez un visualiseur autonome avec '''pivy''' :
<syntaxhighlight>
<syntaxhighlight>
from pivy.sogui import *
from pivy.sogui import *
from pivy.coin import *
from pivy.coin import *
import sys
import sys

def myViewer():
def myViewer():
# Initialize Coin. This returns a main window to use.
# Initialize Coin. This returns a main window to use.
# If unsuccessful, exit.
# If unsuccessful, exit.
myWindow = SoGui.init(sys.argv[0])
myWindow = SoGui.init(sys.argv[0])
if myWindow == None: sys.exit(1)
if myWindow == None: sys.exit(1)

# Make an empty scene and add our node to it
# Make an empty scene and add our node to it
scene = SoSeparator()
scene = SoSeparator()
scene.addChild(myNode)
scene.addChild(myNode)

# Create a viewer in which to see our scene graph.
# Create a viewer in which to see our scene graph.
viewer = SoGuiExaminerViewer(myWindow)
viewer = SoGuiExaminerViewer(myWindow)

# Put our scene into viewer, change the title
# Put our scene into viewer, change the title
viewer.setSceneGraph(scene)
viewer.setSceneGraph(scene)
viewer.setTitle("FreeCAD Object Viewer")
viewer.setTitle("FreeCAD Object Viewer")
viewer.show()
viewer.show()

SoGui.show(myWindow) # Display main window
SoGui.show(myWindow) # Display main window
SoGui.mainLoop() # Main Coin event loop
SoGui.mainLoop() # Main Coin event loop
</syntaxhighlight>
</syntaxhighlight>
Ensuite, il vous suffit de lancer votre visualiseur :
Then you just need to run your viewer:
<syntaxhighlight>
<syntaxhighlight>
myViewer()
myViewer()
</syntaxhighlight>
</syntaxhighlight>
==Using the quarter module==
==Utilisation d'un module tiers==


Au lieu d'utiliser le visualiseur '''Sogui''', vous pouvez aussi utiliser un module tiers plus moderne. C'est probablement la meilleure des 3 solutions.
Instead of using the sogui viewer, you can also use the more modern quarter module. This is probably the best solution of the 3.
<syntaxhighlight>
<syntaxhighlight>
#!/usr/bin/env python
#!/usr/bin/env python
Line 300: Line 286:
def main():
def main():
app = QApplication(sys.argv)
app = QApplication(sys.argv)
mdi = MdiMainWindow(app)
mdi = MdiMainWindow(app)
mdi.show()
mdi.show()
Line 320: Line 305:
main()
main()
</syntaxhighlight>
</syntaxhighlight>
== Without even firing up the FreeCAD Gui ==


== Sans exécuter le Gui de FreeCAD ==
Starting from FreeCAD rev2760, 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 FreeCADGui module, you need to fire it up with the setupWithoutGUI() method, after which you can use all of FreeCAD's view providers to obtain coin/openInventor nodes.

A partir de FreeCAD rev2760, il est maintenant possible d'obtenir la représentation d'un objet quelconque '''coin''' FreeCAD sans devoir ouvrir la fenêtre principale.<br />
Il est donc extrêmement facile de mettre en œuvre votre propre visualiseur, et, rester transparent pour les mises à jour de FreeCAD.<br />
Après avoir importé le module '''FreeCADGui''', vous devez le mettre à niveau avec la méthode '''setupWithoutGUI()''', après quoi, vous pouvez utiliser tous les fournisseurs de vue de FreeCAD, pour obtenir les nœuds '''coin/openInventor'''.
<syntaxhighlight>
<syntaxhighlight>
import os, sys, FreeCAD, FreeCADGui
import os, sys, FreeCAD, FreeCADGui
Line 387: Line 375:
main()
main()
</syntaxhighlight>
</syntaxhighlight>
Ou, si vous utilisez le module '''pivy Sogui''' et qu'il ne fonctionne pas chez vous, (le module '''Sogui''' devient obsolète, et, les développeurs, désormais favorisent la bibliothèque '''coin''', qui a une interaction bien meilleure avec qt), c'est le même scénario, mais avec une aide tierce :
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:
<syntaxhighlight>
<syntaxhighlight>
#!/usr/bin/env python
#!/usr/bin/env python
Line 463: Line 451:
def main():
def main():
FreeCADGui.setupWithoutGUI()
app = QApplication(sys.argv)
app = QApplication(sys.argv)
FreeCADGui.setupWithoutGUI()
mdi = MdiMainWindow(app)
mdi = MdiMainWindow(app)
mdi.show()
mdi.show()
Line 475: Line 463:
</syntaxhighlight>
</syntaxhighlight>


[[Category:Poweruser Documentation]]
[[Category:Poweruser Documentation/fr]]
[[Category:Python Code]]
[[Category:Python Code/fr]]


{{clear}}
{{clear}}
<languages/>

Revision as of 18:13, 4 December 2019

Vous savez déjà que vous pouvez importer le module FreeCAD dans une application Python, et d'utiliser tous les outils de l'application hôte. Mais l'interface utilisateur FreeCAD (GUI) peut également être importée en tant que module Python. Normalement, vous pouvez importer l'interface complète dans son ensemble, et non pas des portions de celui-ci. C'est parce que le système d'interface FreeCAD n'est pas seulement faite de widgets indépendants, et, de barres d'outils, mais, cette interface est une construction complexe, où plusieurs composantes invisibles (tels que le système de sélection, etc) sont nécessaires à la vue 3D, pour pouvoir fonctionner.

Mais, avec un peu de "hacking", il est possible d'importer toute l'interface de FreeCAD, alors déplacez la vue 3D de FreeCAD dans votre propre application Qt.

Nous vous montrons ici, 3 méthodes différentes.

En utilisant directement le widget vue 3D de FreeCAD

Soyez conscient qu'il y a d'énormes problèmes dans cette approche. La gestion des événements Qt ne semble pas fonctionner (pourquoi ?? aucune idée !), et, si vous utilisez la vue 3D, du menu contextuel, l'application se bloque. Une meilleure façon serait de créer votre propre vue 3D SoQtExaminerViewer ou SoQtViewer et "pousser" le contenu de la vue 3d de FreeCAD dans votre code, comme indiqué dans les sections ci-dessous.

Tout d'abord, obtenir la fenêtre principale via PyQt :

from PySide import QtGui
from PySide import QtCore
   
def getMainWindow():
   toplevel = QtGui.qApp.topLevelWidgets()
   for i in toplevel:
      if i.metaObject().className() == "Gui::MainWindow":
         return i
   raise Exception("No main window found")

mw=getMainWindow()

Ensuite, récupérez les View3DInventor de la même façon :

def get3dview(mw):
      childs=mw.findChildren(QtGui.QMainWindow)
      for i in childs:
         if i.metaObject().className()=="Gui::View3DInventor":
            return i
      return None

v=get3dview(mw)

Le code suivant est généré automatiquement, par la création d'un fichier création d'un fichier UI avec QtDesigner, et de le convertir en code Python avec l'outil pyuic:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Sun Dec 27 11:18:56 2009
#      by: PySide UI code generator 4.6
#
# Modify for PySide 11/02/2015
#      Python version: 2.7.8
#      Qt version: 4.8.6
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(508, 436)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.mdiArea = QtGui.QMdiArea(self.centralwidget)
        self.mdiArea.setViewMode(QtGui.QMdiArea.TabbedView)
        self.mdiArea.setTabPosition(QtGui.QTabWidget.South)
        self.mdiArea.setObjectName("mdiArea")
        self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 508, 27))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))

Ensuite, créez la fenêtre principale, qui devrait être la fenêtre principale de votre application, et appliquez la configuration UI (UI setup) ci-dessus, dans le but d'ajouter une zone MDI et "bouger" notre point de vue 3d pour elle.

ui=Ui_MainWindow()
my_mw=QtGui.QMainWindow()
ui.setupUi(my_mw)
ui.mdiArea.addSubWindow(v)
my_mw.show()

Création d'un examinateur "soGui Examiner Viewer"

Alternativement, vous pouvez également utiliser le module FreeCADGui pour extraire une représentation coin/openInventor des objets de votre scène, puis utilisez des données coin, dans une visionneuse externe (votre application).

Voici un moyen facile d'obtenir la représentation 3D d'un objet:

    FreeCAD.activeDocument().addObject("Part::Box","myBox")
    s=FreeCADGui.activeDocument().getObject("myBox").toString() # store as string
    from pivy import coin
    inp.setBuffer(s)
    myNode=coin.SoDB.readAll(inp) # restore from string

Ensuite, créez un visualiseur autonome avec pivy :

from pivy.sogui import *
from pivy.coin import *
import sys

def myViewer():
    # Initialize Coin. This returns a main window to use.
    # If unsuccessful, exit.
    myWindow = SoGui.init(sys.argv[0])
    if myWindow == None: sys.exit(1)

    # Make an empty scene and add our node to it
    scene = SoSeparator()
    scene.addChild(myNode)

    # Create a viewer in which to see our scene graph.
    viewer = SoGuiExaminerViewer(myWindow)

    # Put our scene into viewer, change the title
    viewer.setSceneGraph(scene)
    viewer.setTitle("FreeCAD Object Viewer")
    viewer.show()

    SoGui.show(myWindow) # Display main window
    SoGui.mainLoop()     # Main Coin event loop

Ensuite, il vous suffit de lancer votre visualiseur :

myViewer()

Utilisation d'un module tiers

Au lieu d'utiliser le visualiseur Sogui, vous pouvez aussi utiliser un module tiers plus moderne. C'est probablement la meilleure des 3 solutions.

    #!/usr/bin/env python
 
    ###
    # Copyright (c) 2002-2008 Kongsberg SIM
    #
    # Permission to use, copy, modify, and distribute this software for any
    # purpose with or without fee is hereby granted, provided that the above
    # copyright notice and this permission notice appear in all copies.
    #
    # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    #
 
    import os
    import sys
 
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication
 
    from pivy.coin import SoInput, SoDB
    from pivy.quarter import QuarterWidget
 
    import FreeCAD, FreeCADGui
 
    def getMainWindow():
       toplevel = QtGui.qApp.topLevelWidgets()
       for i in toplevel:
          if i.metaObject().className() == "Gui::MainWindow":
             return i
       raise Exception("No main window found")
 
    class MdiQuarterWidget(QuarterWidget):
        def __init__(self, parent, sharewidget):
            QuarterWidget.__init__(self, parent=parent, sharewidget=sharewidget)
 
        def loadFile(self, filename):
            in_ = SoInput()
            if (in_.openFile(str(filename.toLatin1()))):
                root = SoDB.readAll(in_)
            if (root):
                self.setSceneGraph(root)
                self.currentfile = filename
                self.setWindowTitle(filename)
                return True
            return False
 
        def currentFile(self):
            return self.currentfile
 
        def minimumSizeHint(self):
            return QtCore.QSize(640, 480)
 
    class MdiMainWindow(QMainWindow):
        def __init__(self, qApp):
            QMainWindow.__init__(self)
            self._firstwidget = None
            self._workspace = QWorkspace()
            self.setCentralWidget(self._workspace)
            self.setAcceptDrops(True)
            self.setWindowTitle("Pivy Quarter MDI example")
 
            filemenu = self.menuBar().addMenu("&File")
            windowmenu = self.menuBar().addMenu("&Windows")
 
            fileopenaction = QAction("&Create Box", self)
            fileexitaction = QAction("E&xit", self)
            tileaction = QAction("Tile", self)
            cascadeaction = QAction("Cascade", self)
 
            filemenu.addAction(fileopenaction)
            filemenu.addAction(fileexitaction)
            windowmenu.addAction(tileaction)
            windowmenu.addAction(cascadeaction)
 
            self.connect(fileopenaction, QtCore.SIGNAL("triggered()"), self.createBoxInFreeCAD)
            self.connect(fileexitaction, QtCore.SIGNAL("triggered()"), QtGui.qApp.closeAllWindows)
            self.connect(tileaction, QtCore.SIGNAL("triggered()"), self._workspace.tile)
            self.connect(cascadeaction, QtCore.SIGNAL("triggered()"), self._workspace.cascade)
 
            windowmapper = QtCore.QSignalMapper(self)
            self.connect(windowmapper, QtCore.SIGNAL("mapped(QWidget *)"), self._workspace.setActiveWindow)
 
            self.dirname = os.curdir       
 
        def dragEnterEvent(self, event):
            # just accept anything...
            event.acceptProposedAction()
 
        def dropEvent(self, event):
            mimedata = event.mimeData()
            if mimedata.hasUrls():
                path = mimedata.urls().takeFirst().path()
                self.open_path(path)
 
        def closeEvent(self, event):
            self._workspace.closeAllWindows()
 
        def open(self):
            self.open_path(QFileDialog.getOpenFileName(self, "", self.dirname))
 
        def open_path(self, filename):
            self.dirname = os.path.dirname(str(filename.toLatin1()))
            if not filename.isEmpty():
                existing = self.findMdiChild(filename)
                if existing:
                    self._workspace.setActiveWindow(existing)
                    return
            child = self.createMdiChild()
            if (child.loadFile(filename)):
                self.statusBar().showMessage("File loaded", 2000)
                child.show()
            else:
                child.close()
 
        def findMdiChild(self, filename):
            canonicalpath = QtCore.QFileInfo(filename).canonicalFilePath()
            for window in self._workspace.windowList():
                mdiwidget = window
                if mdiwidget.currentFile() == canonicalpath:
                    return mdiwidget
            return 0;
 
        def createMdiChild(self):
            widget = MdiQuarterWidget(None, self._firstwidget)
            self._workspace.addWindow(widget)
            if not self._firstwidget:
                self._firstwidget = widget
            return widget
 
        def createBoxInFreeCAD(self):
            widget = MdiQuarterWidget(None, self._firstwidget)
            self._workspace.addWindow(widget)
            if not self._firstwidget:
                self._firstwidget = widget
            widget.show()
            doc = FreeCAD.newDocument()
            doc.addObject("Part::Box","myBox")
            iv_=FreeCADGui.getDocument(doc.Name).getObject("myBox").toString()
            in_ = SoInput()
            in_.setBuffer(iv_)
            root = SoDB.readAll(in_)
            if (root):
                widget.setSceneGraph(root)
 
    def main():
        app = QApplication(sys.argv) 
        mdi = MdiMainWindow(app)   
        mdi.show()
        FreeCADGui.showMainWindow() # setup the GUI stuff of FreeCAD
        mw=getMainWindow()
        mw.hide() # hide all
        if len(sys.argv)==2:
            mdi.open_path(QtCore.QString(sys.argv[1]))
        sys.exit(app.exec_())
 
    def show():
        mdi = MdiMainWindow(QtGui.qApp)   
        mdi.show()
        mw=getMainWindow()
        #mw.hide() # hide all
 
    if __name__ == '__main__':
        main()

Sans exécuter le Gui de FreeCAD

A partir de FreeCAD rev2760, il est maintenant possible d'obtenir la représentation d'un objet quelconque coin FreeCAD sans devoir ouvrir la fenêtre principale.
Il est donc extrêmement facile de mettre en œuvre votre propre visualiseur, et, rester transparent pour les mises à jour de FreeCAD.
Après avoir importé le module FreeCADGui, vous devez le mettre à niveau avec la méthode setupWithoutGUI(), après quoi, vous pouvez utiliser tous les fournisseurs de vue de FreeCAD, pour obtenir les nœuds coin/openInventor.

    import os, sys, FreeCAD, FreeCADGui
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication
    from pivy.coin import SoInput, SoDB, sogui
 
    class MdiMainWindow(QMainWindow):
        def __init__(self, qApp):
            QMainWindow.__init__(self)
            self._firstwidget = None
            self._workspace = QWorkspace()
            self.setCentralWidget(self._workspace)
            self.setAcceptDrops(True)
            self.setWindowTitle("Pivy Quarter MDI example")
            self.viewers=[]
 
            filemenu = self.menuBar().addMenu("&File")
            windowmenu = self.menuBar().addMenu("&Windows")
 
            fileopenaction = QAction("&Create Box", self)
            fileexitaction = QAction("E&xit", self)
            tileaction = QAction("Tile", self)
            cascadeaction = QAction("Cascade", self)
 
            filemenu.addAction(fileopenaction)
            filemenu.addAction(fileexitaction)
            windowmenu.addAction(tileaction)
            windowmenu.addAction(cascadeaction)
 
            self.connect(fileopenaction, QtCore.SIGNAL("triggered()"), self.createBoxInFreeCAD)
            self.connect(fileexitaction, QtCore.SIGNAL("triggered()"), QtGui.qApp.closeAllWindows)
            self.connect(tileaction, QtCore.SIGNAL("triggered()"), self._workspace.tile)
            self.connect(cascadeaction, QtCore.SIGNAL("triggered()"), self._workspace.cascade)
 
            windowmapper = QtCore.QSignalMapper(self)
            self.connect(windowmapper, QtCore.SIGNAL("mapped(QWidget *)"), self._workspace.setActiveWindow)
 
        def closeEvent(self, event):
            self._workspace.closeAllWindows()
 
        def createBoxInFreeCAD(self):
            widget = QtGui.QWidget(self._firstwidget)
            viewer = sogui.SoGuiExaminerViewer(widget)
            self._workspace.addWindow(widget)
            if not self._firstwidget:
                self._firstwidget = widget
            widget.show()
            self.viewers.append(viewer)
            doc = FreeCAD.newDocument()
            obj=doc.addObject("Part::Box","myBox")
            doc.recompute()
            root=FreeCADGui.subgraphFromObject(obj)
            viewer.setSceneGraph(root)
 
    def main():
        app = QApplication(sys.argv)
        mdi = MdiMainWindow(app)   
        mdi.show()
        FreeCADGui.setupWithoutGUI()
        sys.exit(app.exec_())
 
    if __name__ == '__main__':
        main()

Ou, si vous utilisez le module pivy Sogui et qu'il ne fonctionne pas chez vous, (le module Sogui devient obsolète, et, les développeurs, désormais favorisent la bibliothèque coin, qui a une interaction bien meilleure avec qt), c'est le même scénario, mais avec une aide tierce :

    #!/usr/bin/env python
 
    import os
    import sys
 
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QApplication
 
    from pivy.coin import SoInput, SoDB
    from pivy.quarter import QuarterWidget
    import FreeCADGui
 
 
    class MdiQuarterWidget(QuarterWidget):
        def __init__(self, parent, sharewidget):
            QuarterWidget.__init__(self, parent=parent, sharewidget=sharewidget)
 
        def minimumSizeHint(self):
            return QtCore.QSize(640, 480)
 
 
    class MdiMainWindow(QMainWindow):
        def __init__(self, qApp):
            QMainWindow.__init__(self)
            self._firstwidget = None
            self._workspace = QWorkspace()
            self.setCentralWidget(self._workspace)
            self.setAcceptDrops(True)
            self.setWindowTitle("Pivy Quarter MDI example")
 
            filemenu = self.menuBar().addMenu("&File")
            windowmenu = self.menuBar().addMenu("&Windows")
 
            fileopenaction = QAction("&Create Box", self)
            fileexitaction = QAction("E&xit", self)
            tileaction = QAction("Tile", self)
            cascadeaction = QAction("Cascade", self)
 
            filemenu.addAction(fileopenaction)
            filemenu.addAction(fileexitaction)
            windowmenu.addAction(tileaction)
            windowmenu.addAction(cascadeaction)
 
            self.connect(fileopenaction, QtCore.SIGNAL("triggered()"), self.createBoxInFreeCAD)
            self.connect(fileexitaction, QtCore.SIGNAL("triggered()"), QtGui.qApp.closeAllWindows)
            self.connect(tileaction, QtCore.SIGNAL("triggered()"), self._workspace.tile)
            self.connect(cascadeaction, QtCore.SIGNAL("triggered()"), self._workspace.cascade)
 
            windowmapper = QtCore.QSignalMapper(self)
            self.connect(windowmapper, QtCore.SIGNAL("mapped(QWidget *)"), self._workspace.setActiveWindow)
 
            self.dirname = os.curdir       
 
        def closeEvent(self, event):
            self._workspace.closeAllWindows()
 
        def createBoxInFreeCAD(self):
            d=FreeCAD.newDocument()
            o=d.addObject("Part::Box")
            d.recompute()
            s=FreeCADGui.subgraphFromObject(o)
            child = self.createMdiChild()
            child.show()
            child.setSceneGraph(s)
 
        def createMdiChild(self):
            widget = MdiQuarterWidget(None, self._firstwidget)
            self._workspace.addWindow(widget)
            if not self._firstwidget:
                self._firstwidget = widget
            return widget
 
 
    def main():
        app = QApplication(sys.argv)
        FreeCADGui.setupWithoutGUI()        
        mdi = MdiMainWindow(app)   
        mdi.show()
        sys.exit(app.exec_())
 
 
    if __name__ == '__main__':
        main()