Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Add Python example of embedding FreeCAD
- Loading branch information
Showing
6 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys | ||
#sys.path.append("") | ||
|
||
from PySide import QtCore, QtGui | ||
import FreeCAD, FreeCADGui | ||
|
||
from ui_mainwindow import Ui_MainWindow | ||
|
||
class MainWindow(QtGui.QMainWindow): | ||
def __init__(self, parent = None): | ||
super(MainWindow, self).__init__(parent) | ||
from PySide import QtNetwork | ||
# Webkit is used to create icons from SVG files. This could cause a deadlock | ||
# when setting up the internally used network interface. Doing this before | ||
# creating the icons fixes the issue. | ||
QtNetwork.QNetworkConfigurationManager() | ||
|
||
def showEvent(self, event): | ||
FreeCADGui.showMainWindow() | ||
self.setCentralWidget(FreeCADGui.getMainWindow()) | ||
# Need version >= 0.16.5949 | ||
class BlankWorkbench(FreeCADGui.Workbench): | ||
MenuText = "Blank" | ||
ToolTip = "Blank workbench" | ||
def Initialize(self): | ||
return | ||
def GetClassName(self): | ||
return "Gui::BlankWorkbench" | ||
FreeCADGui.addWorkbench(BlankWorkbench) | ||
FreeCADGui.activateWorkbench("BlankWorkbench") | ||
|
||
@QtCore.Slot() | ||
def on_actionEmbed_triggered(self): | ||
return | ||
|
||
@QtCore.Slot() | ||
def on_actionDocument_triggered(self): | ||
FreeCAD.newDocument() | ||
|
||
@QtCore.Slot() | ||
def on_actionCube_triggered(self): | ||
FreeCAD.ActiveDocument.addObject("Part::Box") | ||
FreeCAD.ActiveDocument.recompute() | ||
FreeCADGui.ActiveDocument.ActiveView.fitAll() | ||
|
||
app=QtGui.QApplication(sys.argv) | ||
ui=Ui_MainWindow() | ||
mw=MainWindow() | ||
ui.setupUi(mw) | ||
ui.actionEmbed.setVisible(False) | ||
mw.resize(1200,800) | ||
mw.show() | ||
app.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>800</width> | ||
<height>600</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QWidget" name="centralwidget"/> | ||
<widget class="QMenuBar" name="menubar"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>800</width> | ||
<height>25</height> | ||
</rect> | ||
</property> | ||
<widget class="QMenu" name="menuFreeCAD"> | ||
<property name="title"> | ||
<string>FreeCAD</string> | ||
</property> | ||
<addaction name="actionEmbed"/> | ||
<addaction name="actionDocument"/> | ||
<addaction name="actionCube"/> | ||
</widget> | ||
<addaction name="menuFreeCAD"/> | ||
</widget> | ||
<widget class="QStatusBar" name="statusbar"/> | ||
<action name="actionEmbed"> | ||
<property name="text"> | ||
<string>Embed</string> | ||
</property> | ||
</action> | ||
<action name="actionDocument"> | ||
<property name="text"> | ||
<string>Document</string> | ||
</property> | ||
</action> | ||
<action name="actionCube"> | ||
<property name="text"> | ||
<string>Cube</string> | ||
</property> | ||
</action> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
#sys.path.append("") | ||
|
||
from PySide import QtCore, QtGui | ||
import FreeCADGui | ||
|
||
class MainWindow(QtGui.QMainWindow): | ||
def __init__(self, parent = None): | ||
super(MainWindow, self).__init__(parent) | ||
from PySide import QtNetwork | ||
# Webkit is used to create icons from SVG files. This could cause a deadlock | ||
# when setting up the internally used network interface. Doing this before | ||
# creating the icons fixes the issue. | ||
QtNetwork.QNetworkConfigurationManager() | ||
|
||
def showEvent(self, event): | ||
FreeCADGui.showMainWindow() | ||
self.setCentralWidget(FreeCADGui.getMainWindow()) | ||
# Need version >= 0.16.5949 | ||
class BlankWorkbench(FreeCADGui.Workbench): | ||
MenuText = "Blank" | ||
ToolTip = "Blank workbench" | ||
def Initialize(self): | ||
self.appendMenu("Menu",["Std_New", "Part_Box"]) | ||
return | ||
def GetClassName(self): | ||
return "Gui::PythonBlankWorkbench" | ||
FreeCADGui.addWorkbench(BlankWorkbench) | ||
FreeCADGui.activateWorkbench("BlankWorkbench") | ||
|
||
app=QtGui.QApplication(sys.argv) | ||
mw=MainWindow() | ||
mw.resize(1200,800) | ||
mw.show() | ||
app.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys | ||
#sys.path.append("") | ||
|
||
from PySide import QtCore, QtGui | ||
import FreeCAD, FreeCADGui | ||
import ctypes | ||
|
||
from ui_mainwindow import Ui_MainWindow | ||
|
||
class MainWindow(QtGui.QMainWindow): | ||
def __init__(self, parent = None): | ||
super(MainWindow, self).__init__(parent) | ||
from PySide import QtNetwork | ||
# Webkit is used to create icons from SVG files. This could cause a deadlock | ||
# when setting up the internally used network interface. Doing this before | ||
# creating the icons fixes the issue. | ||
QtNetwork.QNetworkConfigurationManager() | ||
|
||
@QtCore.Slot() | ||
def on_actionEmbed_triggered(self): | ||
FreeCADGui.showMainWindow() | ||
hwnd=self.winId() | ||
PyCObject_AsVoidPtr = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)(('PyCObject_AsVoidPtr', ctypes.pythonapi)) | ||
addr = PyCObject_AsVoidPtr(hwnd) | ||
FreeCADGui.embedToWindow(hex(addr)) | ||
# Need version >= 0.16.5949 | ||
class BlankWorkbench(FreeCADGui.Workbench): | ||
MenuText = "Blank" | ||
ToolTip = "Blank workbench" | ||
def Initialize(self): | ||
return | ||
def GetClassName(self): | ||
return "Gui::BlankWorkbench" | ||
FreeCADGui.addWorkbench(BlankWorkbench) | ||
FreeCADGui.activateWorkbench("BlankWorkbench") | ||
|
||
@QtCore.Slot() | ||
def on_actionDocument_triggered(self): | ||
FreeCAD.newDocument() | ||
|
||
@QtCore.Slot() | ||
def on_actionCube_triggered(self): | ||
FreeCAD.ActiveDocument.addObject("Part::Box") | ||
FreeCAD.ActiveDocument.recompute() | ||
FreeCADGui.ActiveDocument.ActiveView.fitAll() | ||
|
||
app=QtGui.QApplication(sys.argv) | ||
ui=Ui_MainWindow() | ||
mw=MainWindow() | ||
ui.setupUi(mw) | ||
mw.resize(1200,800) | ||
mw.show() | ||
app.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Form implementation generated from reading ui file 'mainwindow.ui' | ||
# | ||
# Created: Fri Nov 20 18:03:04 2015 | ||
# by: pyside-uic 0.2.13 running on PySide 1.1.0 | ||
# | ||
# 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(800, 600) | ||
self.centralwidget = QtGui.QWidget(MainWindow) | ||
self.centralwidget.setObjectName("centralwidget") | ||
MainWindow.setCentralWidget(self.centralwidget) | ||
self.menubar = QtGui.QMenuBar(MainWindow) | ||
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) | ||
self.menubar.setObjectName("menubar") | ||
self.menuFreeCAD = QtGui.QMenu(self.menubar) | ||
self.menuFreeCAD.setObjectName("menuFreeCAD") | ||
MainWindow.setMenuBar(self.menubar) | ||
self.statusbar = QtGui.QStatusBar(MainWindow) | ||
self.statusbar.setObjectName("statusbar") | ||
MainWindow.setStatusBar(self.statusbar) | ||
self.actionEmbed = QtGui.QAction(MainWindow) | ||
self.actionEmbed.setObjectName("actionEmbed") | ||
self.actionDocument = QtGui.QAction(MainWindow) | ||
self.actionDocument.setObjectName("actionDocument") | ||
self.actionCube = QtGui.QAction(MainWindow) | ||
self.actionCube.setObjectName("actionCube") | ||
self.menuFreeCAD.addAction(self.actionEmbed) | ||
self.menuFreeCAD.addAction(self.actionDocument) | ||
self.menuFreeCAD.addAction(self.actionCube) | ||
self.menubar.addAction(self.menuFreeCAD.menuAction()) | ||
|
||
self.retranslateUi(MainWindow) | ||
QtCore.QMetaObject.connectSlotsByName(MainWindow) | ||
|
||
def retranslateUi(self, MainWindow): | ||
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) | ||
self.menuFreeCAD.setTitle(QtGui.QApplication.translate("MainWindow", "FreeCAD", None, QtGui.QApplication.UnicodeUTF8)) | ||
self.actionEmbed.setText(QtGui.QApplication.translate("MainWindow", "Embed", None, QtGui.QApplication.UnicodeUTF8)) | ||
self.actionDocument.setText(QtGui.QApplication.translate("MainWindow", "Document", None, QtGui.QApplication.UnicodeUTF8)) | ||
self.actionCube.setText(QtGui.QApplication.translate("MainWindow", "Cube", None, QtGui.QApplication.UnicodeUTF8)) | ||
|