Scripts
在
Edit → Preferences → General → Macro → Show scripts commands in python console
菜单中开启控制台输出。这时,您将看到在FreeCAD中所执行的每个动作(例如按下按钮)的对应Python命令都输出到了控制台中。这些命令都能记录到宏中。制作宏的主要工具为宏工具栏:
。其中有4个按钮:Record(记录), stop recording(停止记录), edit(编辑)以及执行当前的宏(play)。
Enable the console output in the menu
Edit → Preferences → Python → Macro → Show scripts commands in python console
. You will see that in FreeCAD, every action you do, such as pressing a button, outputs a Python command. Those commands are what can be recorded in a macro. The main tool for making macros is the macros toolbar:
. On it you have 4 buttons: Record, stop recording, edit and play the current macro.
这个工具使用起来也十分方便:按下记录按钮,系统会让您给宏起个名,接下来让您来执行一些动作。待记录的动作执行完毕后,点击停止记录按钮,之前的动作就会被保存下来。随后即可利用编辑按钮来访问宏对话框。
宏对话框, 列举了系统中存在的宏
您可以管理您的宏:删除、编辑、复制、安装,或者从头创建一个新的宏。如果您要编辑一个宏,它便会在编辑器窗口中打开,在此就可以修改其代码。可以用
Addons...
按钮来安装一个新的宏,它将跳转至
Addon Manager
。
按下记录按钮,指定宏的名称,这里设置为"cylinder 10x10",接下来,在
零件工作台
中创建一个半径为10且高为10的圆柱体。随后按下"stop recording(停止记录)"按钮。在编辑宏对话框中,您可以看到刚刚记录的python代码,如有需要就对代码进行调整。要执行编辑器中的宏的时候,只需简单地按下工具栏中的执行按钮即可。您制作的宏将一直存于硬盘,因此,您做的任意更改、或所创的新宏总能在下次开启FreeCAD时继续使用。
Press the record button, give a name, let's say "cylinder 10x10", then, in the
Part Workbench
, create a cylinder with radius = 10 and height = 10. Then, press the "stop recording" button. In the edit macros dialog, you can see the python code that has been recorded, and, if you want, make alterations to it. To execute your macro, simply press the execute button on the toolbar while your macro is in the editor. You macro is always saved to disk, so any change you make, or any new macro you create, will always be available next time you start FreeCAD.
诚然,我们不可能为了使用宏而每次都将其先加载至编辑器中。FreeCAD提供了更便捷的方式令您使用宏,例如为之添加快捷键,或将它加入菜单作为其中的一个选项。只要创建好宏,一切有关的操作都可通过
Tools → Customize
菜单来实现。
Of course it is not practical to load a macro in the editor in order to use it. FreeCAD provides much better ways to use your macro, such as assigning a keyboard shortcut to it or putting an entry in the menu. Once your macro is created, all this can be done via the
Tools → Customize
menu.
借助
自定义工具栏
可使您的宏成为像其他FreeCAD标准工具那样的实体工具。这样,既可将python脚本强大的功能添加至FreeCAD中,又能令您方便地把自己制作的工具加入到界面中。如果您希望了解更多关于
Python
脚本的信息,请阅读
Scripting
页面。
See
Customize Toolbars
for a more detailed description.
通过非记录的方式来创建宏
如何安装宏
您也可以通过不记录GUI动作,而直接复制/粘贴python代码的方式来创建宏。即,简单地创建一个新的宏,对它进行编辑,并粘贴您自己的代码。最后再以保存FreeCAD文档的方式来保存宏。当下一次开启FreeCAD的时候, 此宏将位列宏菜单中的"Installed Macros"项下。
You can also directly copy/paste python code into a macro, without recording GUI action. Simply create a new macro, edit it, and paste your code. You can then save your macro the same way as you save a FreeCAD document. Next time you start FreeCAD, the macro will appear under the "Installed Macros" item of the Macro menu.
See
How to install macros
for a more detailed description.
您可以光临
Macros recipes
页面来挑选需要的宏,并将它添加至FreeCAD中进行安装。
There are two main places for macros. The first one is the official peer-reviewed macro repository on
GitHub
. The second one is the
Macros recipes
page from which you can pick some useful macros to add to your FreeCAD installation. Macros from both repositories can be installed via the
Addon Manager
directly from FreeCAD.
安装更多的工作台
。
如何安装其他的工作台
。
You can manually install extensions, however, it is much simpler to just use the
Addon Manager
.
Scripts
Power user documentation
-
Parametric objects:
Scripted objects
,
Viewproviders
(
Custom icon in tree view
)
-
Scenegraph:
Coin (Inventor) scenegraph
,
Pivy
-
Graphical interface:
Interface creation
,
Interface creation completely in Python
(
1
,
2
,
3
,
4
,
5
)
,
PySide
, PySide examples
beginner
,
intermediate
,
advanced
-
Macros:
Macros
,
How to install macros
-
Embedding:
Embedding FreeCAD
,
Embedding FreeCADGui