An example that creates two menu commands at the bottom of the “View” menu.
It sets all layers whose name begins with “Demo” to red, “New” to gray, and any others to white.
It saves the previous color settings, and adds a complimentary command to reset the layer colors back to previous.
EDIT: But,… this can add a bunch of undo operations to the undo stack.
See the next post, for an example that wraps the commands in named undo operations.
Example without undoable operations (click to expand...)
# encoding: UTF-8
#
# "Set Demo Layer Colors" & "Reset Layer Colors" commands.
#
# A SketchUp menu command example by Dan Rathbun. 2021-07-14
module User # <<<---<<<< Change to YOUR unique toplevel namespace
module DemoLayerColors
if !defined?(@loaded)
UI.menu("View").add_separator
UI.menu("View").add_item("Set Demo Layer Colors") {
for i in Sketchup.active_model.layers.to_a
if i.name.start_with?("Demo")
hue = Sketchup::Color.new("Red")
elsif i.name.start_with?("New")
hue = Sketchup::Color.new("Gray")
else
hue = Sketchup::Color.new("White")
end
props = i.attribute_dictionary("Properties")
ca = i.get_attribute("Properties","Color") rescue nil
if !props || ca.nil? || ca != hue.to_a
i.set_attribute("Properties","Color",i.color.to_a)
end
i.color = hue
end
}
UI.menu("View").add_item("Reset Layer Colors") {
for i in Sketchup.active_model.layers.to_a
ca = i.get_attribute("Properties","Color") rescue nil
i.color = Sketchup::Color::new(ca) if ca
end
}
@loaded = true
end # if not already loaded
end # extension submodule
end # top level namespace module