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

Hi

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

How to change axes inside a component?

Change the axes globally (working):

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

  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

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.

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)

1 Like

Thank you.

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

image

Axes: Move or reorient drawing axes.

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

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

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

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

1 Like