DC Nominal Len X, Y and Z reset and redraw

Hello All,

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

Just a post to bump this back up, still hoping for an assist… Thanks!

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

1 Like

The assignment to dictionaries is unused within the loop.

OK @DanRathbun here it is removed.

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

1 Like

I’d probably reuse the model reference for the redraw loop as well.

Like this…

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

1 Like

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.

Like this?

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
1 Like

Yepper-doodle!

1 Like

# 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