Macros/zh-cn: Difference between revisions

From FreeCAD Documentation
(Created page with "== 如何工作 == 在{{MenuCommand|Edit → Preferences → General → Macro → Show scripts commands in python console}}菜单中开启控制台输出。这时,您将看...")
(Created page with "这个工具使用起来也十分方便:按下记录按钮,系统会让您给宏起个名,接下来让您来执行一些动作。待记录的动作执行完毕后,点...")
Line 13: Line 13:
在{{MenuCommand|Edit → Preferences → General → Macro → Show scripts commands in python console}}菜单中开启控制台输出。这时,您将看到在FreeCAD中所执行的每个动作(例如按下按钮)的对应Python命令都输出到了控制台中。这些命令都能记录到宏中。制作宏的主要工具为宏工具栏:[[Image:Macros_toolbar.jpg]]。其中有4个按钮:Record(记录), stop recording(停止记录), edit(编辑)以及执行当前的宏(play)。
在{{MenuCommand|Edit → Preferences → General → Macro → Show scripts commands in python console}}菜单中开启控制台输出。这时,您将看到在FreeCAD中所执行的每个动作(例如按下按钮)的对应Python命令都输出到了控制台中。这些命令都能记录到宏中。制作宏的主要工具为宏工具栏:[[Image:Macros_toolbar.jpg]]。其中有4个按钮:Record(记录), stop recording(停止记录), edit(编辑)以及执行当前的宏(play)。


这个工具使用起来也十分方便:按下记录按钮,系统会让您给宏起个名,接下来让您来执行一些动作。待记录的动作执行完毕后,点击停止记录按钮,之前的动作就会被保存下来。随后即可利用编辑按钮来访问宏对话框。
It is very simple to use: Press the record button, you will be asked to give a name to your macro, then perform some actions. When you are done, click the stop recording button, and your actions will be saved. You can now access the macro dialog with the edit button.


[[Image:Macros.png]]
[[Image:Macros.png]]

Revision as of 05:04, 25 June 2019

Standard Menu
Introduction to Python

简介

宏是一种在FreeCAD中生成一系列复杂动作的简便方式。您可以借助此功能方便地将多个动作记录下来,将其保存在所取名下,在需要的时候反复执行。由于宏是一系列Python命令,所以可以对它们进行编辑,并创建出非常复杂的脚本。

Python脚本通常以.py作为扩展名,而FreeCAD中的宏则以.FCMacro作为扩展名。由经验丰富者所编写的宏的合集可从macros recipes页面找到。

参考Introduction to Python来学习Python编程语言,再阅读Python scripting tutorialFreeCAD Scripting Basics来学习如何编写宏。

如何工作

Edit → Preferences → General → Macro → Show scripts commands in python console菜单中开启控制台输出。这时,您将看到在FreeCAD中所执行的每个动作(例如按下按钮)的对应Python命令都输出到了控制台中。这些命令都能记录到宏中。制作宏的主要工具为宏工具栏:。其中有4个按钮:Record(记录), stop recording(停止记录), edit(编辑)以及执行当前的宏(play)。

这个工具使用起来也十分方便:按下记录按钮,系统会让您给宏起个名,接下来让您来执行一些动作。待记录的动作执行完毕后,点击停止记录按钮,之前的动作就会被保存下来。随后即可利用编辑按钮来访问宏对话框。

Macro dialog, listing the macros available in the system

There you can manage your macros, delete, edit, duplicate, install or create new ones from scratch. If you edit a macro, it will be opened in an editor window where you can make changes to its code. New macros can be installed using the Addons... button, which links to the Addon Manager.

Example

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.

Customizing

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.

Customize Toolbars This way you can make your macro become a real tool, just like any standard FreeCAD tool. This, added to the power of python scripting within FreeCAD, makes it possible to easily add your own tools to the interface. Read on to the Scripting page if you want to know more about Python scripting.

Creating macros without recording

How to install 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.

Macros repository

Visit the Macros recipes page to pick some useful macros to add to your FreeCAD installation.

Links

Installing more workbenches.

Tutorials

How to install additional workbenches.

Standard Menu
Introduction to Python