Rounding a value for export with a ":" separator
-
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. -
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 withsprintf
...
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
-
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 -
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
-
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 returnstrue
/false
in anif
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 NOTX1=
butx1=
etc... - the
-
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
-
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
Advertisement