How to set and put an object to an layer with ruby script in sketchup


#1

i have an question… how to select an object in sketchup with ruby script and how to put the
selected object to an specific layer


#2
# Assumes you have a reference to an entity already as `entity`
model = Sketchup.active_model
model.selection.add(entity)
layer = model.layers['MyLayer'] # Return `nil` if there is no such layer
entity.layer = layer

Note that you don’t have to select an entity when changing their properties in the API, this works just as well:

layer = model.layers['MyLayer'] # Return `nil` if there is no such layer
entity.layer = layer

Only select entities if you want the user to see them as selected.

(Btw, when posting questions it helps to include the context of what you are trying to do, and explain what you have tried so far.)


#3

Hi,
please find below my method to always get a layer

def Adebeo::get_layer(layerName)
	#puts "get or create the require layer"
	layers = Sketchup.active_model.layers
	if layers[layerName]
		return layers[layerName]
	else
		layerName = "layer" if layerName == ""
		return layers.add layerName	
	end
end

best regards


#4

Actually using:
layer = Sketchup.active_model.layers.add(layerName)
always works and returns the reference to ‘layer’ with the name ‘layerName’.
If that layer already exists it is used in the reference, if that layer name is not in use then a new layer is added and that is used in the reference.

The layers.add() method is unlike similar methods to add component definitions and materials., which will always add a new one, adding a suffix to the name given if it’s already in use !


#5

good to know !


#6

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selectio
grp = ent.add_group # Make group

def Adebeo::get_layer(layerName)
#puts "get or create the require layer"
layers = Sketchup.active_model.layers
if layers[layerName]
return layers[layerName]
else
layerName = “layer” if layerName == ""
return layers.add layerName
end
end
your ruby script will give the following warning … " uninitialized constant Adebeo "


#7

That because you have badly formed code.
You have no method called Adebeo !
I’m unsure as to why you have put a def method in the middle of you code - do you know why ?
You are also defining several references that are unused - like mod, ent, sel and grp

I explained the simpler way to do this…

Incidentally you add an empty group into the model entities and then ignore it !?
Why ?


#8

Sorry I didn’t remove Adebeo module

do like this

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selectio
grp = ent.add_group # Make group
layer = model.layers.add "newLayerName"
grp.layer = layer

end


#9

You are still not proofreading what you post !
You set a reference to the Sketchup.active_model named 'mod'
But later on you use ‘model’ - which you have NOT defined.
You also set ‘sel’ which is also never used.

Tip: in the layer.add code remember to parenthesize your arguments !
.layers.add "newLayerName"
should be
.layers.add("newLayerName")
Both ways will work, but the () version is always preferred - because in Ruby the non-parenthesized arguments are due to be deprecated - one day !

At the moment your code adds an empty group to the model entities, and assigns it a layer.
However, unless you add something into the group.entities that group will vanish at the end of your code - because all empty containers are deleted from the model during garbage-collection !

You might be better to set out your desires/ideas in ‘plain-text’, rather than ‘code’.
I always do this - that way I know what my mile-posts and goals are, getting bogged-down in code that is imperfect, will cause all kinds of problems , and dispirit you in the process !


#10

Sorry TIG.
thanks for your advice


#11

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.