[SOLVED] Determine if a System Setting Exists or Not

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

[SOLVED] Determine if a System Setting Exists or Not

Post by cblt2l »

Take the following example:

Code: Select all

>>> grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/TEST")
>>> grp.GetFloat("RANDOM")
0.0
I would like to know if RANDOM has a value of 0.0 OR if it exists as a freecad setting (i.e. it is or is not in the system.cfg file). Unfortunately the method IsEmpty() only tells you if TEST is empty or not.

Here is an example of what I'm trying to do (grp.Exists is made up by me). I'm not sure the best way to accomplish it. Any ideas :?:

Code: Select all

grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/TEST")

if not grp.Exists("RANDOM"):
    # Initialize RANDOM in system.cfg
    grp.SetFloat("RANDOM", 5.0)
else:
   return grp.GetFloat("RANDOM")

# Export all of the settings in TEST to a text file.
grp.Export("/home/user/.FreeCAD/SETTINGSLIST.txt")
Last edited by cblt2l on Mon May 05, 2014 9:20 pm, edited 1 time in total.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Determine if a System Setting Exists or Not

Post by shoogen »

in this special case you could supply any non-zero default value. As long as you only use the result for the test.

Code: Select all

return grp.GetFloat("RANDOM",-1e200) != 0.0
to check if it is not zero
or

Code: Select all

abs(grp.GetFloat("RANDOM",-1e200)) > 1e-6
to check if it is not small
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

Re: Determine if a System Setting Exists or Not

Post by cblt2l »

Thanks shoogen. That does the trick. :P
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by shoogen »

but the normal way would be to define your default value in a globally visible variable

Code: Select all

RandomDefault = 5.0
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/TEST")


grp.GetFloat("RANDOM",RANDOMDefault)
This will use the default as, long as the Value is not set.
But if there is a Preferences pages for that Value, you will also need to set the default there.
This will set the default, when the user opens the preferences Page for the first time.
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by shoogen »

shoogen wrote:but the normal way would be to define your default value in a globally visible variable

Code: Select all

RandomDefault = 5.0
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/TEST")


grp.GetFloat("RANDOM",RANDOMDefault)
This will use the default as, long as the Value is not set.
But if there is a Preferences pages for that Value, you will also need to set the default there.
This will set the default, when the user opens the preferences Page for the first time.
The test i proposed before, makes only sense, if the value of 0.0 is forbidden and has to be replaced every time.
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by cblt2l »

shoogen wrote:but the normal way would be to define your default value in a globally visible variable

Code: Select all

RandomDefault = 5.0
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/TEST")


grp.GetFloat("RANDOM",RANDOMDefault)
For the record, this all applies to the curaengine macro that I have been working on.

All of the default settings (approx 58 of them) are stored in a dictionary with methods similar to what you posted for reading the variables. Basically, the settings are all read from the dictionary until the user changes them in the gui. Then they are written internally....

Long story short, I needed a way to import and export the settings from the macro. For this I decided to use grp.Export and grp.Import, which works perfectly except for the fact that unless the user has manually changed a setting in the gui, the setting is not written to system.cfg and therefore is not included in the exported settings file. So basically I needed a way to make sure that every setting in the macro is written to internally to freecad so they can all be exported. I added a routine here which uses your original method to check if the setting is exists internally. If not it writes the default value (from dictionary) to system.cfg.

There may be a better way but so far its working good. I hope my explanation makes sense. :?
User avatar
quick61
Veteran
Posts: 3803
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by quick61 »

Maybe I'm missing something, but when I click on export settings, nothing happens, and when I click OK, I get this in my report view -

Code: Select all

Traceback (most recent call last):
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 147, in accept
    retVal = self.sliceParts(self.Vars.readMisc("CuraPath"), stlParts)
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 181, in sliceParts
    _cmdList = self.getSettings()
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 209, in getSettings
    _tmpDic = self.Vars.exportSettings()
<type 'exceptions.AttributeError'>: SliceDef instance has no attribute 'exportSettings'
Did I miss something?

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
cblt2l
Posts: 155
Joined: Sat May 15, 2010 3:59 am

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by cblt2l »

quick61 wrote:Maybe I'm missing something, but when I click on export settings, nothing happens, and when I click OK, I get this in my report view -

Code: Select all

Traceback (most recent call last):
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 147, in accept
    retVal = self.sliceParts(self.Vars.readMisc("CuraPath"), stlParts)
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 181, in sliceParts
    _cmdList = self.getSettings()
  File "/home/mark/FreeCAD/Macro/CuraEngineSlicerPanel.FCMacro", line 209, in getSettings
    _tmpDic = self.Vars.exportSettings()
<type 'exceptions.AttributeError'>: SliceDef instance has no attribute 'exportSettings'
Did I miss something?

Mark
What is "CuraEngineSlicerPanel.FCMacro"? It looks like an older version of SlicerPanel.py, which is missing all of the export stuff that I added.

If you cloned the git repo just run "SlicerPanel.py"
User avatar
quick61
Veteran
Posts: 3803
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: [SOLVED] Determine if a System Setting Exists or Not

Post by quick61 »

Oh, duh... OK not sure how that was working with the new ones but I had that toolbar shortcut set up from the beginning. Never went back to check if it was still valid. This explains some things. It's all working now, Well all except the Toolbar shortcuts. They wont work with .py files. I think there was a feature request for that a while back, but I don't think anybody got around to it. Only .fcmacro files can be set up to run from a custom toolbar.

Anyway, looking good now. Another very useful feature in an excellent plugin.

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
Post Reply