# Component load from Ruby

**URL:** <https://forums.sketchup.com/t/component-load-from-ruby/25004>\
**Category:** Ruby API\
**Created:** [May 11, 2016, 2:29pm UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004 "2016-05-11T14:29:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanlion](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/vanlion/32/24743_2.png) [@vanlion](https://forums.sketchup.com/u/vanlion)\
**Post date:** [May 11, 2016, 2:29pm UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004/1 "2016-05-11T14:29:38Z")

</div>

Hello,

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

```auto
# 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:

 ![](https://global.discourse-cdn.com/sketchup/original/3X/b/4/b49ec70e7850ba6f6c09e4f6dd9ecd2d7debf67c.jpg)

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

````nohighlight
    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
````

---

<div class="post-metadata">

**Author:** ![vanlion](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/vanlion/32/24743_2.png) [@vanlion](https://forums.sketchup.com/u/vanlion)\
**Post date:** [May 12, 2016, 9:14am UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004/2 "2016-05-12T09:14:16Z")

</div>

Problem solved!

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

---

<div class="post-metadata">

**Author:** ![maxB](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/maxb/32/8212_2.png) [@maxB](https://forums.sketchup.com/u/maxB)\
**Post date:** [May 14, 2016, 5:44am UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004/3 "2016-05-14T05:44:23Z")

</div>

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.05_3.937,0.54_3.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.05_3.937,0.54_3.937,hoogte-1.88\*3.937] )

---

<div class="post-metadata">

**Author:** ![vanlion](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/vanlion/32/24743_2.png) [@vanlion](https://forums.sketchup.com/u/vanlion)\
**Post date:** [May 15, 2016, 11:33am UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004/4 "2016-05-15T11:33:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [May 15, 2016, 12:13pm UTC](https://forums.sketchup.com/t/component-load-from-ruby/25004/5 "2016-05-15T12:13:21Z")

</div>

> [@maxB](#):
>
> `defn3 = model.active_entities.add_instance( defn3, [0.05*3.937,0.54*3.937,hoogte-1.88*3.937] )`  
> … next you’re adding the defn3 componentdefinition to the active entities

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 ?

```ruby
# 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.
