Hello!
I’m working on a function to toggle displaying the area size of faces on the face. I’ve got the text-part to work:
Module Bob
def self.text_on_face( txt, face, group )
# Print the angle on the surface
dim = group.entities.add_3d_text( txt, TextAlignCenter, "Arial", true, false, 7, 0.0, 0.0, false, 0.0 )
# 1) Translate to center of the text
tr = Geom::Transformation.translation( [-0.5*group.local_bounds.width, -0.5*group.local_bounds.height, 0] )
group.transform!( tr )
# 2) Flip the text so up is up
tr = Geom::Transformation.rotation( ORIGIN, X_AXIS, 90.degrees )
group.transform!( tr )
# 3) Find the orientation of the face to orient the text
new_Y_AXIS = face.normal.reverse
new_X_AXIS = new_Y_AXIS * Z_AXIS
tr = Geom::Transformation.axes( center_of(face), new_X_AXIS, new_Y_AXIS )
troup.transform!( tr )
return dim
end
def self.center_of(face)
pts = face.vertices.map{|v| v.position}
Geom::Point3d.new(
avg( pts.map{|pt| pt.x } ),
avg( pts.map{|pt| pt.y } ),
avg( pts.map{|pt| pt.z } )
).project_to_plane(
face.plane # make sure point is on the face's plane
)
end
end
I would like dim to update if the face changes but of course it doesn’t. If I adjust the face now, the text doesn’t move with the face, and if the area changes, the text doesn’t update. So it doesn’t work like a Sketchup::Dimension yet.
I’ve read several earlier posts about this topic and understand that the ruby version of add_dimension_linear is limited compared to the UI version. So rather than using add_dimension_linear and trying to adjust its appearance, I’m thinking about making a subclass of Dimension with some new functionality to make this possible. I’m thinking maybe something like this:
class DimensionArea < Sketchup::Dimension
# Magic goes here...
# Goal is that this dimension updates when the model updates
# Like all other dimensions do....
end
class Sketchup::Entities
def add_dimension_area( face )
txt = Sketchup.format_area( face.area )
dim = Bob::text_on_face( txt, face, myGroup )
return dim
end
end
Would this be possible? And would it be ok to add methods to existing classes in Sketchup or better not?