Erstellung von Arbeitsbereichen

From FreeCAD Documentation
Revision as of 20:09, 19 November 2019 by FuzzyBot (talk | contribs) (Updating to match new version of source page)

Diese Seite zeigt, wie FreeCAD neue Arbeitsbereiche hinzugefügt werden. Arbeitsbereiche sind Container für FreeCAD Befehle. Sie können in Python, C++ oder in einer Mischung aus beiden programmiert werden. Letzteres hat den Vorteil, die Geschwindigkeit von C++ mit der Flexibilität von Python zu verbinden. In allen Fällen werden Arbeitsbereiche durch einen Satz von zwei Pythondateien gestartet.

Struktur eines Arbeitsbereichs

Das Vorgehen ist einfach: Sie brauchen einen Ordner mit einem Namen Ihrer Wahl, der im Mod Verzeichnis angelegt wird, und eine Init.py Datei sowie optional eine InitGui.py Datei. Die Init Datei wird immer ausgeführt, wenn FreeCAD startet gefolgt von der InitGui Datei, die nur ausgeführt wird, wenn sich FreeCAD im GUI Modus (nicht im Konsolenmodus) befindet. Dies ist alles, was nötig ist, damit FreeCAD Ihren Arbeitsbereich beim Start findet und ihn zum Interface hinzufügt.

Innerhalb dieser Dateien können Sie beliebige Dinge machen. Üblicherweise werden sie wie folgt verwendet:

  • In der Init.py Datei fügen Sie in der Regel verschiedene Dinge hinzu, die benutzt werden, auch wenn FreeCAD im Konsolenmodus arbeitet, z.B. Dateiimport- und Exportfilter.
  • In der InitGUI.py Datei definieren Sie üblicherweise einen Arbeitsbereich, der einen Namen, ein Symbol und eine Reihe von FreeCAD Befehlen (siehe unten) enthält. Der Arbeitsbereich definiert außerdem Funktionen, die ausgeführt werden, wenn FreeCAD geladen wird (hier sollte so wenig wie möglich getan werden, um das Starten nicht zu verlangsamen), der Arbeitsbereich aktiviert (hier wird die meiste Arbeit geleistet) sowie deaktiviert wird (hier können Dinge, wenn nötig, entfernt werden).

Struktur eines C++ Arbeitsbereichs

Wenn Sie einen Arbeitsbereich in Python programmieren, sind keine weiteren Dinge zu beachten und weitere Pythondateien können zusammen mit Init.py sowie InitGui.py gespeichert werden. Wenn Sie jedoch mit C++ arbeiten, sollten Sie besonders eine fundamentale Regel von FreeCAD beachten: Die Trennung des Arbeitsbereichs in einen Anwendungsteil (,der im Konsolenmodus ohne GUI Elemente laufen kann) und einen GUI Teil, der nur geladen wird, wenn FreeCAD in voller GUI Umgebung geladen wird. Wenn Sie also einen C++ Arbeitsbereich erstellen, werden Sie tatsächlich zwei Module erstellen (Applikation und GUI). Diese beiden Module müssen natürlich von Python aus aufrufbar sein. Jedes FreeCAD Modul (Applikation oder GUI) besteht zumindest 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!")

Sie können ein Lizenzmodell Ihrer Wahl für Ihren Arbeitsbereich wählen. Sie sollte sich jedoch darüber bewusst sein, dass ein Arbeitsbereich, der zu einem gewissen Zeitpunkt in FreeCAD integriert und mit dem Quellcode veröffentlicht werden soll, der LPGL2+ Lizenz unterliegen muss. Die Funktionen FreeCAD.addImportType() und addEXportType() ermöglichen die Vergabe eines Namens und einer Dateiendung sowie ein Pythonmodul, das für diesen Import zuständig ist. Im obigen Beispiel ist importOwn.py für den Umgang mit .own Dateien verantwortlich. Schauen Sie unter Codeschnipsel nach weiteren Beispielen.

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)