How to install macros: Difference between revisions

From FreeCAD Documentation
(Marked this version for translation)
(Marked this version for translation)
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<languages/>
<translate>
<translate>
<!--T:1-->
<!--T:1-->
{{TutorialInfo
{{TutorialInfo|Topic=Programming|Level=Medium programmer|Time=15 minutes|FCVersion=All|Author=[[User:Mario52|Mario52]]}}
|Topic=Programming
|Level=Medium programmer
|Time=15 minutes
|FCVersion=All
|Author=[[User:Mario52|Mario52]]
}}


==Description== <!--T:2-->
==Description== <!--T:2-->
This short tutorial will show you how to install and use FreeCAD Macros.
This page will show you how to install and use FreeCAD [[macros|macros]].


<!--T:3-->
<!--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.
This tutorial was made on the Windows platform so a small adjustment may be necessary for other systems.
It was updated to cover the Macintosh platform.


==Python== <!--T:4-->
<!--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.
Python is a programming language which is very simple to use and very fast to [[Introduction_to_Python|learn]].


<!--T:5-->
<!--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.
Python files have a file extension of '''.py''' while FreeCAD macros (which are also Python files) use the file extension '''.FCMacro'''. The Macro facility of FreeCAD can also execute files with the extension '''.py'''. Python was invented by [https://www.python.org/~guido/ Guido Van Rossun]


<!--T:143-->
==The Macro Menu and Toolbar Icons== <!--T:6-->
Here is a video on [https://wiki.opensourceecology.org/wiki/Installing_Macros_in_FreeCAD installing FreeCAD macros in Ubuntu].


<!--T:7-->
==The Macro menu and toolbar== <!--T:6-->
[[Image:Macro-record.svg|32px|record]] [[Std_DlgMacroRecord|Launch Record]]
[[Image:Macro-stop.svg|32px|stop]] [[Std_DebugStop|Stop Record]]
[[Image:Std_DlgMacroExecuteDirect.svg|32px|open editor]] [[Std_DlgMacroExecuteDirect|Open Menu macro]]
[[Image:Macro-execute.svg|32px|execute]] [[Std_DlgMacroExecute|Execute]]
[[Image:Debug-start.svg|32px|start]] [[Std_DebugMacro|Debug Macro]]
[[Image:Debug-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]


==Location and destination of macros== <!--T:8-->
===Toolbar=== <!--T:7-->
* [[Image:Macro-record.svg|32px|record]] [[Std_DlgMacroRecord|Record]]
* [[Image:Macro-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]
* [[Image:Std_DlgMacroExecuteDirect.svg|32px|open editor]] [[Std_DlgMacroExecuteDirect|Execute macro dialog]]
* [[Image:Macro-execute.svg|32px|execute]] [[Std_DlgMacroExecute|Execute]]


<!--T:9-->
===Menu=== <!--T:144-->
Besides the tools in the toolbar, the following functions are also available in the {{MenuCommand|Macro}} menu.
This section shows you how to locate the folder containing your FreeCAD macros.
* [[Image:Debug-start.svg|32px|start]] [[Std_DebugMacro|Debug Start]]
* [[Image:Debug-stop.svg|32px|stop]] [[Std_DebugStop|Debug Stop]]
* Step over
* Step into
* Toggle breakpoint


<!--T:10-->
==Macros directory== <!--T:8-->
'''1 :''' click '''Menu > Macro > Macros''' (which has the icon [[File:Std DlgMacroExecuteDirect.svg|24px]] and the tool tip "Open a dialog to let you execute a recorded macro")


</translate>
<!--T:11-->
<div class="toccolours mw-collapsible mw-collapsed">
[[File:Dxf_Importer_Install_01.png|640px|center]]
<translate>


<!--T:12-->
<!--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}}.
'''2 :''' This will open a dialog box


<!--T:13-->
<!--T:146-->
Downloaded macros should also be placed in this directory.
'''3 :''' The address of "Macro destination" ('''C:\Users\your_user_name\AppData\Roaming\FreeCAD\''' in the screen snapshot below)
* Windows: the form is usually '''drive:\Users\your_user_name\AppData\Roaming\FreeCAD\'''
* Ubuntu: the form is usually '''/home/your_user_name/.FreeCAD'''
* Macintosh: the form is usually "/Users/your_user_name/Library/Preferences/FreeCAD"


</translate>
<!--T:15-->
'''4 :''' copy the address of "Macro destination" (here '''C:\Users\your_user_name\AppData\Roaming\FreeCAD\''')

<!--T:16-->
[[File:Dxf_Importer_Install_02.png|640px|center]]

<!--T:17-->
'''5 :''' View the macro folder by:
* Windows: paste the address into your File explorer and confirm
* Macintosh: 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)

<!--T:18-->
[[File:Dxf_Importer_Install_03.png|640px|center]]

<!--T:19-->
'''6 :''' Access the files by:
*Windows: leave open the file explorer
*Macintosh: 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 etc. (Note: version 0.14 of FreeCAD does not support Aliases but does support the SideBar)

<!--T:20-->
[[File:Dxf_Importer_Install_04.png|640px|center]]
{{clear}}


<!--T:21-->
'''Section to read if you are not familiar with Python programming (common mistakes copy/paste).'''

<!--T:136-->
<div class="toccolours mw-collapsible mw-collapsed">

==The indentation errors examples wrong code== <!--T:22-->
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<translate>


<!--T:23-->
=== Default directory === <!--T:147-->
Macros can be simply copied into
The indentation in the python programming is very important and integral part of the code, a space or an inappropriate shift causes an indentation error ex :


</translate>
<!--T:24-->
{{Code|code=
<span style="color:red">
$ROOT_DIR/
'''<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'))'''
}}
</span>
<translate>


<!--T:25-->
<!--T:148-->
where {{incode|$ROOT_DIR}} is a top level directory searched by FreeCAD on startup.
gives the error returned 'expected an indented block' block indentation expected to line '''21, 3,''' normal after a '''class p ():''' must be the next line an indentation at least one space.


<!--T:26-->
<!--T:149-->
The {{incode|$ROOT_DIR}} could be a system wide directory, in which case the macro is installed for all users.
'''1 :''' In this example, the code was stuck without any indentation and of course does not work! here definitely a programmer error when pasting the code on the page as it would have never known it to work.
* 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:27-->
<!--T:150-->
The {{incode|$ROOT_DIR}} could be a particular user's directory.
[[Image:Macro Install HowTo 09.png|center|the code was stuck without any indentation]]
* 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-->
<!--T:28-->
1. Open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}} to open the [[Std_DlgMacroExecuteDirect|Execute macro dialog]].
'''2 :''' the code was correct indentations in the right place.


<!--T:29-->
<!--T:11-->
[[Image:Macro Install HowTo 10.png|center|the code was correct indentations in the right place]]
[[Image:Dxf_Importer_Install_01.png|center]]
{{Caption|align=center|Opening the macro dialog}}


<!--T:30-->
<!--T:13-->
2. Set the appropriate {{incode|User macros location}}.
'''3 :''' we select the code, and we see that the selection is at the edge of the code, the macro must works so good
* Linux: usually {{incode|/home/username/.FreeCAD/}}
* Windows: usually {{incode|C:\Users\username\AppData\Roaming\FreeCAD\}}
* MacOS: usually {{incode|/Users/username/Library/Preferences/FreeCAD/}}


<!--T:31-->
<!--T:16-->
[[Image:Macro Install HowTo 11.png|center|the macro must works so good]]
[[Image:Dxf_Importer_Install_02.png|center]]
{{Caption|align=center|Setting of the macros directory}}


<!--T:32-->
<!--T:17-->
3. Navigate to that directory in your computer.
'''4 :''' Here additional space is selected (it can happen) then you need to copy the code into a word processor to remove '''one space all lines'''
* Linux: paste the address into your file manager, "Nautilus" or other. You may have to press {{KEY|Ctrl}}+{{KEY|H}} to make the hidden directory {{incode|.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 {{incode|file:///}} prefix in the "File explorer" for a file on disk.


<!--T:33-->
<!--T:18-->
[[Image:Macro Install HowTo 12.png|center|remove one space all lines]]
[[Image:Dxf_Importer_Install_03.png|center]]
{{Caption|align=center|Accessing the macros directory in the operating system}}


<!--T:34-->
<!--T:19-->
4. Add macro files to this directory.
'''5 :''' Here the code has been copied in a forum window with the {{KEY|Select all}} button apparently the selection is good
* 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.


<!--T:35-->
<!--T:20-->
[[Image:Macro Install HowTo 14.png|center|640px|Here the code has been copied in a forum]]
[[Image:Dxf_Importer_Install_04.png|center]]
{{Caption|align=center|Macros directory}}


</translate>
<!--T:36-->
{{clear}}
'''6 :''' But the selection pasted into the FreeCAD editor gives a surprise, an indent of four spaces has been added by the system ? the code is not good
</div>
</div>
<translate>


<!--T:37-->
== Installing macros == <!--T:152-->
[[Image:Macro Install HowTo 15.png|center|640px|But the selection pasted into the FreeCAD editor gives a surprise]]


</translate>
<!--T:38-->
<div class="toccolours mw-collapsible mw-collapsed">
'''7 :''' You must delete all the extra space that is four spaces on each line, for Windows word processing [http://notepad-plus-plus.org/ notepad-plus-plus] enables vertical selection with a combination of buttons {{KEY|Alt}} + Mouse dragging or Menu> Edit> Indent> Decrease the indentation
<translate>


<!--T:39-->
=== Automatic method === <!--T:153-->
[[Image:Macro Install HowTo 16.png|center|640px|You must delete all the extra space]]


<!--T:40-->
<!--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.
'''8 :''' Here the selection also take the column numbers which will also give an error


</translate>
<!--T:41-->
<div class="mw-collapsible-content">
[[Image:Macro_Install_HowTo_29.png|center|640px|Here the selection also take the column numbers]]
<translate>


<!--T:42-->
<!--T:179-->
In past versions of FreeCAD you could use two automated ways to install macros and other addons:
'''9 :''' Perfect code.
* [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:43-->
<!--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.
[[Image:Macro Install HowTo 13.png|center|Perfect code]]


</translate>
<!--T:44-->
<br />
</div>
</div>
</div>
</div>

<!--T:45-->
'''Method 1:'''


<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
<translate>
==Method copy the code in one window and paste in the FreeCAD editor== <!--T:46-->

===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.

</translate>
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<translate>


<!--T:47-->
<!--T:180-->
For example we will copy the macro [[File:Part_Prism_Apothem.svg|16px]] [[Macro_Apothem_Based_Prism_GUI|Macro_Apothem_Based_Prism_GUI]]
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|Macros recipes]].
If there are one or more icon (s) download also position your mouse over the icon, click the right mouse button and click "Save image as ..." the icon or icons are placed in the macro directory and one of these icons serve as a shortcut icon to place on the [[Customize_Toolbars|toolbar.]]

<!--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]].


<!--T:49-->
<!--T:49-->
[[File:Macro Install HowTo 28.png|300px|center|Download icon]]
[[File:Macro Install HowTo 28.png|center]]
{{Caption|align=center|Downloading the icon from the macro page}}


<!--T:50-->
<!--T:50-->
2. In the macro page, select the code inside the {{Emphasis|Script}} or {{Emphasis|Macro}} sections, and copy it.
After copying your code we will paste the code in FreeCAD editor.


<!--T:51-->
<!--T:51-->
3. In FreeCAD, open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}} to open the [[Std_DlgMacroExecuteDirect|Execute macro dialog]].
'''1 :''' Open FreeCAD and open the editor in FreeCAD


<!--T:52-->
<!--T:52-->
[[File:Dxf_Importer_Install_01.png|640px|center]]
[[Image:Dxf_Importer_Install_01.png|center]]
{{Caption|align=center|Opening the execute macro dialog}}


<!--T:53-->
<!--T:53-->
4. Click {{Button|Create}}.
'''2 :''' The window macros file opens, click {{KEY|Create}} button


<!--T:54-->
<!--T:54-->
[[File:Macro Install HowTo 17.png|300px|center|The window macros file opens]]
[[Image:Macro Install HowTo 17.png|center]]
{{Caption|align=center|Creating a new macro}}


<!--T:55-->
<!--T:55-->
'''3 :''' A new window opens, enter the macro name (here "'''Macro_Apothem_Based_Prism_GUI'''")and click the create {{KEY|Ok}} button
5. Enter the macro name, here {{incode|Macro_Apothem_Based_Prism_GUI}}, and press {{Button|OK}}.


<!--T:56-->
<!--T:56-->
[[File:Macro Install HowTo 18.png|300px|center|enter the macro name]]
[[Image:Macro Install HowTo 18.png|center]]
{{Caption|align=center|Entering the macro name}}


<!--T:57-->
<!--T:57-->
'''4 :''' The editing window FreeCAD macros is now available and has the name of our future macro.
6. The macro editor opens, showing the full path of the new macro.


<!--T:58-->
<!--T:58-->
[[File:Macro Install HowTo 19.png|640px|center|The editing window FreeCAD macros]]
[[Image:Macro Install HowTo 19.png|center]]
{{Caption|align=center|The macro editor}}


<!--T:59-->
<!--T:59-->
'''5 :''' Paste your code in the macro editor window and click the little '''cross''' to close the window.
7. Paste the code in the editor window, and then click the cross on the tab to close the window.


<!--T:60-->
<!--T:60-->
[[File:Macro Install HowTo 20.png|640px|center|close the window]]
[[Image:Macro Install HowTo 20.png|center]]
{{Caption|align=center|Closing the macro editor}}


<!--T:61-->
<!--T:61-->
'''6 :''' A warning window appears asking for confirmation of save code, click on {{KEY|Yes}}
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:62-->
<!--T:160-->
Restart FreeCAD to correctly register the new macro.
'''PS:''' For the change silk consideration must save the macro and reopen it and just run it.


<!--T:63-->
<!--T:63-->
[[File:Macro Install HowTo 27.png|300px|center|A warning window appears asking for confirmation of save code]]
[[Image:Macro Install HowTo 27.png|center]]
{{Caption|align=center|Asking for confirmation to save the code}}


<!--T:64-->
<!--T:64-->
'''7 :''' Repeat the number '''1 :''' , Click on your new macro and button {{KEY|Execute}}
9. Open the menu again, {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}}, select the new macro and press {{Button|Execute}}.


<!--T:65-->
<!--T:65-->
[[File:Macro Install HowTo 21.png|300px|center|Click on your new macro and button Execute]]
[[Image:Macro Install HowTo 21.png|center]]
{{Caption|align=center|Selecting the macro to run it}}


<!--T:66-->
<!--T:66-->
'''8 :''' The macro runs, complete the fields with your values and click the {{KEY|OK}} button
10. The macro now runs. Fill in the fields with your values and click the {{Button|OK}} button.


<!--T:67-->
<!--T:67-->
[[File:Macro Install HowTo 22.png|640px|center|The macro runs, complete the fields]]
[[Image:Macro Install HowTo 22.png|center]]
{{Caption|align=center|The macro in action; fill in the information and press OK when ready}}


<!--T:68-->
<!--T:68-->
9 : The macro returns an error ! we do not have to open document, open a document [[File:Document-new.svg|24px]] and repeat the operation '''7''' and '''8'''. Some macros open a new document if it does not find one.
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.


<!--T:69-->
<!--T:69-->
[[File:Macro Install HowTo 23.png|640px|center|The macro returns an error!]]
[[Image:Macro Install HowTo 23.png|center|]]
{{Caption|align=center|The macro returning an error if no document is active}}


<!--T:70-->
<!--T:70-->
12. Once an active document is available, the macro runs and creates an object.
10 : Here is your prism


<!--T:71-->
<!--T:71-->
[[File:Macro Install HowTo 24.png|640px|center|your prism]]
[[Image:Macro Install HowTo 24.png|center]]
{{Caption|align=center|Object created by the macro}}


<!--T:72-->
<!--T:72-->
11 : You can also open your macro in the editor to run or modify, click the {{KEY|Edit}} button
13. You can open the macro in the editor again to run it or modify it. Go to {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}}, select the macro and press {{Button|Edit}}.


<!--T:73-->
<!--T:73-->
[[File:Macro Install HowTo 25.png|300px|center|You can also open your macro in the editor]]
[[Image:Macro Install HowTo 25.png|center]]
{{Caption|align=center|Opening the macro in the editor}}


<!--T:74-->
<!--T:74-->
12: The macro is now in the FreeCAD editor you can run through the menu "Macro Run Macro" or by clicking on the triangle [[File:Macro-execute.svg|16px]] green in the macros toolsbar
14. The macro can now be run with {{MenuCommand|Macro [[Image:Macro-execute.svg|16px]] [[Std_DlgMacroExecute|Execute macro]]}}, or by clicking on the {{Button|[[Image:Macro-execute.svg|16px]] [[Std_DlgMacroExecute|Execute]]}} button in the toolbar.


<!--T:75-->
<!--T:75-->
[[File:Macro Install HowTo 26.png|640px|center|The macro is now in the FreeCAD editor]]
[[Image:Macro Install HowTo 26.png|center]]
{{Caption|align=center|Running the macro that is loaded in the editor}}
</translate>


<!--T:76-->
</div>
</div>
</div>
</div>

<!--T:77-->
'''Method 2:'''


<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
<translate>
==Method macro in a compressed .ZIP file== <!--T:78-->

<div class="mw-collapsible-content">
===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 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.
Download the file compressed here (example) [http://forum.freecadweb.org/viewtopic.php?f=22&t=6558#p52887 Macro_screw_maker 1.7]. (the page [[Macro_screw_maker1_2|Macro_screw_maker]])


</translate>
<!--T:80-->
<div class="mw-collapsible-content">
Free for Windows [http://www.7-zip.org/ 7-zip] ou [http://www.kanmandet.dk/?p=37 L-Zarc] ou [http://www.quickzip.org/quickzip51.html quickzip]
<translate>


<!--T:81-->
<!--T:181-->
We will use [[Image:Text-x-python.png|24px]] [[Macro_screw_maker1_2|Macro screw maker]] as an example.
For Linux use this on the terminal


<!--T:82-->
<!--T:164-->
1. Download the compressed code from the forum, [http://forum.freecadweb.org/viewtopic.php?f=22&t=6558#p52887 Screw Maker].

<!--T:80-->
You need to use a decompressor to get the internal files.
* 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

</translate>
{{Code|code=
{{Code|code=
unzip your_file.zip -d your_directory
unzip your_file.zip -d your_directory
}}
}}
<translate>

<!--T:83-->
you must unzip the zip and copy the file (or all the files and folders) in your macro directory.

<!--T:84-->
'''Process'''


<!--T:85-->
<!--T:85-->
'''1 :''' Download your file in your local folder here the folder '''Temp'''
2. Download the compressed archive with the macro code to a local folder.


<!--T:86-->
<!--T:86-->
[[Image:Macro Install HowTo 01.png|center|640px|Download your file in your local folder here the folder Temp]]
[[Image:Macro Install HowTo 01.png|center]]
{{Caption|align=center|Downloading the compressed archive to a local directory}}


<!--T:87-->
<!--T:87-->
'''2 :''' Unzip your file in the folder.
3. Decompress the file in the folder.


<!--T:88-->
<!--T:88-->
[[Image:Macro Install HowTo 02.png|center|640px|Unzip your file in the folder.
[[Image:Macro Install HowTo 02.png|center]]
{{Caption|align=center|Decompressing the file in the folder}}
]]


<!--T:89-->
<!--T:89-->
'''3 :''' The decompressor finished his work and created a new folder with the unpacked file
4. The decompressor creates a new directory with the unpacked files.


<!--T:90-->
<!--T:90-->
[[Image:Macro Install HowTo 03.png|center|640px|The decompressor finished his work and created a new folder with the unpacked file]]
[[Image:Macro Install HowTo 03.png|center]]
{{Caption|align=center|New directory created after unpacking the archive}}


<!--T:91-->
<!--T:91-->
'''4 :''' Enter in the newly created directory, move about the file, click the right mouse button and click on '''Cut'''.
5. Go inside the new directory, and copy or cut the macro file.


<!--T:92-->
<!--T:92-->
[[Image:Macro Install HowTo 04.png|center|640px|Enter in the newly created directory]]
[[Image:Macro Install HowTo 04.png|center]]
{{Caption|align=center|Entering the newly created directory with the decompressed macro file}}


<!--T:93-->
<!--T:93-->
6. Go to the macro directory and paste the file there.
'''5 :''' Return to your File explorer remained open in the macro location (here '''C:\Users\your_user_name\AppData\Roaming\FreeCAD\''') and close the File explorer.


<!--T:94-->
<!--T:94-->
[[Image:Macro Install HowTo 05.png|center|640px|Return to your File explorer remained open]]
[[Image:Macro Install HowTo 05.png|center]]
{{Caption|align=center|Placing the macro file in the macro directory}}


<!--T:95-->
<!--T:95-->
7. In FreeCAD, open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] [[Std_DlgMacroExecuteDirect|Macros]]}} to open the [[Std_DlgMacroExecuteDirect|Execute macro dialog]].
'''6 :''' Open FreeCAD click '''Menu > Macro > Macros''' or the click the {{KEY|bottom}} [[File:Std_DlgMacroExecuteDirect.svg|18px]] "Open a dialog to let you execute a macro Recorded"


<!--T:96-->
<!--T:96-->
[[Image:Macro Install HowTo 06.png|center|640px|Open FreeCAD]]
[[Image:Macro Install HowTo 06.png|center]]
{{Caption|align=center|Opening the execute macro dialog}}


<!--T:97-->
<!--T:97-->
'''7 :''' The macros window open , select your macro and click the button {{KEY|Execute}}
8. Select the new macro and press {{Button|Execute}}.


<!--T:98-->
<!--T:98-->
[[Image:Macro Install HowTo 07.png|center|640px|The macros window open]]
[[Image:Macro Install HowTo 07.png|center]]
{{Caption|align=center|Selecting the macro to run it}}


<!--T:99-->
<!--T:99-->
'''8 :''' Your macro is executed enter the data and click the {{KEY|Create}} button
9. The macro now runs. Select the desired options, and click the {{Button|Create}} button.


<!--T:100-->
<!--T:100-->
[[Image:Macro Install HowTo 08.png|center|640px|Your macro is executed]]
[[Image:Macro Install HowTo 08.png|center|640px]]
{{Caption|align=center|The macro in action; select the desired options, and press Create when ready}}

<!--T:101-->
'''9''' : Whaouu


<!--T:102-->
<!--T:102-->
[[Image:Macro Install HowTo 30.png|center|640px|Whaouu]]
[[Image:Macro Install HowTo 30.png|center]]
{{Caption|align=center|Object created by the macro}}


</translate>
<!--T:103-->
</div>
</div>
</div>
</div>
<translate>


== Execute a macro in command line == <!--T:182-->
<!--T:107-->
'''Section if the text are not display:'''


</translate>
<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
<translate>
==For those who see no information is displayed.== <!--T:108-->

<!--T:183-->
Command line execute a macro (.FCMacro or .py)

</translate>
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<translate>
Some macros display information on the screen in general they are displayed in the report view.


<!--T:109-->
<!--T:184-->
on Windows
FreeCAD use two methods to display the information in the window view report.


<!--T:110-->
'''1 : Commands'''
</translate>
</translate>
{{Code|code=
{{Code|code=
"C:\Program Files\FreeCAD\bin\FreeCAD.exe" "C:\Users\userName\AppData\Roaming\FreeCAD\Mod\WorkFeature\start_WF.FCMacro"
App = FreeCAD
App.Console.PrintMessage ("Hello World ! \n")
App.Console.PrintError ("Hello World ! \n")
App.Console.PrintWarning ("Hello World ! \n")
}}
}}
<translate>
<translate>

<!--T:111-->
<!--T:185-->
or
on Linux

</translate>
</translate>
{{Code|code=
{{Code|code=
todo
FreeCAD.Console.PrintMessage ("Hello World ! \n")
FreeCAD.Console.PrintError ("Hello World ! \n")
FreeCAD.Console.PrintWarning ("Hello World ! \n")
}}
}}

</div>
</div>
<translate>
<translate>

== Errors in macros == <!--T:165-->

<!--T:136-->
<div class="toccolours mw-collapsible mw-collapsed">

=== Indentation errors === <!--T:22-->

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

<!--T:167-->
<div class="mw-collapsible-content">

<!--T:24-->
A typical indentation error looks like this:

</translate>
{{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'))
}}
<translate>

==== Example 1 ==== <!--T:26-->

<!--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.

<!--T:170-->
This error is possible if the user doesn't copy the code correctly, and all spaces are accidentally removed.

<!--T:27-->
[[Image:Macro Install HowTo 09.png|center]]
{{Caption|align=center|Python code that lacks any indentation; it will cause an error when it's run}}

<!--T:28-->
Indentation problem fixed.

<!--T:29-->
[[Image:Macro Install HowTo 10.png|center]]
{{Caption|align=center|Python code with the correct indentation}}

<!--T:30-->
If the code is selected, all lines should be highlighted all the way to the left edge, indicating that the lines are aligned.

<!--T:31-->
[[Image:Macro Install HowTo 11.png|center]]
{{Caption|align=center|Python code highlighted, showing that all lines start at the left edge}}

==== Example 2 ==== <!--T:32-->

<!--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.

<!--T:33-->
[[Image:Macro Install HowTo 12.png|center]]
{{Caption|align=center|Python code with additional space on each line}}

==== Example 3 ==== <!--T:34-->

<!--T:172-->
Here the code has been copied from a forum thread by using the {{Button|Select all}} button. Apparently the selection is good.

<!--T:35-->
[[Image:Macro Install HowTo 14.png|center]]
{{Caption|align=center|Python code copied from a forum}}

<!--T:36-->
However, when the selection is pasted into the macro editor, undesirable indentation seems to appear.

<!--T:37-->
[[Image:Macro Install HowTo 15.png|center]]
{{Caption|align=center|Python code copied from a forum into the macro editor; unnecessary indentation is added}}

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

<!--T:39-->
[[Image:Macro Install HowTo 16.png|center]]
{{Caption|align=center|Python code with the correct indentation}}

==== Example 4 ==== <!--T:40-->

<!--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.

<!--T:41-->
[[Image:Macro_Install_HowTo_29.png|center]]
{{Caption|align=center|Selection that also selects the line numbers; if this code is pasted into the macro editor, it won't work}}

==== Good code ==== <!--T:42-->

<!--T:43-->
[[Image:Macro Install HowTo 13.png|center]]
{{Caption|align=center|Python code with the correct indentation}}

</translate>
</div>
</div>

<div class="toccolours mw-collapsible mw-collapsed">
<translate>

===No text output from macros=== <!--T:108-->

<!--T:175-->
Macros may output information to the report view to detail what the code is doing when it is running.

<!--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">
<translate>

==== Printing information ==== <!--T:177-->
FreeCAD macros have two methods to print information to the report view.

<!--T:110-->
The FreeCAD functions
</translate>

{{Code|code=
FreeCAD.Console.PrintMessage("Hello World! \n")
FreeCAD.Console.PrintError("Hello World! \n")
FreeCAD.Console.PrintWarning("Hello World! \n")
}}
<translate>

<!--T:137-->
<!--T:137-->
The simple Python function
or
</translate>
</translate>

{{Code|code=
{{Code|code=
print "Hello World !"
print("Hello World!")
}}
}}
<translate>
<translate>

<!--T:112-->
==== Enabling the report view ==== <!--T:112-->
To see the information displayed in the console you should:
To see the information displayed in the console you should:


<!--T:113-->
<!--T:113-->
1. Go to the menu {{MenuCommand|View → Panels}}.
'''1 :''' Open FreeCAD


<!--T:114-->
<!--T:114-->
[[Image:Macro Install HowTo 31.png|center|640px|Open FreeCAD]]
[[Image:Macro Install HowTo 31.png|center]]

<!--T:115-->
'''2 :''' Click the '''View menu''' and '''Views'''


<!--T:116-->
<!--T:116-->
[[Image:Macro Install HowTo 32.png|center|640px|Click the View menu and Views]]
[[Image:Macro Install HowTo 32.png|center]]
{{Caption|align=center|Making the panels visible in the menu View → Panels}}


<!--T:117-->
<!--T:117-->
'''3 :''' Check '''Report View''' and '''Python Console'''
2. Enable the {{incode|Report view}} and the {{incode|Python console}}.


<!--T:118-->
<!--T:118-->
[[Image:Macro Install HowTo 33.png|center|640px|Check Report View and Python Console]]
[[Image:Macro Install HowTo 33.png|center]]
{{Caption|align=center|Enabling the report view and the Python console}}


<!--T:119-->
<!--T:119-->
'''4 :''' the windows are enabled and available commands like "'''App.Console.PrintMessage'''" is configured to the "Report View"
3. The panels are now visible, and commands like {{incode|FreeCAD.Console.PrintMessage()}} now print information that appears in the {{incode|Report view}}.


<!--T:120-->
<!--T:120-->
[[Image:Macro Install HowTo 34.png|center|640px|Hello World!]]
[[Image:Macro Install HowTo 34.png|center]]
{{Caption|align=center|FreeCAD main window with the Report view and the Python console}}


<!--T:121-->
'''2 : command "print" which is a Python command.'''


==== Enabling the <tt>print()</tt> command ==== <!--T:121-->
<!--T:122-->
FreeCAD may need to be configured so the {{incode|print()}} function of [[Python|Python]] redirects its output correctly to the report view.
It is possible that this command does not display the expected string.

<!--T:123-->
For information to be displayed in the window do this :


<!--T:124-->
<!--T:124-->
'''1 :''' Click the '''Edit menu''' and then '''Preferences'''
1. Go into the [[Preferences Editor|Preferences Editor]] with the menu {{MenuCommand|Edit Preferences}}.


<!--T:125-->
<!--T:125-->
[[Image:Macro Install HowTo 35.png|center|640px|Edit menu]]
[[Image:Macro Install HowTo 35.png|center]]
{{Caption|align=center|Going into the preferences editor}}


<!--T:126-->
<!--T:126-->
2. Go to {{MenuCommand|General}} section, and then {{MenuCommand|Output window → Python interpreter}}.
'''2 :''' In the new window, click '''General''', and select the '''Output window''' tab


<!--T:127-->
<!--T:127-->
[[Image:Macro Install HowTo 36.png|center|640px|General]]
[[Image:Macro Install HowTo 36.png|center]]
{{Caption|align=center|Output window preferences}}


<!--T:128-->
<!--T:128-->
'''3 :''' check both boxes:
3. Check both boxes:


<!--T:129-->
<!--T:129-->
[[Image:Case_a_cocher_O.png|16px]] Redirect internal Python output to Report view
* [[Image:Case_a_cocher_O.png|16px]] Redirect internal Python output to report view


<!--T:130-->
<!--T:130-->
[[Image:Case_a_cocher_O.png|16px]] Redirect internal Python errors to Report view
* [[Image:Case_a_cocher_O.png|16px]] Redirect internal Python errors to report view


<!--T:131-->
<!--T:131-->
and click the {{KEY|OK}} button
and then press the {{Button|OK}} button.


<!--T:132-->
<!--T:132-->
[[Image:Macro Install HowTo 37.png|center|640px|Redirect internal]]
[[Image:Macro Install HowTo 37.png|center]]
{{Caption|align=center|Redirecting the Python output to the report view}}

<!--T:133-->
'''4 :''' the setup is complete you should see all.


<!--T:134-->
<!--T:134-->
[[Image:Macro Install HowTo 38.png|center|640px|the setup is complete]]
[[Image:Macro Install HowTo 38.png|center]]
{{Caption|align=center|Python commands printing information to the report view}}

</translate>
<!--T:135-->
</div>
</div>
</div>
</div>

<!--T:104-->
End tutorial good job.

==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>
<languages/>

Revision as of 18:20, 4 September 2019

Tutorial
Topic
Programming
Level
Medium programmer
Time to complete
15 minutes
Authors
Mario52
FreeCAD version
All
Example files
None
See also
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 → Macros to open the Execute macro dialog.

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/

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.

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.

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

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 → Macros to open the Execute macro dialog.

Opening the execute macro dialog

4. Click Create.

Creating a new macro

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

Entering the macro name

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

The macro editor

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

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.

Asking for confirmation to save the code

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

Selecting the macro to run it

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

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 → New, and then repeat the previous steps to execute the macro.

The macro returning an error if no document is active

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

Object created by the macro

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

Opening the macro in the editor

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

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

Downloading the compressed archive to a local directory

3. Decompress the file in the folder.

Decompressing the file in the folder

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

New directory created after unpacking the archive

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

Entering the newly created directory with the decompressed macro file

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

Placing the macro file in the macro directory

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

Opening the execute macro dialog

8. Select the new macro and press Execute.

Selecting the macro to run it

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

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

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.

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

Indentation problem fixed.

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.

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.

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.

Python code copied from a forum

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

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.

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.

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

Good code

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.

Making the panels visible in the menu View → Panels

2. Enable the Report view and the Python console.

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.

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.

Going into the preferences editor

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

Output window preferences

3. Check both boxes:

  • Redirect internal Python output to report view
  • Redirect internal Python errors to report view

and then press the OK button.

Redirecting the Python output to the report view

Python commands printing information to the report view