I hope everyone is surviving the apocalypse! I am wondering if anyone could help me with a ruby script that will purge all “_nominal_lenx,y,z” attributes and then trigger a re-draw of the current selection. What I don’t want to do is to add a right click menu item. There are several snippets to do exactly that by @DanRathbun and @jim_foltz in the linked discussions (thanks both), I posted @jim_foltz ’s bit below. What I do want to do is paste the code into the console and hit enter to run it on the current selection, e.g. a shed with sub groups of framing, concrete, roofing etc… Also, none of these seem to work on grouped selections, ideally it would execute on all components in the selection regardless of grouping depth.
I use this code to correct any errors in reported DC quantities before I generate a report from a model. Component lengths become out of sync when you alter the geometry inside the component instead of scaling it, throwing off the quantity. Being able to execute on a group selection ensures that nothing gets missed. Relevant discussions are here:
Thanks in advance for any help!
Jim’s Reset Script:
UI.add_context_menu_handler do |menu|
ins = Sketchup.active_model.selection[0]
if ins && ins.class == Sketchup::ComponentInstance && ins.definition.attribute_dictionary("dynamic_attributes")
menu.add_item("DC Actual Size") do
%w(x y z).each {|a|
ins.definition.delete_attribute("dynamic_attributes", "_len#{a}_nominal")
}
$dc_observers.get_latest_class.redraw_with_undo(ins)
end
end
end
OK here it is… This will reset nominal XYZ for the entire model and redraw to correct any affected formulas. Thanks for the @DanRathbun@eneroth3 and @jim_foltz for the help along the way.
model = Sketchup.active_model
model.start_operation("Nominal XYZ reset")
([model] + model.definitions.to_a).each do |definition|
dictionaries = definition.delete_attribute("dynamic_attributes", "_lenx_nominal")
dictionaries = definition.delete_attribute("dynamic_attributes", "_leny_nominal")
dictionaries = definition.delete_attribute("dynamic_attributes", "_lenz_nominal")
end
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
model.commit_operation
model = Sketchup.active_model
model.start_operation("Nominal XYZ reset")
([model] + model.definitions.to_a).each do |definition|
definition.delete_attribute("dynamic_attributes", "_lenx_nominal")
definition.delete_attribute("dynamic_attributes", "_leny_nominal")
definition.delete_attribute("dynamic_attributes", "_lenz_nominal")
end
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
model.commit_operation
model = Sketchup.active_model
model.start_operation("Nominal XYZ reset")
([model] + model.definitions.to_a).each do |definition|
definition.delete_attribute("dynamic_attributes", "_lenx_nominal")
definition.delete_attribute("dynamic_attributes", "_leny_nominal")
definition.delete_attribute("dynamic_attributes", "_lenz_nominal")
end
model.selection.grep(Sketchup::ComponentInstance).each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
model.commit_operation
Except a loop’s (or block’s) end (or } ) should line up with the line that starts the loop.
Ie, the line where the if, case, do or { (etc.) is.
(Most editors have indent guidelines that help to signify the starting line of a block and it’s end.)
All direct child statements inside the loop should only indent 2 spaces.
model = Sketchup.active_model
model.start_operation("Nominal XYZ reset")
([model] + model.definitions.to_a).each do |definition|
definition.delete_attribute("dynamic_attributes", "_lenx_nominal")
definition.delete_attribute("dynamic_attributes", "_leny_nominal")
definition.delete_attribute("dynamic_attributes", "_lenz_nominal")
end
model.selection.grep(Sketchup::ComponentInstance).each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
model.commit_operation
# changed this line
model.selection.grep(Sketchup::ComponentInstance).each do |s|
# to this.
model.entities.each do |s|
#Swithched selection for entities to reach all children on redraw, revised below.
model = Sketchup.active_model
model.start_operation("Nominal XYZ reset")
([model] + model.definitions.to_a).each do |definition|
definition.delete_attribute("dynamic_attributes", "_lenx_nominal")
definition.delete_attribute("dynamic_attributes", "_leny_nominal")
definition.delete_attribute("dynamic_attributes", "_lenz_nominal")
end
model.entities.each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
model.commit_operation