Last active
October 26, 2015 23:16
-
-
Save jimfoltz/21a129ea9c6a0deb8a05 to your computer and use it in GitHub Desktop.
component-layers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Right-click an Instance to move it and all sub-components | |
# to a new set of Layers prefixed by component name. | |
# Use to keep imported models off your Layers. | |
# | |
module JF | |
module ComponentLayers | |
def self.separator | |
"__" | |
end | |
def self.hash_by_layer(ent, h) | |
layer = ent.layer | |
h[layer] << ent | |
if ent.is_a? Sketchup::Group | |
ent.entities.each {|e| hash_by_layer(e, h)} | |
elsif ent.is_a? Sketchup::ComponentInstance | |
ent.definition.entities.each {|e| hash_by_layer(e, h)} | |
end | |
end | |
UI.add_context_menu_handler do |menu| | |
menu.add_item("Componet Layers") do | |
model = Sketchup.active_model | |
sel = model.selection[0] | |
res = UI.inputbox(["Prefix"], [sel.definition.name], "Prefix") | |
if res && !res[0].empty? | |
model.start_operation("Component Layers", true) | |
h = Hash.new{|h,k| h[k] = []} | |
JF::ComponentLayers.hash_by_layer(sel, h) | |
h.each {|layer, ent_ary| | |
if layer.name.index("#{res[0]}#{separator()}") == 0 | |
new_name = layer.name | |
else | |
new_name = "#{res[0]}#{separator()}#{layer.name}" | |
end | |
new_layer = Sketchup.active_model.layers.add(new_name) | |
new_layer.visible = layer.visible? | |
new_layer.color = layer.color | |
new_layer.page_behavior = layer.page_behavior | |
ent_ary.each {|entity| entity.layer = new_layer } | |
} | |
model.layers.purge_unused | |
model.commit_operation | |
end | |
end | |
end # menu | |
end # ComponentLayers | |
end # JF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment