Component load from Ruby

Hello,

I have a ruby code that loads a profile with a hole pattern in sketchup.

# 1e C-profiel------------------------------------------------------------------------------------------------------------
definitions = model.definitions
componentdefinition = definitions.load ( "Z:/Algemeen 2015/Systeemvloer/Tekeningen/Sketchup/C-profiel.skp" )

defn3 = componentdefinition 
defn3 = model.active_entities.add_instance( defn3, [0.05*3.937,0.54*3.937,hoogte-1.88*3.937] ) 

 #afmeting en hoogte bepalen

defn3.set_attribute( 'dynamic_attributes', '_lenx_formula', "#{str1*2.54-1}" )

  dcs = $dc_observers.get_latest_class
    dcs.redraw_with_undo(defn3)

# 1e C-profiel 2------------------------------------------------------------------------------------------------------------
for i in 0..velden1-3 do

defn3 = componentdefinition 
defn3 = model.active_entities.add_instance( defn3, [str1+i*str1+0.05*3.937,0.54*3.937,hoogte-1.88*3.937] ) 
defn3.set_attribute( 'dynamic_attributes', '_lenx_formula', "#{str1*2.54-1}" )

end

When i change the second profile in the drawing after load something strange happens. See picture:

The hole pattern updates the right size but the profile isnt. When i at the following code under the second profile:

    dcs.redraw_with_undo(defn3)```

This is working good but the definition is now C-profiel#1 instead of C-profiel (with the same size as the first profile). So the load time will be much more and also for counting components this is not working well. Is there a code were this can't happen and only #1 will appear when you change size?

I can't find the solution by myself.

Thanks

Problem solved!

It was something in the dynamic component what wasn’t named correctly

Nice you found a solution yourself. I notice something strange (to me):

defn3 = componentdefinition
→ first, you set defn3 to the componentdefinition you just loaded

defn3 = model.active_entities.add_instance( defn3, [0.053.937,0.543.937,hoogte-1.88*3.937] ) → next you’re adding the defn3 componentdefinition to the active entities AND at the same time you’re setting the result (start of the sentence) to the same variable.

Why not use:
defn3 = model.active_entities.add_instance( componentdefinition, [0.053.937,0.543.937,hoogte-1.88*3.937] )

Thanks MaxB for pointing me out to a better code.

This is my first ruby project and this code is part of a mezzanine floor builder. Everything is working. You can give up height, length, depth and number of fields etc.

Everything was done with different codes got from Internet, Dan and Tig. But now I want simplifying the code. So thank you!

No actually the add_instance() method adds a component instance of the defn3, so using that reference* is misleading. (*Ruby doesn’t really have variables.)

Does this make more sense ?

# 1e C-profiel------------------------------------------------------------------------------------------------------------
dlist = model.definitions
cdef3 = dlist.load( "Z:/Algemeen 2015/Systeemvloer/Tekeningen/Sketchup/C-profiel.skp" )
 
inst3 = model.active_entities.add_instance( cdef3, [0.05*3.937,0.54*3.937,hoogte-1.88*3.937] ) 

 #afmeting en hoogte bepalen

cdef3.set_attribute( 'dynamic_attributes', '_lenx_formula', "#{str1*2.54-1}" )

  dcs = $dc_observers.get_latest_class
  dcs.redraw_with_undo(inst3)

# 1e C-profiel 2------------------------------------------------------------------------------------------------------------

inst = []

for i in 0..velden1-3

  inst[i]= model.active_entities.add_instance( cdef3, [str1+i*str1+0.05*3.937,0.54*3.937,hoogte-1.88*3.937] )

end

Note:

I myself avoid using any reference names that begin with “def”. This way only method definitions start with lines beginning with those characters.

Also the for loop does not need a do keyword, and this will sometimes confuse color lexers of editors. It may also generate a warning when loading files.