Script User Options
-
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.
-
@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.
-
Thank you , that makes things very straightforward and easy.
Advertisement