Problem with the Ruby case statement [Solved]

case

#1

I got wrong syntax with the .case ruby method
" unexpected tCONSTANT, expecting keyword_when When “IfcWall” then “C-Murs”

This is my code
model.definitions.each{|d|
next if d.image?
d.instances.each{|i|
layer_name = case i.name[0…6]
When “IfcWall” then "C-Murs"
when “IfcDoor” then "C-Men interieure"
else "layer 0"
end
new_layer = layers.add layer_name
i.layer = new_layer

				}
			}

I don’t understand how it must works…


#2

You have an upper case W in When. when is a keyword and must be all lowercase.


#3

Arg you 're right, that was so simple…


#4

Also “layer 0” needs to be “Layer0” (no space)


#5

Thanks


#6

Still not the good way :frowning:
"# NameError: undefined local variable or method `layers’ for VicoIFC:Module"
I’m not good with this case method


#7

In your code, you would use this line:

new_layer = model.layers.add(layer_name)

The error is saying layers is not a variable or a method. model.layers is a method which returns the Layers collection of the model, but only because in your code model is a reference to Sketchup.active_model

You can in all cases get a model’s Layers by using Sketchup.active_model.layers


#8

Ok I just find I forgot to put the “layers = model.layers” in may code

model = Sketchup.active_model
layers = model.layers
model.start_operation(“IFC to Layer”)
model.definitions.each{|d|
next if d.image?
d.instances.each{|i|
layer_name = case i.name[0…6]
when ‘IfcWall’ then 'C-Murs’
when ‘IfcDoor’ then 'C-Men interieure’
else 'layer0’
end
new_layer = layers.add layer_name
i.layer = new_layer

				}
			}

Thanks for the quick answers


#9

No problem. This is for the IFC to Layers extension?


#10

Yes I try to improve it (it’s to long for big model)