How to install macros: Difference between revisions
(A few edits) |
(More complete method 1) |
||
Line 39: | Line 39: | ||
<!--T:10--> |
<!--T:10--> |
||
1. Open the menu {{MenuCommand|Macro → [[Image:Std DlgMacroExecuteDirect.svg|16px]] 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]]. |
||
<!--T:11--> |
<!--T:11--> |
||
[[ |
[[Image:Dxf_Importer_Install_01.png|center]] |
||
<!--T:13--> |
<!--T:13--> |
||
Line 51: | Line 51: | ||
<!--T:16--> |
<!--T:16--> |
||
[[ |
[[Image:Dxf_Importer_Install_02.png|center]] |
||
<!--T:17--> |
<!--T:17--> |
||
Line 60: | Line 60: | ||
<!--T:18--> |
<!--T:18--> |
||
[[ |
[[Image:Dxf_Importer_Install_03.png|center]] |
||
<!--T:19--> |
<!--T:19--> |
||
Line 69: | Line 69: | ||
<!--T:20--> |
<!--T:20--> |
||
[[ |
[[Image:Dxf_Importer_Install_04.png|center]] |
||
{{clear}} |
{{clear}} |
||
== Adding macros == |
|||
<!--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"> |
|||
<!--T:23--> |
|||
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 : |
|||
<!--T:24--> |
|||
<span style="color:red"> |
|||
'''<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> |
|||
<!--T:25--> |
|||
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--> |
|||
'''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. |
|||
<!--T:27--> |
|||
[[Image:Macro Install HowTo 09.png|center|the code was stuck without any indentation]] |
|||
<!--T:28--> |
|||
'''2 :''' the code was correct indentations in the right place. |
|||
<!--T:29--> |
|||
[[Image:Macro Install HowTo 10.png|center|the code was correct indentations in the right place]] |
|||
<!--T:30--> |
|||
'''3 :''' we select the code, and we see that the selection is at the edge of the code, the macro must works so good |
|||
<!--T:31--> |
|||
[[Image:Macro Install HowTo 11.png|center|the macro must works so good]] |
|||
<!--T:32--> |
|||
'''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''' |
|||
<!--T:33--> |
|||
[[Image:Macro Install HowTo 12.png|center|remove one space all lines]] |
|||
<!--T:34--> |
|||
'''5 :''' Here the code has been copied in a forum window with the {{KEY|Select all}} button apparently the selection is good |
|||
<!--T:35--> |
|||
[[Image:Macro Install HowTo 14.png|center|640px|Here the code has been copied in a forum]] |
|||
<!--T:36--> |
|||
'''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 |
|||
<!--T:37--> |
|||
[[Image:Macro Install HowTo 15.png|center|640px|But the selection pasted into the FreeCAD editor gives a surprise]] |
|||
<!--T:38--> |
|||
'''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 |
|||
<!--T:39--> |
|||
[[Image:Macro Install HowTo 16.png|center|640px|You must delete all the extra space]] |
|||
<!--T:40--> |
|||
'''8 :''' Here the selection also take the column numbers which will also give an error |
|||
<!--T:41--> |
|||
[[Image:Macro_Install_HowTo_29.png|center|640px|Here the selection also take the column numbers]] |
|||
<!--T:42--> |
|||
'''9 :''' Perfect code. |
|||
<!--T:43--> |
|||
[[Image:Macro Install HowTo 13.png|center|Perfect code]] |
|||
<!--T:44--> |
|||
<br /> |
|||
</div> |
|||
</div> |
|||
<!--T:45--> |
|||
'''Method 1:''' |
|||
<div class="toccolours mw-collapsible mw-collapsed"> |
<div class="toccolours mw-collapsible mw-collapsed"> |
||
==Method |
===Method 1. Copy the code to the FreeCAD editor=== <!--T:46--> |
||
<div class="mw-collapsible-content"> |
<div class="mw-collapsible-content"> |
||
<!--T:47--> |
<!--T:47--> |
||
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]]. If there is a custom icon download it: click 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 |
[[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--> |
||
[[ |
[[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--> |
||
[[ |
[[Image:Macro Install HowTo 17.png|center]] |
||
{{Caption|align=center|The execute macro dialog}} |
|||
<!--T:55--> |
<!--T:55--> |
||
5. Enter the macro name, here {{incode|Macro_Apothem_Based_Prism_GUI}}, and press {{Button|OK}}. |
|||
<!--T:56--> |
<!--T:56--> |
||
[[ |
[[Image:Macro Install HowTo 18.png|center]] |
||
{{Caption|align=center|Entering the macro name}} |
|||
<!--T:57--> |
<!--T:57--> |
||
6. The macro editor opens, showing the full path of the new macro. |
|||
<!--T:58--> |
<!--T:58--> |
||
[[ |
[[Image:Macro Install HowTo 19.png|center]] |
||
{{Caption|align=center|The macro editor}} |
|||
<!--T:59--> |
<!--T:59--> |
||
7. Paste the code in the editor window, and then click the cross at the bottom of the tab to close the window. |
|||
<!--T:60--> |
<!--T:60--> |
||
[[ |
[[Image:Macro Install HowTo 20.png|center]] |
||
{{Caption|align=center|Closing the macro editor}} |
|||
<!--T:61--> |
<!--T:61--> |
||
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. |
|||
Restart FreeCAD to correctly register your macro. |
|||
<!--T:62--> |
|||
'''PS:''' For the change silk consideration must save the macro and reopen it and just run it. |
|||
<!--T:63--> |
<!--T:63--> |
||
[[ |
[[Image:Macro Install HowTo 27.png|center]] |
||
{{Caption|align=center|Asking for confirmation to save the code}} |
|||
<!--T:64--> |
<!--T:64--> |
||
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--> |
||
[[ |
[[Image:Macro Install HowTo 21.png|center]] |
||
{{Caption|align=center|Selecting the macro to run it}} |
|||
<!--T:66--> |
<!--T:66--> |
||
10. The macro now runs. Fill in the fields with your values and click the {{Button|OK}} button. |
|||
<!--T:67--> |
<!--T:67--> |
||
[[ |
[[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--> |
||
11. This macro should return an error if no document is active. Create a new document with {{MenuCommand|File → [[Image:Document-new.svg|16px]] [[Std_New|New]]}}, then repeat the previous steps to execute the macro again. Some macros open a new document if none exists. |
|||
<!--T:69--> |
<!--T:69--> |
||
[[ |
[[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 a prism. |
|||
10 : Here is your prism |
|||
<!--T:71--> |
<!--T:71--> |
||
[[ |
[[Image:Macro Install HowTo 24.png|center]] |
||
{{Caption|align=center|Prism created by the macro}} |
|||
<!--T:72--> |
<!--T:72--> |
||
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--> |
||
[[ |
[[Image:Macro Install HowTo 25.png|center]] |
||
{{Caption|align=center|Opening the macro in the editor}} |
|||
<!--T:74--> |
<!--T:74--> |
||
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--> |
||
[[ |
[[Image:Macro Install HowTo 26.png|center]] |
||
{{Caption|align=center|Running the macro that is loaded in the editor}} |
|||
<!--T:76--> |
|||
</div> |
</div> |
||
</div> |
</div> |
||
<!--T:77--> |
|||
'''Method 2:''' |
|||
<div class="toccolours mw-collapsible mw-collapsed"> |
<div class="toccolours mw-collapsible mw-collapsed"> |
||
==Method macro |
===Method 2. Add a macro file from a compressed .zip file=== <!--T:78--> |
||
<div class="mw-collapsible-content"> |
<div class="mw-collapsible-content"> |
||
Line 287: | Line 212: | ||
<!--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|640px|Unzip your file in the folder.]] |
||
]] |
|||
<!--T:89--> |
<!--T:89--> |
||
Line 331: | Line 255: | ||
<!--T:102--> |
<!--T:102--> |
||
[[Image:Macro Install HowTo 30.png|center|640px|Whaouu]] |
[[Image:Macro Install HowTo 30.png|center|640px|Whaouu]] |
||
</div> |
|||
</div> |
|||
== Errors in the macros == |
|||
<!--T:136--> |
|||
<div class="toccolours mw-collapsible mw-collapsed"> |
|||
=== Indentation errors === <!--T:22--> |
|||
<div class="mw-collapsible-content"> |
|||
<!--T:23--> |
|||
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 : |
|||
<!--T:24--> |
|||
<span style="color:red"> |
|||
'''<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> |
|||
<!--T:25--> |
|||
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--> |
|||
'''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. |
|||
<!--T: |
<!--T:27--> |
||
[[Image:Macro Install HowTo 09.png|center|the code was stuck without any indentation]] |
|||
<!--T:28--> |
|||
'''2 :''' the code was correct indentations in the right place. |
|||
<!--T:29--> |
|||
[[Image:Macro Install HowTo 10.png|center|the code was correct indentations in the right place]] |
|||
<!--T:30--> |
|||
'''3 :''' we select the code, and we see that the selection is at the edge of the code, the macro must works so good |
|||
<!--T:31--> |
|||
[[Image:Macro Install HowTo 11.png|center|the macro must works so good]] |
|||
<!--T:32--> |
|||
'''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''' |
|||
<!--T:33--> |
|||
[[Image:Macro Install HowTo 12.png|center|remove one space all lines]] |
|||
<!--T:34--> |
|||
'''5 :''' Here the code has been copied in a forum window with the {{KEY|Select all}} button apparently the selection is good |
|||
<!--T:35--> |
|||
[[Image:Macro Install HowTo 14.png|center|640px|Here the code has been copied in a forum]] |
|||
<!--T:36--> |
|||
'''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 |
|||
<!--T:37--> |
|||
[[Image:Macro Install HowTo 15.png|center|640px|But the selection pasted into the FreeCAD editor gives a surprise]] |
|||
<!--T:38--> |
|||
'''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 |
|||
<!--T:39--> |
|||
[[Image:Macro Install HowTo 16.png|center|640px|You must delete all the extra space]] |
|||
<!--T:40--> |
|||
'''8 :''' Here the selection also take the column numbers which will also give an error |
|||
<!--T:41--> |
|||
[[Image:Macro_Install_HowTo_29.png|center|640px|Here the selection also take the column numbers]] |
|||
<!--T:42--> |
|||
'''9 :''' Perfect code. |
|||
<!--T:43--> |
|||
[[Image:Macro Install HowTo 13.png|center|Perfect code]] |
|||
</div> |
</div> |
||
</div> |
</div> |
||
<!--T:107--> |
|||
'''Section if the text are not display:''' |
|||
<div class="toccolours mw-collapsible mw-collapsed"> |
<div class="toccolours mw-collapsible mw-collapsed"> |
||
== |
===No text output from the macros=== <!--T:108--> |
||
<div class="mw-collapsible-content"> |
<div class="mw-collapsible-content"> |
||
Some macros display information on the screen in general they are displayed in the report view. |
Some macros display information on the screen in general they are displayed in the report view. |
||
Line 447: | Line 441: | ||
</div> |
</div> |
||
<!--T:104--> |
|||
End tutorial good job. |
|||
==Links== <!--T:105--> |
==Links== <!--T:105--> |
Revision as of 01:09, 5 December 2018
Tutorial |
Topic |
---|
Programming |
Level |
Medium programmer |
Time to complete |
15 minutes |
Authors |
Mario52 |
FreeCAD version |
All |
Example files |
None |
See also |
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.
Toolbar
Menu
Besides the tools in the toolbar, the following functions are also available in the Macro menu.
- Debug Start
- Debug Stop
- Step over
- Step into
- Toggle breakpoint
Location and destination of macros
1. Open the menu Macro → Macros to open the Execute 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
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.
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.
Adding macros
Method 1. Copy the code to the FreeCAD 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 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.
The execute macro dialog
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 at the bottom of 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 your 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. Create a new document with File → New, then repeat the previous steps to execute the macro again. Some macros open a new document if none exists.
The macro returning an error if no document is active
12. Once an active document is available, the macro runs and creates a prism.
Prism 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
Method 2. Add a macro file from a compressed .zip file
Download the file compressed here (example) Macro_screw_maker 1.7. (the page Macro_screw_maker)
Free for Windows 7-zip ou L-Zarc ou quickzip
For Linux use this on the terminal
unzip your_file.zip -d your_directory
you must unzip the zip and copy the file (or all the files and folders) in your macro directory.
Process
1 : Download your file in your local folder here the folder Temp
2 : Unzip your file in the folder.
3 : The decompressor finished his work and created a new folder with the unpacked file
4 : Enter in the newly created directory, move about the file, click the right mouse button and click on Cut.
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.
6 : Open FreeCAD click Menu > Macro > Macros or the click the bottom "Open a dialog to let you execute a macro Recorded"
7 : The macros window open , select your macro and click the button Execute
8 : Your macro is executed enter the data and click the Create button
9 : Whaouu
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.
2 : 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
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
5 : Here the code has been copied in a forum window with the Select all button apparently the selection is good
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
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
8 : Here the selection also take the column numbers which will also give an error
9 : 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
2 : Click the View menu and Views
3 : Check Report View and Python Console
4 : the windows are enabled and available commands like "App.Console.PrintMessage" is configured to the "Report View"
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
2 : In the new window, click General, and select the Output window tab
3 : check both boxes:
Redirect internal Python output to Report view
Redirect internal Python errors to Report view
and click the OK button
4 : the setup is complete you should see all.
Links
Macro addons_installer.FCMacro for easy install the macro
Other easy macro installer freecad-pluginloader
The Macros recipes page