I’m extending the code snippet given by me in your other topic.
Reference links (see in code comments)
(1) Face#normal-instance_method
(2) Vector3d#axes-instance_method
(3) Axes#set-instance_method
(4) top-level-namespace
class TesttoolAxes
def initialize
@face = nil
@tr = IDENTITY
end
def deactivate(view)
# restore the original axes when the tool deactivated /link (3) & (4)/
# the result is same as the user right click one of the axis
# and choose "Reset" from context menu
Sketchup.active_model.axes.set(ORIGIN, X_AXIS, Y_AXIS, Z_AXIS)
end
def get_trans(ph, obj)
ph.count.times{|i|
if ph.leaf_at(i) == obj
return ph.transformation_at(i)
end
}
IDENTITY
end
def onMouseMove(flags, x, y, view)
ph_fe = view.pick_helper
ph_fe.do_pick(x, y, 0)
face = ph_fe.picked_face
@face = face ? face : nil
@tr = get_trans( ph_fe, face ) if face
view.invalidate
end
def onLButtonUp(flags, x, y, view)
if @face
@c_face = @face
@c_tr = @tr
puts "#{@c_face} outer loop is highlighted."
# get new z-axis from the face normal /link (1)/
# considering it's parent's transformation
# this is perpendicular to face
new_zaxis = @face.normal.transform( @tr )
# compute an arbitrary set of axes with the given vector
# as the z-axis direction /link (2)/
new_xaxis = new_zaxis.axes[0]
new_yaxis = new_zaxis.axes[1]
# set the axes with the new vectors / link (3)/
# in this example I keep the origin, but
# the first parameter can be other (Geom::Point3d)
Sketchup.active_model.axes.set(ORIGIN, new_xaxis, new_yaxis, new_zaxis)
end
view.invalidate
end
def draw(view)
if @face
loop = @face.outer_loop.vertices.map{|v|
v.position.transform( @tr )
}
view.drawing_color = [0,0,256, 64]
view.draw(GL_POLYGON, loop)
view.drawing_color = [0,0,256, 32]
view.draw2d(GL_POLYGON, loop.map{|p| view.screen_coords(p)})
end
if @c_face
c_loop = @c_face.outer_loop.vertices.map{|v|
v.position.transform( @c_tr )
}
view.line_width = 3
view.drawing_color ="blue"
view.draw(GL_LINE_LOOP, c_loop)
end
end
end
Sketchup.active_model.select_tool(TesttoolAxes.new)
EDIT:
In case you want to go back as it was before the tool activated, 2 methods will looks like
def initialize
@face = nil
@tr = IDENTITY
@orig_origin, @orig_xaxis, @orig_yaxis, @orig_zaxis = Sketchup.active_model.axes.to_a
end
def deactivate(view)
# restore the original axes as before the tool were activated
# when the tool deactivated
Sketchup.active_model.axes.set(@orig_origin, @orig_xaxis, @orig_yaxis, @orig_zaxis)
end
…and: Sketchup.active_model.axes.set(ORIGIN, new_xaxis, new_yaxis, new_zaxis)
def onLButtonUp(flags, x, y, view)
.
.
Sketchup.active_model.axes.set(@orig_origin, new_xaxis, new_yaxis, new_zaxis)
end