I need a script to select cololrs in a file

I have an interesting problem that can be solved easily with a suitable script.

The problem is as follows:
I do a large amount of laser cutting for people and i get the files in all kinds of formats like AI, CDR, DXF etc. I do what is needed to get them into Rhino and once I have them in Rhino I have to ensure that the colors are appropriately set for my setup i.e. black for cut, red for engrave etc.

I often get files that were created in ColelDraw and the colors come through as converted from CMYK. So I have 3 shades of black, 5 shades of red etc.

I have been sorting this out to date by process of elimination and moving stuff to the appropriate layer but it is very time consuming.

The ideal would be to run a script that would accumulate all the different colors into a checkbox. Then to be able to check specific colors and set them to a specific color and or layer.

I think that the routine could be used for a variety of things other than just selecting colors and changing them to be the same - The framework of the routine could be modified to select any criteria into the list box and combine them based on a particular criteria (I am a programmer so I tend to think in terms of writing code in a object oriented way so that the structure of the code can be used in different ways.)

Anyway - i am happy to write what I need, the issues that I have are as follows:
I don’t know how to pick the unique colors up and then to put them into a Checkbox for selection.
I don’t know how to run though the checkbox and then get the checked colors back to compare with the objects.

All the looping though objects etc is not a problem its just really loading and unloading the checkbox that is an issue.

1 Like

Hello, i think the answer to your question relies a bit on what programming language you use.

If you use Python then it is recommended to make use of the eto framework especially in your case
you would use the CheckBox class and the ‘CheckedChanged’ event it raises.
There are some examples on how to use eto with python in rhino here (just ctrl+f and search for eto)

If you use C# (which i prefer for ui) then you need visual studio and then you can use all of wpf, winforms or even eto, too.

There are some examples here for c#, namely SampleCsEto, SampleCsWpf and SampleCsWinForms

Hope this helps to get you started.

Personally i encounter the same problem quite frequently but the files i get rarely have more then 5 colors so i can just use ‘SelColor’ and a small script to set all attributes to “by layer” and put the curves on the respective layers.

I am proficient in Python and RhinoScript - I am just looking to see if anybody has a couple of pieces of code that can do the bit that is troubling me.

Add colors to a list and then make a set out of that list using set(listname). Now you have a list with unique colors from which you can feed your checkbox.
The checkbox should return a list of indicies which you can then use to access the items in your set.

https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-CheckListBox

Here is a snippet of code that might help get you started…

import rhinoscriptsyntax as rs

def ColorListBox():
    objs=rs.NormalObjects()
    if objs:
        col_list=[]
        for obj in objs:
            col=rs.ObjectColor(obj)
            rgb="{},{},{}".format(col.R,col.G,col.B)
            if not rgb in col_list: col_list.append(rgb)
        if col_list:
            resp=rs.MultiListBox(col_list,"Color List","Select one or more colors")
            if resp:
                rs.UnselectAllObjects()
                for item in resp:
                    r,g,b=item.split(",")
                    obj_col=rs.coercecolor([int(r),int(g),int(b)])
                    rs.ObjectsByColor(obj_col,True)
ColorListBox()

Brilliant - Thank you very much.

This is exactly what I was looking for. I will use this as a basis and then build the rest of the code around it.