How to install macros: Difference between revisions

From FreeCAD Documentation
m (adding "(Only the English page)" to the banner)
No edit summary
(25 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<languages/>
{{VeryImportantMessage|'''This tutorial has been translated with [https://translate.google.com/ Google translate] a rereading correction will be appreciated (Only the English page), thank you after confirmation to remove this banner'''}}
<translate>
<translate>
<!--T:1-->
<!--T:1-->
{{TutorialInfo|Class=Programming|Level=Medium programmer|Time=15 minutes|FCVersion=All|Author=[[User:Mario52|Mario52]]|FCVersion=All}}
{{TutorialInfo|Topic=Programming|Level=Medium programmer|Time=15 minutes|FCVersion=All|Author=[[User:Mario52|Mario52]]}}


==Description== <!--T:2-->
==Description== <!--T:2-->
This short tutorial will guide you on how to use macros available and incorporate them into the FreeCAD editor.
This short tutorial will show you how to install and use FreeCAD Macros.


<!--T:3-->
<!--T:3-->
This tutorial was made on the Windows platform a small adjustment path may be necessary for other systems.
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-->
==Python== <!--T:4-->
Python is a programming language, very simple to use and very fast to [[Introduction_to_Python|learn]].
Python is a programming language which is very simple to use and very fast to [[Introduction_to_Python|learn]].


<!--T:5-->
<!--T:5-->
Extending Python files is '''.py'''. FreeCAD macros have the extension '''.FCMacro''' but can also be executed with the extension '''.py'''. Python was invented to [https://www.python.org/~guido/ Guido Van Rossun]
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]


==The Macro Menu and Toolbar Icons== <!--T:6-->
==The Macro Menu and Toolbar Icons== <!--T:6-->
Line 26: Line 27:
[[Image:Debug-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]
[[Image:Debug-stop.svg|32px|stop]] [[Std_DlgMacroStop|Stop]]


==Location and destination macros== <!--T:8-->
==Location and destination of macros== <!--T:8-->


<!--T:9-->
<!--T:9-->
This section allows you to locate the final location of your macros.
This section shows you how to locate the folder containing your FreeCAD macros.


<!--T:10-->
<!--T:10-->
'''1 :''' click '''Menu > Macro > Macros''' or the click the bottom [[File:Std DlgMacroExecuteDirect.svg|24px]] "Open a dialog to let you execute a macro Recorded"
'''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")


<!--T:11-->
<!--T:11-->
Line 38: Line 39:


<!--T:12-->
<!--T:12-->
'''2 :''' one dialog box open
'''2 :''' This will open a dialog box


<!--T:13-->
<!--T:13-->
'''3 :''' the address of "Macro destination" (here '''C:\Users\your_user_name\AppData\Roaming\FreeCAD\''')
'''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'''
<!--T:14-->
In Ubuntu, this is normally '''/home/your_user_name/.FreeCAD'''
* Macintosh: the form is usually "/Users/your_user_name/Library/Preferences/FreeCAD"


<!--T:15-->
<!--T:15-->
Line 53: Line 54:


<!--T:17-->
<!--T:17-->
'''5 :''' paste the address into your browser and confirm
'''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-->
<!--T:18-->
Line 59: Line 62:


<!--T:19-->
<!--T:19-->
'''6 :''' leave open the explorer
'''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-->
<!--T:20-->
Line 69: Line 74:
'''Section to read if you are not familiar with Python programming (common mistakes copy/paste).'''
'''Section to read if you are not familiar with Python programming (common mistakes copy/paste).'''


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

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


<!--T:48-->
<!--T:48-->
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_ToolsBar|toolsbar.]]
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:49-->
<!--T:49-->
Line 214: Line 221:


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


<!--T:69-->
<!--T:69-->
Line 294: Line 301:


<!--T:93-->
<!--T:93-->
'''5 :''' Return to your browser remained open in the macro location (here '''C:\Users\your_user_name\AppData\Roaming\FreeCAD\''') and close the browser.
'''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 browser remained open]]
[[Image:Macro Install HowTo 05.png|center|640px|Return to your File explorer remained open]]


<!--T:95-->
<!--T:95-->
Line 324: Line 331:


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

<!--T:107-->
'''Section if the text are not display:'''

<div class="toccolours mw-collapsible mw-collapsed">
==For those who see no information is displayed.== <!--T:108-->
<div class="mw-collapsible-content">
Some macros display information on the screen in general they are displayed in the report view.

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

<!--T:110-->
'''1 : Commands'''
</translate>
{{Code|code=
App = FreeCAD
App.Console.PrintMessage ("Hello World ! \n")
App.Console.PrintError ("Hello World ! \n")
App.Console.PrintWarning ("Hello World ! \n")
}}
<translate>
<!--T:111-->
or
</translate>
{{Code|code=
FreeCAD.Console.PrintMessage ("Hello World ! \n")
FreeCAD.Console.PrintError ("Hello World ! \n")
FreeCAD.Console.PrintWarning ("Hello World ! \n")
}}
<translate>
<!--T:137-->
or
</translate>
{{Code|code=
print "Hello World !"
}}
<translate>
<!--T:112-->
To see the information displayed in the console you should:

<!--T:113-->
'''1 :''' Open FreeCAD

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

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

<!--T:116-->
[[Image:Macro Install HowTo 32.png|center|640px|Click the View menu and Views]]

<!--T:117-->
'''3 :''' Check '''Report View''' and '''Python Console'''

<!--T:118-->
[[Image:Macro Install HowTo 33.png|center|640px|Check Report View and Python Console]]

<!--T:119-->
'''4 :''' the windows are enabled and available commands like "'''App.Console.PrintMessage'''" is configured to the "Report View"

<!--T:120-->
[[Image:Macro Install HowTo 34.png|center|640px|Hello World!]]


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

<!--T:122-->
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-->
'''1 :''' Click the '''Edit menu''' and then '''Preferences'''

<!--T:125-->
[[Image:Macro Install HowTo 35.png|center|640px|Edit menu]]

<!--T:126-->
'''2 :''' In the new window, click '''General''', and select the '''Output window''' tab

<!--T:127-->
[[Image:Macro Install HowTo 36.png|center|640px|General]]

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

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

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

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

<!--T:132-->
[[Image:Macro Install HowTo 37.png|center|640px|Redirect internal]]

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

<!--T:134-->
[[Image:Macro Install HowTo 38.png|center|640px|the setup is complete]]

<!--T:135-->
</div>
</div>
</div>
</div>
Line 333: Line 451:


<!--T:106-->
<!--T:106-->
Macro [https://github.com/FreeCAD/FreeCAD-addons addons_installer.FCMacro] for easy install the macro
The [[Macros_recipes|Macros_recipes]] page

<!--T:138-->
Other easy macro installer [https://github.com/microelly2/freecad-pluginloader freecad-pluginloader]

<!--T:139-->
The [[Macros_recipes|Macros recipes]] page


</translate>
</translate>
<languages/>

Revision as of 13:15, 4 August 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.

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

Python is a programming language which is very simple to use and very fast to learn.

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 Guido Van Rossun

The Macro Menu and Toolbar Icons

record Launch Record stop Stop Record open editor Open Menu macro execute Execute start Debug Macro stop Stop

Location and destination of macros

This section shows you how to locate the folder containing your FreeCAD macros.

1 : click Menu > Macro > Macros (which has the icon and the tool tip "Open a dialog to let you execute a recorded macro")

2 : This will open a dialog box

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"

4 : copy the address of "Macro destination" (here C:\Users\your_user_name\AppData\Roaming\FreeCAD\)

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)

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)


Section to read if you are not familiar with Python programming (common mistakes copy/paste).

The indentation errors examples wrong code

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
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
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
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
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
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
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
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
Here the selection also take the column numbers

9 : Perfect code.

Perfect code
Perfect code


Method 1:

Method copy the code in one window and paste in the FreeCAD editor

For example we will copy the macro Macro_Apothem_Based_Prism_GUI

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

Download icon
Download icon

After copying your code we will paste the code in FreeCAD editor.

1 : Open FreeCAD and open the editor in FreeCAD

2 : The window macros file opens, click Create button

The window macros file opens
The window macros file opens

3 : A new window opens, enter the macro name (here "Macro_Apothem_Based_Prism_GUI")and click the create Ok button

enter the macro name
enter the macro name

4 : The editing window FreeCAD macros is now available and has the name of our future macro.

The editing window FreeCAD macros
The editing window FreeCAD macros

5 : Paste your code in the macro editor window and click the little cross to close the window.

close the window
close the window

6 : A warning window appears asking for confirmation of save code, click on Yes

PS: For the change silk consideration must save the macro and reopen it and just run it.

A warning window appears asking for confirmation of save code
A warning window appears asking for confirmation of save code

7 : Repeat the number 1 : , Click on your new macro and button Execute

Click on your new macro and button Execute
Click on your new macro and button Execute

8 : The macro runs, complete the fields with your values and click the OK button

The macro runs, complete the fields
The macro runs, complete the fields

9 : The macro returns an error ! we do not have to open document, open a document and repeat the operation 7 and 8. Some macros open a new document if it does not find one.

The macro returns an error!
The macro returns an error!

10 : Here is your prism

your prism
your prism

11 : You can also open your macro in the editor to run or modify, click the Edit button

You can also open your macro in the editor
You can also open your macro in the editor

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 green in the macros toolsbar

The macro is now in the FreeCAD editor
The macro is now in the FreeCAD editor

Method 2:

Method macro in 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

Download your file in your local folder here the folder Temp
Download your file in your local folder here the folder Temp

2 : Unzip your file in the folder.

Unzip your file in the folder.
Unzip your file in the folder.

3 : The decompressor finished his work and created a new folder with the unpacked file

The decompressor finished his work and created a new folder with the unpacked file
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.

Enter in the newly created directory
Enter in the newly created directory

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.

Return to your File explorer remained open
Return to your File explorer remained open

6 : Open FreeCAD click Menu > Macro > Macros or the click the bottom "Open a dialog to let you execute a macro Recorded"

Open FreeCAD
Open FreeCAD

7 : The macros window open , select your macro and click the button Execute

The macros window open
The macros window open

8 : Your macro is executed enter the data and click the Create button

Your macro is executed
Your macro is executed

9 : Whaouu

Whaouu
Whaouu

Section if the text are not display:

For those who see no information is displayed.

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
Open FreeCAD

2 : Click the View menu and Views

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
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!
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
Edit menu

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

General
General

3 : check both boxes:

Redirect internal Python output to Report view

Redirect internal Python errors to Report view

and click the OK button

Redirect internal
Redirect internal

4 : the setup is complete you should see all.

the setup is complete
the setup is complete

End tutorial good job.

Links

Macro addons_installer.FCMacro for easy install the macro

Other easy macro installer freecad-pluginloader

The Macros recipes page