“I want this edge to be translated according to the coordinate system of the outermost component. However, after running my code, the edge is being translated based on the coordinate system of the nearest component. Where could the problem be? Thank you for your help!”
mod = Sketchup.active_model
mod.start_operation('Move Edge', true)
begin
# Get the selected edge and its parent component instance
edge = mod.selection.grep(Sketchup::Edge).first
return unless edge
# Get the top-level parent component instance
def get_top_parent(entity)
current = entity
while current.parent.is_a?(Sketchup::ComponentDefinition)
parent = current.parent.instances[0]
break unless parent
current = parent
end
current
end
parent_instance = get_top_parent(edge)
# Define the movement vector in local coordinates
local_offset = Geom::Vector3d.new(1000.mm, 0, 0) # Move 1 meter along X-axis
# 1. First convert the local vector to global coordinates
global_vector = parent_instance.transformation * local_offset
# 2. Create a global translation transformation
transform = Geom::Transformation.translation(global_vector)
# Apply the transformation to the edge
edge.parent.entities.transform_entities(transform, edge)
mod.commit_operation
rescue => e
mod.abort_operation
UI.messagebox("Error: #{e.message}")
end