Topic |
---|
Programming |
Level |
Medium programmer |
Time to complete |
15 minutes |
Authors |
Mario52 |
FreeCAD version |
All |
Example files |
None |
See also |
None |
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.
Besides the tools in the toolbar, the following functions are also available in the Macro menu.
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 → Python → Macro → Macro recording settings.
Downloaded macros should also be placed in this 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.
/usr/share/freecad/
C:\Program Files\FreeCAD\
/Applications/FreeCAD/
The $ROOT_DIR
could be a particular user's directory.
/home/username/.local/share/FreeCAD/
(version 0.20 and above) or /home/username/.FreeCAD/
(version 0.19 and below).C:\Users\username\AppData\FreeCAD\
/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)
5 : View the macro folder by:
6 : Access the files by:
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:
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.
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 .
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
3 : A new window opens, enter the macro name (here "Macro_Apothem_Based_Prism_GUI")and click the create Ok button
4 : The editing window FreeCAD macros is now available and has the name of our future macro.
5 : Paste your code in the macro editor window and click the little cross to 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.
7 : Repeat the number 1 : , Click on your new macro and button Execute
8 : The macro runs, complete the fields with your values and click the OK button
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.
10 : Here is your prism
11 : You can also open your macro in the editor to run or modify, click the Edit button
12: The macro is now in the FreeCAD editor you can run through the menu "Macro Run Macro" or by clicking on the triangle green in the macros toolsbar
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.
unzip your_file.zip -d your_directory
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
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
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.
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
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.
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.
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
In Windows, Notepad++ can perform selection with Alt + Mouse dragging, and then use Edit → Indent → Decrease the indentation.
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.
9 : Perfect code.
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.
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
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.
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:
and click the OK button