Why can not I get the value of the lenx, leny and lenz dynamic attributes in ruby?

ruby
dynamiccomponents
development

#1

Why can not I get the value of the lenx, leny and lenz dynamic attributes in ruby?

I’m trying to get the value of _lenx_formula, _leny_formula and _lenz_formula and it does not work, however with the attributes _x_formula, _y_formula, _z_formula, _rotx_formula, _roty_formula, and _rotz_formula works.
Why does this happen, what do I need to do?
My intention is to list some main attributes, put in a substitute component and then delete the resident component.
Here’s a template to make it easier.
Teste.skp (2.9 MB)

This is the code

model = Sketchup.active_model
sel = model.selection

model.start_operation("X")

#Método buscar componentes com modelocategoria e trocar
	def get_dc_instances_replace(ents)
	  ents.grep(Sketchup::ComponentInstance).each do |e|
		attribut = e.get_attribute 'dynamic_attributes','modelocategoria'
		  unless attribut.nil?
			if attribut.include?("portas")
			  x = e.get_attribute 'dynamic_attributes', '_x_formula', ''.to_f
			  y = e.get_attribute 'dynamic_attributes', '_y_formula', ''.to_f
			  z = e.get_attribute 'dynamic_attributes', '_z_formula', ''.to_f
			  lenx = e.get_attribute 'dynamic_attributes', '_lenx_formula', ''.to_f
			  leny = e.get_attribute 'dynamic_attributes', '_leny_formula', ''.to_f
			  lenz = e.get_attribute 'dynamic_attributes', '_lenz_formula', ''.to_f
			  rotx = e.get_attribute 'dynamic_attributes', '_rotx_formula', ''.to_f
			  roty = e.get_attribute 'dynamic_attributes', '_roty_formula', ''.to_f
			  rotz = e.get_attribute 'dynamic_attributes', '_rotz_formula', ''.to_f
			  modcat = e.get_attribute 'dynamic_attributes', 'modelocategoria', ''.to_s
			  puts [x,y,z,lenx,leny,lenz,rotx,roty,rotz,modcat]
			end
		  end
		get_dc_instances_replace(e.definition.entities)
	  end
	end

get_dc_instances_replace(sel)

model.commit_operation
#

#2

Look to the definition of the instance for the “dynamic_attributes” dictionary.


#3

I’d recommend using Attribute Inspector.
https://extensions.sketchup.com/en/content/attribute-inspector

It looks like that property is stored in the definition rather than the instance.


#4

Hi jim_foltz and thanks for replying.
But I did not understand, could you give me an example?
When you paste the code into the console the results only appear for x, y, z, rotx, roty, and rotz.
Look.

I think the problem would only be in lenx, leny and lenz, but I do not know where?


#5

The ComponentInstance does not have the attributes, the ComponentDefinition does.

You are displaying the attributes from the ComponentInstance.

Change this

attribut = e.get_attribute(...)

to this:

attribut = e.definition.get_attribute(...)

#6

Hello Neil_Burkholder and thanks for your reply.
I have this plugin, I’m using it as follows _lenx_formula … and so on, this is not correct?
Why use * 1?


#7

I just did that to make it easier to distinguish between the lenx value and the lenx formula.

In the attribute inspector notice the drop down to select definition or geometry (instance).


#9

Oh, that seems to work, but I should only do this for lenx, leny, and lenz.
Thanks for helping me.
One more thing, would you have a cleaner way to do this?

     ...x = e.get_attribute 'dynamic_attributes', '_x_formula', ''.to_f
        y = e.get_attribute 'dynamic_attributes', '_y_formula', ''.to_f
        z = e.get_attribute 'dynamic_attributes', '_z_formula', ''.to_f
        lenx = e.definition.get_attribute 'dynamic_attributes', '_lenx_formula', ''.to_f
        leny = e.definition.get_attribute 'dynamic_attributes', '_leny_formula', ''.to_f
        lenz = e.definition.get_attribute 'dynamic_attributes', '_lenz_formula', ''.to_f
        rotx = e.get_attribute 'dynamic_attributes', '_rotx_formula', ''.to_f
        roty = e.get_attribute 'dynamic_attributes', '_roty_formula', ''.to_f
        rotz = e.get_attribute 'dynamic_attributes', '_rotz_formula', ''.to_f
        modcat = e.get_attribute 'dynamic_attributes', 'modelocategoria', ''.to_s
        puts [x,y,z,lenx,leny,lenz,rotx,roty,rotz,modcat] ....

See the result.


#10

This is not generally true.

The ComponentInstance's "dynamic_attributes" dictionary WILL have the attributes, IF the instance does not use the same default value as in the definition’s "dynamic_attributes" dictionary.

As an example, the definition may have a material attribute set to a default material / color, but if the user changes a DC option to another color choice, the DC engine will recreate the attribute in the instance’s "dynamic_attributes" dictionary with the specific material choice. The original “default” choice remains as it was in the definition’s "dynamic_attributes" dictionary, so that new instances inserted later will still use the “default” attribute value.


The normal pattern is to always first check the DC instance’s "dynamic_attributes" dictionary for an attribute, and if it is nil THEN check the definition’s "dynamic_attributes" dictionary for the attribute.
This is what the DC engine itself does internally.

Note that "dynamic_attributes" for nested groups (inside dynamic components) are always stored on the group instance itself, not on the group’s definition.

Example …

  def read_dc_attribute(inst,key)
    dydict = 'dynamic_attributes'
    val = inst.get_attribute(dydict,key,nil)
    return val if !val.nil?
    inst.definition.get_attribute(dydict,key,nil)
  rescue
    return nil
  end

What is the point of attributes attached to **groups**?
#11

As I said at the beginning:

But the new component inserted does not respond, it should redraw by adjusting to the copied values. Because it does not work?
Look at the code, do not judge me badly, I’m a beginner.

model = Sketchup.active_model
definitions = model.definitions
entities = model.active_entities
path = Sketchup.find_support_file "../../../../../../Dropbox/Modelos Dinâmicos Sketchup/Laterais/Lateral.skp", "Components"
component = definitions.load path
sel = model.selection

model.start_operation("X")

#Método buscar componentes com modelocategoria e trocar
	def get_dc_instances_replace(ents,component)
	  ents.grep(Sketchup::ComponentInstance).each do |e|
		attribut = e.get_attribute 'dynamic_attributes','modelocategoria'
		  unless attribut.nil?
			if attribut.include?("portas")
			  x = e.get_attribute 'dynamic_attributes', '_x_formula', ''.to_f
			  y = e.get_attribute 'dynamic_attributes', '_y_formula', ''.to_f
			  z = e.get_attribute 'dynamic_attributes', '_z_formula', ''.to_f
			  lenx = e.definition.get_attribute 'dynamic_attributes', '_lenx_formula', ''.to_f
			  leny = e.definition.get_attribute 'dynamic_attributes', '_leny_formula', ''.to_f
			  lenz = e.definition.get_attribute 'dynamic_attributes', '_lenz_formula', ''.to_f
			  rotx = e.get_attribute 'dynamic_attributes', '_rotx_formula', ''.to_f
			  roty = e.get_attribute 'dynamic_attributes', '_roty_formula', ''.to_f
			  rotz = e.get_attribute 'dynamic_attributes', '_rotz_formula', ''.to_f
			  mat = e.get_attribute 'dynamic_attributes', '_material_formula' ''.to_s
			  modcat = e.get_attribute 'dynamic_attributes', 'modelocategoria', ''.to_s
			  
			  # Adicionar componentes
			  transformation = Geom::Transformation.new([0,0,0])
			  componentinstance = ents.add_instance(component, transformation)
			  componentinstance.make_unique
			  componentinstance.set_attribute 'dynamic_attributes','_x_formula', x
			  componentinstance.set_attribute 'dynamic_attributes','_y_formula', y
			  componentinstance.set_attribute 'dynamic_attributes','_z_formula', z
			  componentinstance.set_attribute 'dynamic_attributes','_lenx_formula', lenx
			  componentinstance.set_attribute 'dynamic_attributes','_leny_formula', leny
			  componentinstance.set_attribute 'dynamic_attributes','_lenz_formula', lenz
			  componentinstance.set_attribute 'dynamic_attributes','_rotx_formula', rotx
			  componentinstance.set_attribute 'dynamic_attributes','_roty_formula', roty
			  componentinstance.set_attribute 'dynamic_attributes','rotz', ''
			  componentinstance.set_attribute 'dynamic_attributes','_rotz_formula', rotz
			  componentinstance.set_attribute 'dynamic_attributes','_material_formula', mat
			  componentinstance.set_attribute 'dynamic_attributes','modelocategoria', modcat
			  $dc_observers.get_latest_class.redraw(e)
			end
		  end
		get_dc_instances_replace(e.definition.entities,component)
	  end
	end

get_dc_instances_replace(sel,component)

model.commit_operation
#

#12

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