Layer structure/tree for a given entity(s) in a model?


#1

Hi guys! How do I access the Layer structure for a given entity(s) in a model?

e.g
–>Layer0 has LayerWalls and LayerRoofs
–>LayerWalls has LayerWindows,LayerDoors and LayerVentilator
–>LayerWindows has LayerWindowFrame and LayerWindowPanes

How can I access a tree of the Layers for layer WindowFrames , as in when the user selects a window frame it should show its parent layers in a structured way as:

Layer0->LayerWalls->LayerWindows->LayerWindowFrame

Thanks in advance.


#2

SketchUp has no nested layers / layer tree hierarchy. Because of that an entity has exactly one layer and no parent layers.

Can you explain what exactly you have in mind? If you refer to a certain extension, could you mention which?
Or have you “emulated” a layer hierarchy by using a strict naming convention (child layer name = parent layer name + suffix)?
Or are you thinking of the component hierarchy, and want to get the layers of the entity and the layers of all its parent components?


#3

Your wording also suggests you may be thinking of “layers” the way other CAD programs use the term, as content separators, rather than the way SketchUp uses it, as visibility controls.


#4

Sorry, I should have known better. I though layers followed the similar structure as they do in other modelling tools.

so as you pointed

Could you give me a pointer as to how could I achieve this?

All I can think of currently is

 layers=Sketchup.active_models.layers

but that would print out all the layers. Right?
How could I print only the selected entity(s) layers and also its parent’s layer using the SelectionObserver?

Thanks for your help! :smile:


#5

@slbaumgartner My mistake, thats exactly what was was thinking. So what I can do now is get the entity(s) layer and all the layer of its parents components.


#6

An entity (which may be a component instance) is contained either in the model or in a component definition. This results in a hierarchy of containment. (As mentioned earlier, SketchUp layers have neither a hierarchy nor an order).

If an entity is contained in a component, you can encounter it in many instances of that component. So the way from an ‘entity’ to its containing component instance is ambiguous. If you use a selection observer, it will select only in the active entities anyways, so we could take this as reference.

layers = (entity.model.active_path.nil?) ? [] : 
  entity.model.active_path.map{ |component_instance| component_instance.layer }
layers << entity.layer

(You may also want to make this array .uniq and get the layer names .map{ |layer| layer.name } ).

If you just want to print:

unless entity.model.active_path.nil?
  entity.model.active_path.each{ |component_instance|
    puts "#{component_instance} is on layer '#{component_instance.layer}'"
  }
end
puts "#{entity} is on layer '#{entity.layer}'"

This does not mean that the entity is on all these layers, the entity is exactly on one layer. It just means that all these layers must be visible, so that all containing component instances are visible, so that the entity can be seen.