How to store web-dialog parameters/settings?
-
What would be ideal for storing web-dialog parameters/settings?
I already tried to use HTML5 local storage but it seems not work locally without having a server installed (don't have server installed).
jiminy-billy-bob suggested Sketchup.write_default ... Is this the best option? Can someone post an easy to follow example on how to implement it?
Currently, when storing data I tend to use Sketch Up entity type group for its name or description space but I wan to learn other ways. Thanks in advance!
-
There are a number of ways of storing data, both inside and outside of SketchUp and SKPs.
You can store data globally 'for SketchUp' for that user using:Sketchup.write_default(section, variable, value)
and then useread_default()
to get it back, the type of data you can store is limited; there are 'tricks' like saving anarray
as a string and usingeval
on it after you read it back in... Many authors store some data this way.
You can write data into a file [and read it back] in the userstemp=ENV['TEMP']
folder - need tests to find the different ones for different OS's, but that can vanish in a system tidy up, sotemp=File.dirname(temp)
might use the enduring container of that 'Temp' folder, instead [make your on directory in there to store your data...]. That's how the Plugins Manager Sets work.
You can set/get attributes for a Model, which will endure across sessions if the Model is saved, so Model Specific settings will be remembered. Writing such data files to Plugins is fraught as permissions to the Plugins folder can limit access !
You can set/get attributes for a 'Thing' within a Model [Instance/Layer etc], which will endure across sessions if the Model is saved, so 'Thing' Specific settings will be remembered. You run the risk of it getting deleted though !
During a SketchUp session using@xxx
variables in Modules or@@xxx
variables in Classes can remember those settings from one use of a tool to the next, just during that SketchUp session - those variables default when SketchUp first restarts. This is how input dialogs can remember the last used values, rather than always default them... [See my recent 'TIG-scale2volume' plugin for a very simple example of this, where it 'remembers' the units and anchor types that the user has initially chosen when the tool is first used, but not the previously entered volume...] -
@renderiza said:
jiminy-billy-bob suggested Sketchup.write_default ... Is this the best option? Can someone post an easy to follow example on how to implement it?
For example, this is the way I store which render engine the user has selected in the list of the latest version of Layers Panel :
For instance, when he clicks on "Vray", I have a javascript function sending to ruby
window.location = 'skp:useRenderEngine@vray'
A callback receives it and store "vray" in the registry
<span class="syntaxdefault"></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"useRenderEngine"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">wdl</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> engine</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">write_default</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"jbb_layers_panel"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"render_engine"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> engine</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#callback </span><span class="syntaxdefault"></span>
Then when the user opens up again the Webdialog, a JS function is triggered when the document is ready, and asks ruby what is stored in the registry :
window.location = 'skp:getRenderEngine';
Ruby reads the registry and answers to JS :
<span class="syntaxdefault"></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"getRenderEngine"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">wdl</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> action</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> engine </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read_default</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"jbb_layers_panel"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"render_engine"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> useRenderEngine </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"useRenderEngine('#{engine}');"<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">useRenderEngine</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#callback </span><span class="syntaxdefault"></span>
And JS uses this information ("vray") to display Vray's render buttons.
Hope this helps
-
TIG
That is very valuable info thank you for sharing!
jiminy-billy-bob
Thank you for the example I am currently trying to apply it to my plugin... Hope it will work!(crossing fingers)
Will let you know if it worked or not soon...Cheers!
-
It works!
Did simple test that saved some of the web-dialog settings.
def activate @@check = "start" end def deactivate(view) @@check = "end" end def push_frame(dialog,data) params = query_to_hash(data) ################ ## START ## ################ if @@check == "start" @@check = "no" saved = Sketchup.read_default("canvas_panel", "store_dlg") UI.messagebox saved #Will prompt last selected value before closing from web-dialog end ################ ## END ## ################ if @@check == "end" @@check = "no" inp = params['sel_ip'].to_s #This is value from web-dialog Sketchup.write_default("canvas_panel", "store_dlg", inp) end end
Need to do what TIG suggested which was to make array into string to store multiple web-dialog settings. After that will have to use that data to make web-dialog use the settings last used when it was last closed.
Advertisement