Wie man Makros installiert

From FreeCAD Documentation
Revision as of 14:53, 6 January 2020 by Maker (talk | contribs) (Created page with "==== Beispiel 1 ====")
Tutorium
Thema
Programmierung
Niveau
durchschnittliche Programmierer
Zeit zum Abschluss
Not provided
Autoren
Mario52
FreeCAD-Version
Alle
Beispieldateien
None
Siehe auch
None

Beschreibung

Diese Seite zeigt dir, wie du FreeCAD Makros installieren und benutzen kannst.

Makros sind Befehlssequenzen, die zur Ausführung einer komplexen Zeichenoperation verwendet werden. Makros sind Python Skripte, d.h. sie sind Textdateien, die mit einem Texteditor geschrieben und bearbeitet werden können.

Während Python Skripte normalerweise die Erweiterung .py haben, sollten FreeCAD Makros die Erweiterung .FCMacro haben. Eine Sammlung von Makros, die von erfahrenen Anwendern geschrieben wurden, findest Du auf der Seite Makrozepte.

Siehe Einführung in Python, um mehr über die Programmiersprache Python zu erfahren, und dann Python Skript Tutorium und FreeCAD Skript Grundlagen, um das Schreiben von Makros zu erlernen.

Hier ist ein Video über Installieren von FreeCAD Makros in Ubuntu.

Das Makro Menü und die Werkzeugleiste

Werkzeugleiste

Menü

Neben den Werkzeugen in der Werkzeugleiste sind auch die folgenden Funktionen im Menü Template:MenuCommand/de verfügbar.

Makros Verzeichnis

Makros werden in einem bestimmten Ordner unter dem FreeCAD Benutzerverzeichnis erstellt. Dieses Verzeichnis kann im Makrodialog ausführen, oder im Einstellungseditor, über das Menü Template:MenuCommand/de.

Heruntergeladene Makros sollten ebenfalls in diesem Verzeichnis abgelegt werden.

Standardverzeichnis

Makros können einfach kopiert werden in

$ROOT_DIR/

wobei $ROOT_DIR ein Verzeichnis der obersten Ebene ist, das von FreeCAD beim Start durchsucht wird.

Der $ROOT_DIR könnte ein systemweites Verzeichnis sein, in diesem Fall wird das Makro für alle Benutzer installiert.

  • Unter Linux ist es normalerweise /usr/share/freecad/
  • Unter Windows ist es normalerweise C:\Program Files\FreeCAD\
  • Unter Mac OSX ist es normalerweise /Applications/FreeCAD/

Der $ROOT_DIR könnte das Verzeichnis eines bestimmten Benutzers sein.

  • Unter Linux ist es normalerweise /home/username/.FreeCAD/
  • Unter Windows ist es normalerweise C:\Benutzername\Benutzername\Anwendungsdaten\FreeCAD\
  • Unter Mac OSX ist es normalerweise /Benutzer/Benutzername/Bibliothek/Einstellungen/FreeCAD/

Konfigurieren des Benutzerverzeichnisses

1. Öffne das Menü Template:MenuCommand/de, um den Makrodialog ausführen.

Opening the macro dialog

2. Setze den entsprechenden Nutzer Makros Standort.

  • Linux: normalerweise /home/username/.FreeCAD/
  • Windows: normalerweise C:\Users\username\AppData\Roaming\FreeCAD\
  • MacOS: normalerweise /Users/username/Library/Preferences/FreeCAD/

Setting of the macros directory

3. Navigiere zu diesem Verzeichnis auf deinem Computer.

  • Linux: Füge die Adresse in deinen Dateimanager ein, "Nautilus" oder andere. Eventuell musst du Ctrl+H drücken, um das versteckte Verzeichnis .FreeCAD/ sichtbar zu machen.
  • Windows: Füge die Adresse in dein "Datei Suchprogramm" ein und bestätige.
  • MacOS: Suche den Ordner im "Finder" oder füge die Adresse in ein "Datei Suchprogramm" ein; merke dir das file:/// Vorsatzzeichen im "Datei Suchprogramm" für eine Datei auf der Festplatte.

Accessing the macros directory in the operating system

4. Füge diesem Verzeichnis Makrodateien hinzu.

  • Linux: Lasse den Dateimanager geöffnet und setze ein Lesezeichen für den schnelleren Zugriff.
  • Windows: Lasse das Datei Suchprogramm geöffnet.
  • MacOS: Lasse entweder ein "Finder"-Fenster geöffnet, oder setze ein Lesezeichen für den Speicherort in deinem "Datei Suchprogramm", oder richte einen "Alias" ein, um darauf zu zeigen, oder ziehe den Ordner in die " Seitenleiste" des "Finders", so dass er von anderen Programmen, wie z.B. Texteditoren, verwendet werden kann.

Macros directory

Makros installieren

Automatische Methode

Ab FreeCAD 0.17 verwende den Zusatzmanager in Template:MenuCommand/de, um ein Makro zu installieren, das in das FreeCAD-macros Repositorium aufgenommen wurde.

In früheren Versionen von FreeCAD konntest du zwei automatisierte Wege nutzen, um Makros und andere Addons zu installieren:

  • addons_installer.FCMacro: selbst ein Makro, dies war der Vorläufer des Zusatzmanagers, und wird im FreeCAD-addons Repositorium beherbergt. Bei Neuinstallationen von FreeCAD muss dieses Tool nicht verwendet werden.
  • freecad-pluginloader: ebenfalls ein Makro, es kann verwendet werden, um neue Komponenten in FreeCAD zu installieren. Es wird nicht mehr weiterentwickelt.

Der empfohlene Weg, Zusätze, d.h. externe Arbeitsbereiche und Makros zu installieren, ist der Zusatzmanager. Du kannst jedoch immer noch Makros mit den in den folgenden Abschnitten beschriebenen manuellen Methoden zu deinem System hinzufügen; dies ist nützlich, wenn du deinen eigenen Code entwickelst und testest.

Manuelle Methode 1. Kopiere den Code in den Makro Editor=

Für Makros, die relativ klein sind, 300 Zeilen oder weniger, kann der Code kopiert und direkt in den FreeCAD Makro-Editor eingefügt werden.

Wir werden Macro Apothem Based Prism GUI als ein Beispiel verwenden.

1. Gehe auf die Makro Wiki Seite, die unter Makro Rezepte aufgelistet sein sollte.

Wenn es ein benutzerdefiniertes Symbol gibt, lade es herunter; klicke mit der rechten Maustaste darauf und wähle Bild speichern unter...; platziere das Symbol im Makroverzeichnis. Dieses Symbol kann als Tastaturkürzel für das Makro in einer benutzerdefinierten Werkzeugleiste verwendet werden. Das Standardsymbol ist .

Downloading the icon from the macro page

2. Wähle auf der Makroseite den Code innerhalb der Abschnitte Skript oder Makro aus und kopiere ihn.

3. Öffne in FreeCad das Menü Template:MenuCommand/de, um den Makro Dialog ausführen.

Opening the execute macro dialog

4. Klicke Erstelle.

Creating a new macro

5. Gib den Makronamen ein, hier Macro_Apothem_Based_Prism_GUI, und drücke OK.

Entering the macro name

6. Der Makro Editor öffnet sich und zeigt den vollständigen Pfad des neuen Makros an.

The macro editor

7. Füge den Code in das Editorfenster ein und klicke dann auf das Kreuz auf der Registerkarte, um das Fenster zu schließen.

Closing the macro editor

8. Ein Fenster erscheint, das nach Bestätigung zum Speichern des Codes fragt; klicke auf Ja. Du kannst auch Ctrl+S verwenden, um die Datei zu speichern.

Starte FreeCAD neu, um das neue Makro korrekt zu registrieren.

Asking for confirmation to save the code

9. Öffne dann das Menü erneut, Template:MenuCommand/de, wähle das neue Makro aus und drücke Ausführen.

Selecting the macro to run it

10. Das Makro läuft jetzt. Fülle die Felder mit deinen Werten aus und klicke auf die Schaltfläche OK.

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; other macros open a new document if none exists.

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

The macro returning an error if no document is active

12. Once an active document is available, the macro runs and creates an object.

Object 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

Manual method 2. Add a macro file from a compressed .zip file

Some macros are too big that it's inconvenient to copy and paste them into the macro editor, or they cannot be hosted in the wiki. In this case, the code may be hosted somewhere else, in a Github repository, or in the FreeCAD forum. The code may also be compressed into a .zip file, tarball .tar.xz, or other type of archive if it contains several files. If the code is distributed in this way, the archive should be extracted and the files placed in the macros directory.

We will use Macro screw maker as an example.

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

You need to use a decompressor to get the internal files.

  • For Windows you can use an application like 7-zip or L-Zarc or quickzip.
  • For Linux you can use a command from the terminal
unzip your_file.zip -d your_directory

2. Download the compressed archive with the macro code to a local folder.

Downloading the compressed archive to a local directory

3. Decompress the file in the folder.

Decompressing the file in the folder

4. The decompressor creates a new directory with the unpacked files.

New directory created after unpacking the archive

5. Go inside the new directory, and copy or cut the macro file.

Entering the newly created directory with the decompressed macro file

6. Go to the macro directory and paste the file there.

Placing the macro file in the macro directory

7. In FreeCAD, open the menu Macro → Macros to open the Execute macro dialog.

Opening the execute macro dialog

8. Select the new macro and press Execute.

Selecting the macro to run it

9. The macro now runs. Select the desired options, and click the Create button.

The macro in action; select the desired options, and press Create when ready

Object created by the macro

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

Indentation errors

The white space at the beginning of the lines (indentation) in the Python programming language is very important, and an integral part of the code. An inappropriate space may cause the code to not run or present errors.

This section describes some errors that may be encountered when copying and pasting, and writing macro code.

A typical indentation error looks like this:

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

Beispiel 1

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.

Python code that lacks any indentation; it will cause an error when it's run

Indentation problem fixed.

Python code with the correct indentation

If the code is selected, all lines should be highlighted all the way to the left edge, indicating that the lines are aligned.

Python code highlighted, showing that all lines start at the left edge

Example 2

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.

Python code with additional space on each line

Example 3

Here the code has been copied from a forum thread by using the Select all button. Apparently the selection is good.

Python code copied from a forum

However, when the selection is pasted into the macro editor, undesirable indentation seems to appear.

Python code copied from a forum into the macro editor; unnecessary indentation is added

In this case, the initial spaces need to be removed. This can be done with a specialized text editor to quickly decrease the indentation of the lines.

In Windows, Notepad++ can perform selection with Alt + Mouse dragging, and then use Edit → Indent → Decrease the indentation.

Python code with the correct indentation

Example 4

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.

Selection that also selects the line numbers; if this code is pasted into the macro editor, it won't work

Good code

Python code with the correct indentation

No text output from macros

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.

The FreeCAD functions

FreeCAD.Console.PrintMessage("Hello World! \n")
FreeCAD.Console.PrintError("Hello World! \n")
FreeCAD.Console.PrintWarning("Hello World! \n")

The simple Python function

print("Hello World!")

Enabling the report view

To see the information displayed in the console you should:

1. Go to the menu View → Panels.

Making the panels visible in the menu View → Panels

2. Enable the Report view and the Python console.

Enabling the report view and the Python console

3. The panels are now visible, and commands like FreeCAD.Console.PrintMessage() now print information that appears in the Report view.

FreeCAD main window with the Report view and the Python console

Enabling the print() command

FreeCAD may need to be configured so the print() function of Python redirects its output correctly to the report view.

1. Go into the Preferences Editor with the menu Edit → Preferences.

Going into the preferences editor

2. Go to General section, and then Output window → Python interpreter.

Output window preferences

3. Check both boxes:

  • Redirect internal Python output to report view
  • Redirect internal Python errors to report view

and then press the OK button.

Redirecting the Python output to the report view

Python commands printing information to the report view