Create a FreeCAD Workbench – InitGui.py

This class gets run when you workbench gets loaded. It defines which commands your workbench contains, it’s toolbar and all the names.

Class Level Variables

There are three class level variables that the Workbench Class calls in order to “build” your workbench.

  • Icon
  • MenuText
  • ToolTIp

The Icon variable contains a string that defines an XPM image file. This icon is used in the Workbench Menu selector.

The MenuText variable is a string title likely containing the name of the workbench

The ToolTip variable should contain a short description of the purpose for the workbench.

The basic methods in the InitGui class are

  • GetClassName
  • Initialize
  • Activated
  • Deactivated

Initialize

This is where most of the magic happens. The initialize class loads all the supporting classes that will be used in the workbench.

The classes are imported using the filename, not the classname.

Classes that represent commands can be added to the Toolbar and Menu using the two methods of the workbench class

  • appendToolbar
  • appendMenu.

Both of these methods take as an argument a Tuple containing a String and a List. The string will define the name of the toolbar and the lsit will include the imported classes ( containing the workbench commands )

Sample Code:

This is a vary basic sample Workbench, hopefully clear enough to modified.

class SampleWorkbench ( Workbench ):
	"Sample workbench object"
	Icon = """
			/* XPM */
			static const char *test_icon[]={
			"16 16 2 1",
			"a c #000000",
			". c None",
			".................",
			"...############..",
			"...############..",
			"...##............",
			"...##............",
			"...##............",
			"...##............",
			"...############..",
			"...############..",
			".............##..",
			".............##..",
			".............##..",
			".............##..",
			".............##..",
			"...############..",
			"...############..",
			"................."};
			"""
	MenuText = "Sample Workbench"
	ToolTip = "A workbench for educational purposes,ie., how to make a workbench"
	print ("The Sample workbench class has been instantiated\n")

	def GetClassName(self):
		return "Gui::PythonWorkbench"
	
	def Initialize(self):
		import sample, sample2
		self.appendToolbar("Sample",["Sample","Sample2"])
		self.appendMenu("Sample", ["Sample","Sample2"])
		Log ("The Sample Module Initialize method has been run \n")

	def Activated(self):
		# do something here if needed...
		Msg ("SampleWorkbench.Activated()\n")

	def Deactivated(self):
		# do something here if needed...
		Msg ("SampleWorkbench.Deactivated()\n")

FreeCADGui.addWorkbench(SampleWorkbench)

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: