# How to change axes inside a component? (error in code fixed)

**URL:** <https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262>\
**Category:** Developers\
**Tags:** component, axes\
**Created:** [October 16, 2018, 9:39pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262 "2018-10-16T21:39:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [October 16, 2018, 9:39pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262/1 "2018-10-16T21:39:45Z")

</div>

Hi

EDIT: just realized some errors in the code, eg. that I forgot to instantiate the component 😬🙄  
Now the code snippets should work…

How to change axes inside a component?

Change the axes globally (working):

```ruby
  model = Sketchup.active_model 
  ents = model.active_entities
  defs = model.definitions		
  
  my_component = defs.add "my_component"
  t = Geom::Transformation.new [0,0,0]
		
  my_component.entities.add_face [[0,0,0], [100,0,0], [100,0,100], [0,0,100]]
  my_component_instance = ents.add_instance(my_component, t)

  xaxis = Geom::Vector3d.new(3, 5, 0)
  yaxis = xaxis * Z_AXIS
  model.axes.set([10,0,0], xaxis, yaxis, Z_AXIS) # change axes globally as expected  

```

Trying to change axes inside component (not working):

```ruby
  model = Sketchup.active_model 
  ents = model.active_entities
  defs = model.definitions		
  
  my_component = defs.add "my_component"
  t = Geom::Transformation.new [0,0,0]
		
  my_component.entities.add_face [[0,0,0], [100,0,0], [100,0,100], [0,0,100]]
  my_component_instance = ents.add_instance(my_component, t)

  xaxis = Geom::Vector3d.new(3, 5, 0)
  yaxis = xaxis * Z_AXIS
 
  my_component_instance.model.axes.set([10,0,0], xaxis, yaxis, Z_AXIS) # why is the axes set globally and not inside the component?

```

- God bless

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [October 18, 2018, 9:44pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262/2 "2018-10-18T21:44:14Z")

</div>

Although a component definition is quite similar to a SketchUp model of its own, `my_component_instance.model` refers to the one and single outer model. It is a reference to the model on which you change again the global axes.

When a user changes axes at the global level, the displayed axes will be changed (not the coordinate system).  
When a user changes axes inside a component or group, the component definition’s axes will be changed (the coordinate system).

If you want to change the coordinate system of a component definition, you have to apply the transformation to all component instances and the reverse transformation to all entities within the component definition to compensate for it again. The coordinate system and axes change, but the entities stay where they were.

```auto
transformation = Geom::Transformation.axes([10,0,0], xaxis, yaxis, Z_AXIS)
my_component_definition = my_component_instance.definition
# Transform all instances
my_component_definition.instances.each{ |instance|
  instance.transform!(transformation)
}
# Apply the inverse transformation to the contained entities
my_component_definition.entities.transform_entities(transformation.inverse, 
                                                    my_component_definition.entities.to_a)

```

---

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [October 20, 2018, 5:43pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262/3 "2018-10-20T17:43:47Z")

</div>

Thank you.

What I would like ruby to do is what this button does:

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

Axes: Move or reorient drawing axes.

I will try to see if I can use your way!

---

<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:** [October 20, 2018, 6:42pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262/4 "2018-10-20T18:42:48Z")

</div>

> [@rvs1977](#):
>
> What I would like ruby to do is what this button [_the AxisTool_] does:

Ruby code must **explicitly** honor the user’s custom axis using …

- [**`Sketchup::Axes#transformation()`**](http://ruby.sketchup.com/Sketchup/Axes.html#transformation-instance_method)

The native tools will **automatically** use the `Sketchup.active_model.axes.transformation`.

---

<div class="post-metadata">

**Author:** ![rvs1977](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rvs1977/32/15210_2.png) [@rvs1977](https://forums.sketchup.com/u/rvs1977)\
**Post date:** [October 24, 2018, 1:04pm UTC](https://forums.sketchup.com/t/how-to-change-axes-inside-a-component-error-in-code-fixed/79262/5 "2018-10-24T13:04:35Z")

</div>

Thank you.

Right now I try to take a different approach - more specific trying to catch up on “lines and planes in 3D” learned 24 years ago in high school.

# Calculus 3 Lecture 11.5: Lines and Planes in 3-D

[![](https://global.discourse-cdn.com/sketchup/original/3X/2/f/2f515f2e13e28f194d779fc3b3477cefb14cba1a.jpeg "Calculus 3 Lecture 11.5: Lines and Planes in 3-D") ](https://www.youtube.com/watch?v=IB1-lrPQjCw)
