Rotating faces to create a u-shape
-
Hello, I am just learning the Ruby API and have a hopefully simple question.
My goal is to draw a face in an arbitrary point in space (not at the origin). Then I want to copy the face and rotate it 90 degrees to create a corner shape. Then copy the second face and rotate it 90 degrees to create a u-shape
This is some example code I came up with to do this, however, I'm running into a problem in that I can't seem to access the points of the rotated face. The positions of the points are identical the ones in the original face for some reason and do not reflect the transformed shape.
Here is the script.
model = Sketcup.active_model active_entities = model.active_entities # draw first face # p3 p2 # +---------+ # | | # | | # | | # | | # +---------+ # p0 p1 model.start_operation("first face",true,false,true) pts = [[10,10,0], [20,10,0], [20,10,10], [10,10,10]] pt0, pt1, pt2, pt3 = pts group1 = active_entities.add_group g1_entities = group1.entities face1 = g1_entities.add_face(pts) # print out the vertexes of the first face puts("first group") group1.entities.each do |thing| if thing.is_a?(Sketchup;;Edge) puts "#{thing.start.position} #{thing.end.position}" end end # end of first face model.commit_operation
So far so good, here is the face, ready to go
Now on to the second face
model.start_operation("second face",true,false,true) # make a copy of the first face # then rotate it to create a corner shape # pt0 # +---------+ # /| | # / | | # / | | # + | | # | +---------+ # | /pt3 # | / # |/ # + group2 = group1.copy g2_entities = group2.entities axis = pt0.vector_to(pt3) angle = -90.degrees xform = Geom;;Transformation.rotation(pt0, axis, angle) group2.transform!(xform) # notice that the points of the group2 face have not been rotated # they still reflect the points of the first face puts("second group") group2.entities.each do |thing| puts thing if thing.is_a?(Sketchup;;Edge) puts "#{thing.start.position} #{thing.end.position}" end end # end of second face model.commit_operation
Second face rotated, looks good except if I query the second face for the positions of its vertices, it just reports the same positions as the first face.
By the way, I must be doing something really weird because when I run this script in Ruby Console Plus, it freezes and then goes blank. I haven't had any trouble with the console until I ran this script! I guess it's because I really should put all this logic into a module or class instead of just in the global namespace.
Now I would like to rotate the second face to create a u-shape, but I don't know what points to pick because the points given don't reflect the rotation
model.start_operation("third face",true,false,true) # make a copy of the second face # then rotate it to create a u-shape group3 = group2.copy g3_entities = group3.entities # goal # +---------+ # /| | # / | | # / | | # +---------+ | # | +-----|---+ # | / | # | / | # |/ | # +---------+ # at this point I don't really know which points to choose to create # the axis for rotating the third face as I don't seem to be able # to access the rotated shape's actual points? puts("third group") group3.entities.each do |thing| puts thing if thing.is_a?(Sketchup;;Edge) puts "#{thing.start.position} #{thing.end.position}" end end # end of third face # model.commit_operation
Thanks for any clues
j_jones -
After some struggles, I came up with this script which "solves" it, however I the ruby console plus crashes about 20 seconds after running this script, I have yet to figure out how to trap that error, whatever it is
link to animated gif demonstration:
https://drive.google.com/file/d/16QQNpN4UhxjZRFghHKzPZY_zYuAYbiHz/view?usp=drive_link
Here is the revised script that at least does what I was trying to do.
The difference is that I used transform_entities instead of transform!
that seemed to have the desired effect on the resulting face so that I could extract it's vertices after the xform. An idea I got from here: https://forums.sketchup.com/t/move-a-group-api-ruby/130338/5module RotateFaces def self.get_face_from_entities(things) face = nil things.each do |thing| if thing.is_a?(Sketchup;;Face) face = thing break end end return face end def self.rotate_face(entz, face, axis_pt1, axis_pt2, angle) axis = axis_pt1.vector_to(axis_pt2) xform = Geom;;Transformation.rotation(axis_pt1, axis, angle) entz.transform_entities(xform, face) return face end def self.get_face_vertices(face) verts = [] face.outer_loop.vertices.each{|v| verts << v.position} return verts end def self.create_rotated_face(entz, pts, axis_pt1, axis_pt2, angle) model = Sketchup.active_model active_entities = model.active_entities model.start_operation("create rotated face",true,false,true) face = self.get_face_from_entities(entz) face_xformed = self.rotate_face(entz, face, axis_pt1, axis_pt2, angle) verts = self.get_face_vertices(face_xformed) model.commit_operation return [verts, face_xformed] end end model = Sketchup.active_model active_entities = model.active_entities begin things1 = [] group1 = active_entities.add_group pts1 = [[10,10,0], [20,10,0], [20,10,10], [10,10,10]] pt0, pt1, pt2, pt3 = pts1 g1_entities = group1.entities face1 = g1_entities.add_face(pts1) puts "face1; ", face1 puts "group1;", group1 group2 = group1.copy() things2 = RotateFaces.create_rotated_face(group2.entities, pts1, pts1[0], pts1[3], -90.degrees) puts "create_rotated_face returned; ", things2 pts2 = things2[0] face2 = things2[1] puts "group2;", group2 # (10", 10", 0") # (10", 0", 0") # (10", 0", 10") # (10", 10", 10") group3 = group2.copy() things3 = RotateFaces.create_rotated_face(group3.entities, pts2, pts2[1], pts2[2], -90.degrees) puts "create_rotated_face returned; ", things3 pts3 = things3[0] fac3 = things3[1] # (20", -0", 0") # (10", 0", 0") # (10", 0", 10") # (20", -0", 10") puts "group3;", group3 group4 = group3.copy() things4 = RotateFaces.create_rotated_face(group4.entities, pts3, pts3[0], pts3[3], -90.degrees) puts "create_rotated_face returned; ", things3 pts4 = things4[0] fac4 = things4[1] puts "group4;", group4 puts "made it this far" rescue StandardError => e puts "error last global outer try;", e end
Advertisement