@adrigon, @TIG pointed me toward an old thread on SketchUcation in which …
@AdamB proposed a totally mathematical solution, [u]without a temporary group[/u].
Following is my rendition of this solution.
Mine does not modify API class Geom::Point3d
, but converts to an Array
which already has a #dot
method.
Mine also uses #grep
which is very fast when comparing class identity, … instead of calling #is_a?
on each and every object in the geometry
collection on the Ruby-side, during each iteration.
# calculate_volume(geometry)
# Calculate volume on a set of faces without a temporary group.
#
# Based on an old SCF topic thread post by AdamB:
# http://sketchucation.com/forums/viewtopic.php?p=14598#p14598
#
# @param geometry [Array<Drawingelement>] Array of collected geometry references.
# @return [Float] the computed volume (in inches) if successful, 0.0 if not.
def calculate_volume(geometry)
geometry.grep(Sketchup::Face).map {|f|
(2 * f.area * (f.vertices[0].position.to_a.dot(f.normal))) / 6
}.reduce(:+)
rescue => e
puts e.inspect
return 0.0
end
Snipped off volume format string to new topic: