sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Rounding a value for export with a ":" separator

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 3 Posters 412 Views 3 Watching
    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.
    • IltisI Offline
      Iltis
      last edited by

      Hello,
      I'm working on a plugin to export the 2D coordinates of the selected faces. (Okay, with big help from others, I'm newbie in Ruby)

      		  # Open a file for writing
      		  File.open(filepath, "w"){ |file|
      			selection = Sketchup.active_model.selection
      			# Get an array of faces that are in the selection.
      			faces = selection.grep(Sketchup;;Face) 
      			faces.each_with_index{ |face, index|
      			  # Write a label for the face.
      			  file.puts("Face#{index+1}")
      			  # Get a transformation object that translates from model space to 2d space of the face.
      			  t = Geom;;Transformation.axes(face.vertices.first.position, *face.normal.axes).inverse
      			  # Write all vertices to the file.
      			  face.outer_loop.vertices.each{ |vertex|
      				# Get the point of the vertex and apply the transformation.
      				point = vertex.position.transform(t)
      				# Convert the coordinates
      				u, v = point.to_a.map{ |c| c.to_f }
      				# Write the coordinates to the file.
      				file.puts("#{u*25.4};#{v*25.4}")
      			  }
      			}
      		  }
      

      It works, but I have two problems to solve, can you help me?
      1 / The numbers close to zero are represented by scientific notation (1e-14), which I have problems to read the text file.
      See for example this export of a circle :

      Face1
      0.0;0.0
      6.81483474218631;51.7638090205041
      -9.02389274415327e-014;103.527618041008
      ...
      -393.185165257814;51.7638090205041
      -386.370330515627;-9.02389274415327e-014
      -366.390246014701;-48.236190979496
      ...
      -19.9800845009259;-48.2361909794959
      

      I think I could use the command "sprintf", but I don't know exactly how to write it.

      2 / I need to repeat the first point at the end (to close the outline) and I don't know how to properly store this value for reuse.

      Thanks for your help.
      Renaud.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        Try u.to_mm etc on your 'inch' values, so the tolerance is preserved.
        Some of your small exponent numbers are in effect 0.0, at least as far a SketchUp's 3d geometry is concerned.
        Here are some examples with sprintf...
        For example:
        1.0.to_mm 25.4 1.0135056009.to_mm 25.74304226286 0.000135056009.to_mm 0.0034304226286 0.00001.to_mm 0.000254 0.0001.to_mm 0.00254 0.000000135056009.to_mm 3.4304226285999997e-06
        Then:
        sprintf("%.6f", 0.254) 0.254000 sprintf("%.6f", 0.00254) 0.002540 sprintf("%.6f", 0.0000254) 0.000025 sprintf("%.6f", 0.000000254) 0.000000

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          driven
          last edited by

          I use the much cruder

          file.puts("#{(u*10000*25.4).to_i.to_f/10000};#{(v*10000*25.4).to_i.to_f/10000}")
          

          to return

          @unknownuser said:

          Face1
          0.0:0.0
          -3682.0:0.0
          -3682.0:-3138.9999
          0.0:-3138.9999

          learn from the mistakes of others, you may not live long enough to make them all yourself...

          1 Reply Last reply Reply Quote 0
          • IltisI Offline
            Iltis
            last edited by

            Hello TIG and driven. The solution of driven work good. Thanks a lot!

            My second problem is how to code the ********lines in Ruby :

              # Open a file for writing
                    File.open(filepath, "w"){ |file|
                     selection = Sketchup.active_model.selection
                     # Get an array of faces that are in the selection.
                     faces = selection.grep(Sketchup;;Face)
                     faces.each_with_index{ |face, index|
                       # Write a label for the face.
                       file.puts("Face#{index+1}")
                       # Get a transformation object that translates from model space to 2d space of the face.
                       t = Geom;;Transformation.axes(face.vertices.first.position, *face.normal.axes).inverse
                       # Write all vertices to the file.
                       
                    ********* blnFirstPoint=true
            
                       face.outer_loop.vertices.each{ |vertex|
                        # Get the point of the vertex and apply the transformation.
                        point = vertex.position.transform(t)
                        # Convert the coordinates
                        u, v = point.to_a.map{ |c| c.to_f }
            
                        ********* if blnFirstPoint=true then
                        *********    X1=u
                        *********    Y1=v
                        *********    blnFirstPoint=false
                        ********* end if
            
                        # Write the coordinates to the file.
                        file.puts("#{(u*10000*25.4).to_i.to_f/10000};#{(v*10000*25.4).to_i.to_f/10000}")
                       }
                       ********* file.puts("#{(X1*10000*25.4).to_i.to_f/10000};#{(Y1*10000*25.4).to_i.to_f/10000}")
                      }
                     } 
            

            It's to close the faces (see the attached picture).
            Thanks,
            Renaud


            Non-closed export

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              if blnFirstPoint=**=**true then
              Note it is == and NOT = when you are testing a value

              • the = assigns the first reference to be the second value, while the == compares two values and returns true/ false in an if test...

              Also only use a Capital letter at the start of a Constant, and make your 'variables' starting with lower-case letter.
              So it's NOT X1= but x1= etc...

              TIG

              1 Reply Last reply Reply Quote 0
              • D Offline
                driven
                last edited by

                the alternate approach is to build an array that includes the start point twice...

                   all_points = []
                           #add the first at the end as well...
                           all_points.push(face.outer_loop.vertices[0..-1]).push(face.outer_loop.vertices[0])
                           #then run your code
                            all_points.flatten.each{ |vertex|
                

                there are more elegant ways...

                john

                learn from the mistakes of others, you may not live long enough to make them all yourself...

                1 Reply Last reply Reply Quote 0
                • IltisI Offline
                  Iltis
                  last edited by

                  OK, this works well :

                              begin
                  		  # Open a file for writing
                  		  File.open(filepath, "w"){ |file|
                  			selection = Sketchup.active_model.selection
                  			# Get an array of faces that are in the selection.
                  			faces = selection.grep(Sketchup;;Face) 
                  			faces.each_with_index{ |face, index|
                  			  # Write a label for the face.
                  			  file.puts("Face#{index+1}")
                  			  # Get a transformation object that translates from model space to 2d space of the face.
                  			  t = Geom;;Transformation.axes(face.vertices.first.position, *face.normal.axes).inverse
                  			  # Write all vertices to the file.
                  			  blnFirstPoint = true
                  			  first_point_u=0
                  			  first_point_v=0
                  			    face.outer_loop.vertices.each{ |vertex|
                  				# Get the point of the vertex and apply the transformation.
                  				point = vertex.position.transform(t)
                  				# Convert the coordinates
                  				u, v = point.to_a.map{ |c| c.to_f }
                              if blnFirstPoint==true
                  				  first_point_u = u
                  				  first_point_v = v
                  				  blnFirstPoint = false
                  				end #if
                  				# Write the coordinates to the file.
                  				file.puts("#{(u*10000*25.4).to_i.to_f/10000};#{(v*10000*25.4).to_i.to_f/10000}")
                  	            } 
                  			  file.puts("#{(first_point_u*10000*25.4).to_i.to_f/10000};#{(first_point_v*10000*25.4).to_i.to_f/10000}")
                  			  }
                  		   }
                  

                  Thanks a lot! 😍
                  Renaud

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

                  Advertisement