Create a Dynamic component and define dimensions

hello,
Can some one help me…The goal of the code below was to change a component to a dynamic one adding lenx, leny and lenz attributes. And force values to 100, 200 and 300

  #Change a component to dynamic component
def crdc
   model = Sketchup.active_model
    selection = model.selection

    if selection.empty? or selection.length > 1.0
			
		UI.messagebox ("Select one entity")

		else
		ents=selection[0]  
		
		compo=ents.definition  #component definition
		
		#add attributs
		compo.set_attribute("dynamic_attributes","lenx", "100.0")		
		compo.set_attribute("dynamic_attributes","leny", "200.0")	
		compo.set_attribute("dynamic_attributes","lenz", "300.0")	
		
		#attribus labels
		compo.set_attribute("dynamic_attributes","_lenx_label", "L")
		compo.set_attribute("dynamic_attributes","_leny_label", "P")
		compo.set_attribute("dynamic_attributes","_lenz_label", "H")
		
		#attributs format
		compo.set_attribute("dynamic_attributes" , "_lenx_access", "TEXTBOX")
		compo.set_attribute("dynamic_attributes" , "_leny_access", "TEXTBOX")
		compo.set_attribute("dynamic_attributes" , "_lenz_access", "TEXTBOX")
		
		#regen
		$dc_observers.get_latest_class.redraw_with_undo(ents) 
		end	
end

The component is a cube 1000mm x 1000mm x 1000mm
cube

This code is ok to transform the component into dynamic one. But the dimension are not changed, still 1000mm x 1000mm x 1000m.

However we can see with “Attribute Helper” that values have been changed !!!

is this a regen issue ? Or else…

Thanks in advance

BR

You cannot directly set the 'lenx', 'leny' and 'lenz' attributes.

You must create your own custom attributes, and then set a formula for the above attributes that points at your custom attributes.

Also to have your attributes appear in non-alpha order you need to use prefixes that will collate in the order that you wish.

# Change a component to dynamic component
def crdc
  model = Sketchup.active_model
  selection = model.selection
  return if selection.empty?

  dict = 'dynamic_attributes'
  inst = selection[0]  
  compo = inst.definition  #component definition

  compo.set_attribute(dict,'_name', 'Cuboid')
  compo.set_attribute(dict,'_formatversion','1')
  compo.set_attribute(dict,'_lengthunits','CENTIMETERS')
  
  #add attributs
  compo.set_attribute(dict,'d1l', '100')
  compo.set_attribute(dict,'d2p', '100')
  compo.set_attribute(dict,'d3h', '100')

  compo.set_attribute(dict,'_lenx_formula', 'd1l')
  compo.set_attribute(dict,'_leny_formula', 'd2p')
  compo.set_attribute(dict,'_lenz_formula', 'd3h')
  
  #attribus labels
  compo.set_attribute(dict,'_d1l_label', 'L')
  compo.set_attribute(dict,'_d2p_label', 'P')
  compo.set_attribute(dict,'_d3h_label', 'H')
  
  #attributs format
  compo.set_attribute(dict , '_d1l_access', 'TEXTBOX')
  compo.set_attribute(dict , '_d2p_access', 'TEXTBOX')
  compo.set_attribute(dict , '_d3h_access', 'TEXTBOX')
  
  #regen
  $dc_observers.get_latest_class.redraw_with_undo(inst) 
 
end

Instances dictionaries will not have the size attributes IF they are the same as the definition, but if the instance will be a different size, then you must make the size changes to the instance’s "dynamic_attributes" dictionary.

Then for a method that takes 3 sizes in inches, but expressed as cm like:

xyz( 100.cm, 200.cm, 300.cm )

… the xyz() method:

def xyz(x,y,z)
  model = Sketchup.active_model
  selection = model.selection
  return if selection.empty?

  dict = 'dynamic_attributes'
  inst = selection[0] # the instance
  compo = inst.definition  # component definition

  for key in ['_name','_formatversion','_lengthunits']
    inst.set_attribute(dict, key, compo.get_attribute(dict,key))    
  end

  units = compo.get_attribute(dict,'_lengthunits')
  keys = ['d1l','d2p','d3h']

  for key in keys
    arg = [x,y,z][keys.index(key)]
    #add attributs
    if units == "CENTIMETERS"
      inst_val = arg.to_cm.round(3).to_s
    else
      inst_val = arg.round(3).to_s
    end
    comp_val = compo.get_attribute(dict,key)
    prev_val = inst.get_attribute(dict,key)
    if ( prev_val.nil? && inst_val != comp_val ) || prev_val != inst_val
      inst.set_attribute(dict, key, inst_val)
    end
  end
  
  #regen
  $dc_observers.get_latest_class.redraw_with_undo(inst) 

end

Thanks so much Dan,

It’s OK now

Best regards

Patrick

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.