sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    How to use Ruby API

    Scheduled Pinned Locked Moved Developers' Forum
    28 Posts 5 Posters 1.1k Views 5 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.
    • TIGT Online
      TIG Moderator
      last edited by

      
      points.each do |e|
          $mesh_file.puts(e.x.to_s)
          $mesh_file.puts(e.y.to_s)
          $mesh_file.puts(e.z.to_s)
      end ###loop
      
      

      Adds x/y/z into file on separate lines...
      to test if it's at least getting the points values use plain puts so it appears in the console

      
      points.each do |e|
          puts(e.x.to_s)
          puts(e.y.to_s)
          puts(e.z.to_s)
      end ###loop
      
      

      To write an xyz line use

      
      points.each do |e|
          $mesh_file.puts(e.x.to_s+","+e.y.to_s+","+e.z.to_s)
      end ###loop
      
      

      Does

      $mesh_file=File.new(out_path,"w")
      

      actually make the file ?
      You need to open the file for writing AND then close it after you are done writing to it.

      $mesh_file.close
      

      perhaps it's still open and so a second [separate] attempt to write to it fails...

      TIG

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        @thomthom said:

        Normal convention is to type constants in all caps.

        Oh yeah! I rarely intialize them. Thanks Thom,

        Chris

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          TIG, Just got up, and finished reading at your post. Thanks I think I understand, and have a lot to work with.

          $mesh_file=File.new(out_path,"w")

          Made a 0 byte file that I can see in "My Computer". I assumed it was open, guess it wasn't:-). Looks like a lot of stuff to work on later today. Good advice about using "puts" to check output. I use the messagebox a lot but that is slow.

          puts(e.x.to_s) prints Sketchup::Vertex:0xea24668 ?-(, Crossed this bridge once before, but don't recall how to get output as a number.

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            puts(e.x.to_s) should return a numerical "x" position if e is a Point3d object. if e is a vertex, it should return an error. If e is a vertex, you can do e.position.x and that will return the x position of the vertex.

            That sounded confusing to me. A few more lines of your code and I could give a slightly more complete example.

            Great job so far!

            Chris

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • honoluludesktopH Offline
              honoluludesktop
              last edited by

              "oops: - I" πŸ™‚ A little slow this morning, didn't get it until now. Thanks guys, lets see how far I can get with what I have before I, OOPS:-(, need help getting off the floor. Might not take too long.

              Tig, I can now open, write to, and close files:-)

              Chris above here is where I stumbled on the code.

              It occurred to me that a "method", a "function" in other languages (I missed this one), was in my day know as a "procedure", having evolved from a "[C}all". Before this I did a Basic "sub routine", itself born out of "jump xxxx:xxxx, where xxxx:xxxx began a set of instruction that ended in "return". Before "Buddha" it was all 1111's and 0000's πŸ™‚

              1 Reply Last reply Reply Quote 0
              • honoluludesktopH Offline
                honoluludesktop
                last edited by

                Help! How do I get a number that repersents the location of a face vertices? The following is setup for Web Console. When I output to UI.messagebox(e.vertices), I get:

                #Sketchup::Vertex:0xfc83800#Sketchup::Vertex:0xfc837e8#Sketchup::Vertex:0xfc837d0

                The main proc is at the bottom, and the methods are above it.

                
                model = Sketchup.active_model
                entities = model.active_entities
                selection = model.selection
                
                ### Test is selection made
                if model.selection.empty?
                  entities = model.active_entities
                else
                  entities = model.selection
                end
                
                ###create file ComponentName.txt
                def make_comp_face_file(the_name,faces,counter,model)
                  the_line=the_name+" ",counter," faces"
                  UI.messagebox(the_line)
                  model_filename = File.basename(model.path)
                  path=File.dirname(model.path)+"\\"
                  out_name=the_name+".txt"
                  out_path=path+out_name
                end
                
                ###make vertices collection of faces in component
                def add_face_vertices_collect(faces,points)
                  faces.each do |e|
                    points.push e.outer_loop
                  end ### loop
                end
                
                ###make 3dpoint from vertex HELP HERE!!!! I think
                def print_vertices_collect(points)
                  points.each do |e|
                    UI.messagebox(e.vertices)       ###puts to test will be 
                    ###$points_file.puts(e.vertices)###put here when works
                  end
                end
                
                ###Get su;;component collection
                entities.each do |e|
                  if e.is_a? Sketchup;;ComponentInstance
                    the_name=e.definition.name
                    stuff=e.definition.entities
                    faces=[]
                    counter=0
                    ###Locate faces in each component and make faces collection
                    stuff.each do |e|
                	if e.is_a? Sketchup;;Face
                	  counter=counter+1
                	  faces.push e
                	end ###if
                    end ###loop
                    make_comp_face_file(the_name,faces,counter,model)####Have to
                    points=[]
                    add_face_vertices_collect(faces,points)          ####nest these
                    print_vertices_collect(points)                   ####later
                    $points_file.close
                  end ###if
                end ###loop
                
                
                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  The Vertex entity has a position.

                  
                  point = vertex.position
                  
                  

                  The position will be relative to parent container, and not relative the SketchUp axes.

                  Hi

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

                    
                    ### points>>>vertices
                    vertices.each do |vertex|
                      $points_file.puts(vertex.vertices.point.to_a)
                      ### FILE of VERTICES POINTS AS AN ARRAY ONE PER LINE...
                    end
                    
                    

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • honoluludesktopH Offline
                      honoluludesktop
                      last edited by

                      Hi guys, Thanks for your help. I stumbled on e.vertices.x.position after starting over. Now how do I read the SU Ruby API to make this easier to understand?-(

                      
                      model = Sketchup.active_model
                      entities = model.entities
                      selection = model.selection
                      
                      if model.selection.empty?
                      	entities = model.active_entities
                      else
                      	entities = model.selection
                      end
                      
                      entities.each do |e|
                      	if e.is_a? Sketchup;;ComponentInstance
                      		ci_name=e.definition.name
                      		#UI.messagebox(ci_name)
                      		ci_collection=e.definition.entities
                      		#UI.messagebox(ci_collection)
                      		ci_collection.each do |e|
                      			ci_faces_collection=[]
                      			if e.is_a? Sketchup;;Face
                      				ci_faces_collection.push e
                      				ci_faces_collection.each do |e|
                      					faces_vertices_collection=[]
                      					if e.vertices
                      						faces_vertices_collection.push e
                      						puts(e.vertices.x.position)
                      					end
                      				end
                      			end
                      		end
                      	end 
                      end
                      
                      
                      
                      1 Reply Last reply Reply Quote 0
                      • TIGT Online
                        TIG Moderator
                        last edited by

                        Edges, Loops, Curves, Faces, etc have vertices and those vertices have a position vertices.position (i.e. a point)
                        points have x/y/z values vertices.position.x
                        DON'T muddle them up! ❗

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • honoluludesktopH Offline
                          honoluludesktop
                          last edited by

                          OK, so how do I get the coordinate values for each vertex on each face.vertices? I think I understand vertex.position, and face.vertices, but how do I get a collection of vertex for each vertices.

                          Can't be impossible, can it?

                          1 Reply Last reply Reply Quote 0
                          • Chris FullmerC Offline
                            Chris Fullmer
                            last edited by

                            look at the Face.vertices here:

                            http://code.google.com/apis/sketchup/docs/ourdoc/face.html#vertices

                            it says what it returns - an array of vertx objects. So to get positions for all those vertices, you need to loop through that array. Something like this:

                            ` vert_array = my_face.vertices
                            position_array = []
                            x_position_array = []

                            vert_array.each do |v|
                            position_array << v.position
                            x_position_array << v.position.x
                            end`

                            Lately you've been tan, suspicious for the winter.
                            All my Plugins I've written

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

                              @honoluludesktop said:

                              OK, so how do I get the coordinate values for each vertex on each face.vertices? I think I understand vertex.position, and face.vertices, but how do I get a collection of vertex for each vertices.
                              Can't be impossible, can it?

                              That is not logical ❗
                              If you have run vertices=face.vertices you already have a collection [i.e. an array] of the face's vertices !
                              If you then look at each of them you can get their positions as another array,
                              e.g. positions=[]; vertices.each{|v|positions<< v.position}
                              Now you can look at each 'position' in that array (i.e. it's a point) and get the x/y/z values etc
                              positions.each{|p|puts p.x;puts p.y;puts p.z}
                              ❓
                              You seem to making this more complicated than it needs to be...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • honoluludesktopH Offline
                                honoluludesktop
                                last edited by

                                AGH!!!, Thats what happens when you don't really know what you are doing. I think I got it, again:-) puts(face_pts.position.to_a) almost at the bottom. Basically, I took a "vertices", got tne number of points by "vertices.length", then extracted each "vertex" by "This_number=vertices[0 to (length-1)]". The following runs in Web Console.

                                
                                model = Sketchup.active_model
                                entities = model.entities
                                selection = model.selection
                                
                                if model.selection.empty?
                                	entities = model.active_entities
                                else
                                	entities = model.selection
                                end
                                
                                def open_comp_inst_file(comp_inst_name,model)
                                	comp_inst_file_name=File.basename(model.path)
                                	path=File.dirname(model.path)+"\\"
                                	out_name=comp_inst_name+".txt"
                                	out_path=path+out_name
                                	$dxf_ci_file=File.open(out_path,"w")
                                end
                                
                                entities.each do |e|
                                	if e.is_a? Sketchup;;ComponentInstance
                                		puts("Component")
                                		comp_inst_name=e.definition.name
                                		comp_inst_collection=e.definition.entities
                                		#open text file for output
                                		#open_comp_inst_file(comp_inst_name,model)
                                		comp_inst_collection.each do |e|
                                			comp_inst_faces_collection=[]
                                			if e.is_a? Sketchup;;Face
                                				#make collection of faces
                                				comp_inst_faces_collection.push e
                                				vertices_collection=[]
                                				comp_inst_faces_collection.each do |e|
                                						if e.vertices
                                							#vertice collection
                                							vertices_collection.push e.vertices
                                							vertices_collection.each do |e|
                                								no_vert=e.length
                                								cnt_vert=no_vert
                                								puts("Vertices")
                                								no_vert.times do
                                									face_pts=(e[no_vert-cnt_vert])
                                									cnt_vert=cnt_vert-1
                                									puts("Vertex")
                                									puts(face_pts.position.to_a)
                                								end
                                							end
                                						end
                                				end #loop
                                			end #if
                                		end #loop
                                		#close text file
                                		#$dxf_ci_file.close
                                	end 
                                end
                                
                                

                                Here is some sample output:
                                %(#408000)[Start Component
                                Starts Vertices
                                Start Vertex
                                69.6952995222671
                                5.52489094225734
                                0.0
                                Start Vertex
                                26.421563780178
                                0.0
                                0.0
                                Start Vertex
                                0.0
                                34.7137089981752
                                0.0
                                Start Vertex
                                16.8521719619112
                                74.9523089386077
                                0.0
                                .
                                .]
                                Looks OK?

                                1 Reply Last reply Reply Quote 0
                                • J Offline
                                  Jim
                                  last edited by

                                  @honoluludesktop said:

                                  AGH!!!, Thats what happens when you don't really know what you are doing.

                                  Well, what are you doing? Try to make a precise statement of the purpose of the routine. In
                                  English first, then Ruby.

                                  This prints a summary of faces and vertices in the Definitions.

                                      model = Sketchup.active_model
                                      entities = model.entities
                                      selection = model.selection
                                  
                                      if model.selection.empty?
                                          entities = model.active_entities
                                      else
                                          entities = model.selection
                                      end
                                  
                                      instance_count = 0
                                      entities.each do |e|
                                          if e.is_a? Sketchup;;ComponentInstance
                                              instance_count += 1
                                              puts("Component; #{instance_count} #{e.definition.name}")
                                              face_count = 0
                                              e.definition.entities.each do |e|
                                                  if e.is_a? Sketchup;;Face
                                                      face_count += 1
                                                      puts "  face; #{face_count}"
                                                      vertex_count = 0
                                                      e.vertices.each do |v|
                                                          vertex_count += 1
                                                          puts "    vertex #{vertex_count} id;#{v.object_id}  pos;#{v.position}"
                                                      end
                                                  end
                                              end
                                          end
                                      end
                                  
                                  

                                  Note since some faces share vertices, and all vertices are listed, some vertives will be duplicated.

                                  Component; 2 Component#2
                                    face; 1
                                      vertex 1 id;48589944  pos;(2.160711", 1.853015", 0")
                                      vertex 2 id;48589932  pos;(0", 1.853015", 0")
                                      vertex 3 id;48589920  pos;(0", 1.853015", 2.160338")
                                      vertex 4 id;48589908  pos;(2.160711", 1.853015", 2.160338")
                                    face; 2
                                      vertex 1 id;48589944  pos;(2.160711", 1.853015", 0")
                                      vertex 2 id;48589692  pos;(2.160711", 0", 0")
                                      vertex 3 id;48589680  pos;(0", 0", 0")
                                      vertex 4 id;48589932  pos;(0", 1.853015", 0")
                                    face; 3
                                      vertex 1 id;48589680  pos;(0", 0", 0")
                                      vertex 2 id;48589692  pos;(2.160711", 0", 0")
                                      vertex 3 id;48589464  pos;(2.160711", 0", 2.160338")
                                      vertex 4 id;48589452  pos;(0", 0", 2.160338")
                                    face; 4
                                      vertex 1 id;48589932  pos;(0", 1.853015", 0")
                                      vertex 2 id;48589680  pos;(0", 0", 0")
                                      vertex 3 id;48589452  pos;(0", 0", 2.160338")
                                      vertex 4 id;48589920  pos;(0", 1.853015", 2.160338")
                                    face; 5
                                      vertex 1 id;48589464  pos;(2.160711", 0", 2.160338")
                                      vertex 2 id;48589908  pos;(2.160711", 1.853015", 2.160338")
                                      vertex 3 id;48589920  pos;(0", 1.853015", 2.160338")
                                      vertex 4 id;48589452  pos;(0", 0", 2.160338")
                                    face; 6
                                      vertex 1 id;48589692  pos;(2.160711", 0", 0")
                                      vertex 2 id;48589944  pos;(2.160711", 1.853015", 0")
                                      vertex 3 id;48589908  pos;(2.160711", 1.853015", 2.160338")
                                      vertex 4 id;48589464  pos;(2.160711", 0", 2.160338")
                                  
                                  

                                  Hi

                                  1 Reply Last reply Reply Quote 0
                                  • honoluludesktopH Offline
                                    honoluludesktop
                                    last edited by

                                    WOW! very compact, and fast, I will take a hard look at your code, and learn from it. Lets see how far I can get before having to post for help πŸ™‚

                                    Addenda: Well, I did have redundant code, and really need to learn how to format output. Would have never figured out Puts "#{e.position.x.to_f}" by myself.

                                    1 Reply Last reply Reply Quote 0
                                    • honoluludesktopH Offline
                                      honoluludesktop
                                      last edited by

                                      Darn, I finished coding only to discover the "putdxf" I use for my CAD import has a bug that fails to locate multiple blocks correctly. I confirmed this by being unable to "putdxf" with the system's "getdxf". Will have to rewrite as a "command line" script. Thanks for everything, sorry I couldn't understand much of what you tried to explain to me. But, because of your help, I will get better in time πŸ™‚

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

                                      Advertisement