Tools>Customize>Macros in user.cfg

A forum for research and development of the user interface of FreeCAD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: Tools>Customize>Macros in user.cfg

Post by drmacro »

chennes wrote: Sun Sep 18, 2022 7:57 pm It's similar to what was happening with the toolbars: when FreeCAD exits, it is basically overwriting the config data with the "actual" data, asking the Command Manager for its list of commands and storing that.
Back thinking about this.

So, the post macro writes the definition of the custom macro to the parameters.

So, the command manager has a different version of what is current?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
chennes
Veteran
Posts: 3883
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Tools>Customize>Macros in user.cfg

Post by chennes »

That depends -- if all you are doing is editing the FreeCAD preferences in your macro, then yes: just adding and removing things from preferences doesn't change the underlying state of FreeCAD. That is, it doesn't actually add the command, which is why you need to use FreeCADGui.Command.createCustomCommand to actually create the command. It's easier to see what's happening if we talk about something like a window position. When FreeCAD starts up it reads the preferences (once) and sets the window positions as it sees in the file. If later on you change the preferences entry for the window position, it doesn't matter, because that doesn't move the window. So when FreeCAD saves its settings on close, it saves the actual position of the window, not whatever random data is in the preferences.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: Tools>Customize>Macros in user.cfg

Post by drmacro »

I can create a complete persistent toolbars section and it gets saved. I figured it would be as easy as that.

I wanted to create a rather comprehensive PP that included additions (like the snip macro) to the global toolbar.

Not sure how much code is sensible in a post macro...and, TBH, I get the idea of what the Addon manager code does, but, not how to duplicate it for this. Maybe I'll stare at it some more and I'll have an Aha! moment. 8-)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
chennes
Veteran
Posts: 3883
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Tools>Customize>Macros in user.cfg

Post by chennes »

Only a tiny bit of the Addon Manager code there is really relevant to your case: the FreeCADGui.Command.createCustomCommand is the real workhorse, everything around it is mostly UI niceties so the user can place the buttons where they want them. I'm happy to explain individual bits if it would help, but overall what you need is probably just something like:

Code: Select all

        custom_toolbar = FreeCAD.ParamGet(
            "User parameter:BaseApp/Workbench/Global/Toolbar/MyCustomToolbar"
        )
    command_name = FreeCADGui.Command.createCustomCommand(
        "MyGreatMacro.FCMacro",
        "Menu text here",
        "This is a tooltip",
        "This is the what's this text",
        "This is the status tip",
        "icon_file.xpm",
    )
   custom_toolbar.SetString(command_name, "FreeCAD")

    # Force the toolbars to be recreated
    wb = FreeCADGui.activeWorkbench()
    wb.reloadActive()
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: Tools>Customize>Macros in user.cfg

Post by drmacro »

chennes wrote: Wed Sep 28, 2022 7:56 pm Only a tiny bit of the Addon Manager code there is really relevant to your case: the FreeCADGui.Command.createCustomCommand is the real workhorse, everything around it is mostly UI niceties so the user can place the buttons where they want them. I'm happy to explain individual bits if it would help, but overall what you need is probably just something like:

Code: Select all

        custom_toolbar = FreeCAD.ParamGet(
            "User parameter:BaseApp/Workbench/Global/Toolbar/MyCustomToolbar"
        )
    command_name = FreeCADGui.Command.createCustomCommand(
        "MyGreatMacro.FCMacro",
        "Menu text here",
        "This is a tooltip",
        "This is the what's this text",
        "This is the status tip",
        "icon_file.xpm",
    )
   custom_toolbar.SetString(command_name, "FreeCAD")

    # Force the toolbars to be recreated
    wb = FreeCADGui.activeWorkbench()
    wb.reloadActive()
I my mind I'm trying to mimic the process of a user creating one or more "commands" in the Tools>Customize, Macros tab.
(if I understand the code above, this is equivalent to FreeCADGui.Command.createCustomCommand)

These are then available to the user in the Tools>Customize, Toolbars tab in the Macros "toolbar" and can be added to one or more workbench toolbars.
(Again, if I understand the code above, this is equivalent to FreeCAD.ParamGet and then custom_toolbar.SetString and I would do this for each toolbar where I might want the "commands".)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: Tools>Customize>Macros in user.cfg

Post by drmacro »

@chennes

Have been experimenting.

Delete user.cfg and start FC. Parameter Editor looks like:
FreshParams.png
FreshParams.png (27.91 KiB) Viewed 901 times
Run the following:

Code: Select all

custom_toolbar = FreeCAD.ParamGet("User parameter:BaseApp/Workbench/Global/Toolbar/MyCustomToolbar")
command_name = FreeCADGui.Command.createCustomCommand(
    "Snip.FCMacro",
    "Screen cap",
    "This is a tooltip",
    "This is the what's this text",
    "This is the status tip",
    "camera-photo.svg",)
custom_toolbar.SetString(command_name, "FreeCAD")

# Force the toolbars to be recreated
wb = FreeCADGui.activeWorkbench()
wb.reloadActive()
And params look like:
AfterParams.png
AfterParams.png (35.15 KiB) Viewed 901 times
Tools>Customize, Macros tab:
AfterMacros.png
AfterMacros.png (34.5 KiB) Viewed 901 times
Tools>Customize, Toolbars tab (Note: name blank...):
ToolbarsGlobal.png
ToolbarsGlobal.png (43.71 KiB) Viewed 901 times
Close the Tool>Customize dialog and look at parameters:
ParamsAfterCustomize.png
ParamsAfterCustomize.png (38.24 KiB) Viewed 901 times
ParamsAfterCustomize.png
ParamsAfterCustomize.png (38.24 KiB) Viewed 901 times
Note: Custom_1 has been created and taken contents of MyCustomToolbar and the later is now empty.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
chennes
Veteran
Posts: 3883
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Tools>Customize>Macros in user.cfg

Post by chennes »

Probably I over-edited my example code: I think in the AddonManager I do use the “custom_#” toolbar name. If you retry the process using custom_1 does it work as expected?
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: Tools>Customize>Macros in user.cfg

Post by drmacro »

chennes wrote: Thu Sep 29, 2022 2:10 pm Probably I over-edited my example code: I think in the AddonManager I do use the “custom_#” toolbar name. If you retry the process using custom_1 does it work as expected?
Interesting note, reference last post. Every time I open Tools>Customize, Toolbars tab, Global. Another blank Custom_# command is created consuming the contents of the lesser numbered Custom_#.

To answer you question: It appears Custom_1 creates Custom_1. The Name parameter is still blank. Std_Macro_0 is set to FreeCAD. But, accessing Tools>Customize, Toolbars tab, Global does not keep adding new commands.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply