Create a FreeCAD Workbench – Command Class

The Command Class is intended to be setup and called by the of your workbench make sure you have created this class first.

The “Command” Class

When called the workbench class and FreeCADGUI uses the expected methods and variables in order to interact with your custom command. The command class will be named like: 

class Sample_Command:

This is arbitrary as the command is added using:

FreeCADGui.addCommand('Command Name String', CommandClass()) 

This method can be called using any class name. However, it makes your code a great deal easier to understand if we all abide by this convention.


GetResources – The GetResources method of the command class is called by the Workbench class to add the form and functionality of the command to the workbench.

There are four components we use to setup the command. They are:

  • image_path
  • MenuText
  • TootlTip
  • PixMap

image_path – will be the icon shown on the toolbar.

MenuText – is a string value thet will be displayed in the FreeCAD menu under the the new workbench.

ToolTip – is a string value that will be displayed when the user hovers over the Toolbbar icon.

PixMap – is a XMP string that will add an image next to the sub-menu item.


Can be called to determine if the command is currently running.


The Activated method is executed when the user either selects the command from the menu or clicks the Toolbar icon. Code required to complete the new command begins execution here. Any python class can reasonably be called here.

Initially adding the command:

When the workbench is first loaded ( selected from the menu ) all the commands are added. Each class instantiates and adds itself to the Gui. This could be consolidated to a central location of course, but adds the possibility of a missing or removed command generating errors.

The following code adds the command to the tool palette.

FreeCADGui.addCommand('Sample', Sample_Command())

Let’s Build our Command Class

This sample command adds the SampleObject, a Property category and a Property. It adds an object from the Part library. It does not add a child Shape object which contains the geometry ( although it could ).

The code below contains the Sample_Command class.

class Sample_Command:

	def GetResources(self):
		print ("Run getResources() for sample_command")
		image_path = '/sample/icons/sample.png'
		global_path = FreeCAD.getHomePath()+"Mod"
		user_path = FreeCAD.getUserAppDataDir()+"Mod"
		icon_path = ""
		if os.path.exists(user_path + image_path):
			icon_path = user_path + image_path
		elif os.path.exists(global_path + image_path):
			icon_path = global_path + image_path
		return {"MenuText": "Sample Menu",
			"ToolTip": "Tooltip for sample command",
			'Pixmap' : str(icon_path) } 

	def IsActive(self):
		if FreeCAD.ActiveDocument == None:
			#print "Sample command is NOT active"
			return False
			#print "Sample command IS active"
			return True

	def Activated(self):

		print ("Sample command activated")

		newsampleobject = Sample(b)

Leave a Reply