Import csv data using ruby
-
Howdy folks, I'm hoping someone can help me out. I want to do three things, can anyone cleverer than me tell me whether this is possible?
1: Using the ruby window, access xyz data in a csv file
2: define each xyz co-ordinate as a point
3: draw a face between the points.
I can define points and draw the face no problem. But when I try to access the csv file I just get error messages. Is there somewhere I need to put the csv file? Even better, does anyone know a plugin that will do this for me?
Regards,
Cathal
-
If you could post the error message it would help. That provides valuable information.
-
There are already several points cloud tools in the Plugins Index.
It's easy to get the file to be read in as an array of values.
Read their code.
There are tools to add cpoints at each X,Y,Z CSV point [there are then tools to make a mesh from the cpoints], others make the mesh faces directly from the CSV X,Y,Z values directly...
One issue you'll find when making faces is that you'll need a delauney tool to work out the triangulation of many points forming the mesh...
UNLESS you structure you CSV file's points in a specific way, so you know that every three points [lines of X,Y,Z] in the file define a triangular facet...
If your points form say square faces you need something to tell your tool when one face is done and another starts, also all points must be coplanar - this is automatic with triangular faces, because any three points are by definition coplanar and will form a face [provided that that are not all coincident or colinear!]
Delauney will fail on vertical faces as it has no way of knowing which facet is which... -
Hi, thanks for replying so fast.
I'll post an error code as soon as I can find it again: I'm on a different machine right now.
Regarding making faces, I was simply using:
pt1 = [0,0,0]
pt2 = [0,1,0]
pt3 = [1,1,0]
pt4 = [1,0,0]face = Sketchup.active_model.entities. add_face pt1, pt2, pt3, pt4
I can continue to do that with the rest of the points. But how do I tell it that the first line of the csv file defines "pt1", the second line defines "pt2", etc?
Apologies if this is very basic stuff, I only started with Ruby last week
Cathal
-
Assuming your CSV file looks like this
1,2,3 1,3,3 0,2,3
etc
Then you need to specify the path to the CSV file
This line opens a browser and if you choose a file it returns the path - you ought to check for validity of the file etc...
csv=UI.openpanel("Choose CSV File...")
Then you can read the file into an array thus:
lines=IO.readlines(csv)
Then process each line in turn - chomping the '\n' off the end and skipping blanks etc
` lines.each{|line|
line.chomp!
next if line.empty?do your stuff here on 'line'
}#end of each
'Your stuff' is to split each line into its X,Y,Z values. It's currently a string
"1,2,3"so use split
xyz=line.split(",")which gives you
["1", "2", "3"]So
x=xyz.x.to_f
y=xyz.y.to_f
z=xyz.z.to_fWhere the three values are made into 'float' numbers for x/y/z. Now use those to say add a cpoint...
Sketchup.active_model.active_entities.add_cpoint([x, y, z])`
Do this inside the 'each' {} block and each point gets a cpoint added...
-
That's fantastic, thanks so much.
Advertisement