Beginner Ruby Problmes
-
I'm trying to create a ruby that when I create a group it just gives me a prompt to name the group. Just like it does for a component. I'm really struggling.
Any help would be greatly appreciated.
Thanks!
-
#----------------------------------------------------------------------------- require 'sketchup.rb' #----------------------------------------------------------------------------- module AM_Named_Group unless file_loaded?('am_named_group.rb') # Add menu items. plugins_menu = UI.menu('Edit') plugins_menu.add_item('Make Named Group') { self.named_group } end def self.named_group model = Sketchup.active_model sel = model.selection # Don't do anything if the selection is empty. return if sel.empty? # Ask user for group name. prompts = ['Group Name; '] defaults = [model.definitions.unique_name('Group#1')] input = UI.inputbox(prompts, defaults, 'New Group') # Check if the user cancelled. return if input == false # The add_group method can be buggy when you pass on entities to it's argument. But there's # no real options. group = model.active_entities.add_group(sel) name = model.definitions.unique_name(input[0]) # We set the name we will see in the Entities window. group.name = name # We set the definition name as well. If you convert the group to a component, this is the # name it will get. group.entities.parent.name = name # Select the new group sel.clear sel.add(group) end end #----------------------------------------------------------------------------- file_loaded('am_named_group.rb') #-----------------------------------------------------------------------------
-
Awesome! I have one more request. How could I get a list of layers in the model to define what layer it lives in. I can get a list but I can't get it to show the layers in the model or place that newly created group on the layer.
Thanks!!!!
-
@arkman said:
Awesome! I have one more request. How could I get a list of layers in the model to define what layer it lives in. I can get a list but I can't get it to show the layers in the model or place that newly created group on the layer.
Thanks!!!!
You can get a list? But you can't get it to show the layers in the model...
I'm not quite following you. What list are you "getting"? You mean a list in the inputbox?All the layers are under
Skethcup.active_model.layers
http://code.google.com/apis/sketchup/docs/ourdoc/layers.html -
#----------------------------------------------------------------------------- require 'sketchup.rb' #----------------------------------------------------------------------------- module AM_Named_Group unless file_loaded?('am_named_group.rb') # Add menu items. plugins_menu = UI.menu('Edit') plugins_menu.add_item('Make Named Group') { self.named_group } end def self.named_group model = Sketchup.active_model sel = model.selection # Don't do anything if the selection is empty. return if sel.empty? # Build an array of the layer names. layers = model.layers.to_a.collect { |l| l.name } # Ask user for group name. list = ['', layers.join('|')] prompts = ['Group Name; ', 'Layer; '] defaults = [model.definitions.unique_name('Group#1'), model.active_layer.name] input = UI.inputbox(prompts, defaults, list, 'New Group') # Check if the user cancelled. return if input == false # Get data from result array into variables group_name, group_layer = input # Make it into one Undoable action model.start_operation('Make Named Group') # The add_group method can be buggy when you pass on entities to it's argument. But there's # no real options. group = model.active_entities.add_group(sel) name = model.definitions.unique_name(group_name) # We set the name we will see in the Entities window. group.name = name # We set the definition name as well. If you convert the group to a component, this is the # name it will get. group.entities.parent.name = name # Set group layer group.layer = model.layers[group_layer] # Select the new group sel.clear sel.add(group) model.commit_operation end end #----------------------------------------------------------------------------- file_loaded('am_named_group.rb') #-----------------------------------------------------------------------------
-
Ok, sorry that was unclear.
I can add a list to the inputbox but I cant get that list to show the layers in the model. I can make a list with just random terms that I picked. Like below.
list = ["item1|item2"]
So I guess my question is "How do I get a list in the inputbox that shows all the layers in the model?" I found that help file you showed me but I dont know how to use that. I tried:
list = [Sketchup.active_model.layers]
That didn't work. I'm not really sure how to get the list to use that Skethcup.active_model.layers
Once the list displays the layers in the model, how do I make the object go onto the layer selected in the inputbox?
Thanks for all your help. Sorry for all the questions.
-
@arkman said:
list = [Skethcup.active_model.layers]
That didn't work. I'm not really sure how to get the list to use that Skethcup.active_model.layers
That's because Skethcup.active_model.layers is a collection of Layer objects. You need to collect all the layer names from each object. You see an example in the code I posted.
(And
Skethcup
was mis-spelled) -
Incredible! Thank you so much. You just made my life a lot easier!
-
Maybe I should post it as a downloadable plugin?
-
That would probably be extremely helpful. I know a lot of people that could use something like this.
Thanks again!!
Josh
-
Posted it in a separate plugin for easy searching: http://forums.sketchucation.com/viewtopic.php?f=180&t=23582
-
Ththomthom I am Just starting out learning Ruby I made a plugin to create a simple rectangle based on the tutorial and would like to insert the name group module you posted could you give me some pointers?
-
There are some sticky thread in the Developer section where links to various best-practices is posted.
Advertisement