How to install macros/ro

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

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 extension, FreeCAD macros should have the  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
Launch Record Stop Record Open Menu macro Execute Debug Macro Stop

Menu
Besides the tools in the toolbar, the following functions are also available in the menu.
 * [[Image:Debug-start.svg|32px|start]] Debug Start
 * [[Image:Debug-stop.svg|32px|stop]] Debug Stop
 * Step over
 * Step into
 * Toggle breakpoint

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.

Downloaded macros should also be placed in this directory.

Default directory
Macros can be simply copied into

where is a top level directory searched by FreeCAD on startup.

The could be a system wide directory, in which case the macro is installed for all users.
 * On Linux it is usually
 * On Windows it is usually
 * On Mac OSX it is usually

The could be a particular user's directory.
 * On Linux it is usually
 * On Windows it is usually
 * On Mac OSX it is usually

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)



Automatic method
Starting with FreeCAD 0.17, use the Addon Manager in 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 ; 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 button



3 : A new window opens, enter the macro name (here "Macro_Apothem_Based_Prism_GUI")and click the create 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

Restart FreeCAD to correctly register the new macro.



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



8 : The macro runs, complete the fields with your values and click the 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, 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 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



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

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  "Open a dialog to let you execute a macro Recorded"



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



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





Execute a macro in command line
Command line execute a macro (.FCMacro or .py)

on Windows

on Linux

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.

 : ('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 and function definitions, as well as control structures  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 button apparently the selection is good

Here the code has been copied from a forum thread by using the 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 + Mouse dragging or Menu> Edit> Indent> Decrease the indentation

In Windows, Notepad++ can perform selection with + Mouse dragging, and then use.



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.



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

or

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:

Redirect internal Python output to Report view

Redirect internal Python errors to Report view

and click the button