Please help me to fix and improve the code.
On the sketchucation forum I found the code Help please. How to get transformation for the face • sketchUcation • 1
With the help of AI, I was able to refine it a bit for my needs. I added display information about the name of the object and its dimensions. But then I encountered the problem of incorrect display of object sizes if they are rotated relative to the global axes (shown on the screen). Please help me correct the actual size calculation.
The second thing I would like to fix is the selection of the whole object (group/component) instead of just the faces.
Here is the code:
Підсумок
class FaceHighlighterTool
def initialize
@ip = Sketchup::InputPoint.new
@triplets = []
@edge_points = []
@hovered_face = nil
@hovered_face_tra = nil
@face_color = Sketchup::Color.new(0,40,255,80)
@edge_color = Sketchup::Color.new(0,0,255,255)
@edge_width = 3
end
def deactivate(view)
@hovered_face = nil
view.invalidate
end
def onMouseMove(flags, x, y, view)
@ip.pick(view, x, y)
face = @ip.face
if face.nil?
if @hovered_face
@hovered_face = nil
view.invalidate
end
return
end
if face != @hovered_face
@hovered_face = face
@hovered_face_tra = @ip.transformation
# In order to draw a face with holes, we must draw its mesh
mesh = face.mesh
polygons_size = mesh.count_polygons
@triplets = Array.new(polygons_size)
for i in 0...polygons_size
# Obtain one of the triangles making up the face
triplet = mesh.polygon_points_at(i+1)
# Transform to global space
triplet.each { |pt| pt.transform!(@hovered_face_tra) }
# Store for drawing
@triplets[i] = triplet
end
# Get all edges for drawing a border
@edge_points.clear
face.edges.each { |edge|
@edge_points << edge.start.position
@edge_points << edge.end.position
}
# Transform all edge points to global space
@edge_points.each { |point| point.transform!(@hovered_face_tra) }
# Trigger the drawing
view.invalidate
end
@in_path = face ? face.parent.instances : nil
if @in_path && @in_path.first.is_a?(Sketchup::ComponentInstance)
@ttip = @in_path.first.name
else
@ttip = nil
end
end
def draw(view)
return unless @hovered_face
view.drawing_color = @face_color
@triplets.each { |triplet|
view.draw(GL_POLYGON, triplet)
}
view.drawing_color = @edge_color
view.line_width = @edge_width
view.line_stipple = ''
view.draw(GL_LINES, @edge_points)
view.tooltip = @ttip if @ttip
if @in_path
entity = @in_path.first
bounds = entity.bounds
dimensions = [bounds.width, bounds.height, bounds.depth].sort
length = dimensions.last
width = dimensions[1]
thickness = dimensions.first
text = "Name: #{entity.name}\nLength: #{length}\nWidth: #{width}\nThickness: #{thickness}"
draw_text_with_bigger_font(view, [40, 20], text, 16)
end
end
def draw_text_with_bigger_font(view, position, text, font_size)
options = {
color: "black",
font: "Arial",
size: font_size,
bold: true,
align: TextAlignLeft
}
view.draw_text(position, text, options)
end
end
Sketchup.active_model.select_tool(FaceHighlighterTool.new)