Difference between revisions of "How to install macros"

From FreeCAD Documentation
Jump to: navigation, search
(Put the information inside a collapsible element, so the user doesn't have to scroll to reach the rest of the information)
(Removed excessive use of the word "macro")
Line 90: Line 90:
 
===Method 1. Copy the code to the macro editor=== <!--T:46-->
 
===Method 1. Copy the code to the macro editor=== <!--T:46-->
  
For macros that are small, the code can be copied and pasted directly into the macro editor to create a new macro.
+
For macros that are small, the code can be copied and pasted directly into the FreeCAD macro editor.
  
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">

Revision as of 04:10, 5 December 2018

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 short tutorial will show you how to install and use FreeCAD macros.

Macros are sequences of commands or drawing actions which are used to perform a complex 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, macros should have the .FCMacro extension.

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.

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.

External macros should also be placed in this 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/your_user_name/.FreeCAD
  • Windows: usually C:\Users\your_user_name\AppData\Roaming\FreeCAD\
  • MacOS: usually /Users/your_user_name/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


Adding macros

Method 1. Copy the code to the macro editor

For macros that are small, 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 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

The execute macro dialog


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 at the bottom of 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 your 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. Create a new document with File → Document-new.svg New, then repeat the previous steps to execute the macro again. Some macros open a new document if none exists.

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

Macro Install HowTo 24.png

Prism 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

Method 2. Add a macro file from a compressed .zip file

Some macros are too big that they cannot be hosted on the wiki. In this case the code may be placed somewhere else, in a Github repository, or in the FreeCAD forum. Moreover it may be compressed into a .zip file, .tar.xz, or other archive.

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

Errors in the macros

Indentation errors

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 :

<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'))

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.

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.

the code was stuck without any indentation

2 : the code was correct indentations in the right place.

the code was correct indentations in the right place

3 : we select the code, and we see that the selection is at the edge of the code, the macro must works so good

the macro must works so good

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

remove one space all lines

5 : Here the code has been copied in a forum window with the Select all button apparently the selection is good

Here the code has been copied in a forum

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

But the selection pasted into the FreeCAD editor gives a surprise

7 : You must delete all the extra space that is four spaces on each line, for Windows word processing notepad-plus-plus enables vertical selection with a combination of buttons Alt + Mouse dragging or Menu> Edit> Indent> Decrease the indentation

You must delete all the extra space

8 : Here the selection also take the column numbers which will also give an error

Here the selection also take the column numbers

9 : Perfect code.

Perfect code

No text output from the macros

Some macros display information on the screen in general they are displayed in the report view.

FreeCAD use two methods to display the information in the window view report.

1 : Commands

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

or

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

or

print "Hello World !"

To see the information displayed in the console you should:

1 : Open FreeCAD

Open FreeCAD

2 : Click the View menu and Views

Click the View menu and Views

3 : Check Report View and Python Console

Check Report View and Python Console

4 : the windows are enabled and available commands like "App.Console.PrintMessage" is configured to the "Report View"

Hello World!


2 : command "print" which is a Python command.

It is possible that this command does not display the expected string.

For information to be displayed in the window do this :

1 : Click the Edit menu and then Preferences

Edit menu

2 : In the new window, click General, and select the Output window tab

General

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 click the OK button

Redirect internal

4 : the setup is complete you should see all.

the setup is complete


Links

Macro addons_installer.FCMacro for easy install the macro

Other easy macro installer freecad-pluginloader

The Macros recipes page