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

Problems saving user preferences

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 3 Posters 368 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.
  • H Offline
    honoluludesktop
    last edited by 21 Oct 2010, 10:05

    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 as values.push e and values=["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 by values.push e, when accessed by results = inputbox prompts, values, enums, my_file_name+" Options" results in a blank input box. When values is the result of values=["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?


    Blank inputbox via values.push e and desired inputbox by value = [[xxxx],[xxxx],....](see above)

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 21 Oct 2010, 10:19

      Why not use Sketchup.write_default and Sketchup.read_default ?

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 21 Oct 2010, 10:25

        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 use prefsarray=prefstring.split(":") to get an array.
        On writing you'd use prefstring=prefsarray.join(":") to make a string.

        TIG

        1 Reply Last reply Reply Quote 0
        • H Offline
          honoluludesktop
          last edited by 21 Oct 2010, 10:27

          Don't know what they are. I will look them up, thanks.

          1 Reply Last reply Reply Quote 0
          • H Offline
            honoluludesktop
            last edited by 21 Oct 2010, 10:41

            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:inread_default'`

            Do I have to open the registry and do what? Well I found

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 21 Oct 2010, 11:19

              @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:inread_default'`
              Do I have to open the registry and do what? Well I found

              Re-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"]`

              TIG

              1 Reply Last reply Reply Quote 0
              • H Offline
                honoluludesktop
                last edited by 21 Oct 2010, 11:28

                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.

                1 Reply Last reply Reply Quote 0
                • H Offline
                  honoluludesktop
                  last edited by 21 Oct 2010, 12:55

                  Wow, Sketchup.read_default and Sketchup.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

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

                  Advertisement