Add a new command to an existing workbench

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
antonio.bacciaglia
Posts: 5
Joined: Wed Oct 24, 2018 3:23 pm

Add a new command to an existing workbench

Post by antonio.bacciaglia »

Hi everyone,
I have FC 0.17. I would like to add a new command to the exiting TechDraw workbench. How I can do it? I have tried to modify the InitGui.py of that workbench.
This is the code:

Code: Select all

class TechDrawWorkbench (Workbench):
    "Technical Drawing workbench object"
    def __init__(self):
        self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/TechDraw/Resources/icons/preferences-techdraw.svg"
        self.__class__.MenuText = "TechDraw"
        self.__class__.ToolTip = "Technical Drawing workbench"
	
	def Initialize(self):
        #import SandwichGUI, Lattice1 # import here all the needed files that create your FreeCAD commands
        self.list = ["LatticeCommand"]
		self.appendToolbar("Lattice",self.list) # creates a new toolbar with your commands	
		self.appendMenu("Lattice",self.list)
	
	def Activated(self):
         return

    def Deactivated(self):
         return

    def ContextMenu(self, recipient):
        self.appendContextMenu("Lattice",self.list) # add commands to the context menu
		
    def Initialize(self):
        # load the module
        import TechDrawGui
    def GetClassName(self):
        return "TechDrawGui::Workbench"

Gui.addWorkbench(TechDrawWorkbench())

class LatticeCommand_Class():

    def GetResources(self):
        return {'Pixmap' : 'C:\Program Files\FreeCAD 0.17\Mod\TechDraw\Lat_icon.bmp',
                'Accel': "",
                'MenuText': "Lattice",
                'ToolTip': "Quote lattice structure"}

    def Activated(self):
        import Lattice_QuoteGUI
        while True:
             reload(Lattice_QuoteGUI)
             return


FreeCADGui.addCommand('LatticeCommand',LatticeCommand_Class())

But in this way it doesn't work and as Freecad starts, the Techdraw workbench doesn't appear in the workbenches list. Thank you in advance for your support!
User avatar
HarryGeier
Veteran
Posts: 1231
Joined: Mon Jul 10, 2017 12:36 pm
Location: Hof Germany

Re: Add a new command to an existing workbench

Post by HarryGeier »

You can add your OWN toolbar to any workbench.
There you can add icons from other workbenches as well as for you own macros

You can acces that from here:
2019-03-21 12_10_35-FreeCAD 0.18.png
2019-03-21 12_10_35-FreeCAD 0.18.png (79.75 KiB) Viewed 498 times

If you want to create your own workbench, you have to dig deeper into the development areas...
Kaum macht man´s richtig , gehts´s
My Video Tutorials on Youtube: https://www.youtube.com/channel/UCoe3B ... p8Q/videos
My FreeCAD Stuff on Hidrive: https://my.hidrive.com/share/qr3l1yddy6#$/
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Add a new command to an existing workbench

Post by DeepSOIC »

You can inject a toolbar into any workbench with a script, using the same mechanism user-defined toolbars are remembered.
Lattice2 add-on for example injects a toolbar into PartDesign workbench:
https://github.com/DeepSOIC/Lattice2/bl ... oolbars.py
(registerPDToolbar is called from InitGui.py)
Post Reply