Ruby Script for coloring the selected objects
-
I am a newbie in both sketchup and Ruby
The following is the part of ruby script I am working right now
cmd1 = UI;;Command.new("Show 01") {show_1} cmd1.set_validation_proc { true ? MF_ENABLED ; MF_GRAYED } cmd1.small_icon = "01_small.png" cmd1.large_icon = "01_large.png" cmd1.tooltip = "Show layer 01 with materials" toolbar1 = UI;;Toolbar.new "Color the objects" toolbar1.add_item cmd1 toolbar1.show
Till now, I am only able to create the icon on the toolbar with the image "01" I tried a lot to let the icon execute some commands but I failed all the time. (Still working on this for more than 72 hrs)
What I wanted to do is -
-
When some faces or entity is selected on the sketchup, and then, if I pressed that icon, I wanted to apply either color (from material library, such as color_A01),or texture (any jpg) on those selected face. Is it possible to do this?
-
Another thing I wished to do was put a roll down menu which gives the names of all the layers present in the sketchup model and when i press above mention key "01", it should select all the objects in the layer chosen and apply color or texture. Is it possible to do this?
I prefer color than texture.
How shall I do it?
Thank in advance
-
-
At the moment your button runs something called 'show_1'.
Keep the Ruby Console open as you are testing your script...
What happens when you click the button?
I won't go into the complexities of tools/class/module [yet] - but you should structure your tools approrpiately... but for now if you have some code in the same file like this
def show_1() UI.messagebox("'show_1' has run!") end
on a restart the button should open the message ??
If you have that running you are on the road to success...
Inside the method add your code.
Have you read examples of other scripts doing something similar - if not, do so now...
Also Read the on-line API notes...At the start of you code set some shortcut references to save typing
model=Sketchup.active_model ss=model.selection materials=model.materials layers=model.layers
Now check what is in the selection... in this example we extract the faces
faces=[] ss.each{|e|faces << e if e.class==Sketchup::Face}
Now process the faces
faces.each{|face| face.material='Red'}
There is a standard list of colors that can be called directly like 'Red', 'Black' etc, if they aren't in the model's materials then they are added, using the color's name and the appropriate RGB values.You CANNOT load material-files [SKMs] from external libraries [at least not without special tools that I have invented - but we are talking about basic API here!].
You can use any material already in the model's materials.
You can add a new material to the model's materials.
You can change the color [RGB] of any existing material.
You can change the transparency of any existing material.
You can add an image-file as the texture of any existing material.So let's assume we have an existing material called 'MyMaterial' to use it simply specify its name as
face.material='MyMaterial'
ormat=materials['MyMaterial']
, thenface.material=mat
...Let's now assume we want to make a new material called 'MaterialX'... you do this...
matx=materials.add('MaterialX')
and it's added as black [0,0,0], solid material, with no texture.
If it already existed then this new material is named 'MaterialX1'.
To adjust it's color use
matx.color=[0,255,0]
Here we've made it 'green'
To give it transparency use
matx.alpha=0.5
To add a texture is more complex...
matx.texture.filename="C:\\....\\materialX.jpg"
you add the path needed...
You can adjust the texture's properties too - like its size.
You can also get theavc=matx.texture.average_color
and use that as thematerial.color=avc
so when view mode is without a texture it's approximately the same color...
So you can see there are lots of possibilities...You can't add dropdown menus to a toolbar button - you can add them to an inputbox [or a webdialog - but that's a bit complex at the start].
Let's see how we might make a list of layer names...
` defname='Layer0'or whatever it's called in your locale
layer_names=[]
model.layers.each{|e|layer_names << e.name unless e.name==defname}
layer_names.sort!
layer_names=[defname]+layer_namesthis is a sorted list of layer names with 'Layer0' at the start
as in the native Layer Browser pane.`
Now for the inputbox...
` joined_names=layer_names.join("|")they need to be in the form 'Layer0|AA|AB|BBB' etc
prompts=["Layer: "]
values=[defname]
drops=[joined_names]
title="Choose Layer"
results=UI.inputbox(prompts,values,drops,title)
return nil if not resultsi.e. the user pressed Cancel
layer_name=results[0]
layer=model.layers[layer_name]`
Now apply the layer to 'object'...
[ruby:35i6hjzv]object.layer=layer[/ruby:35i6hjzv] etc etc -
-
Thank you very much. The code worked and will be uploading soon...
And I am learning ruby for sketchup from basics now..
Advertisement