Place for parameters for macros

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
galou_breizh
Posts: 362
Joined: Wed Sep 15, 2010 9:38 am

Place for parameters for macros

Postby galou_breizh » Sat May 02, 2020 6:09 am

Some macros start to use parameters to save some information. My question is where should these parameters be?

Candidates for macro 'MyMacro':
- 'User parameter:BaseApp/Preferences/Mod/MyMacro'
- 'User parameter:BaseApp/Preferences/Mod/Macros/MyMacro'
- 'User parameter:BaseApp/Preferences/Macros/MyMacro'
- 'User parameter:BaseApp/Macro/MyMacro'
- 'User parameter:BaseApp/Macro/Macros/MyMacro'
- 'User parameter:Plugins/MyMacro'
- 'User parameter:Plugins/Macros/MyMacro'

My choice would be 'User parameter:BaseApp/Preferences/Macros/MyMacro' but the path 'User parameter:BaseApp/Preferences/Macros' doesn't exist yet.

By the way, the "User parameters" are quite a mess but I guess nobody wants to clean them because it'll just bring troubles.

Thanks for your feedback!
Gaël
vocx
Posts: 5205
Joined: Thu Oct 18, 2018 9:18 pm

Re: Place for parameters for macros

Postby vocx » Sat May 02, 2020 4:17 pm

galou_breizh wrote: Sat May 02, 2020 6:09 am Some macros start to use parameters to save some information. My question is where should these parameters be?
...
I think a macro should probably not store preferences. A macro is more like short snippet of code that is supposed to be self-contained in a single file.

Once you require preferences, then that tells me it is becoming a more complex piece of software, and it makes sense to adopt a more workbench-like structure.
...but the path 'User parameter:BaseApp/Preferences/Macros' doesn't exist yet.
Parameter paths are created automatically when set for the first time, so in general, they can be renamed easily. Although, of course, renaming a parameter will result in losing the previously saved one. Well, the old parameter won't be deleted, it just won't be used. But you could manually migrate it as well.

Code: Select all

old_pth = App.ParamGet("old/path/")
new_pth = App.ParamGet("new/path/")

old = old_pth.GetInt("old_parameter", 0)  # use it if it exists or the default 0
new_pth.SetInt("new_parameter", old)  # set old value as default
new = new_pth.GetInt("new_parameter", 0)
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.