Select all in layer
-
Hi everyone,
I was wondering if it was possible to select all objects in a given layer in code, without having to pre-select an object in the same layer?
I know it's possible through various plugins to add to the selection all the objects that are in the same layer as the selected one, but I would like to do it without having to make this pre-selection.
Thanks for your help!
-
All you need is a reference to the required layer - that's all you get from the preselected object with
layer=object.layer
in any case.
So if you know its name (e.g. "MyLayer") then you can usexlayer=Sketchup.active_model.layers["MyLayer"]
.
Now you can select all active entities that share that layer with
Sketchup.active_model.selection.clear Sketchup.active_model.active_entities.each{|e|Sketchup.active_model.selection.add(e) if e.layer==xlayer}
or alternatively do it by layer name directly [comparing strings is probably slower]
Sketchup.active_model.selection.clear Sketchup.active_model.active_entities.each{|e|Sketchup.active_model.selection.add(e) if e.layer.name=="MyLayer"}
etc etc... -
Thanks a lot that was helpful!
-
@tig said:
comparing strings is probably slower
It is. Much slower! At all times avoid string comparisons if possible.
Advertisement