# Ruby code input box

**URL:** <https://forums.sketchup.com/t/ruby-code-input-box/19220>\
**Category:** Ruby API\
**Created:** [January 19, 2016, 9:20am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220 "2016-01-19T09:20:13Z")\
**Posts on this page:** 15\
**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:** [January 19, 2016, 9:20am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/1 "2016-01-19T09:20:13Z")

</div>

Hello,

I’m working on a code that is making a column stucture with dynamic components.

I can give up the info with an inputbox. Everything is working except the height of the dynamic component.

The default input for the height is 3000 mm (called “hoogte” in this code). When i run the code the dynamic component height is 0. How can you call the “hoogte” that the code see it’s a value? When i replace “hoogte” with for example “100”  
it works correct.

Here is the code:

prompts = [" Breedte vloer “, " Diepte vloer “, " Hoogte “,” Aantal Velden in de breedte “,” Aantal Velden in de diepte “, " Stramienmaat in de breedte “, " Stramienmaat in de diepte “, " Type Kolom “, " Hartmaat kinderbalken “, " RAL kleur “]  
defaults = [8000.mm, 10000.mm, 3000.mm, 2, 2, 4000.mm, 5000.mm, 100, 1050, ‘RAL 5003’,]  
list = [””, “”,””, “”,””, “”,””,“100|120|140”,“”,“RAL 5003|RAL 5010|RAL 7035|RAL 7016|RAL 9010|RAL 9017”]  
results = inputbox(prompts, defaults,list, “Invoergegevens”)  
return nil unless results

breedte, diepte, hoogte, velden1, velden2, str1, str2, kolom, hoh , ral = results

#veld kolommen  
model = Sketchup.active\_model  
defn = model.definitions.load( “Z:/Algemeen 2015/Systeemvloer/Tekeningen/Sketchup/t1.skp” )  
for i in 0…velden1-1 do  
for j in 0…velden2-1 do  
inst = model.active\_entities.add\_instance( defn, [i\*str1,j\*str2,0] )  
#afmeting en hoogte bepalen  
t1 = Sketchup.active\_model.entities[0]  
t1\_def = t1.definition

t1\_def.set\_attribute ‘dynamic\_attributes’, ‘\_lenz\_formula’, ‘“hoogte”’

dcs = $dc\_observers.get\_latest\_class  
dcs.redraw\_with\_undo(t1)

inst.material = ‘red’  
end  
end

---

<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:** [January 19, 2016, 10:09am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/2 "2016-01-19T10:09:50Z")

</div>

My knowledge of dynamic components is limited but you’re trying to change the height of the definition here. Maybe you should try changing the height of the instance you just inserted?

> inst.set\_attribute ‘dynamic\_attributes’, ‘lenzformula’, ‘“hoogte”’

---

<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:** [January 19, 2016, 11:20am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/3 "2016-01-19T11:20:09Z")

</div>

Hi MaxB,

Thank you for the response. But im getting the same result.

The problem is that the code doesn’t recognize ‘“hoogte”’

---

<div class="post-metadata">

**Author:** ![pcmoor](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/pcmoor/32/3427_2.png) [@pcmoor](https://forums.sketchup.com/u/pcmoor)\
**Post date:** [January 19, 2016, 11:25am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/4 "2016-01-19T11:25:38Z")

</div>

try

inst.set\_attribute ‘dynamic\_attributes’, ‘lenzformula’, hoogte

---

<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:** [January 19, 2016, 11:27am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/5 "2016-01-19T11:27:50Z")

</div>

First try to see if the variable still exists. Try to output the variable by adding

`puts hoogte`

just before this line:

`t1_def.set_attribute 'dynamic_attributes', 'lenzformula', '"hoogte"'`

If that doesn’t give you an error the variable still exists. If not, lets take it from there.

In that last line, the variable hoogte is using quotes so maybe you’re passing a string with contents hoogte instead of the value?

---

<div class="post-metadata">

**Author:** ![pcmoor](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/pcmoor/32/3427_2.png) [@pcmoor](https://forums.sketchup.com/u/pcmoor)\
**Post date:** [January 19, 2016, 11:45am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/6 "2016-01-19T11:45:13Z")

</div>

just checked, the variable without quotes works

---

<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:** [January 19, 2016, 2:49pm UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/7 "2016-01-19T14:49:17Z")

</div>

Hi pcmoor,

I give your option a try and the code gives no error.

But it doesn’t take over the height you give up. When te code has run every column is 4000 mm heigh.

---

<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:** [January 19, 2016, 3:15pm UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/8 "2016-01-19T15:15:07Z")

</div>

> [@maxB](#):
>
> puts hoogte

I get no error but the height result you fill in doesn’t work in the code. Column is getting 4000 mm heigh

---

<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:** [January 19, 2016, 4:21pm UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/9 "2016-01-19T16:21:06Z")

</div>

can you post the t1.skp file you’re testing here with as well? Without it its just a wild goose chase

---

<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:** [January 19, 2016, 6:32pm UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/10 "2016-01-19T18:32:54Z")

</div>

Oké thanks.

I will sent the t1.skp tomorrow when i’m back at work. It’s a very simple model from a column with a Base plate. The column is scalable with height and the Base plate is locked from scaling

You will see it tomorrow

---

<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:** [January 20, 2016, 12:05am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/11 "2016-01-20T00:05:32Z")

</div>

> [@pcmoor](#):
>
> inst.set\_attribute ‘dynamic\_attributes’, ‘lenzformula’, hoogte

All attributes are always stored as strings. So the value referenced by **`hoogte`** could be passed to the **`set_attribute`** method as a string representation of a numerical value.

Using Ruby’s **`"#{}"`** [string interpolation](http://ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html) within a **double-quoted** string:

```ruby
inst.set_attribute( 'dynamic_attributes', '_lenz_formula', "#{hoogte}" )

```

Or the [**`Sketchup::format_length`**](http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchup#format_length) module method:

```ruby
inst.set_attribute( 'dynamic_attributes', '_lenz_formula', Sketchup::format_length(hoogte) )

```

**EDIT** : fixed attribute names.

---

<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:** [January 20, 2016, 7:56am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/12 "2016-01-20T07:56:33Z")

</div>

[t1.skp](https://forums.sketchup.com/uploads/short-url/1AdTbYCwYAvyojCKFTgD2fo30MU.skp) (537.5 KB)

Hello,

Also tried this code but it doesn’t take over the height and stays at the default height of 100 mm. I attached the dynamic component file t1. Really don’t know where it goes wrong. But when this problem is solved i can finish the rest 😄

Thanks again.

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [January 20, 2016, 9:32am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/13 "2016-01-20T09:32:33Z")

</div>

At least part of the issue is that the attribute name is `'_lenz_formula'` not `'lenzformula'`

---

<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:** [January 20, 2016, 11:14am UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/14 "2016-01-20T11:14:20Z")

</div>

Yes,

Got the solution.

This is the code that is working:

inst.set\_attribute( ‘dynamic\_attributes’, ‘\_lenz\_formula’, “#{hoogte\*2.54}” )

I think the problem was to translate inch to mm.

Thanks for all the help 🙂

---

<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:** [January 20, 2016, 7:47pm UTC](https://forums.sketchup.com/t/ruby-code-input-box/19220/15 "2016-01-20T19:47:19Z")

</div>

> [@vanlion](#):
>
> This is the code that is **_REALLY_** working:
> 
> ```ruby
> inst.set_attribute( 'dynamic_attributes', '_lenz_formula', "#{hoogte*2.54}" )
> 
> ```

Wrapped in ````ruby`  
` # code here`

````` tags (because underscores are italicize markdown characters.)
