Minimum dimension of a component
-
I'm trying to ascertain the smallest edge at the origin of a component. I think I'm nearly there but something's not right in the code below! Any suggestions would be appreciated : )
sel.grep(Sketchup::ComponentInstance){|cdi| org = cdi.transformation.origin cents = cdi.definition.entities vert = [0] cents.grep(Sketchup::Vertex).each{|v| vert << v if v == org } thickness = vert.edges.min p thickness }
-
@deanby7 said:
I'm trying to ascertain the smallest edge at the origin of a component. I think I'm nearly there but something's not right in the code below! Any suggestions would be appreciated : )
sel.grep(Sketchup::ComponentInstance){|cdi| org = cdi.transformation.origin cents = cdi.definition.entities vert = [0] cents.grep(Sketchup::Vertex).each{|v| vert << v if v == org } thickness = vert.edges.min p thickness }
Don't think Sketchup::Vertex is "searchable" also, it is not a valid comparison for a Vertex and a Point3d, you need to compare the transformed Vertex.position.
Here is my solution
mod = Sketchup.active_model sel = mod.selection sel.grep(Sketchup;;ComponentInstance){|cdi| org = cdi.transformation.origin cents = cdi.definition.entities trn = cdi.transformation; vert = nil cents.grep(Sketchup;;Edge){|e| e.vertices.each{|v| vpt=v.position.transform(trn) vert = v if vpt == org; } } unless vert==nil thickness = 1e9 vert.edges.each{|e| thickness = [thickness,e.length].min } puts "Thickness of #{cdi.definition.name} is #{thickness}" end }
-
Many thanks. Works a treat.
-
My solution:
def thickness(obj) ents =( obj.definition.entities rescue obj.entities ) tran =( obj.transformation rescue IDENTITY ) edges = ents.grep(Sketchup;;Edge).find_all {|e| e.start.position == ORIGIN || e.end.position == ORIGIN } if edges.size < 3 return 0.to_l # not a 3D object or edges array empty end shortest = edges.min_by {|e| e.length } shortest.start.position.transform(tran).distance( shortest.end.position.transform(tran) ) end
This should work with an argument that is a definition, group or component instance.
If a ComponentDefinition is passed, the transform will be the
IDENTITY
transform, otherwise an instance or group will be it's transform (which also might be ==IDENTITY
.)Within the entities the local origin is ==
ORIGIN
, so there is no need to create any temporary point object for comparison, just to select the 3 edges.Once we have the shortest (untransformed) edge, we apply the transformation to both it's vertex positions, to account for any rotation and scaling (either uniform or not.)
Now, this assumes that the shortest definition edge is always to be considered the thickness, even in the instances.
But what if someone does non-uniform scaling and makes one of the longer edges shorter than the one that is supposed to be the shortest ?
I suppose, if you control the definitions, you could attach an attribute to the "thickness" edge, and find it by attribute.
Advertisement