• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Import csv data using ruby

Scheduled Pinned Locked Moved SketchUp Discussions
sketchup
6 Posts 3 Posters 2.5k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    installtechireland
    last edited by 18 Jan 2012, 11:42

    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

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 18 Jan 2012, 11:59

      If you could post the error message it would help. That provides valuable information.

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 18 Jan 2012, 12:08

        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...

        TIG

        1 Reply Last reply Reply Quote 0
        • I Offline
          installtechireland
          last edited by 18 Jan 2012, 12:22

          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

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 18 Jan 2012, 12:40

            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...
            🤓

            TIG

            1 Reply Last reply Reply Quote 0
            • I Offline
              installtechireland
              last edited by 18 Jan 2012, 13:03

              That's fantastic, thanks so much.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              1 / 1
              • First post
                5/6
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement