Ruby code input box


#1

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


#2

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”’


#3

Hi MaxB,

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

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


#4

try

inst.set_attribute ‘dynamic_attributes’, ‘lenzformula’, hoogte


#5

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?


#6

just checked, the variable without quotes works


#7

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.


#8

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


#9

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


#10

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


#11

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 within a double-quoted string:

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

Or the Sketchup::format_length module method:

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

EDIT: fixed attribute names.


#12

t1.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 :smile:

Thanks again.


#13

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


#14

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 :slight_smile:


#15

Wrapped in ```ruby
# code here

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