How to install macros/ro: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
Line 34: Line 34:
===Menu===
===Menu===
Besides the tools in the toolbar, the following functions are also available in the {{MenuCommand|Macro}} menu.
Besides the tools in the toolbar, the following functions are also available in the {{MenuCommand|Macro}} menu.
* [[Std_MacroAttachDebugger|Attach to remote debugger]]
* [[Image:Debug-start.svg|32px|start]] [[Std_DebugMacro|Debug Start]]
* [[Image:Debug-stop.svg|32px|stop]] [[Std_DebugStop|Debug Stop]]
* [[Image:Std_MacroStartDebug.svg|32px]] [[Std_MacroStartDebug|Debug macro]]
* [[Image:Std_MacroStopDebug.svg|32px]] [[Std_MacroStopDebug|Stop debugging]]
* Step over
* Step into
* [[Std_MacroStepOver|Step over]]
* [[Std_MacroStepInto|Step into]]
* Toggle breakpoint
* [[Std_ToggleBreakpoint|Toggle breakpoint]]


<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Line 46: Line 47:
<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">


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}}.
Macros are created in a specific folder under the user's FreeCAD directory. This directory can be configured in the [[Std_DlgMacroExecute|Execute macro dialog]], or in the [[Preferences Editor|Preferences Editor]], through the menu {{MenuCommand|Edit → Preferences → General → Macro → Macro recording settings}}.


Downloaded macros should also be placed in this directory.
Downloaded macros should also be placed in this directory.
Line 232: Line 233:
</div>
</div>


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.
Create a new document with {{MenuCommand|File → [[Image:Std_New.svg|16px]] [[Std_New|New]]}}, and then repeat the previous steps to execute the macro.


<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Line 614: Line 615:
</div>
</div>


{{Userdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}

Revision as of 19:28, 22 June 2020

Tutorial
Topic
Programming
Level
Medium programmer
Time to complete
15 minutes
Authors
Mario52
FreeCAD version
All
Example files
None
See also
None

Descriere

Acest tutorial scurt vă va arăta cum să instalați și să utilizați macrocomenzile FreeCAD.

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

Meniul Macro Menu și Iconițele Instrumentelor

Menu

Besides the tools in the toolbar, the following functions are also available in the Macro menu.

Locația și destinația mcrocomanzilor

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/

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

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"

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)

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.

Method copy the code in one window and paste in the FreeCAD 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.

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.

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 .

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

Restart FreeCAD to correctly register the new macro.

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.

Create a new document with File → New, and then repeat the previous steps to execute the macro.

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 macro in a compressed .ZIP file

Download the file compressed here (example) Macro_screw_maker 1.7. (the page Macro_screw_maker)

We will use Macro screw maker as an example.

1. Download the compressed code from the forum, Screw Maker.

Free for Windows 7-zip ou L-Zarc ou quickzip

unzip your_file.zip -d your_directory

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

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

The indentation errors examples wrong code

Indentarea în programarea python este foarte importantă și este parte integrantă a codului, un spațiu sau o schimbare necorespunzătoare provoacă o eroare de indentare de ex,:

This section describes some errors that may be encountered when copying and pasting, and writing macro 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'))

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

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.

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.

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

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.

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 from a forum thread by using 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

In Windows, Notepad++ can perform selection with Alt + Mouse dragging, and then use 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 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.

Here the selection also take the column numbers
Here the selection also take the column numbers

9 : Perfect code.

Perfect code
Perfect code

For those who see no information is displayed.

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

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.

1 : Commands

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

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
the setup is complete
the setup is complete