How to install macros/cs

Description
Since v0.17 it is easy to add macros by using the Addon Manager. A regular user doesn't need to do more than use this tool. Keep reading for more information regarding installation of 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 extension, FreeCAD macros should have the  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.

Toolbar

 * [[Image:Std_DlgMacroRecord.svg|32px]] Macro recording...
 * [[Image:Std_MacroStopRecord.svg|32px]] Stop macro recording
 * [[Image:Std_DlgMacroExecute.svg|32px]] Macros...
 * [[Image:Std_DlgMacroExecuteDirect.svg|32px]] Execute macro

Menu
Besides the tools in the toolbar, the following functions are also available in the menu.
 * Attach to remote debugger
 * [[Image:Std_MacroStartDebug.svg|32px]] Debug macro
 * [[Image:Std_MacroStopDebug.svg|32px]] Stop debugging
 * Step over
 * Step into
 * Toggle breakpoint

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.

Downloaded macros should also be placed in this directory.

Default directory
Macros can be simply copied into

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

The could be a system wide directory, in which case the macro is installed for all users.
 * On Linux it is usually
 * On Windows it is usually
 * On Mac OSX it is usually

The could be a particular user's directory.
 * On Linux it is usually
 * On Windows it is usually
 * On Mac OSX it is usually

Configuring the user directory
1. Open the menu to open the Execute macro dialog.



2. Set the appropriate.
 * Linux: usually
 * Windows: usually
 * MacOS: usually



3. Navigate to that directory in your computer.
 * Linux: paste the address into your file manager, "Nautilus" or other. You may have to press + to make the hidden directory 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 prefix in the "File explorer" for a file on disk.



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.



Automatic method
Starting with FreeCAD 0.17, use the Addon Manager in 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 ; 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.



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

3. In FreeCAD, open the menu to open the Execute macro dialog.



4. Click.



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



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



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



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

Restart FreeCAD to correctly register the new macro.



9. Open the menu again,, select the new macro and press.



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



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



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



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



14. The macro can now be run with, or by clicking on the button in the toolbar.



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 file, tarball, 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

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



3. Decompress the file in the folder.



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



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



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



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



8. Select the new macro and press.



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





Execute a macro in command line
Command line execute a macro (.FCMacro or .py)

on Windows

on Linux

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:

Example 1
If the code lacks any indentation, the code won't work. Class and function definitions, as well as control structures  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.



Indentation problem fixed.



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



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.



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



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



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 + Mouse dragging, and then use.



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.



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

The simple Python function

Enabling the report view
To see the information displayed in the console you should:

1. Go to the menu.





2. Enable the and the.



3. The panels are now visible, and commands like now print information that appears in the.



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

1. Go into the Preferences Editor with the menu.



2. Go to section, and then.



3. Check both boxes:


 * [[Image:Case_a_cocher_O.png|16px]] Redirect internal Python output to report view


 * [[Image:Case_a_cocher_O.png|16px]] Redirect internal Python errors to report view

and then press the button.