Hello everybody,
I’m trying to find a reliable method to retrieve the correct global position of a Face
or Edge
in SketchUp. Most of the time, the entity is part of the global model, but it can also be nested inside one or more groups or components, which themselves can be nested within other groups or components.
Problem:
- The
Face
orEdge
is not selected. - I cannot rely on user input to select the entity (e.g., using
PickHelper
). - I want to receive the global positioning of a
Face
orEdge
(or the correct transformation).
What I have so far:
Here’s the code I am currently using to handle cases where the Face
or Edge
is inside a ComponentInstance
:
def get_parent_transformation
found_instance = nil
if !parent.is_a?(Sketchup::Model) && (self.is_a?(Sketchup::Face) || self.is_a?(Sketchup::Edge))
vertices_local = vertices.map { |vertex| vertex.position }
instances = parent.instances
instances.each do |instance|
vertices_global = vertices_local.map { |point| point.transform(instance.transformation) }
match = vertices.each_with_index.all? do |vertex, index|
vertex_global_position = vertex.position.transform(instance.transformation)
puts vertex_global_position.distance(vertices_global[index])
(vertex_global_position.distance(vertices_global[index]) < 0.001)
end
if match
found_instance = instance
break
end
end
elsif !parent.is_a?(Sketchup::Model) && self.is_a?(Sketchup::ComponentInstance)
found_instance = find_parent_instance(self)
end
if found_instance
parent_transformation = found_instance.get_parent_transformation * found_instance.transformation
else
parent_transformation = Geom::Transformation.new
end
parent_transformation
end
Current Behavior:
- This code works when the
Face
orEdge
is nested one level deep inside aComponentInstance
. - It works by comparing the global and local positions of vertices to determine the correct instance.
Problem I’m Facing:
- The issue arises when the
Face
orEdge
is nested deeper within multiple levels ofComponentInstance
s (i.e., aComponentInstance
inside anotherComponentInstance
). - In this case, I’m unsure how to accurately identify which
ComponentInstance
is the correct parent, especially when there are multiple instances of the sameComponentDefinition
.
Question:
How can I reliably determine the correct ComponentInstance
(or global positioning) when a Face
or Edge
is nested deeper inside multiple component hierarchies? Is there a way to ensure that I apply the correct transformations recursively when dealing with complex nesting?
Any insights or suggestions would be greatly appreciated!