How to use Ruby API
-
Edges, Loops, Curves, Faces, etc have
vertices
and thosevertices
have a positionvertices.position
(i.e. a point)
points have x/y/z valuesvertices.position.x
DON'T muddle them up! -
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?
-
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` -
@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 runvertices=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... -
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? -
@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")
-
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.
-
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
Advertisement