Convert Layer Pointer to Layer Name, How?
-
Anyone know how to check a layer name when the return looks like this?
I do this in Ruby console:
fs=Sketchup.active_model.selection.each { |entity| puts entity.layer}
Ruby Console return data:
#Sketchup::Layer:0x00000000447780
#Sketchup::Layer:0x0000000b356ed8
#Sketchup::Layer:0x0000000b356ed8Question is, if I wanted to do an "if" layer name check, with that pointer "0x00000000447780", how is it done?
When I do this:
if entity.layer = '0x00000000447780'; entity.material = 'red' end };
I get this:
@unknownuser said:
Error: #<ArgumentError: Cannot find layer named "0x00000000447780">
When I do this:
fs=Sketchup.active_model.selection.each { |entity| if entity.layer =0x00000000447780; entity.material = 'red' end };
I get this error:
@unknownuser said:
Error: #<ArgumentError: No implicit conversion to layer>
<main>:inlayer=' <main>:in
block in <main>'
<main>:ineach' <main>:in
<main>'
SketchUp:1:in `eval'So how do you convert a layer pointer, so Ruby knows what you're looking for?
Thanks
-
You're so close.
Sketchup.active_model.selection.each do |entity| if entity.layer.name == "Some Layer name" entity.material = "Red" end end
What you were seeing is the representation of a Layer object. Layer objects have these methods.
Also note the comparison operator in Ruby is
==
and not=
-
Thanks. But in the grep iterator, it only returns that layer pointer, and won't recognize the name of the layer, or this code has an error. The problem is, it turns all the faces red, even if they are in a layer other than "Bozworth".
fs=Sketchup.active_model.selection.each { |entity| if entity.layer == 'Bozworth'; entity.material = 'red' end };
-
Your code is not correct - look at my example more carefully. Layer objects have names. A Layer's name is represented as a String.
-
As @Jim says, you need to access the layer by its '
.name
'
So you need something like:
Sketchup.active_model.selection.each{|e|e.material="red" if e.layer**.name**=="Bozworth"}
Advertisement