How to get or set multi objects attribute DC via code

is there any ruby code to change attribute multy object whithout changing other attribute? in the compunent option its not work.
there is a problem that when i have two box Red and Green and when i select these and change just LenX attribute. but in the compunent option this make all parameter same and material change too!

Yes it is possible for a script to modify one attribute without effecting the others.
There seems to be a general reluctance to encourage such, probably because such may be seen as hacking a Trimble product. However I can not see any alternative but to write scripts to compliment the DC script and provided you do not try to create your own DC functions it should be okay.
I believe that these scripts are hard to be universal and most likely need to be customized to your DC style

So…do you have any experience in ruby scripting?
What is the range of tasks you hope to achieve?

The following code will somewhat work, but as you develop your DCs there may need to be amendments. It is set up for metric, so those that use inches need to remove some of the code.

You can run it from “toolbar editor”
https://extensions.sketchup.com/extension/e413511f-0517-4115-8d81-90039a20a72c/toolbar-editor

This code has no warranties, neither does it have the finesse of some with more experience.
updated 5.21 22.05.2020

module PCMoor_DCtools 

module ChangeOneAttribute
   extend self
   
@att_array = []
@same_att_array = []
@inst_array = []
@label_array = []


Sketchup.active_model.selection.grep(Sketchup::ComponentInstance){|i|

 if i.definition.attribute_dictionaries["dynamic_attributes"]
   @inst_array << i
   da = i.definition.attribute_dictionaries["dynamic_attributes"]
     
   da.each {|k, v| 
      i.get_attribute 'dynamic_attributes', k, v
         
      if k.include? "_formlabel"
        v ? v = v.strip : v = "" 
          
        if v.length>0
          @att_array << k
        end
      end
    }
  end 
       
   chosen = 0      
   if @same_att_array.length == 0 and chosen == 0
      @same_att_array += @att_array
      chosen = 1
   else
      @same_att_array =  @same_att_array & @att_array 
   end
   @att_array.clear
}
 
if  @same_att_array.length > 0     
   @same_att_array.each{|a|
     att = @inst_array[0].definition.get_attribute 'dynamic_attributes', a, ""
     @label_array << att
  }
  choice = UI.inputbox(
  ["Item:"],
  [@label_array[0]],
  [@label_array.join("|")],
  "Select the attribute to change: ")

   a_index = @label_array.index (choice[0])  
   mem =  @same_att_array[a_index].chomp("_formlabel")[1..-1]

  if mem.include? "__"
    mem_formula = mem.gsub("inst_","") + "_formula"
    unit =  mem.gsub("inst_","") + "_units"
    useinst = 1
    unit_type = @inst_array[0].get_attribute 'dynamic_attributes', unit, ""
    aval = @inst_array[0].get_attribute 'dynamic_attributes', mem.gsub("inst__",""), ""  
  else
    mem_formula = "_" + mem + "_formula"
    unit = "_" + mem + "_units"
    useinst = 0
    unit_type = @inst_array[0].definition.get_attribute 'dynamic_attributes', unit, ""
    aval = @inst_array[0].definition.get_attribute 'dynamic_attributes', mem, ""  
  end
  
  if unit_type == "DEFAULT"
    unit_type = "CENTIMETER"
    aval = aval*2.54
  end

  result = UI.inputbox(
  ["enter"],
  [aval],
  [""],
  choice[0] + ": (" + unit_type + ")" )
 
  if result
    @inst_array.each{|e| 
    if useinst==1
      e.set_attribute 'dynamic_attributes', mem_formula, result[0].inspect
    else
      e.definition.set_attribute 'dynamic_attributes', mem_formula, result[0].inspect
    end
    $dc_observers.get_latest_class.redraw_with_undo(e)   
    }
  end
 
else
UI.messagebox('No common attributes')
end
1 Like

hi @pcmoor
it is another great help from you. it is not working for me and the console says as in attached image. i did attached it to toolbar editor but nothing is happening. tried to use it one nested component .
image

I replaced
v = v.strip
with
v ? v = v.strip : v = ""

updated to cover more exceptions

Yes that made it work. Tried it quickly in the morning. But one more question. Is it not changing the attributes set as option list? It doesn’t show option list dialog but only text dialog. I am still unable to read the scripts properly as much as I try. And reading a lot. :slightly_smiling_face:

Hi Miroslav,

I still playing around with this, however this is closer

replace_one_attribute.rb (3.5 KB)

Philip

@pcmoor
Philip I tried the rb you shared. Just want you to know what it does in my computer.
So I pasted it to plugins folder,opened sketchup.
Immediately message box appeared saying no common attributes… just like that with no even components selected.
Than I opened my universal carcase to select both sides to chang one of common attributes they have and right mouse click to select your plugin edit atribute window.
i dont know what happened but it opened new sketchup and the selected components were not there both only one and again messadge box saying as before. even the one component (side) is exploded (because it is made of groups for ability toshow edging and other cutout and so…).
i pm avi to you of bandi cam for you to have a look as well.