some questions about InitGui.py

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
xc22143
Posts: 139
Joined: Fri Dec 03, 2021 9:52 am

some questions about InitGui.py

Post by xc22143 »

Hello, everyone. I am sorry that I am a novice. I have some 'stupid' questions about the InitGui.py which is shown on the wiki pagehttps://wiki.freecadweb.org/Workbench_creation.

Code: Select all

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'?
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: some questions about InitGui.py

Post by onekk »

Probably somewhere here:

https://github.com/FreeCAD/FreeCAD/tree/master/src/Gui


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.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: some questions about InitGui.py

Post by openBrain »

xc22143 wrote: Fri Oct 07, 2022 8:27 am How did it find the definition of the class 'Workbench'?
That's indeed a bit strange as is.
To be sure imports are done, either you should add before :

Code: Select all

from FreeCADGui import Workbench
Or change the class definition to :

Code: Select all

class MyWorkbench (FreeCADGui.Workbench):
xc22143
Posts: 139
Joined: Fri Dec 03, 2021 9:52 am

Re: some questions about InitGui.py

Post by xc22143 »

openBrain wrote: Fri Oct 07, 2022 9:20 am
xc22143 wrote: Fri Oct 07, 2022 8:27 am How did it find the definition of the class 'Workbench'?
That's indeed a bit strange as is.
To be sure imports are done, either you should add before :

Code: Select all

from FreeCADGui import Workbench
Or change the class definition to :

Code: Select all

class MyWorkbench (FreeCADGui.Workbench):
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?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: some questions about InitGui.py

Post by openBrain »

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.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: some questions about InitGui.py

Post by wmayer »

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
Post Reply