Main Menu

search

You are here

FreeCAD: Macros

[last updated: 2023-11-14]
FreeCAD
Python for FreeCAD scripts and macros

(link to:) FreeCAD - Python tutorial
(link to:) FreeCAD - scripting examples
(link to:) FreeCAD - pyCollada for .dae files
-----

  • Create a Macro:
    --------------------------------------------------------

  • Edit a macro:
    • Edit by typing commands directly into the macro edit window.
      Test your macro as you progress by executing it (see below).
      If you are interfacing to FC, however, it can be necessary to use the Python Console to find and test commands.
    • Start by setting up your FC environment:
      menu: View --> Panels --> Python Console
      You may also want to show the Report View panel.
        Tip: When the views get cluttered,
        right-click --> Clear

    • Entering commands into the Python Console:
      If you type in the console, at the ">>>" prompt:
      "FreeCAD." - Note this is "FreeCAD" followed by a period/dot (case-sensitive)
      then a box will come up showing your options.
      Notice when you scroll down through the list, a tool tip will display at the side explaining syntax and usage for each of the options.
      • "Hello World" example/demo:
        Type FreeCAD. at the prompt
        Scroll down with arrow key until "Console" is highlighted, then press Enter.
        Then press period ("dot") and notice a new list of options is shown.
        Scroll down to find "PrintMessage", and notice that the tool tip shows you that it requires a string parameter, in brackets,
        which will be the string that is printed to the console.
        When you add the string parameter to the command line, you must enclose it in brackets, and the string itself must be in quotes.

        Press enter to select the PrintMessage command, and "PrintMessage" will be added to the prompt/command line.
        Now type: ("Hello World") and press Enter, and see that indeed "Hello World" is printed to the output bar below the console window.

    • So far, this has all been happening in the python console.
      But you can highlight and copy the command from the console, - FreeCAD.Console.PrintMessage("helloworld")
      and paste it into your macro editing window,
      then Menu: Macro --> Execute Macro, and see that indeed "Hello World" is again printed to the output bar.
        Or you can type the shortcut for the "execute macro" command, which is Ctrl-F6
        However this did not work on my new mechanical keyboard ...
    • When done editing, save with Ctrl-S or the save icon in the top menubar, or menu: File --> Save
        ??? you may need to have the macro tab/window open for this to work... must investigate ...

    --------------------------------------------------------

  • Create icon and add macro to toolbar:
    --------------------------------------------------------

  • Execute a Macro:
    • from Macro menu:
      • Macro (top menu bar) --> Macros
        Execute Macro window opens with a list of all defined macros.
      • Single-click to select the one you want to run,
        then click Execute (top of right-hand menu).
      • - OR - double-click desired macro
    • Click its icon:
      ... that is, if you have created one and added it to a toolbar as above ...

    -----------------------------------------------------------------------

.

.

.

eof