Erstellung von Arbeitsbereichen
Diese Seite zeigt dir, wie du einen neuen Arbeitbereich zur FreeCAD Oberfläche hinzufügst. Arbeitbereiche sind Behälter für FreeCAD Befehle. Sie können in Python, in C++ oder in einer Mischung aus beidem programmiert werden, was den Vorteil hat, die Geschwindigkeit von C++ mit der Flexibilität von Python zu verbinden. In allen Fällen wird dein Arbeitsbereich jedoch durch einen Satz von zwei Pythondateien gestartet.
Die Arbeitsbereichsstruktur
Im Grunde ist es einfach: Du benötigst einen Ordner mit einem beliebigen Namen im Mod Verzeichnis, mit einer Init.py Datei und, optional, einer InitGui.py Datei. Die Init Datei wird immer beim Start von FreeCAD ausgeführt, und die InitGui.py Datei wird unmittelbar danach ausgeführt, aber nur, wenn FreeCAD im GUI Modus und nicht im Konsolenmodus gestartet wird. Das ist alles, was FreeCAD braucht, um deinen Arbeitsbereich beim Start zu finden und in seine Oberfläche einzufügen.
Innerhalb dieser Dateien kannst du tun, was immer du willst. Normalerweise werden sie so verwendet:
- In der Datei Init.py fügst du einfach ein paar Dinge hinzu, die auch dann verwendet werden, wenn FreeCAD im Konsolenmodus arbeitet, z.B. die Datei Importeure und -Exporteure
- In der Datei InitGui.py definierst du normalerweise einen Arbeitsbereich, der einen Namen, ein Symbol und eine Reihe von FreeCAD Befehlen enthält (siehe unten). Dieser Arbeitsbereich definiert auch Funktionen, die ausgeführt werden, wenn FreeCAD geladen wird (Du versuchst, dort so wenig wie möglich zu tun, um den Start nicht zu verlangsamen), eine weitere, die ausgeführt wird, wenn der Arbeitsbereich aktiviert ist (dort wirst Du die meiste Arbeit erledigen), und eine dritte, wenn der Arbeitsbereich deaktiviert ist (damit Du Dinge, wenn nötig, entfernen kannst).
C++ Arbeitsbereichsstruktur
Wenn du deinen Arbeitsbereich in Python programmieren willst, brauchst du keine besondere Vorsicht walten zu lassen und kannst einfach deine anderen Python Dateien zusammen mit deinen Init.py und InitGui.py Dateien unterbringen. Wenn du jedoch mit C++ arbeitest, solltest du größere Sorgfalt walten lassen und damit beginnen, eine grundlegende Regel von FreeCAD zu beachten: Die Trennung deines Arbeitsbereichs zwischen einem Anwendungsteil (der im Konsolenmodus laufen kann, ohne jedes GUI Element) und einem Gui Teil, der nur geladen wird, wenn FreeCAD mit seiner vollständigen GUI Umgebung läuft. Wenn du also einen C++ Arbeitsbereich verwendest, wirst du höchstwahrscheinlich zwei Module verwenden, eine Anwendung und eine Gui. Diese beiden Module müssen natürlich von Python aus aufrufbar sein. Jedes FreeCAD Modul (Anwendung oder Gui) besteht mindestens aus einer Modul Init Datei. Dies ist eine typische AppMyModuleGui.cpp Datei:
extern "C" {
void MyModuleGuiExport initMyModuleGui()
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
}
try {
// import other modules this one depends on
Base::Interpreter().runString("import PartGui");
// run some python code in the console
Base::Interpreter().runString("print('welcome to my module!')");
}
catch(const Base::Exception& e) {
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
(void) Py_InitModule("MyModuleGui", MyModuleGui_Import_methods); /* mod name, table ptr */
Base::Console().Log("Loading GUI of MyModule... done\n");
// initializes the FreeCAD commands (in another cpp file)
CreateMyModuleCommands();
// initializes workbench and object definitions
MyModuleGui::Workbench::init();
MyModuleGui::ViewProviderSomeCustomObject::init();
// add resources and reloads the translators
loadMyModuleResource();
}
}
Die Init.py Datei
"""FreeCAD init script of XXX module"""
# ***************************************************************************
# * Copyright (c) 2015 John Doe john@doe.com *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with FreeCAD; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************/
FreeCAD.addImportType("My own format (*.own)", "importOwn")
FreeCAD.addExportType("My own format (*.own)", "importOwn")
print("I am executing some stuff here when FreeCAD starts!")
Du kannst jede beliebige Lizenz für deinen Arbeitsbereich wählen, aber sei dir bewusst, dass, wenn du deinen Arbeitsbereich irgendwann in den FreeCAD Quellcode integrierst und mit diesem verteilt sehen möchtest, diese wie im obigen Beispiel unter LGPL2+ stehen muss. Siehe Lizenz.
The FreeCAD.addImportType()
and addEXportType()
functions allow you to give the name and extension of a file type, and a python module responsible for its import. In the example above, an importOwn.py
module will handle .own
files. See Code snippets for more examples.
Python Arbeitsbereiche
class MyWorkbench (Workbench):
MenuText = "My Workbench"
ToolTip = "A description of my workbench"
Icon = """paste here the contents of a 16x16 xpm icon"""
def Initialize(self):
"""This function is executed when FreeCAD starts"""
import MyModuleA, MyModuleB # import here all the needed files that create your FreeCAD commands
self.list = ["MyCommand1, MyCommand2"] # A list of command names created in the line above
self.appendToolbar("My Commands",self.list) # creates a new toolbar with your commands
self.appendMenu("My New Menu",self.list) # creates a new menu
self.appendMenu(["An existing Menu","My submenu"],self.list) # appends a submenu to an existing menu
def Activated(self):
"""This function is executed when the workbench is activated"""
return
def Deactivated(self):
"""This function is executed when the workbench is deactivated"""
return
def ContextMenu(self, recipient):
"""This is executed whenever the user right-clicks on screen"""
# "recipient" will be either "view" or "tree"
self.appendContextMenu("My commands",self.list) # add commands to the context menu
def GetClassName(self):
# this function is mandatory if this is a full python workbench
return "Gui::PythonWorkbench"
Gui.addWorkbench(MyWorkbench())
Im Gegensatz hierzu können Sie hier tun, was Sie möchten: Sie könnten Ihren gesamten Arbeitsbereich Code in der InitGui.py platzieren, wenn Sie möchten. Allerdings ist es üblicher und angenehmer, die verschiedenen Funktionen Ihres Arbeitsbereichs in getrennten Dateien zu speichern. Auf diese Weise werden die Dateien kleiner und leichter zu lesen. Danach importieren Sie diese Dateien innerhalb Ihrer InitGui.py Datei. Sie können jene Dateien organisieren ganz wie Sie möchten. Ein gutes Beispiel ist eine Datei für jeden hinzugefügten FreeCAD Befehl.
Preferences
Your can add a Preferences page for your Python workbench. The Preferences pages look for a preference icon with a specific name in the Qt Resource system. If your icon isn't in the resource system or doesn't have the correct name, your icon won't appear on the Preferences page.
Adding your workbench icon:
- the preferences icon needs to be named "preferences-" + "modulename" + ".svg" (all lowercase)
- make a qrc file containing all icon names
- in the main *.py directory, run pyside-rcc -o myResources.py myqrc.qrc
- in InitGui.py, add import myResource(.py)
- update your repository(git) with myResources.py and myqrc.qrc
You'll need to redo the steps if you add/change icons.
@kbwbe has created a nice script to compile resources for the A2Plus workbench. See below.
Adding your preference page(s):
- You need to compile the Qt designer plugin that allows you to add preference settings with Qt Designer
- Create a blank widget in Qt Designer (no buttons or anything)
- Design your preference page, any setting that must be saved (preferences) must be one of the Gui::Pref* widgets that were added by the plugin)
- In any of those, make sure you fill the PrefName (the name of your preference value) and PrefPath (ex: Mod/MyWorkbenchName), which will save your value under BaseApp/Preferences/Mod/MyWorkbebchName
- Save the ui file in your workbench, make sure it's handled by cmake
- In your workbench, for ex. inside the InitGui file, inside the Initialize method (but any other place works too), add: FreeCADGui.addPreferencePage("/path/to/myUiFile.ui","MyGroup"), "MyGroup" being one of the preferences groups on the left. FreeCAD will automatically look for a "preferences-mygroup.svg" file in its known locations (which you can extend with FreeCADGui.addIconPath())
- Make sure the addPreferencePage() method is called only once, otherwise your pref page will be added several times
C++ Arbeitsbereiche
Wenn Sie Ihren Arbeitsbereich in C++ programmieren, werden Sie vermutlich die Arbeitsbereichsdefinition selbst ebenfalls in C++ erstellen (,was jedoch nicht nötig ist: Sie auch die nur die Werzeuge in C++ programmieren und die Arbeitsbereichsdefinition in Python belassen). In diesem Fall wird die InitGui.py Datei sehr einfach. Sie könnte etwa nur eine Zeile enthalten:
import MyModuleGui
wobei MyModule Ihr kompletter C++ Arbeitsbereich ist, der die Befehle und Arbeitsbereichsdefinition einschließt.
Die Programmierung von C++ Arbeitsbereichen funktioniert auf sehr ähnlich Art und Weise. Dies eine typische Workbench.cpp Datei, die in den GUI Teil Ihres Moduls eingebunden werden kann.
namespace MyModuleGui {
class MyModuleGuiExport Workbench : public Gui::StdWorkbench
{
TYPESYSTEM_HEADER();
public:
Workbench();
virtual ~Workbench();
virtual void activated();
virtual void deactivated();
protected:
Gui::ToolBarItem* setupToolBars() const;
Gui::MenuItem* setupMenuBar() const;
};
}
Preferences
Your can add a Preferences page for C++ workbenches too. The steps are similar to those for Python.
FreeCAD Befehle
FreeCAD Befehle sind die Grundbausteine des FreeCAD Interface. Sie können als Knöpfe in Werkzeugleisten und als Einträge in Menüs erscheinen. Es handelt sich dabei immer um den selben Befehl. Ein Befehl ist einfach eine Python Klasse, die eine Reihe von vordefinierten Attributen und Funktionen enthält, wie der Befehlsname, das Symbol und der Code, der ausgeführt wird, wenn der Befehl aktiviert wird.
Python Befehlsdefinition
class My_Command_Class():
"""My new command"""
def GetResources(self):
return {'Pixmap' : 'My_Command_Icon', # the name of a svg file available in the resources
'Accel' : "Shift+S", # a default shortcut (optional)
'MenuText': "My New Command"
'ToolTip' : "What my new command does"}
def Activated(self):
"""Do something here"""
return
def IsActive(self):
"""Here you can define if the command must be active or not (greyed) if certain conditions
are met or not. This function is optional."""
return True
FreeCADGui.addCommand('My_Command',My_Command_Class())
C++ Befehlsdefinition
In ähnlicher Art können Sie Befehle in C++ programmieren und haben dazu typischerweise eine Commands.cpp Datei in Ihrem GUI Modul. Dies ist eine typische Commands.cpp Datei.
DEF_STD_CMD_A(CmdMyCommand);
CmdMyCommand::CmdMyCommand()
:Command("My_Command")
{
sAppModule = "MyModule";
sGroup = QT_TR_NOOP("MyModule");
sMenuText = QT_TR_NOOP("Runs my command...");
sToolTipText = QT_TR_NOOP("Describes what my command does");
sWhatsThis = QT_TR_NOOP("Describes what my command does");
sStatusTip = QT_TR_NOOP("Describes what my command does");
sPixmap = "some_svg_icon_from_my_resource";
}
void CmdMyCommand::activated(int iMsg)
{
openCommand("My Command");
doCommand(Doc,"print('Hello, world!')");
commitCommand();
updateActive();
}
bool CmdMyCommand::isActive(void)
{
if( getActiveGuiDocument() )
return true;
else
return false;
}
void CreateMyModuleCommands(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdMyCommand());
}
"Compiling" your resource file
compileA2pResources.py from the A2Plus workbench:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#***************************************************************************
#* *
#* Copyright (c) 2019 kbwbe *
#* *
#* Portions of code based on hamish's assembly 2 *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
# This script compiles the A2plus icons for py2 and py3
# For Linux only
# Start this file in A2plus main directory
# Make sure pyside-rcc is installed
import os, glob
qrc_filename = 'temp.qrc'
if os.path.exists(qrc_filename):
os.remove(qrc_filename)
qrc = '''<RCC>
\t<qresource prefix="/">'''
for fn in glob.glob('./icons/*.svg'):
qrc = qrc + '\n\t\t<file>%s</file>' % fn
qrc = qrc + '''\n\t</qresource>
</RCC>'''
print(qrc)
f = open(qrc_filename,'w')
f.write(qrc)
f.close()
os.system(
'pyside-rcc -o a2p_Resources2.py {}'.format(qrc_filename))
os.system(
'pyside-rcc -py3 -o a2p_Resources3.py {}'.format(qrc_filename))
os.remove(qrc_filename)
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub