Problems saving user preferences
-
I am trying to save and retrieve user selected preferences. To read the data:
values=[] if FileTest.exist?("C;\\Dxf_In.Dat") dataFile = File.open("C;\\Dxf_In.Dat", "r") dataFile.each_line do |e| values.push e end dataFile.close else values=["None","Inches","Model","to Faces","by Number"] end
Its used here
results = inputbox prompts, values, enums, my_file_name+" Options"
and after the options are selected by the user, it is saved by the following:
File.open("C;\\Dxf_In.Dat", "w") do |dataFile| dataFile.puts ["None",scale_sel,origin_sel,dface_sel,mater_sel] end
When I
puts value
to test the code asvalues.push e
andvalues=["None","Inches","Model","to Faces","by Number"]
, it displays in the "Ruby Console", example as follows:None Inches Model to Faces by Number
value
generated byvalues.push e
, when accessed byresults = inputbox prompts, values, enums, my_file_name+" Options"
results in a blank input box. Whenvalues
is the result ofvalues=["None","Inches","Model","to Faces","by Number"]
, the default selections are viewed in the inputbox. Any Ideas as to what I could be doing wrong?
-
Why not use
Sketchup.write_default
andSketchup.read_default
? -
Why not save the user's preferences as an attribute with the SKP model itself?
This way they are remembered individually with the model across sessions.
Initializing with
dlist=["None","Inches","Model","to Faces","by Number"]
You get the startup 'prefs' thus
prefs=model.get_attribute("DXFdata","prefs",dlist)
This will then be either as they were last saved OR the default 'dlist'...
Later after they have OK'd a dialog and changed their 'prefs' you'll simply remember the new 'prefs' again with
model.set_attribute("DXFdata","prefs",prefs)
Saving to a temp file means it might not exists later and temp folders vary across platforms etc...
Alternatively make it an 'ini' file saved with the model itself ? or a common .ini. saved in your Plugins [sub]folder - though 'access-rights' might not always be good!Alternatively you could save the users values with SketchUp itself [so they are common across all SKPs once saved] using the paired methods
Sketchup.read_default(section,variable,default) Sketchup.write_default(section,variable,value)
The section could be named 'DXFdata'
You could have a separate variables for each 'pref' OR make one block string called 'prefstring' by adding say a ':' between them [assuming there are no ':' used in the text otherwise...]...
On reading them you'd useprefsarray=prefstring.split(":")
to get an array.
On writing you'd useprefstring=prefsarray.join(":")
to make a string. -
Don't know what they are. I will look them up, thanks.
-
The following lines of code:
values=[] result = Sketchup.read_default "section", values, ["None","Inches","Model","to Faces","by Number"]
are getting the following error in the Ruby Console:
Error: #<TypeError: can't convert Array into String> C:/Program Files/Google/Google SketchUp 8/Plugins/my_dxf_In_v1.13.rb:82:in
read_default'`Do I have to open the registry and do what? Well I found
-
@honoluludesktop said:
The following lines of code:
values=[] result = Sketchup.read_default "section", values, ["None","Inches","Model","to Faces","by Number"]
are getting the following error in the Ruby Console:
Error: #<TypeError: can't convert Array into String> C:/Program Files/Google/Google SketchUp 8/Plugins/my_dxf_In_v1.13.rb:82:in
read_default'`
Do I have to open the registry and do what? Well I foundRe-read my post!
You can only pass a string to the Registry- so you must first make your Ruby array into a string using 'join'
` array=["None","Inches","Model","to Faces","by Number"]
str=array.join(":")
"None:Inches:Model:to Faces:by Number"
Then write '
str' using
write_default(). To read '
value' string back into a Ruby array use 'split'
value="None:Inches:Model:to Faces:by Number"
array=value.split(":")
["None","Inches","Model","to Faces","by Number"]` - so you must first make your Ruby array into a string using 'join'
-
Tig, I missed your post, while looking up
Sketchup.read_default
. Then...sigh.... my internet provider went down. OK I am back on line, and can read it. -
Wow,
Sketchup.read_default
andSketchup.write_default
were easy to implement. Attaching the defaults to individual models can also be advantageous, and I will have to think about the best way to save user defaults. Thanks guys.a_values=[] s_value="" s_value = Sketchup.read_default "honoluludesktop","values","None:Inches:Model:to Faces:by Number" a_values = s_value.split(":") . . results = inputbox prompts,a_values,enums,my_file_name+" Options" . . a_values = ["None",scale_sel,origin_sel,dface_sel,material_sel] s_value = a_values.join(":") result = Sketchup.write_default "honoluludesktop","values",s_value
Advertisement