Create Layers From Component Names
-
Hi,
I am working on a model that has a very large number of imported components.
What I would like to do is run a script that creates a layer for each component. Ideally the layer name would be identical to the component name, and each component would be placed on the corresponding layer.
Has anybody come across something similar to this?
I hope this is clear.
Ross
-
Copy/paste this one-liner into the Ruby Console + <enter>, it's one step undoable.
m=Sketchup.active_model;m.start_operation('c');m.definitions.each{|d|next if d.group? || d.image?;n=d.name;l=m.layers.add(n);d.instances.each{|i|i.layer=l}};m.commit_operation
All instances of every component-definition are placed on a layer named after the definition-name...
-
TIG - thanks so much for that.
Would it be possible to revise the code to ignore any nested components? I guess this could be done with a simple change to the definition?
Also could the layer colour be fixed to black?
Thanks so much for your help
Ross
-
@rossthompson said:
TIG - thanks so much for that.
Would it be possible to revise the code to ignore any nested components? I guess this could be done with a simple change to the definition?
Also could the layer colour be fixed to black?
Thanks so much for your help
Ross
When you say 'nested' I assume you mean you want to re-layer only instances within the 'model.entities'... This new version does that - but actually it re-layers all instances in the 'active_entities' context, so if you run it whilst editing a group etc only instances inside that group are affected, to affect only instances in the model.entities ensure that you are in the model.entities context before running it - this allows you the fullest flexibility, with the opportunity to edit certain nested instances separately, if so desired...m=Sketchup.active_model;m.start_operation('c');m.active_entities.each{|e|next unless e.is_a?(Sketchup;;ComponentInstance);n=e.definition.name;l=m.layers.add(n);e.layer=l};m.commit_operation
Layer colors cannot currently be accessed/changed by the API, you'll have to do that change to 'black' manually. There are some convoluted workarounds [e.g. make layer.color= developers' method hack], but it'll be just as fast to do it manually in the Layer Browser...
Advertisement