Module Creation

From FreeCAD Documentation
Jump to: navigation, search
This page is a translated version of the page Module Creation and the translation is 36% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎polski • ‎română • ‎русский • ‎svenska • ‎Türkçe • ‎中文(中国大陆)‎

Adding new modules and workbenches in FreeCAD is very easy. A module is any extension of FreeCAD, while a workbench is a special GUI configuration that groups some toolbars and menus. Usually you create a new module which contains its own workbench.

Modules can be programmed in C++ or in python, or in a mixture of both, but the module init files must be in python. Setting up a new module with those init files is easy, and can be done either manually or with the FreeCAD build tool.

Using the FreeCAD Build tool

Att skapa en ny applikationsmodul i FreeCAD är ganska lätt. I FreeCAD's utvecklingsträd så finns FreeCAD Byggverktyg (fcbt) som gör de viktigaste sakerna för dig. Det är ett [Python] skript som är placerat i

trunk/src/Tools/fcbt.py

När din python tolk är korrekt installerad så kan du köra skriptet från en kommandorad med

python fcbt.py

Det kommer att visa följande meny:

FreeCAD Build Tool
Usage:
   fcbt <command name> [command parameter] 
possible commands are:
 - DistSrc         (DS)   Build a source Distr. of the current source tree
 - DistBin         (DB)   Build a binary Distr. of the current source tree
 - DistSetup       (DI)   Build a Setup Distr. of the current source tree
 - DistSetup       (DUI)  Build a User Setup Distr. of the current source tree
 - DistAll         (DA)   Run all three above modules
 - NextBuildNumber (NBN)  Increase the Build Number of this Version
 - CreateModule    (CM)   Insert a new FreeCAD Module in the module directory
 - CreatePyModule  (CP)   Insert a new FreeCAD Python Module in the module directory

For help on the modules type:
  fcbt <command name> ?

Skriv CM vid kommandoprompten för att starta skapandet av en modul:

Insert command: ''CM''

Nu efterfrågas det om ett namn på din nya modul. Låt oss till exempel kalla den för TestMod :

Please enter a name for your application: ''TestMod''

Efter att du har tryckt enter så startar fcbt att kopiera alla nödvändiga filer för din modul till en ny mapp vid

trunk/src/Mod/TestMod/

Sedan ändras alla filer med ditt nya modulnamn. Den enda saken du behöver göra nu är att lägga till de två nya projekten "appTestMod" och "appTestModGui" till din arbetsyta (på Windows) eller till dina makefile mål (unix). Det är allt!

Setting up a new module manually

You need two things to create a new module:

  • A new folder in the FreeCAD Mod folder (either in InstalledPath/FreeCAD/Mod or in UserPath/.FreeCAD/Mod). You can name it as you like.
  • Inside that folder, an InitGui.py file. That file will be executed automatically on FreeCAD start (it can be empty, but just to test it,, put a print("hello world") inside)

Additionally, you can also add an Init.py file. InitGui.py file is loaded only when FreeCAD runs in GUI mode, whereas the Init.py file is always loaded. Since we're creating a workbench, however, we'll put our code in InitGui.py, since workbenches are GUI-only tools.

Creating a new workbench

Inside the InitGui.py file, the first thing you will want to do is define a workbench. You can use the following example as a template:

class MyWorkbench ( Workbench ):
	"My workbench object"
	Icon = """
			/* XPM */
			static const char *test_icon[]={
			"16 16 2 1",
			"a c #000000",
			". c None",
			"................",
			"................",
			"..############..",
			"..############..",
			"..############..",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"................",
			"................"};
			"""
	MenuText = "My Workbench"
	ToolTip = "This is my extraordinary workbench"

        def GetClassName(self):
               return "Gui::PythonWorkbench"
	
	def Initialize(self):
		import myModule1, myModule2
		self.appendToolbar("My Tools", ["MyCommand1","MyCommand2"])
		self.appendMenu("My Tools", ["MyCommand1","MyCommand2"])
		Log ("Loading MyModule... done\n")

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

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

FreeCADGui.addWorkbench(MyWorkbench)

The workbench must have all these attributes defined:

  • Icon - An XPM image. Most image software (like GIMP) can convert an image into xpm format, which is a text file. You can then paste the contents here.
  • MenuText - The workbench name as it appears in the workbenches list
  • Tooltip - Defines the tooltip text
  • Initialize() - Executed when FreeCAD loads and creates all menus and toolbars that the workbench will use. If you are going to make your module in C++, you can also define your menus and toolbars inside the C++ module rather than the InitGui.py file. The important thing is that they are created now, before the module is activated.
  • Activated() - Executed when the user switches to your workbench
  • Deactivated() - Executed when the user switches away to another workbench or leaves FreeCAD

Creating FreeCAD commands in Python

Usually you define all your tools (called Commands in FreeCAD) in another module, then import that module before creating the toolbars and menus. Here is a simple example that can be used as a template:

import FreeCAD,FreeCADGui

class MyTool:
	"My tool object"

       def GetResources(self):
               return {"MenuText": "My Command",
                       "Accel": "Ctrl+M",
                       "ToolTip": "My extraordinary command",
                       "Pixmap"  : """
			/* XPM */
			static const char *test_icon[]={
			"16 16 2 1",
			"a c #000000",
			". c None",
			"................",
			"................",
			"..############..",
			"..############..",
			"..############..",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"......####......",
			"................",
			"................"};
			"""}

       def IsActive(self):
               if FreeCAD.ActiveDocument == None:
                       return False
               else:
                       return True

	def Activated(self):
               # do something here...

FreeCADGui.addCommand('MyCommand1',MyTool())

Note there are three methods defined:

  • GetResources() - Returns a dictionary with the visual attributes of your tool. The Accel parameter defines an optional shortcut key.
  • IsActive() - Determines whether or not the command is active. Inactive commands are indicated by a greyed-out icon in the menus and toolbars.
  • Activated() - Executed when the Command is called, whether through a toolbar button, menu or script API call.

Creating FreeCAD Commands in C++

To Be Documented

Links