Difference between revisions of "How to install macros"

From FreeCAD Documentation
Jump to: navigation, search
(Renamed sectio: no text output from macros)
(Marked this version for translation)
(16 intermediate revisions by 3 users not shown)
Line 11: Line 11:
  
 
==Description== <!--T:2-->
 
==Description== <!--T:2-->
This page will show you how to install and use FreeCAD [[macros]].
+
This page will show you how to install and use FreeCAD [[macros|macros]].
  
Macros are sequences of commands which are used to perform a complex drawing operation. Macros are [[Python]] scripts, which means they are text files that can be written and edited with a text editor.
+
<!--T:140-->
 +
Macros are sequences of commands which are used to perform a complex drawing operation. Macros are [[Python|Python]] scripts, which means they are text files that can be written and edited with a text editor.
  
While Python scripts normally have the {{incode|.py}} extension, FreeCAD macros should have the {{incode|.FCMacro}} extension. A collection of macros written by experienced users is found in the [[macros recipes]] page.
+
<!--T:141-->
 +
While Python scripts normally have the {{incode|.py}} extension, FreeCAD macros should have the {{incode|.FCMacro}} extension. A collection of macros written by experienced users is found in the [[macros recipes|macros recipes]] page.
  
See [[Introduction to Python]] to learn about the Python programming language, and then [[Python scripting tutorial]] and [[FreeCAD Scripting Basics]] to learn about writing macros.
+
<!--T:142-->
 +
See [[Introduction to Python|Introduction to Python]] to learn about the Python programming language, and then [[Python scripting tutorial|Python scripting tutorial]] and [[FreeCAD Scripting Basics|FreeCAD Scripting Basics]] to learn about writing macros.
 +
 
 +
<!--T:143-->
 +
Here is a video on [https://wiki.opensourceecology.org/wiki/Installing_Macros_in_FreeCAD installing FreeCAD macros in Ubuntu].
  
 
==The Macro menu and toolbar== <!--T:6-->
 
==The Macro menu and toolbar== <!--T:6-->
  
===Toolbar===
+
===Toolbar=== <!--T:7-->
<!--T:7-->
 
 
* [[Image:Macro-record.svg|32px|record]] [[Std_DlgMacroRecord|Record]]
 
* [[Image:Macro-record.svg|32px|record]] [[Std_DlgMacroRecord|Record]]
 
* [[Image:Macro-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]
 
* [[Image:Macro-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]
Line 28: Line 33:
 
* [[Image:Macro-execute.svg|32px|execute]] [[Std_DlgMacroExecute|Execute]]
 
* [[Image:Macro-execute.svg|32px|execute]] [[Std_DlgMacroExecute|Execute]]
  
===Menu===
+
===Menu=== <!--T:144-->
 
Besides the tools in the toolbar, the following functions are also available in the {{MenuCommand|Macro}} menu.
 
Besides the tools in the toolbar, the following functions are also available in the {{MenuCommand|Macro}} menu.
 
* [[Image:Debug-start.svg|32px|start]] [[Std_DebugMacro|Debug Start]]
 
* [[Image:Debug-start.svg|32px|start]] [[Std_DebugMacro|Debug Start]]
Line 38: Line 43:
 
==Macros directory== <!--T:8-->
 
==Macros directory== <!--T:8-->
  
 +
</translate>
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
Macros are created in a specific folder under the user's FreeCAD directory. This directory can be configured in the [[Std_DlgMacroExecuteDirect|execute macro dialog]], or in the [[Preferences Editor]], through the menu {{MenuCommand|Edit → Preferences → General → Macro → Macro recording settings}}.
+
<translate>
 +
 
 +
<!--T:178-->
 +
Macros are created in a specific folder under the user's FreeCAD directory. This directory can be configured in the [[Std_DlgMacroExecuteDirect|execute macro dialog]], or in the [[Preferences Editor|Preferences Editor]], through the menu {{MenuCommand|Edit → Preferences → General → Macro → Macro recording settings}}.
  
External macros should also be placed in this directory.
+
<!--T:146-->
 +
Downloaded macros should also be placed in this directory.
  
 +
</translate>
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
<!--T:10-->
+
<translate>
 +
 
 +
=== Default directory === <!--T:147-->
 +
Macros can be simply copied into
 +
 
 +
</translate>
 +
{{Code|code=
 +
$ROOT_DIR/
 +
}}
 +
<translate>
 +
 
 +
<!--T:148-->
 +
where {{incode|$ROOT_DIR}} is a top level directory searched by FreeCAD on startup.
 +
 
 +
<!--T:149-->
 +
The {{incode|$ROOT_DIR}} could be a system wide directory, in which case the macro is installed for all users.
 +
* On Linux it is usually {{incode|/usr/share/freecad/}}
 +
* On Windows it is usually {{incode|C:\Program Files\FreeCAD\}}
 +
* On Mac OSX it is usually {{incode|/Applications/FreeCAD/}}
 +
 
 +
<!--T:150-->
 +
The {{incode|$ROOT_DIR}} could be a particular user's directory.
 +
* On Linux it is usually {{incode|/home/username/.FreeCAD/}}
 +
* On Windows it is usually {{incode|C:\Users\username\Application Data\FreeCAD\}}
 +
* On Mac OSX it is usually {{incode|/Users/username/Library/Preferences/FreeCAD/}}
 +
 
 +
=== Configuring the user directory === <!--T:10-->
 
1. Open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}} to open the [[Std_DlgMacroExecuteDirect|Execute macro dialog]].
 
1. Open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}} to open the [[Std_DlgMacroExecuteDirect|Execute macro dialog]].
  
Line 53: Line 90:
 
<!--T:13-->
 
<!--T:13-->
 
2. Set the appropriate {{incode|User macros location}}.
 
2. Set the appropriate {{incode|User macros location}}.
* Linux: usually {{incode|/home/your_user_name/.FreeCAD}}
+
* Linux: usually {{incode|/home/username/.FreeCAD/}}
* Windows: usually {{incode|C:\Users\your_user_name\AppData\Roaming\FreeCAD\}}
+
* Windows: usually {{incode|C:\Users\username\AppData\Roaming\FreeCAD\}}
* MacOS: usually {{incode|/Users/your_user_name/Library/Preferences/FreeCAD}}
+
* MacOS: usually {{incode|/Users/username/Library/Preferences/FreeCAD/}}
  
 
<!--T:16-->
 
<!--T:16-->
Line 81: Line 118:
 
{{Caption|align=center|Macros directory}}
 
{{Caption|align=center|Macros directory}}
  
 +
</translate>
 
{{clear}}
 
{{clear}}
 
</div>
 
</div>
 
</div>
 
</div>
 +
<translate>
  
== Adding macros ==
+
== Installing macros == <!--T:152-->
  
 +
</translate>
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
===Method 1. Copy the code to the macro editor=== <!--T:46-->
+
<translate>
 +
 
 +
=== Automatic method === <!--T:153-->
 +
 
 +
<!--T:154-->
 +
Starting with FreeCAD 0.17, use the [[Addon Manager|Addon Manager]] in {{MenuCommand|Tools → Addon manager}} to install a macro that has been included in the [https://github.com/FreeCAD/FreeCAD-macros FreeCAD-macros] repository.
  
 +
</translate>
 +
<div class="mw-collapsible-content">
 +
<translate>
 +
 +
<!--T:179-->
 +
In past versions of FreeCAD you could use two automated ways to install macros and other addons:
 +
* [https://github.com/FreeCAD/FreeCAD-addons addons_installer.FCMacro]: itself a macro, this was the precursor to the Addon Manager, and is hosted in the [https://github.com/FreeCAD/FreeCAD-addons FreeCAD-addons] repository. You don't need to use this tool in new installations of FreeCAD.
 +
* [https://github.com/microelly2/freecad-pluginloader freecad-pluginloader]: also a macro, it could be used to install new components to FreeCAD. It is no longer developed.
 +
 +
<!--T:156-->
 +
The recommended way to install addons, that is, [[external workbenches|external workbenches]] and macros, is the [[Addon Manager|Addon Manager]]. However, you can still add macros to your system with the manual methods described in the following sections; this is useful if you are developing and testing your own code.
 +
 +
</translate>
 +
</div>
 +
</div>
 +
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
<translate>
 +
 +
===Manual method 1. Copy the code to the macro editor=== <!--T:46-->
 +
 +
<!--T:158-->
 
For macros that are relatively small, 300 lines or less, the code can be copied and pasted directly into the FreeCAD macro editor.
 
For macros that are relatively small, 300 lines or less, the code can be copied and pasted directly into the FreeCAD macro editor.
  
 +
</translate>
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
<!--T:47-->
+
<translate>
 +
 
 +
<!--T:180-->
 
We will use [[Image:Part_Prism_Apothem.svg|24px]] [[Macro_Apothem_Based_Prism_GUI|Macro Apothem Based Prism GUI]] as an example.
 
We will use [[Image:Part_Prism_Apothem.svg|24px]] [[Macro_Apothem_Based_Prism_GUI|Macro Apothem Based Prism GUI]] as an example.
  
 
<!--T:48-->
 
<!--T:48-->
1. Go to the macro wiki page, which should be listed in [[Macros recipes]].
+
1. Go to the macro wiki page, which should be listed in [[Macros recipes|Macros recipes]].
  
 +
<!--T:159-->
 
If there is a custom icon download it; click on it with the right mouse button and select {{incode|Save image as...}}; place the icon in the macros directory. This icon can be used as a shortcut for the macro in a [[Customize_Toolbars|custom toolbar]]. The default icon is [[Image:Text-x-python.png|24px]].
 
If there is a custom icon download it; click on it with the right mouse button and select {{incode|Save image as...}}; place the icon in the macros directory. This icon can be used as a shortcut for the macro in a [[Customize_Toolbars|custom toolbar]]. The default icon is [[Image:Text-x-python.png|24px]].
  
Line 146: Line 217:
 
8. A window appears asking for confirmation to save the code; click on {{Button|Yes}}. You can also use {{KEY|Ctrl}}+{{KEY|S}} to save the file.
 
8. A window appears asking for confirmation to save the code; click on {{Button|Yes}}. You can also use {{KEY|Ctrl}}+{{KEY|S}} to save the file.
  
 +
<!--T:160-->
 
Restart FreeCAD to correctly register the new macro.
 
Restart FreeCAD to correctly register the new macro.
  
Line 169: Line 241:
 
11. This macro should return an error if no document is active; other macros open a new document if none exists.
 
11. This macro should return an error if no document is active; other macros open a new document if none exists.
  
 +
<!--T:161-->
 
Create a new document with {{MenuCommand|File → [[Image:Document-new.svg|16px]] [[Std_New|New]]}}, and then repeat the previous steps to execute the macro.
 
Create a new document with {{MenuCommand|File → [[Image:Document-new.svg|16px]] [[Std_New|New]]}}, and then repeat the previous steps to execute the macro.
  
Line 195: Line 268:
 
[[Image:Macro Install HowTo 26.png|center]]
 
[[Image:Macro Install HowTo 26.png|center]]
 
{{Caption|align=center|Running the macro that is loaded in the editor}}
 
{{Caption|align=center|Running the macro that is loaded in the editor}}
 +
</translate>
 +
 
</div>
 
</div>
 
</div>
 
</div>
  
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 +
<translate>
  
===Method 2. Add a macro file from a compressed .zip file=== <!--T:78-->
+
===Manual method 2. Add a macro file from a compressed .zip file=== <!--T:78-->
  
 
<!--T:79-->
 
<!--T:79-->
Some macros are too big that it's inconvenient to copy and paste them, or they cannot be hosted in the wiki. In this case the code may be placed somewhere else, in a Github repository, or in the [https://forum.freecadweb.org/ FreeCAD forum]. The code may also be compressed into a {{incode|.zip}} file, tarball {{incode|.tar.xz}}, or other type of archive.
+
Some macros are too big that it's inconvenient to copy and paste them into the macro editor, or they cannot be hosted in the wiki. In this case, the code may be hosted somewhere else, in a Github repository, or in the [https://forum.freecadweb.org/ FreeCAD forum]. The code may also be compressed into a {{incode|.zip}} file, tarball {{incode|.tar.xz}}, or other type of archive if it contains several files. If the code is distributed in this way, the archive should be extracted and the files placed in the macros directory.
  
 +
</translate>
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
 +
<translate>
 +
 +
<!--T:181-->
 
We will use [[Image:Text-x-python.png|24px]] [[Macro_screw_maker1_2|Macro screw maker]] as an example.
 
We will use [[Image:Text-x-python.png|24px]] [[Macro_screw_maker1_2|Macro screw maker]] as an example.
  
 +
<!--T:164-->
 
1. Download the compressed code from the forum, [http://forum.freecadweb.org/viewtopic.php?f=22&t=6558#p52887 Screw Maker].
 
1. Download the compressed code from the forum, [http://forum.freecadweb.org/viewtopic.php?f=22&t=6558#p52887 Screw Maker].
  
 
<!--T:80-->
 
<!--T:80-->
 
You need to use a decompressor to get the internal files.
 
You need to use a decompressor to get the internal files.
* For Windows you can use and application like [http://www.7-zip.org/ 7-zip] or [http://www.kanmandet.dk/?p=37 L-Zarc] or [http://www.quickzip.org/quickzip51.html quickzip].
+
* For Windows you can use an application like [http://www.7-zip.org/ 7-zip] or [http://www.kanmandet.dk/?p=37 L-Zarc] or [http://www.quickzip.org/quickzip51.html quickzip].
 
* For Linux you can use a command from the terminal
 
* For Linux you can use a command from the terminal
 +
 +
</translate>
 
{{Code|code=
 
{{Code|code=
 
unzip your_file.zip -d your_directory
 
unzip your_file.zip -d your_directory
 
}}
 
}}
 +
<translate>
  
 
<!--T:85-->
 
<!--T:85-->
Line 277: Line 361:
 
[[Image:Macro Install HowTo 30.png|center]]
 
[[Image:Macro Install HowTo 30.png|center]]
 
{{Caption|align=center|Object created by the macro}}
 
{{Caption|align=center|Object created by the macro}}
 +
 +
</translate>
 +
</div>
 +
</div>
 +
<translate>
 +
 +
== Execute a macro in command line == <!--T:182-->
 +
 +
</translate>
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
<translate>
 +
 +
<!--T:183-->
 +
Command line execute a macro (.FCMacro or .py)
 +
 +
</translate>
 +
<div class="mw-collapsible-content">
 +
<translate>
 +
 +
<!--T:184-->
 +
on Windows
 +
 +
</translate>
 +
{{Code|code=
 +
"C:\Program Files\FreeCAD\bin\FreeCAD.exe" "C:\Users\userName\AppData\Roaming\FreeCAD\Mod\WorkFeature\start_WF.FCMacro"
 +
}}
 +
<translate>
 +
 +
<!--T:185-->
 +
on Linux
 +
 +
</translate>
 +
{{Code|code=
 +
todo
 +
}}
 +
 
</div>
 
</div>
 
</div>
 
</div>
 +
<translate>
  
== Errors in the macros ==
+
== Errors in macros == <!--T:165-->
  
 
<!--T:136-->
 
<!--T:136-->
Line 288: Line 409:
  
 
<!--T:23-->
 
<!--T:23-->
The white space at the beginning of the lines (indentation) in the [[Python]] programming language is very important, and an integral part of the code. An inappropriate space may cause the code to not run or present errors.
+
The white space at the beginning of the lines (indentation) in the [[Python|Python]] programming language is very important, and an integral part of the code. An inappropriate space may cause the code to not run or present errors.
  
 +
<!--T:166-->
 
This section describes some errors that may be encountered when copying and pasting, and writing macro code.
 
This section describes some errors that may be encountered when copying and pasting, and writing macro code.
  
 +
<!--T:167-->
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
  
Line 297: Line 420:
 
A typical indentation error looks like this:
 
A typical indentation error looks like this:
  
 +
</translate>
 
{{Code|code=
 
{{Code|code=
 
<unknown exception traceback><type 'exceptions.IndentationError'>: ('expected an indented block', ('C:/Users/d/AppData/Roaming/FreeCAD/Macro_Apothem_Based_Prism_GUI.FCMacro', 21, 3, 'def priSm(self):\n'))
 
<unknown exception traceback><type 'exceptions.IndentationError'>: ('expected an indented block', ('C:/Users/d/AppData/Roaming/FreeCAD/Macro_Apothem_Based_Prism_GUI.FCMacro', 21, 3, 'def priSm(self):\n'))
 
}}
 
}}
 +
<translate>
  
<!--T:26-->
+
==== Example 1 ==== <!--T:26-->
==== Example 1 ====
 
  
 +
<!--T:169-->
 
If the code lacks any indentation, the code won't work. Class ({{incode|class}}) and function definitions ({{incode|def()}}), as well as control structures ({{incode|if}}, {{incode|while}}, {{incode|for}}) should be followed by a block of indented code.
 
If the code lacks any indentation, the code won't work. Class ({{incode|class}}) and function definitions ({{incode|def()}}), as well as control structures ({{incode|if}}, {{incode|while}}, {{incode|for}}) should be followed by a block of indented code.
  
 +
<!--T:170-->
 
This error is possible if the user doesn't copy the code correctly, and all spaces are accidentally removed.
 
This error is possible if the user doesn't copy the code correctly, and all spaces are accidentally removed.
  
Line 326: Line 452:
 
{{Caption|align=center|Python code highlighted, showing that all lines start at the left edge}}
 
{{Caption|align=center|Python code highlighted, showing that all lines start at the left edge}}
  
<!--T:32-->
+
==== Example 2 ==== <!--T:32-->
==== Example 2 ====
 
  
 +
<!--T:171-->
 
If an additional space is introduced at the beginning of all lines, the Python interpreter will fail and complain about unnecessary indentation. In this case, all lines need the initial space removed.
 
If an additional space is introduced at the beginning of all lines, the Python interpreter will fail and complain about unnecessary indentation. In this case, all lines need the initial space removed.
  
Line 335: Line 461:
 
{{Caption|align=center|Python code with additional space on each line}}
 
{{Caption|align=center|Python code with additional space on each line}}
  
<!--T:34-->
+
==== Example 3 ==== <!--T:34-->
==== Example 3 ====
 
  
 +
<!--T:172-->
 
Here the code has been copied from a forum thread by using the {{Button|Select all}} button. Apparently the selection is good.
 
Here the code has been copied from a forum thread by using the {{Button|Select all}} button. Apparently the selection is good.
  
Line 354: Line 480:
 
In this case, the initial spaces need to be removed. This can be done with a specialized text editor to quickly decrease the indentation of the lines.
 
In this case, the initial spaces need to be removed. This can be done with a specialized text editor to quickly decrease the indentation of the lines.
  
 +
<!--T:173-->
 
In Windows, [http://notepad-plus-plus.org/ Notepad++] can perform selection with {{KEY|Alt}} + Mouse dragging, and then use {{MenuCommand|Edit → Indent → Decrease the indentation}}.
 
In Windows, [http://notepad-plus-plus.org/ Notepad++] can perform selection with {{KEY|Alt}} + Mouse dragging, and then use {{MenuCommand|Edit → Indent → Decrease the indentation}}.
  
Line 360: Line 487:
 
{{Caption|align=center|Python code with the correct indentation}}
 
{{Caption|align=center|Python code with the correct indentation}}
  
<!--T:40-->
+
==== Example 4 ==== <!--T:40-->
==== Example 4 ====
 
  
 +
<!--T:174-->
 
Here the selection also selects the line numbers in the code example. If this selection is pasted into the macro editor, it won't work. All line numbers need to be removed, and the spaces adjusted so that the Python code has the proper indentation.
 
Here the selection also selects the line numbers in the code example. If this selection is pasted into the macro editor, it won't work. All line numbers need to be removed, and the spaces adjusted so that the Python code has the proper indentation.
  
Line 369: Line 496:
 
{{Caption|align=center|Selection that also selects the line numbers; if this code is pasted into the macro editor, it won't work}}
 
{{Caption|align=center|Selection that also selects the line numbers; if this code is pasted into the macro editor, it won't work}}
  
<!--T:42-->
+
==== Good code ==== <!--T:42-->
==== Good code ====
 
  
 
<!--T:43-->
 
<!--T:43-->
 
[[Image:Macro Install HowTo 13.png|center]]
 
[[Image:Macro Install HowTo 13.png|center]]
 
{{Caption|align=center|Python code with the correct indentation}}
 
{{Caption|align=center|Python code with the correct indentation}}
 +
 +
</translate>
 
</div>
 
</div>
 
</div>
 
</div>
  
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 +
<translate>
 +
 
===No text output from macros=== <!--T:108-->
 
===No text output from macros=== <!--T:108-->
  
Macros may output information to the report view when running, to detail what the code is doing.
+
<!--T:175-->
 +
Macros may output information to the report view to detail what the code is doing when it is running.
  
If no information is displayed, the report view must be visible and certain options may need to be enabled.
+
<!--T:176-->
 +
If no information is displayed, make sure the report view and [[Python|Python]] console are visible, and that the output is directed tot he report view.
  
 +
</translate>
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
<!--T:109-->
+
<translate>
  
==== Printing information ====
+
==== Printing information ==== <!--T:177-->
 
FreeCAD macros have two methods to print information to the report view.
 
FreeCAD macros have two methods to print information to the report view.
  
Line 394: Line 527:
 
The FreeCAD functions
 
The FreeCAD functions
 
</translate>
 
</translate>
 +
 
{{Code|code=
 
{{Code|code=
 
FreeCAD.Console.PrintMessage("Hello World! \n")
 
FreeCAD.Console.PrintMessage("Hello World! \n")
Line 404: Line 538:
 
The simple Python function
 
The simple Python function
 
</translate>
 
</translate>
 +
 
{{Code|code=
 
{{Code|code=
 
print("Hello World!")
 
print("Hello World!")
Line 409: Line 544:
 
<translate>
 
<translate>
  
==== Enabling the report view ====
+
==== Enabling the report view ==== <!--T:112-->
<!--T:112-->
 
 
To see the information displayed in the console you should:
 
To see the information displayed in the console you should:
  
Line 437: Line 571:
 
{{Caption|align=center|FreeCAD main window with the Report view and the Python console}}
 
{{Caption|align=center|FreeCAD main window with the Report view and the Python console}}
  
==== Enabling the <tt>print()</tt> command ====
+
==== Enabling the <tt>print()</tt> command ==== <!--T:121-->
<!--T:121-->
+
FreeCAD may need to be configured so the {{incode|print()}} function of [[Python|Python]] redirects its output correctly to the report view.
FreeCAD may need to be configured so the {{incode|print()}} function of [[Python]] redirects its output correctly to the report view.
 
  
 
<!--T:124-->
 
<!--T:124-->
1. Go into the [[Preferences Editor]] with the menu {{MenuCommand|Edit → Preferences}}.
+
1. Go into the [[Preferences Editor|Preferences Editor]] with the menu {{MenuCommand|Edit → Preferences}}.
  
 
<!--T:125-->
 
<!--T:125-->
Line 474: Line 607:
 
[[Image:Macro Install HowTo 38.png|center]]
 
[[Image:Macro Install HowTo 38.png|center]]
 
{{Caption|align=center|Python commands printing information to the report view}}
 
{{Caption|align=center|Python commands printing information to the report view}}
 +
</translate>
 
</div>
 
</div>
 
</div>
 
</div>
 
==Links== <!--T:105-->
 
 
<!--T:106-->
 
Macro [https://github.com/FreeCAD/FreeCAD-addons addons_installer.FCMacro] for easy install the macro
 
 
<!--T:138-->
 
Other easy macro installer [https://github.com/microelly2/freecad-pluginloader freecad-pluginloader]
 
 
<!--T:139-->
 
The [[Macros_recipes|Macros recipes]] page
 
 
</translate>
 

Revision as of 19:20, 4 September 2019

Other languages:
čeština • ‎Deutsch • ‎English • ‎español • ‎français • ‎hrvatski • ‎italiano • ‎română • ‎русский • ‎svenska
Base ExampleCommandModel.png Tutorial
Topic
Programming
Level
Medium programmer
Time to complete
15 minutes
Author(s)
Mario52
FreeCAD version
All
Example files
None


Description

This page will show you how to install and use FreeCAD macros.

Macros are sequences of commands which are used to perform a complex drawing operation. Macros are Python scripts, which means they are text files that can be written and edited with a text editor.

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 Introduction to Python to learn about the Python programming language, and then Python scripting tutorial and FreeCAD Scripting Basics to learn about writing macros.

Here is a video on installing FreeCAD macros in Ubuntu.

The Macro menu and toolbar

Toolbar

Menu

Besides the tools in the toolbar, the following functions are also available in the Macro menu.

Macros directory

Macros are created in a specific folder under the user's FreeCAD directory. This directory can be configured in the execute macro dialog, or in the Preferences Editor, through the menu Edit → Preferences → General → Macro → Macro recording settings.

Downloaded macros should also be placed in this directory.

Default directory

Macros can be simply copied into

$ROOT_DIR/

where $ROOT_DIR is a top level directory searched by FreeCAD on startup.

The $ROOT_DIR could be a system wide directory, in which case the macro is installed for all users.

  • On Linux it is usually /usr/share/freecad/
  • On Windows it is usually C:\Program Files\FreeCAD\
  • On Mac OSX it is usually /Applications/FreeCAD/

The $ROOT_DIR could be a particular user's directory.

  • On Linux it is usually /home/username/.FreeCAD/
  • On Windows it is usually C:\Users\username\Application Data\FreeCAD\
  • On Mac OSX it is usually /Users/username/Library/Preferences/FreeCAD/

Configuring the user directory

1. Open the menu Macro → Std DlgMacroExecuteDirect.svg Macros to open the Execute macro dialog.

Dxf Importer Install 01.png

Opening the macro dialog


2. Set the appropriate User macros location.

  • Linux: usually /home/username/.FreeCAD/
  • Windows: usually C:\Users\username\AppData\Roaming\FreeCAD\
  • MacOS: usually /Users/username/Library/Preferences/FreeCAD/
Dxf Importer Install 02.png

Setting of the macros directory


3. Navigate to that directory in your computer.

  • Linux: paste the address into your file manager, "Nautilus" or other. You may have to press Ctrl+H to make the hidden directory .FreeCAD/ visible.
  • Windows: paste the address into your "File explorer" and confirm.
  • MacOS: locate the folder in the "Finder" or paste the address into a "File explorer"; remember the file:/// prefix in the "File explorer" for a file on disk.
Dxf Importer Install 03.png

Accessing the macros directory in the operating system


4. Add macro files to this directory.

  • Linux: leave the file manager open, and bookmark the location for faster access.
  • Windows: leave open the file explorer.
  • MacOS: either leave a "Finder" window open, or bookmark the location in your "File explorer", or set up an "Alias" to point to it, or drag the folder into the "SideBar" of the "Finder" so it is there to use from other programs such as text editors.
Dxf Importer Install 04.png

Macros directory


Installing macros

Automatic method

Starting with FreeCAD 0.17, use the Addon Manager in Tools → Addon manager to install a macro that has been included in the FreeCAD-macros repository.

In past versions of FreeCAD you could use two automated ways to install macros and other addons:

  • addons_installer.FCMacro: itself a macro, this was the precursor to the Addon Manager, and is hosted in the FreeCAD-addons repository. You don't need to use this tool in new installations of FreeCAD.
  • freecad-pluginloader: also a macro, it could be used to install new components to FreeCAD. It is no longer developed.

The recommended way to install addons, that is, external workbenches and macros, is the Addon Manager. However, you can still add macros to your system with the manual methods described in the following sections; this is useful if you are developing and testing your own code.

Manual method 1. Copy the code to the macro editor

For macros that are relatively small, 300 lines or less, the code can be copied and pasted directly into the FreeCAD macro editor.

We will use Part Prism Apothem.svg Macro Apothem Based Prism GUI as an example.

1. Go to the macro wiki page, which should be listed in Macros recipes.

If there is a custom icon download it; click on it with the right mouse button and select Save image as...; place the icon in the macros directory. This icon can be used as a shortcut for the macro in a custom toolbar. The default icon is Text-x-python.png.

Macro Install HowTo 28.png

Downloading the icon from the macro page


2. In the macro page, select the code inside the Script or Macro sections, and copy it.

3. In FreeCAD, open the menu Macro → Std DlgMacroExecuteDirect.svg Macros to open the Execute macro dialog.

Dxf Importer Install 01.png

Opening the execute macro dialog


4. Click Create.

Macro Install HowTo 17.png

Creating a new macro


5. Enter the macro name, here Macro_Apothem_Based_Prism_GUI, and press OK.

Macro Install HowTo 18.png

Entering the macro name


6. The macro editor opens, showing the full path of the new macro.

Macro Install HowTo 19.png

The macro editor


7. Paste the code in the editor window, and then click the cross on the tab to close the window.

Macro Install HowTo 20.png

Closing the macro editor


8. A window appears asking for confirmation to save the code; click on Yes. You can also use Ctrl+S to save the file.

Restart FreeCAD to correctly register the new macro.

Macro Install HowTo 27.png

Asking for confirmation to save the code


9. Open the menu again, Macro → Std DlgMacroExecuteDirect.svg Macros, select the new macro and press Execute.

Macro Install HowTo 21.png

Selecting the macro to run it


10. The macro now runs. Fill in the fields with your values and click the OK button.

Macro Install HowTo 22.png

The macro in action; fill in the information and press OK when ready


11. This macro should return an error if no document is active; other macros open a new document if none exists.

Create a new document with File → Document-new.svg New, and then repeat the previous steps to execute the macro.

Macro Install HowTo 23.png

The macro returning an error if no document is active


12. Once an active document is available, the macro runs and creates an object.

Macro Install HowTo 24.png

Object created by the macro


13. You can open the macro in the editor again to run it or modify it. Go to Macro → Std DlgMacroExecuteDirect.svg Macros, select the macro and press Edit.

Macro Install HowTo 25.png

Opening the macro in the editor


14. The macro can now be run with Macro → Macro-execute.svg Execute macro, or by clicking on the Macro-execute.svg Execute button in the toolbar.

Macro Install HowTo 26.png

Running the macro that is loaded in the editor


Manual method 2. Add a macro file from a compressed .zip file

Some macros are too big that it's inconvenient to copy and paste them into the macro editor, or they cannot be hosted in the wiki. In this case, the code may be hosted somewhere else, in a Github repository, or in the FreeCAD forum. The code may also be compressed into a .zip file, tarball .tar.xz, or other type of archive if it contains several files. If the code is distributed in this way, the archive should be extracted and the files placed in the macros directory.

We will use Text-x-python.png Macro screw maker as an example.

1. Download the compressed code from the forum, Screw Maker.

You need to use a decompressor to get the internal files.

  • For Windows you can use an application like 7-zip or L-Zarc or quickzip.
  • For Linux you can use a command from the terminal
unzip your_file.zip -d your_directory

2. Download the compressed archive with the macro code to a local folder.

Macro Install HowTo 01.png

Downloading the compressed archive to a local directory


3. Decompress the file in the folder.

Macro Install HowTo 02.png

Decompressing the file in the folder


4. The decompressor creates a new directory with the unpacked files.

Macro Install HowTo 03.png

New directory created after unpacking the archive


5. Go inside the new directory, and copy or cut the macro file.

Macro Install HowTo 04.png

Entering the newly created directory with the decompressed macro file


6. Go to the macro directory and paste the file there.

Macro Install HowTo 05.png

Placing the macro file in the macro directory


7. In FreeCAD, open the menu Macro → Std DlgMacroExecuteDirect.svg Macros to open the Execute macro dialog.

Macro Install HowTo 06.png

Opening the execute macro dialog


8. Select the new macro and press Execute.

Macro Install HowTo 07.png

Selecting the macro to run it


9. The macro now runs. Select the desired options, and click the Create button.

Macro Install HowTo 08.png

The macro in action; select the desired options, and press Create when ready


Macro Install HowTo 30.png

Object created by the macro


Execute a macro in command line

Command line execute a macro (.FCMacro or .py)

on Windows

"C:\Program Files\FreeCAD\bin\FreeCAD.exe" "C:\Users\userName\AppData\Roaming\FreeCAD\Mod\WorkFeature\start_WF.FCMacro"

on Linux

todo

Errors in macros

Indentation errors

The white space at the beginning of the lines (indentation) in the Python programming language is very important, and an integral part of the code. An inappropriate space may cause the code to not run or present errors.

This section describes some errors that may be encountered when copying and pasting, and writing macro code.

A typical indentation error looks like this:

<unknown exception traceback><type 'exceptions.IndentationError'>: ('expected an indented block', ('C:/Users/d/AppData/Roaming/FreeCAD/Macro_Apothem_Based_Prism_GUI.FCMacro', 21, 3, 'def priSm(self):\n'))

Example 1

If the code lacks any indentation, the code won't work. Class (class) and function definitions (def()), as well as control structures (if, while, for) should be followed by a block of indented code.

This error is possible if the user doesn't copy the code correctly, and all spaces are accidentally removed.

Macro Install HowTo 09.png

Python code that lacks any indentation; it will cause an error when it's run


Indentation problem fixed.

Macro Install HowTo 10.png

Python code with the correct indentation


If the code is selected, all lines should be highlighted all the way to the left edge, indicating that the lines are aligned.

Macro Install HowTo 11.png

Python code highlighted, showing that all lines start at the left edge


Example 2

If an additional space is introduced at the beginning of all lines, the Python interpreter will fail and complain about unnecessary indentation. In this case, all lines need the initial space removed.

Macro Install HowTo 12.png

Python code with additional space on each line


Example 3

Here the code has been copied from a forum thread by using the Select all button. Apparently the selection is good.

Macro Install HowTo 14.png

Python code copied from a forum


However, when the selection is pasted into the macro editor, undesirable indentation seems to appear.

Macro Install HowTo 15.png

Python code copied from a forum into the macro editor; unnecessary indentation is added


In this case, the initial spaces need to be removed. This can be done with a specialized text editor to quickly decrease the indentation of the lines.

In Windows, Notepad++ can perform selection with Alt + Mouse dragging, and then use Edit → Indent → Decrease the indentation.

Macro Install HowTo 16.png

Python code with the correct indentation


Example 4

Here the selection also selects the line numbers in the code example. If this selection is pasted into the macro editor, it won't work. All line numbers need to be removed, and the spaces adjusted so that the Python code has the proper indentation.

Macro Install HowTo 29.png

Selection that also selects the line numbers; if this code is pasted into the macro editor, it won't work


Good code

Macro Install HowTo 13.png

Python code with the correct indentation


No text output from macros

Macros may output information to the report view to detail what the code is doing when it is running.

If no information is displayed, make sure the report view and Python console are visible, and that the output is directed tot he report view.

Printing information

FreeCAD macros have two methods to print information to the report view.

The FreeCAD functions

FreeCAD.Console.PrintMessage("Hello World! \n")
FreeCAD.Console.PrintError("Hello World! \n")
FreeCAD.Console.PrintWarning("Hello World! \n")

The simple Python function

print("Hello World!")

Enabling the report view

To see the information displayed in the console you should:

1. Go to the menu View → Panels.

Macro Install HowTo 31.png
Macro Install HowTo 32.png

Making the panels visible in the menu View → Panels


2. Enable the Report view and the Python console.

Macro Install HowTo 33.png

Enabling the report view and the Python console


3. The panels are now visible, and commands like FreeCAD.Console.PrintMessage() now print information that appears in the Report view.

Macro Install HowTo 34.png

FreeCAD main window with the Report view and the Python console


Enabling the print() command

FreeCAD may need to be configured so the print() function of Python redirects its output correctly to the report view.

1. Go into the Preferences Editor with the menu Edit → Preferences.

Macro Install HowTo 35.png

Going into the preferences editor


2. Go to General section, and then Output window → Python interpreter.

Macro Install HowTo 36.png

Output window preferences


3. Check both boxes:

  • Case a cocher O.png Redirect internal Python output to report view
  • Case a cocher O.png Redirect internal Python errors to report view

and then press the OK button.

Macro Install HowTo 37.png

Redirecting the Python output to the report view


Macro Install HowTo 38.png

Python commands printing information to the report view