sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    How to store web-dialog parameters/settings?

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 704 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • renderizaR Offline
      renderiza
      last edited by

      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!

      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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 use read_default() to get it back, the type of data you can store is limited; there are 'tricks' like saving an array as a string and using eval 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 users temp=ENV['TEMP'] folder - need tests to find the different ones for different OS's, but that can vanish in a system tidy up, so temp=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...]

        TIG

        1 Reply Last reply Reply Quote 0
        • jiminy-billy-bobJ Offline
          jiminy-billy-bob
          last edited by

          @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       &nbsp;</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    &nbsp;</span><span class="syntaxdefault"></span>
          

          And JS uses this information ("vray") to display Vray's render buttons.

          Hope this helps 😉

          25% off Skatter for SketchUcation Premium Members

          1 Reply Last reply Reply Quote 0
          • renderizaR Offline
            renderiza
            last edited by

            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!

            [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

            1 Reply Last reply Reply Quote 0
            • renderizaR Offline
              renderiza
              last edited by

              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.

              [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement