Module Creation/ru


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

Создание нового модуля в FreeCAD достаточно простой процесс. В FreeCAD ветке разработчиков(каталоге) находится Инструмент сборки FreeCAD (fcbt) он делает за вас все самое важное. Это Python сценарий расположенный в

 trunk/src/Tools/fcbt.py

Если важ python интепритатор, установле правильно, вы можете выполнит в командной строке

 python fcbt.py

Это отобразит следующее меню:

 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> ?

В командной строке введите CM , чтобы начать создание модуля:

 Insert command: ''CM''

Теперь вас просят указать имя нового модуля. Давайте назовем его, например, TestMod :

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

После нажатия enter fcbt начнет копировать все необходимые файлы для вашего модуля в папку

 trunk/src/Mod/TestMod/

Когда все файлы будут изменены с вашим новым именем модуля(???). Единственное что сейчас нужно сделать, это добавить два новых проекта "appTestMod" и "appTestModGui" в вашу (workspace)рабочую область (в Windows) или в ваш makefile целей (unix). Это Всё!

Setting up a new module manually

You need two things to create a new module:

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:

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:

Creating FreeCAD Commands in C++

To Be Documented

Links

Online version: "http://www.freecadweb.org/wiki/index.php?title=Module_Creation/ru&oldid=287472"

Navigation menu