Assign attribute to multiple dynamic components using Ruby Script

I have a number of dynamic components all with a custom attribute name Y10_PartNumber. I would like to have a ruby script that would take all the instances and assign a sequential number to them. For example if there 30 different components or instances I would like to assign numbers 1-30 to that Y10_PartNumber attribute in the dynamic component. Any help is greatly appreciated!

Hey, try this:


model = Sketchup.active_model
sel  =  model.selection
dcs = sel.grep(Sketchup::Group).concat(sel.grep(Sketchup::ComponentInstance))
  num = 1
dcs.each do |dc|
  if dc.attribute_dictionary( 'dynamic_attributes', 'Y10_PartNumber')
    dc.set_attribute( 'dynamic_attributes', 'y10_partnumber', num)
    num += 1
  end
end

Worked great! Thanks for taking the time to help me out on this!

1 Like

Glad it worked for you. :wink:

Thank you for your snippet,
I used it to change the attribute (called aaPos) in my dynamic component - it is an forcearrow i use for loadplans for concrete engineers … it works fine…

Now i am keen to change the attribute aaPos with a sequential numper with an additional prefix like “EL_” or something else… My aim is this EL_1 (first component)…EL_2(second component) and so on…I learned that aaPos in ruby will cause an error, when writing aapos it works, but only for the functionality with numbers. My code with the additional prefix is the following…

model = Sketchup.active_model
sel  =  model.selection
dcs = sel.grep(Sketchup::Group).concat(sel.grep(Sketchup::ComponentInstance))
 num = 1
 bez = "EL_"
 
dcs.each do |dc|
 if dc.attribute_dictionary( 'dynamic_attributes', 'aapos')
   numbez = bez + num
   numbez2 = numbez.to_s
   dc.set_attribute( 'dynamic_attributes', 'aapos', numbez2)
   num += 1
 end
end

Ruby code editor+ gives me an error: no implicit conversion of Integer into String (Line 9), I am nealy shure the solution is only a simple trick, please give me a hint,
thanks a lot

You should convert integer to string

numbez = bez + num.to_s

Also you needn’t the next line because numbez is a string