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 the workbench is first activated.
It is executed once in a FreeCAD session followed by the Activated function.
"""
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 whenever the workbench is activated"""
return
def Deactivated(self):
"""This function is executed whenever the workbench is deactivated"""
return
def ContextMenu(self, recipient):
"""This function 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
# This is not a template, the returned string should be exactly "Gui::PythonWorkbench"
return "Gui::PythonWorkbench"
Gui.addWorkbench(MyWorkbench())
How did it find the definition of the class 'Workbench'?
But probably you could not make much more than invoke it:
/** @class WorkbenchPy
* The workbench Python base class doesn't allow you to manipulate the C++ workbench class behind.
* You're only allowed either to activate the workbench class or get its name.
* The WorkbenchPy class is associated to all C++ workbench classes except of PythonWorkbench.
* @see Workbench
* @see PythonWorkbench
* @see PythonWorkbenchPy
* @author Werner Mayer
*/
Hope it helps.
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
xc22143 wrote: ↑Fri Oct 07, 2022 9:30 am
I tested. it is ok without the import code. So I want to know why not need import? May be the other py files have done this?
Most probably FC executes the py file in an environment where Workbench module is already imported.
Init.py or InitGui.py files are not imported (i.e. import Init) but the file content is executed with Python's exec() function. The class Workbench is defined in the built-in script FreeCADGuiInit.py: https://github.com/FreeCAD/FreeCAD/blob ... nit.py#L52