Here is a bit of code which draws the shape shown here.
This is just sample code, but in my actual project, I am going to be making a lot of these shapes and rotating them various ways…
I would like to be able to determine which direction (front, back, left, right, top, bottom) the flap is facing after the rotation. But I’m finding that after the rotation, the face still retains the normal it had in the original shape.
module JJones_example
def self.draw_shape(entities, points, flap_points, material)
sgroup = draw_edges(points, entities)
flap = draw_flap(flap_points, sgroup.entities, material)
[sgroup, flap]
end
def self.draw_edges(points, entities, with_callouts=false)
print("draw_edges")
g = entities.add_group
last_point = points[points.length - 1] #.shift
points.each_with_index{ |point, i|
if with_callouts
self.make_text(g.entities, point, i)
end
g.entities.add_edges(last_point, point)
last_point = point
}
g
end
def self.draw_flap(flap, entities, material)
print("draw_flap")
face = entities.add_face(flap)
face.material = material
face.back_material = material
face
end
def self.move_thing(obj, distance, axis)
print("move_thing")
tr = Geom::Transformation.new([distance,0,0]) if axis == "x"
tr = Geom::Transformation.new([0,distance,0]) if axis == "y"
tr = Geom::Transformation.new([0,0,distance]) if axis == "z"
obj.transform! tr
end
def self.get_rotation(obj, axis, angle)
bounds = obj.bounds
ctr = bounds.center
rv = obj.transformation.zaxis if axis == "z"
rv = obj.transformation.yaxis if axis == "y"
rv = obj.transformation.xaxis if axis == "x"
ro = Geom::Transformation.rotation(ctr, rv, angle.degrees)
ro
end
def self.rotate_thing(obj, axis, angle)
print("rotate_thing")
ro = get_rotation(obj, axis, angle)
obj.transform! ro
end
def self.copy_and_transform(shape, distance, angle, axis)
print("copy_and_transform")
cp = shape.copy
self.move_thing(cp, distance, axis)
self.rotate_thing(cp, axis, angle)
cp
end
def self.get_material(model)
color = Sketchup::Color.new(255,255,255)
material = model.materials.add("translucent")
material.color = color
material.alpha = 0.5
material
end
end #module
model = Sketchup.active_model
entities = model.active_entities
model.start_operation("copy and rotate shape", true)
# 0 1 2 3 4 5 6 7
pts = [[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]
shape_pts = [pts[0],pts[1],pts[3],pts[2],pts[6],pts[7],pts[5],pts[4]]
flap_pts = [pts[3],pts[2],pts[6],pts[7]]
material = JJones_example.get_material(model)
sgroup, flap = JJones_example.draw_shape(entities, shape_pts, flap_pts, material)
print("flap normal: #{flap.normal}")
# now copy and transform the object, rotating it and moving it nearby
copy = JJones_example.copy_and_transform(sgroup, 1.5, 90, 'x')
#is flap on left, right, top, bottom, back, front?
faces = copy.entities.grep(Sketchup::Face)
flap2 = faces[0]
print("flap2 normal: #{flap2.normal}")
model.commit_operation
Here’s an image of the rotated shape, highlighting the face normal. As you can see, it still thinks the face is in the original orientation, before the object was rotated.
How can I find the normal of the face in the rotated shape?
Thank you for any clues
-j_jones