I am not proficient in ruby, but i put this together using AI and a lot of testing. The script looks at the current selection and updates the toolbar icon state to show soften edges ( all, some, none)
require 'sketchup.rb'
module MP_Extensions
module MP_Edge_State_Tool
def self.update_icon
model = Sketchup.active_model
selection = model.selection
edges = selection.grep(Sketchup::Edge)
return if edges.empty?
# Check the current state of the edges
all_soft = edges.all? { |edge| edge.soft? }
none_soft = edges.none? { |edge| edge.soft? }
if all_soft
icon = 'all.png'
elsif none_soft
icon = 'none.png'
else
icon = 'some.png'
end
icon_path = File.join(__dir__, icon)
@cmd.small_icon = @cmd.large_icon = icon_path
end
def self.recreate_toolbar
@toolbar = UI::Toolbar.new("MP Edge State")
@toolbar.add_item(@cmd)
@toolbar.show
end
def self.toggle_soft_edges
model = Sketchup.active_model
selection = model.selection
edges = selection.grep(Sketchup::Edge)
return if edges.empty?
# Check the current state of the edges
all_soft = edges.all? { |edge| edge.soft? }
none_soft = edges.none? { |edge| edge.soft? }
model.start_operation('MP Edge State', true)
if all_soft
# Set all edges to none soft
edges.each { |edge| edge.soft = false }
elsif none_soft
# Set all edges to all soft
edges.each { |edge| edge.soft = true }
end
model.commit_operation
# Update toolbar icon
self.update_icon
self.recreate_toolbar
end
unless @loaded
@loaded = true
@cmd = UI::Command.new("MP Edge State") { toggle_soft_edges }
@cmd.tooltip = "MP Edge State Tool"
@cmd.status_bar_text = "Toggle the state of selected edges"
#update_icon
# Set default icon to "all.png" on load
@cmd.small_icon = @cmd.large_icon = File.join(__dir__, 'all.png')
recreate_toolbar
end
# Add an observer to update the toolbar icon when the selection changes
class SelectionObserver < Sketchup::SelectionObserver
def onSelectionBulkChange(selection)
MP_Edge_State_Tool.update_icon
MP_Edge_State_Tool.recreate_toolbar
end
end
Sketchup.active_model.selection.add_observer(SelectionObserver.new)
end # module MP_Edge_State_Tool
end # module MP_Extensions