Macros: Difference between revisions

From FreeCAD Documentation
(Arrange several links)
(Removing Docnav instead.)
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>

<!--T:19-->
{{Docnav
|[[Standard Menu|Standard Menu]]
|[[Introduction to Python|Introduction to Python]]
}}
</translate>
</translate>
{{TOCright}}
{{TOCright}}
Line 26: Line 22:
== How it works == <!--T:2-->
== How it works == <!--T:2-->


<!--T:23-->
Enable the console output in the menu {{MenuCommand|Edit → Preferences → General → 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: [[Image:Macros_toolbar.jpg]]. On it you have 4 buttons: Record, stop recording, edit and play the current macro.
Enable the console output in the menu {{MenuCommand|Edit → Preferences → General → 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: [[Image:Macros_toolbar.jpg]]. On it you have 4 buttons: Record, stop recording, edit and play the current macro.


Line 40: Line 37:
== Example == <!--T:6-->
== Example == <!--T:6-->


<!--T:24-->
Press the record button, give a name, let's say "cylinder 10x10", then, in the [[Part Workbench|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.
Press the record button, give a name, let's say "cylinder 10x10", then, in the [[Part Workbench|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 == <!--T:7-->
== Customizing == <!--T:7-->


<!--T:25-->
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 {{MenuCommand|Tools → Customize}} menu.
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 {{MenuCommand|Tools → Customize}} menu.


Line 52: Line 51:
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|Scripting]] page if you want to know more about [[Python|Python]] scripting.
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|Scripting]] page if you want to know more about [[Python|Python]] scripting.


<!--T:26-->
See [[Customize_Toolbars|Customize Toolbars]] for a more detailed description.
See [[Customize_Toolbars|Customize Toolbars]] for a more detailed description.


== Creating macros without recording == <!--T:10-->
== Creating macros without recording == <!--T:10-->


<!--T:27-->
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.
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.


<!--T:28-->
See [[How to install macros|How to install macros]] for a more detailed description.
See [[How to install macros|How to install macros]] for a more detailed description.


== Macros repository == <!--T:11-->
== Macros repository == <!--T:11-->


<!--T:29-->
Visit the [[Macros recipes|Macros recipes]] page to pick some useful macros to add to your FreeCAD installation.
Visit the [[Macros recipes|Macros recipes]] page to pick some useful macros to add to your FreeCAD installation.


== Additional information == <!--T:13-->
== Additional information == <!--T:13-->


<!--T:30-->
* [[Macro_at_Startup|Automatically run macro at startup]]
* [[Macro_at_Startup|Automatically run macro at startup]]
* [[Installing_more_workbenches|Installing more workbenches]]
* [[Installing_more_workbenches|Installing more workbenches]]
Line 71: Line 75:
== Tutorials == <!--T:14-->
== Tutorials == <!--T:14-->


<!--T:31-->
You can manually install extensions, however, it is much simpler to just use the [[Std_AddonMgr|Addon Manager]].
You can manually install extensions, however, it is much simpler to just use the [[Std_AddonMgr|Addon Manager]].
* [[How to install macros|How to install macros]]
* [[How to install macros|How to install macros]]
* [[How_to_install_additional_workbenches|How to install additional workbenches]]
* [[How_to_install_additional_workbenches|How to install additional workbenches]]


<!--T:12-->
{{Docnav
|[[Standard Menu|Standard Menu]]
|[[Introduction to Python|Introduction to Python]]
}}


</translate>
</translate>
{{Userdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Formats{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
[[Category:Macros{{#translation:}}]]
{{clear}}
{{clear}}

Revision as of 19:46, 29 December 2020

Introduction

Macros are a convenient way to reproduce complex actions in FreeCAD. You simply record actions as you do them, then save those actions under a name, and replay them whenever you want. Since macros are in reality a list of Python commands, you can also edit them, and create very complex scripts.

While Python scripts normally have the .py extension, FreeCAD macros should have the .FCMacro extension. A collection of macros written by experienced users is found in the macros recipes page.

See the Power users hub to learn more about the Python programming language, and about writing macros. In particular, you should start with these pages:

How it works

Enable the console output in the menu Edit → Preferences → General → 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.

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.

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.

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.

See Customize Toolbars for a more detailed description.

Creating macros without recording

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 repository

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

Additional information

Tutorials

You can manually install extensions, however, it is much simpler to just use the Addon Manager.