Global coordinates of instances with swapped axes

Hello everybody.

For some time now I have been trying to obtain the global coordinates of nested component instances.

I had some success when the instances have the axes with the same positions, the problem is when there are instances with different or rotated axes.
Could anyone help me with this issue? Is this possible?

References used:

My code example:

model = Sketchup.active_model
selection = model.selection

def get_all_instances(entities, path, other)
  entities.grep(Sketchup::ComponentInstance).concat(entities.grep(Sketchup::Group)).each do |dc|
    other = dc if dc.parent.is_a?(Sketchup::Model)
    pt = dc.transformation.origin
    ptt = pt.transform(dc.parent.is_a?(Sketchup::Model) ? Geom::Transformation.new(ORIGIN) : other.transformation)
    puts "#{ptt} - #{dc.definition.name} - #{path}"

    get_all_instances(dc.definition.entities, path + [dc], other)
  end
rescue StandardError => error
  puts error.backtrace
  raise
end

path = model && model.active_path ? model.active_path : []
get_all_instances(selection, path, other = nil)

My SketchUp model:
global_coordinates_instances.skp (416.6 KB)

Video demonstrating the test case:

Note that in the first case (left of the video) the coordinates are correct, in the second case the coordinates are incorrect because the instances were grouped into a group with a different axis than the others.

Thanks in advance for any help.

You will need to use Sketchup::InstancePath.transformation() within the method to get the complete transformation, …

… or pass into the method the cumulative transformation for each level. See the Geom::Tranformation#* method.

1 Like

Thank you @DanRathbun , as always you are very quick and accurate.

I used the method of Class: Sketchup::InstancePath — SketchUp Ruby API Documentation and the code looked like this:

model = Sketchup.active_model
selection = model.selection

def get_all_instances(entities, path, other)
  entities.grep(Sketchup::ComponentInstance).concat(entities.grep(Sketchup::Group)).each do |dc|
    other = dc if dc.parent.is_a?(Sketchup::Model)
    instance_path = Sketchup::InstancePath.new(path + [dc])
    transformation = instance_path.transformation
    pt = transformation.origin
    puts "#{pt} - #{dc.definition.name}"

    get_all_instances(dc.definition.entities, path + [dc], other)
  end
rescue StandardError => error
  puts error.backtrace
  raise
end

path = model && model.active_path ? model.active_path : []
get_all_instances(selection, path, other = nil)

I wasn’t able to do it using the other method mentioned.
But this way it solved my problem. Thanks again…

1 Like