Color components
-
ents = Sketchup.active_model.entities[0] ents.entityID Result-2345
Now, I need to assign the color 'red' to this particular component whose entityID is 2345. How should I do it?
-
You already have a reference to the entity, so it is as simple as:
ents.material = "Red"
If you need to search for that entity again, then the following may work (unless you need to dig into sub-components:)
Sketchup.active_model.entities.each {|entity| if entity.entityID == 2345 entity.material = "Red" break end }
-
The previous example takes advantage of a feature of the
.material=
method which can accept a color name as an argument. You can view the available color names by enteringSketchup::Color.names
in the Ruby Console window.If you want to use a custom color or texture, you need to add a new material, then assign it a color or texture.
mat = Sketchup.active_model.materials.add("New Material") mat.color = Sketchup::Color.new(red, green, blue, alpha) ents.material = mat
Note all parameters to the Color.new method are optional.
-
What are you using entityID for? Note that it's not a reference that last across sessions.
-
Another minor point on 'idiom' - to make it easier for us to help debug your code please use logical names.
If you get ALL of the entities in a selection as an array use
ent**s**=model.selection.to_a
The 's' on its name helps us/you realize/remember that it's an array of several objects...
Then use
ents.each{|ent| ent.material='Red' if ent.class==Sketchup::Face and e.normal.z==0 }
To process each one in turn... Where logicallyent
is one of the things in the collection calledents
To get the first thing selected therefore use a 'single' name like
ent=model.selection[0]
NOT a plural 'ents' which suggests to many that it's a collection... -
Thanks to everyone!!!
What should be the script If I have to add the new parameter like width=='1'? This parameter 'width' is a category from MySQl database.
Sketchup.active_model.entities.each {|entity|
if entity.entityID == 2345 and width=='1'
entity.material = "Blue"
break
end
}end
end
-
well, that depends. Do you just want to assign an attribute called "width" to an object? Or do you actually want to make that object be a specified width? I suppose the same also hold true for the color. Now I remember you're doing the database interaction.
-
It's kind of both.
The component is assigned 'width' attribute and, also parameter 'width' is pulled out from database.
I am doing process by process. Right now, i am taking the parameter 'width' from db and coloring each component different colors. Like assigning Component1 with ID 2345 and width 10" a 'red' color, Component2 with ID 2343 and width 12" a 'blue' color and so on......
-
You're storing entityID in a database?
-
IDs do not subsist across sessions as they are re set every time the SKP opens.
You must give entities attributes which will be carried across sessions to find them successfully...
Advertisement