Pulling attributes from a spreadsheet?
-
I'm looking for a way to color a face based on the value of a cell in a spreadsheet (or CSV or similar).
If I have a cube with attribute faceid=1 to faceid=6, can I color the cube automatically (or from a script) that retrieves the corresponding hex color from the column "faceid" in my spreadsheet?
I've seen scripts that can export to a CSV or similar, but what about importing or linking to a spreadsheet?
Any ideas welcome,
Antony
-
Yeah, that would be possible. How ddi you get the attribute assigned to the faces? Have you already written that portion of the code yourself? Or do you have a script from somewhere else that writes attributes to objects?
The idea is fairly simple, you just have Ruby parse the csv file and then create a hash with attribute values and color values. Then when you find an attribute value, you just assign the associated color to the face.
I have little experience with parsing text files, but others around here have done it for sure. I know its not exactly difficult. It would really be a pretty good beginner script or an intro to Ruby for SketchUp script to write.
Chris
-
Thanks Chris for your reply,
The attributes were created using AttributeManager like http://code.google.com/p/sketchupattributemanager/
In an ideal world, I'd have the faces colored by a range of colours derived from the spreadsheets high and low values, much like your ColorByZ script! Instead of using the z-value from the model, the script would use the spreadsheet's value for that particular faceid. Faces without a faceid would not be colored.
The ColorByValue script would have a UI with a drop down of available columns to choose from. But I'm getting ahead of myself.
I'm brand new to Ruby so it is a little daunting at first go...
-
Let's assume your face-code attribute is a string [say
"6"
] and we have obtained that from@face
using
@code=@face.get_attribute("face_attributes","face_code",nil)
or whatever it's called !
(if it's an integer use
@code=@code.to_s
inside the coloring 'if' test)
then testif @code ### do coloring... end#if @code
The face code's coloring needs a material... so we read that from a file...
you can have its path 'hard-coded' or chosen@filepath=UI.openpanel("Face Color Code Choose File...","","*.csv") if not @filepath puts "FACE COLOR CODE FILE - CANCELED." @go_on=false Sketchup.send_action("selectSelectionTool;") return nil else @go_on=true end#if
if @go_on we continue...
@alltext=IO.readlines(@filepath)
We now use the@alltext
to look for the 'code'0.upto(@alltext.length-1) do |i| line=@alltext[i] if line ### we remove whitespace and carriage-return... line.strip! line.chomp! ### we'll assume csv file in the format 'face_code,material_name' ### e.g. "...\n6,red\n7,green\n..." if line and @code==line.split(",")[0] ### e.g. "6" @face.material=line.split(",")[1] ### set material - e.g. "red" ### you can add extra tests, make materials if not existing etc etc break ### we leave the do loop as we have a 'hit'. end#if @code==... end#if line end#upto
-
Thanks TIG for the snippets - perfect.
For others, this is the code I have so far. It has lots of room for improvement, and I'm happy for others to improve, extend, and post back here.
I assigned attributes to each face with a key/value of faceid/x.
model = Sketchup.active_model entities = model.entities selection = model.selection #Define array faces = [] # choose either the selection, or the active model if model.selection.empty? # UI.messagebox "You have nothing selected. This will color all faces that are not grouped." entities = model.active_entities else entities = model.selection end #go through every entity, and if it is a face then add it to the faces array entities.each do |e| faces.push e if e.is_a? Sketchup;;Face end #Debug; how many faces are going to be shaded #UI.messagebox "No of faces in model/selection; " + faces.length.to_s #get the materials from a csv file which has corresonding faceids @filepath=UI.openpanel("Face Color Code Choose File...","","*.csv") if not @filepath puts "FACE COLOR CODE FILE - CANCELED." @go_on = false Sketchup.send_action("selectSelectionTool;") return nil else @go_on = true end if @go_on #read the file into the instance variable @alltext @alltext=IO.readlines(@filepath) #debug; how many lines/rows were in the file? #UI.messagebox "Number of rows in file is " + @alltext.length.to_s faces.each do |m| @currentfaceid = m.get_attribute("fn","faceid",nil) #UI.messagebox "@currentfaceid = " + @currentfaceid.to_s #UI.messagebox @face #now apply the material to each face 0.upto(@alltext.length-1) do |i| #beginning with 0 line = @alltext[i] if line line.strip! #remove whitespace line.chomp! #remove carriage returns # we'll assume csv file in the format 'faceid,material_name' # e.g. "...\n6,red\n7,green\n..." if line and @currentfaceid == line.split(",")[0] # e.g. "6" #Apply the material @matchedcolor = line.split(",")[1] m.material = @matchedcolor # set material - e.g. "red" m.back_material = @matchedcolor # set material - e.g. "red" ### you can add extra tests, make materials if not existing etc etc break ### we leave the do loop as we have a 'hit'. end end end end end
The contents of the CSV file, for reference:
faceid,material 1,CadetBlue 2,Aqua 3,DarkOrange 4,LawnGreen 5,Red 6,PowderBlue 7,Yellow
Things that need improvement:
make it executable script from a menu
make it work on groups and not just faces
pass the contents of the CSV into an array once rather than reading the file over and overand plenty of more things I'm sure!
Advertisement