SketchUcation Plugin Store

 

 

Beginner Ruby Problmes

Beginner Ruby Problmes

Postby Arkman » Thu Nov 12, 2009 6:50 pm

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!
Arkman
 
Posts: 7
Joined: Thu Nov 12, 2009 6:15 pm
Name: Josh

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 7:19 pm

Code: Select all
#-----------------------------------------------------------------------------
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')
#-----------------------------------------------------------------------------
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby Arkman » Thu Nov 12, 2009 8:47 pm

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
 
Posts: 7
Joined: Thu Nov 12, 2009 6:15 pm
Name: Josh

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 8:58 pm

Arkman wrote: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/do ... ayers.html
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby Arkman » Thu Nov 12, 2009 9:33 pm

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.
Last edited by Arkman on Thu Nov 12, 2009 9:47 pm, edited 1 time in total.
Arkman
 
Posts: 7
Joined: Thu Nov 12, 2009 6:15 pm
Name: Josh

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 9:45 pm

Code: Select all
#-----------------------------------------------------------------------------
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')
#-----------------------------------------------------------------------------
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 9:47 pm

Arkman wrote: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)
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby Arkman » Thu Nov 12, 2009 9:50 pm

Incredible! Thank you so much. You just made my life a lot easier! :berserk:
Arkman
 
Posts: 7
Joined: Thu Nov 12, 2009 6:15 pm
Name: Josh

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 9:58 pm

Maybe I should post it as a downloadable plugin?
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby Arkman » Thu Nov 12, 2009 10:28 pm

That would probably be extremely helpful. I know a lot of people that could use something like this.

Thanks again!!

Josh
Arkman
 
Posts: 7
Joined: Thu Nov 12, 2009 6:15 pm
Name: Josh

Re: Beginner Ruby Problmes

Postby thomthom » Thu Nov 12, 2009 10:36 pm

Posted it in a separate plugin for easy searching: viewtopic.php?f=180&t=23582
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Beginner Ruby Problmes

Postby Scott M » Wed Feb 29, 2012 1:05 pm

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?
Scott M
 
Posts: 51
Joined: Sat Oct 16, 2010 1:43 pm

Re: Beginner Ruby Problmes

Postby thomthom » Wed Feb 29, 2012 5:21 pm

There are some sticky thread in the Developer section where links to various best-practices is posted.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17595
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Vertex Tools for SketchUp. Take control over each vertex with this vertex editor for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: Bing [Bot], loco, tomot and 7 guests