• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Script User Options

Scheduled Pinned Locked Moved Developers' Forum
3 Posts 2 Posters 553 Views 2 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.
  • A Offline
    architectboy
    last edited by 18 Dec 2007, 05:30

    Is there a easy way to make options in a ruby script? I want to have a few user options that wont reset every time the script loads. The only way that I can think of is making a text file and somehow reading and editing it within the script.

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 18 Dec 2007, 05:31

      @unknownuser said:

      Is there a easy way to make options in a ruby script? I want to have a few user options that wont reset every time the script loads. The only way that I can think of is making a text file and somehow reading and editing it within the script.

      Making the tool as a 'Class' and then setting a user's choices as @xxx named class-specific variables remembers those for that tool during that session. Similarly, using $xxx variable names is set as global per session, BUT this is not recommended usually as these might clash with other global variables set by other scripts or extensions etc...
      Here's an example of @xxx use:

      ### PathMemory.rb
      require 'sketchup.rb'
      class PathMemory
      def PathMemory;;memorize
         model=Sketchup.active_model; @memory=[]; model.selection.each{|e|@memory.push(e)if e.typename=="Edge"}
         ### remembers just the edges in a selection in a persistent @xxx variable array called @memory.
      end
      def PathMemory;;recall
         model=Sketchup.active_model; model.selection.clear; model.selection.add(@memory)
      end
      end#class
      # add context menu items
      if( not file_loaded?("PathMemory.rb") )
        UI.add_context_menu_handler do |menu|
          menu.add_separator ### this adds a seperator line
          submenu=menu.add_submenu("Path Memory...")
          submenu.add_item("Memorize"){PathMemory.memorize}
          submenu.add_item("Recall"){PathMemory.recall}
        end#do menu
      end#if
      ###
      file_loaded("PathMemory.rb")
      

      If you want to remember a user's last-chosen general settings across different sessions then use variables that are written / saved as 'default settings' (which is in essence like writing/reading a text file of the settings BUT uses 'the registry') [ http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-Sketchup.html#read_default and http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-Sketchup.html#write_defaults ].

      If the variables relate to a specific instance of something that you are making/editing then writing/reading Attributes which are attached to that entity will persist across sessions too [ http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-Entity.html#get_attribute and http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-Entity.html#set_attribute ] - an entity can be anything, from the whole model, to a group or component, right down to just one face or edge.

      TIG

      1 Reply Last reply Reply Quote 0
      • A Offline
        architectboy
        last edited by 18 Dec 2007, 05:31

        Thank you , that makes things very straightforward and easy. 😄

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

        Advertisement