# \[Partially Solved\] Rotation transformation not working for me - why not?

**URL:** <https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646>\
**Category:** Ruby API\
**Created:** [May 27, 2020, 3:21pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646 "2020-05-27T15:21:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 27, 2020, 3:21pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/1 "2020-05-27T15:21:44Z")

</div>

In an extension I’m working on to array numbers and optionally rotate them individually or as a collection in a group, my first rotation works, but the second almost identical one doesn’t, and I can’t see why not.

```ruby
        # Rotate the text just drawn to the selected angle
          # Calculate text centre point
          cp = Geom::Point3d.new(textx_position, 0, 0)
  # ents.add_cpoint cp # Use for testing to see component origin point position
          text_rotate = Geom::Transformation.rotation cp, Z_AXIS, rotate_angle.degrees
          my_flat_text_group.transform! text_rotate

```

That does what I intend: for example, with the `rotate_angle` set to 45°:  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/6/c/6cb0916730d7ae9dba8b008d04afc0e9e55acb2d.png)

A few lines later, when I have assembled a `text_group` containing several individual items of text (as shown in the image above), I can’t get this group to rotate as a whole.

```ruby
 # Rotate the array to the selected slope_angle
    array_rotate = Geom::Transformation.rotation ORIGIN, Z_AXIS, slope_angle.degrees
    text_group.transform! array_rotate

    name = text_component.definition.name = text_name
    text_component = text_group.to_component
    model.place_component text_component.definition

```

In the Ruby console I checked that all the values in the second `Geom::Transformation` exist

```ruby
slope_angle = 30.0
true
> Z_AXIS
(0, 0, 1)
> ORIGIN
(0", 0", 0")
> 

```

The format of the two transformations is identical. I know that `text_group` exists because in the next few lines of code I convert it to a component and place it in the model. And THAT part works. It just won’t rotate first!

It’s probably something dumb that I’m not seeing, but I’m pretty sure it isn’t a typo, and the two lines with `.transform!` in them appear identical in format.

But one works, and the other doesn’t, and at the moment I don’t understand why not.

In the meantime I’ll get on with working on other parts of the code, but I’d really like to understand what’s wrong with this bit. No errors are shown in the Ruby console.

Suggestions welcome!

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [May 27, 2020, 3:48pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/2 "2020-05-27T15:48:18Z")

</div>

The transformation is a property of an instance (a group is a special sort of instance), not of the definition. When you make the group into a component, the transformation still applies to that instance, but not to the definition, and then when you place a new instance of the definition it does not “inherit” the transformation from the previous instance.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 27, 2020, 3:52pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/3 "2020-05-27T15:52:10Z")

</div>

Makes sense, now. How do I get it to apply, then, to at least this instance of the component?

Or better, to the definition?

I tried

```ruby
text_component.definition.transform! array_rotate

```

But apparently I can’t do that.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 27, 2020, 5:13pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/4 "2020-05-27T17:13:19Z")

</div>

Well, partially solved. I can place a line, at an angle, as a component using this code:

```ruby
model = Sketchup::active_model
entities = model.active_entities
text_group = entities.add_group
ents = text_group.entities
line = ents.add_edges [0,0,0], [10,0,0]
array_rotate = Geom::Transformation.rotation ORIGIN, Z_AXIS, 30.degrees
ents.transform_entities( array_rotate, text_group )
text_component = text_group.to_component
model.place_component text_component.definition

```

And I can get my `text_group` of numbers created at the selected slope angle as a component at the origin, using this:

```ruby
    array_rotate = Geom::Transformation.rotation ORIGIN, Z_AXIS, slope_angle.degrees
    ents.transform_entities( array_rotate, text_group )
    text_component = text_group.to_component
    text_component.definition.name = text_name

```

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/5/0/5088d00ddd0a4328e2ba6c3bd07861e5e939c4ad.png)

What I **can’t** yet figure out how to do is to make the rotated entity the component definition, not the instance, nor how to enable `place_component` to place a rotated instance of it.

**What am I missing?**

* * *

And I’ve noticed a possibly unrelated oddity in SU 2020.1 on Mac Mojave.

When I drag the component out of the Component Browser, it comes into the model a long way from the cursor

 ![place_component from Component browser](https://global.discourse-cdn.com/sketchup/original/3X/f/8/f8204ec0fd310fa18db8713205020ee560af253d.gif)

Anyone know why this is happening?

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 27, 2020, 11:12pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/5 "2020-05-27T23:12:39Z")

</div>

Well, I’m one step further now. After a second search of the forum, I found this topic and this post in it:  
[Transform geometry within comonent or sub-components - #6 by TIG](https://forums.sketchup.com/t/transform-geometry-within-comonent-or-sub-components/37927/6).

That gave me enough clues to simplify it for one component definition only to one line:

```ruby
text_component.definition.entities.transform_entities(array_rotate, 
   text_component.definition.entities.to_a)

```

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/0/0/00546119a145924ab6575b3b982d40dcf46bcefb.png)

This is the placed component, with the internal entries rotated to 45° as asked for with the `slope_angle` set to 45°.

The elements of the component definition have been rotated inside the component definition, but the bounding box no longer fits the entities closely.

Next step - how to redefine the bounding box in code so it looks like this (image created after using the user interface and Change Axes for the component).

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/6/b/6b4e5dca72aaec30add2bb2feaf0e302ea4698e9.png)

Will try a little longer tonight, but if it’s difficult, may have to wait until tomorrow.

[LATER]  
If I have both a `slope_angle` and an opposite text `rotation_angle` set, I get an odd bounding box, which doesn’t even contain the text elements:

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/e/4/e4d445d7657c95d113cc4441b5b3a9fe6c6ec21e.png)

And I’ve searched the forum for how to change a component’s axes using Ruby, and can’t find anything helpful yet.

**Any suggestions, please?**

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [May 28, 2020, 10:02am UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/6 "2020-05-28T10:02:51Z")

</div>

Do you want to apply the rotation in relation to the local axes rather than the parent axes?

Cross multiplication isn’t commutative, meaning the order of the operands matter. You can think of the order of the transformations as the order the action os carried out, move or rotation first.

```ruby
transformation = Geom::Transformation.new(ORIGIN, Z_AXIS, 20.degrees)

# Rotate around local origin.
Sketchup.active_model.selection.each do |entity|
  entity.transformation = entity.transformation * transformation
end

# Rotate around parent origin.
Sketchup.active_model.selection.each do |entity|
  entity.transformation = transformation * entity.transformation
end

```

Also, a transformation doesn’t necessarily represent a change in position but can also be a static position. The transformation of a group or component corresponds to how it would have to be moved from being lined up with the parent axes into having its current placement.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 28, 2020, 10:35am UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/7 "2020-05-28T10:35:07Z")

</div>

Thanks for your response, Julia Christina.

I’ve worked out how to get the number elements in the array in the right place in the model. But the `text_component` axes are still aligned with the model axes, so the bounding box is very much bigger than it needs to be.

I’d like to change the`text_component` axes so its x-axis points along the length of the array, at `slope_angle` relative to the model x-axis.

That’s the bit I can’t work out how to do.

I’ve searched both this forum, and in the SU Ruby API docs, for various combinations of _component_, _component definition_, _axis_, _axes_, _change axes_ and a few others, but can’t find an example.

I’m not sure how I would use your examples to do this, though they will be helpful to my understanding of SU Ruby, which is slowly growing.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [May 28, 2020, 11:03am UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/8 "2020-05-28T11:03:23Z")

</div>

This method moves the axes of a definition.

```ruby
def self.place_axes(definition, new_axes, adjust_instances = true)
    definition.entities.transform_entities(
      new_axes.inverse,
      definition.entities.to_a
    )

    if adjust_instances
      definition.instances.each do |instance|
        instance.transformation *= new_axes
      end
    end

    nil
  end

```

You can use e.g. the bounds min point as origin to copy SketchUp’s behavior for groupign/component creation with the origin in the bottom front left corner.

`transformation = Geom::Transformation.new(definition.bounds.min)`

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 28, 2020, 11:08am UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/9 "2020-05-28T11:08:59Z")

</div>

That looks to be just what I need. I’d never have worked that out on my own!

I’m fine with the component origin where it is - at the model origin before `.place_component` s used, but good to know how to change it.

Many thanks.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [May 28, 2020, 1:06pm UTC](https://forums.sketchup.com/t/partially-solved-rotation-transformation-not-working-for-me-why-not/126646/10 "2020-05-28T13:06:17Z")

</div>

Hmmm. I think I’m not understanding how to set the `new_axes` in @eneroth3’s method.

At first, I though just an array would do the trick:

```ruby
new_axes = [Math.cos(slope_angle), Math.sin(slope_angle), 0]

```

The Ruby console gives a ‘No method’ error for `new_axes.inverse`.

But an array doesn’t have an `#inverse` method, only a `Geom::Transformation` does, I find from consulting the SU Ruby API docs.

Then I thought, well I already have the transformation I want to invert - `array_rotate` - which rotated the elements of `text_component_definition` to the `slope_angle` where I wanted them to go.

So I plugged in `array_rotate` to the place\_axes method call.

```ruby
place_axes(text_component.definition, array_rotate, adjust_instances = true)

```

No error, but that doesn’t work to change the axes, and seems to cancel out the rotation of the definition’s elements, producing this:  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/1/4/14ca2c369af78aee028c8291500b93c67f067b70.png)

… instead of this without the `place_axes` line and `slope_angle` = 30°

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/4/0/4064cfe86c8467538c8b454b46b01f422c559b82.png)

And again, the bounding box doesn’t include the text elements inside it, except for the starting zero.

So sorry, I’m not quite there yet.

**Further help would be welcome.**
