Recording macros and converting to something more usable

Need help, or want to share a macro? Post here!
drmacro
Posts: 3131
Joined: Sun Mar 02, 2014 4:35 pm

Recording macros and converting to something more usable

Postby drmacro » Thu Apr 15, 2021 5:29 pm

After both web search and forum search I've found little on what I'm musing about.
Of course it could be a combination things like poor search terms and/or not enough coffee. :mrgreen:

The general question is: when I record a macro, a huge percentage of the lines have a # a the beginning.

I think that this implies it is a comment line, since with some things this seems obviously to be comments:

Code: Select all

# Macro Begin: /home/mac/SharedData/FC_common/CreateSketchSquare.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
But, there are a lot of other lines that would seem to be important but are commented out:

Code: Select all

# App.setActiveDocument("Unnamed")
So, is there a rule of thumb as to how to deal with this.

Also, I recorded the attached macro. The recording actually created a rectangle. But, running the the macro does everything (i.e. create the document, create the Body, create the sketch) and leaves the sketch empty.
Seems rather useless to record a macro that only captures part of what the user does. Is there a secret sauce or incantation I know not?

(PS: I have searched a LOT on the comments in macros and actually found no mention of them in the interwebs...or the FC wiki. Happy to be shown I'm missing it. :roll: )

Recorded in:
OS: Ubuntu 20.04.2 LTS (XFCE/ubuntustudio)
Word size of FreeCAD: 64-bit
Version: 0.20.24711 (Git)
Build type: Release
Branch: master
Hash: 47a41ae1c8a08fe47ce0e7b5b73df8f849adfe21
Python version: 3.8.5
Qt version: 5.12.8
Coin version: 4.0.0
OCC version: 7.5.1
Locale: English/United States (en_US)
Attachments
CreateSketchSquare.FCMacro
(3.67 KiB) Downloaded 5 times
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."

My beginners, written treatise: Learning FreeCAD
kwahoo
Posts: 410
Joined: Fri Nov 29, 2013 3:09 pm
Contact:

Re: Recording macros and converting to something more usable

Postby kwahoo » Thu Apr 15, 2021 9:01 pm

1. Uncheck in Preferences-General-Macro "Record as comment". GUI commands are recorded as comments by default IIRC.
2.

Code: Select all

# App.setActiveDocument("Unnamed")
That's just info for you, there is no reason to run this since it is executed intenally by earlier App.newDocument("Unnamed")
Put App.newDocument("Unnamed") in the Python console and you will see:

Code: Select all

>>> App.newDocument("Unnamed")
<Document object at 0x55ce8ccc5e70>
>>> # App.setActiveDocument("Unnamed")
>>> # App.ActiveDocument=App.getDocument("Unnamed")
>>> # Gui.ActiveDocument=Gui.getDocument("Unnamed")
User avatar
onekk
Posts: 772
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Recording macros and converting to something more usable

Postby onekk » Fri Apr 16, 2021 8:40 am

Commands with # in front are the command issued by the "GUI itself".

In your macro if you see they are usually selection commands, (when you choose the object on which you want to operate) and some settings, like "ViewPort" Settings and so on, plus some "service information" about start end of an "internal command" or user action.

They are here to make things more human readable, as you could not guess "what is doing where" in the code.

If you intend to transform it in a Macro that i useful you have to operate on a selection, that usually is not the selection (in term of "document name" and "object name") that is done when the macro is recorded.

And maybe the "ViewPort" is not useful but macro is a sort of "copy of the Python Console" as same information are those reported when you operate with Gui and select the proper settings to echo command on "Python Console".

Short answer, the # commands are not essential, but the Macro recorded itself is not "ready to use" and have to be composed in a manner that is "agnostic in term of document and object"

Code: Select all

import FreeCAD
import PartDesign
import PartDesignGui
import Sketcher

DOC = FreeCAD.activeDocument()

DOC.addObject('PartDesign::Body','Body')
DOC.getObject('Body').newObject('Sketcher::SketchObject','Sketch')
DOC.getObject('Sketch').Support = DOC.getObject('XY_Plane'),[''])
DOC.getObject('Sketch').MapMode = 'FlatFace'
DOC.recompute()

This is a rough example of a script that to the thing you have recorded in the macro.

Obviuosly the assignement of DOC is arbitrary, but visually show the operation on the active document.

If you want transform it in a file with .py extension and load into the editor, and launch it with the "big green arrow" of the "macro toolbar" in the ActiveDocument() some object will be created, try to see if it make sense.


Regards

Carlo D.
Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc