Control component with csv file
-
I have a lot of components in a drawing and I would like to be able to change their colors based off an excel spread sheet. I would like to build the components in sketchup export them to a csv file, then in the csv file add a column for color, filter the components and add color, then import it back into sketchup. Just wondering if this is possible with either a regular component or a dynamic component or some kind of ruby script?
Thanks
-
@rpetter said:
I have a lot of components in a drawing and I would like to be able to change their colors based off an excel spread sheet. I would like to build the components in sketchup export them to a csv file, then in the csv file add a column for color, filter the components and add color, then import it back into sketchup. Just wondering if this is possible with either a regular component or a dynamic component or some kind of ruby script?
Thanks
You can only do it in the same session if you use the instance's id in the csv because that changes across sessions.
You could add a custom attribute to each one before exporting the csv - that way you'd access the instances by that later, even in another session.
Filter a selection or entities context etc so you have an array of the required instances...
Then iterate through that array and use e.g.
tid=Time.now.to_f+rand
to give you a unique reference to the instances a tid as they are exported [unless they already have one]
instance.set_attribute("RPetter","tid",tid)unless instance.get_attribute("RPetter","tid",nil)
Now export the instance's with theirtid.to_s
as the first column [A] andinstance.material.color.red
to the second column [B] and .green and .blue to columns C abd D respectively to compile the csv [there are many examples of doing that available].
Then manipulate the csv adding color red/green/blue to columns B/C/D.
Now read in the csv into the SKP as an array of lines usingcsv.IO.readlines
.
Thensplit(",")
to get the row's column entries as strings.
Finally find the matching instanceinstance.get_attribute("RPetter","tid",nil)==rowA.to_f
and the matching instance's material.color becomes a new material with the RGB values.color[rowB.to_i,rowC.to_i,rowC.to_i]
If you want to specify the material's name add a csv column for that... -
Thanks I will give it a go.
-
rpetter, please let us know how this turns out for you. Sounds very interesting. Thanks
-
What am I missing here. I have been investigating this problem and thought I was making some progress until I noticed something rather peculiar. In my model I have 4 components and 5 instances. Two of #1 and 1 each of #2,#3 amd #4. I select anyone of them and change its' material color and the color of all change.
mod = Sketchup.active_model ent = mod.entities sel = mod.selection c = sel.first c.material.color=[0,0,255]
-
The material is applied to all instances. You have changed the color of the material, which is still applied to all instances. Drawingelements have Materials, but not color directly; Materials have color.
-
I would certainly understand if I was editing a component with multiple instances and I changed something then it would change in all instances but in this case I'm not editing anything merely selecting a unique component and changing the color applied to the component. I'm not changing the definition of a color.
-
You are changing the color of the instance's material...
So whatever that is its color changes...
You need to change the material of the instance [which perhaps will have a different color]... -
What you did was the same as editing a Material in the Material Browser. You haven't added a new material, you haven't changed which components the material is applied to - you just changed the color of the Material.
-
Yes I see now. It should be instance.material=[r,g,b] not instance.material.color=[r,g,b] which I thought is what TIG meant in his original suggestions.
After making that change everything seems to be working. I have attached a copy if anyone else is interested in trying it out.
Advertisement