Command Classes¶
Motivation
Encapsulate action users can perform when interacting with FreeCAD’s UI.
Command Classes are executed when users perform various actions in the workbench such as clicking a button in a toolbar or selecting an option in a menu.
OSE Workbench Command Classes are an opinionated extension to FreeCAD Command Classes with the following observed conventions:
Names sound like actions, typically begin with verbs, and always end with a “command” suffix
For example, a command class to add a frame to the document might be named
AddFrameCommand
The command should be located in a module named after the command (e.g.
add_frame_command.py
)
Have a static
NAME
string constantTypically the same name as the command (e.g.
'AddFrameCommand'
)
Important
NAME
must be unique for all commands within the scope of the workbench
Located and exposed by the
command
sub-package of the workbench package.
freecad/ose3dprinter/command
├── _add_frame/
│ ├── add_frame_command.py
│ └── __init__.py
└── __init__.py
Within _add_frame/__init__.py
:
from .add_frame_command import AddFrameCommand
__all__ = ['AddFrameCommand']
Within freecad/ose3dprinter/command/__init__.py
:
"""Commands users can perform in FreeCAD's GUI."""
from ._add_frame import AddFrameCommand
__all__ = ['AddFrameCommand',]
The following is a complete example taken from the ose-3d-printer-workbench:
import FreeCAD as App
from freecad.ose3dprinter.icon import get_icon_path
from freecad.ose3dprinter.part_feature import create_frame
class AddFrameCommand:
"""
Command to add Frame object.
"""
NAME = 'AddFrame'
def Activated(self):
document = App.ActiveDocument
if not document:
document = App.newDocument()
create_frame(document, 'Frame')
document.recompute()
def IsActive(self):
return True
def GetResources(self):
return {
'Pixmap': get_icon_path('Frame.svg'),
'MenuText': 'Add Frame',
'ToolTip': 'Add Frame'
}
For additional information, see Command on the FreeCAD Wiki.