Entities Grouped by layers and group


#1

Q1) Whats are the ways can I group all the entities in a model according to their layers and groups?
Basically am doing an exercise wherein I have to be able to “puts” the layer the entity is on based on selection. i.e I print the layer for the entity on console.

Q2) Similar to Q1 how do I figure out the “Applied Layer of the active group”, and
Q3) Active material on Applied layer

I have figured out most of the tasks from my assignment list except the mentioned.

Just started with the plugin development pardon me if my question sounds too stupid for the forum but I have already spent 2 sleepless nights looking through the forums and boards with no luck.

Please help guys, Cheers!


#2

I don’t understand Q1. Do you want to create a SketchUp::Group, or are you using “group” loosely to mean “collection”? The second sentence doesn’t seem to follow the first, as you write about printing for a single entity and there is no reason to group anything to print the layer of the selected entity. Assuming you have selected a single Entity, just do

puts Sketchup.active_model.selection[0].layer

If the selected entity is a SketchUp::Group, it has #layer and #material methods inherited from Drawingelement, so invoke them as above.

It feels as though I didn’t understand what you are trying to do…?


#3

Q1) It’s better understandable if you express more precisely what you mean, especially in development. If there is a collision between a generic (every-day language) word ‘group’ and a technical term Sketchup::Group, you might better use a synonym to distinguish the former.

I believe you want to “aggregate” objects. Data structures for aggregation are sets and most times lists (Array). Separating objects into sets (or arrays) by a common property is also known as ‘partitioning’.

Now speaking in Ruby, do I understand right that you want to create Arrays where each array contains Sketchup::DrawingElement objects that have the same Sketchup::DrawingElement#layer (analog for material)?
This is a simple algorithmic problem, and algorithms are not specifically programming problems, but instructions for “how to do stuff” to achieve a desired result. Imagine you have playing cards, how would you do it by hand?
You take off one card after the other, for each you check the color. If you have seen the color before, you put the card on a stack of the same color. If you haven’t had the color before, you create a new stack for that color and put the card on it.

partitions = {}
cards.each{ |card|
  color = card.color
  if partitions.include?(color)
    partitions[color].push(card)
  else
    partitions[color] = [] 
    partitions[color].push(card)
  end
}

P.S.: Just curious, is this homework?


#4

Thanks !

do I understand right that you want to create Arrays where each array contains Sketchup::DrawingElement objects that have the same Sketchup::DrawingElement#layer

Yes! Thats what I was required to do, I just needed to print out the entities inside each layer.

P.S.: Just curious, is this homework?

Yep it it was a homework for my Programming class ,


#5

Sorry I didn’t frame the question right

If the selected entity is a SketchUp::Group, it has #layer and #material methods inherited from Drawingelement

Thanks for this though!

At this point not everything is very clear to me. Just feeling my way out to achieve whats on my assignment list.