# Global coordinates of instances with swapped axes

**URL:** <https://forums.sketchup.com/t/global-coordinates-of-instances-with-swapped-axes/248275>\
**Category:** Ruby API\
**Tags:** components, ruby-api\
**Created:** [November 15, 2023, 2:06am UTC](https://forums.sketchup.com/t/global-coordinates-of-instances-with-swapped-axes/248275 "2023-11-15T02:06:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [November 15, 2023, 2:06am UTC](https://forums.sketchup.com/t/global-coordinates-of-instances-with-swapped-axes/248275/1 "2023-11-15T02:06:00Z")

</div>

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:**

> [@How to get components global coordination](https://forums.sketchup.com/t/how-to-get-components-global-coordination/46983/14):
>
> thank you

> [@Curious issues with relative/global vertex positions](https://forums.sketchup.com/t/curious-issues-with-relative-global-vertex-positions/158194):
>
> I have found, like many others, that when a group is open, an edge/face’s vertex positions will show their position in global terms, whereas when a group is closed, that same edge/face will show the local position. My question is, what if you’d like to apply a transformation inside the group along those local axes? If I have a set of vertices and then close the group and apply a translation, it occurs along the local axes, but what if I want to apply the translation with the group open? e.g., …

**My code example:**

```auto
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](https://forums.sketchup.com/uploads/short-url/a2A8TaTXbh3la4n2SK3yERv4vvX.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.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [November 15, 2023, 2:16am UTC](https://forums.sketchup.com/t/global-coordinates-of-instances-with-swapped-axes/248275/2 "2023-11-15T02:16:26Z")

</div>

You will need to use [`Sketchup::InstancePath.transformation()`](https://ruby.sketchup.com/Sketchup/InstancePath.html#transformation-instance_method) within the method to get the complete transformation, …

… or pass into the method the cumulative transformation for each level. See the [`Geom::Tranformation#*`](https://ruby.sketchup.com/Geom/Transformation.html#*-instance_method) method.

---

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [November 15, 2023, 2:47am UTC](https://forums.sketchup.com/t/global-coordinates-of-instances-with-swapped-axes/248275/3 "2023-11-15T02:47:07Z")

</div>

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

I used the method of [Class: Sketchup::InstancePath — SketchUp Ruby API Documentation](https://ruby.sketchup.com/Sketchup/InstancePath.html#transformation-instance_method) and the code looked like this:

```auto
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…
