Get and move group axes

Dear Friends,


entity.model.axes.set(ORIGIN,X_AXIS,Y_AXIS,Z_AXIS)
point = entity.model.axes.origin

These codes can move and get model axes. Do we have similar codes for group axes? How can we get and move group axes only?

Thank you for your attention.

They cannot move the model axes.

Axes#set moves the drawing axes. It applies to tools, but does not move geometry.

The model origin and axes remain as they are.

This has been asked many times. Use the category search:
https://forums.sketchup.com/search?q=group%20axes%20category%3A13

You are right asked many times and I check all but cannot find my answer. In SU when we go to group edit move, we can move group axes in Tools>>Axes. So we cannot move group axes in Ruby Sketchup. Right?

No, you are wrong! :wink:
Yes. We can do!

Make sure, the component / group axes are shown:
image
Copy-paste the code below to Ruby Console and see what happening.
You have to CHECK the screen, the massage boxes as well as the Ruby console.
You maybe need to adjust modell zoom / camera to see what’s happening. In that case start over again…

def create_group_and_transform_study
  model = Sketchup.active_model
  enta = model.active_entities
  group1 = enta.add_group
  #Just to see the bundig box:
  Sketchup.active_model.selection.add group1
  
  UI.messagebox("Empty group1 created in model origin")
  
  # Create points in model cord. system
  p1m = Geom::Point3d.new(0, 1, 1)
  p2m = Geom::Point3d.new(1, 0, 1)
  p3m = Geom::Point3d.new(1, 2, 2)
  face1 = group1.entities.add_face( p1m, p2m, p3m )
  
  # Get the group1 current transformation
  tr_group1 = group1.transformation
  # and see the coordinates in group1 cord. system
  vert0_posm1 = face1.vertices[0].position
  puts "Vertices 0 group1 cord 1st: #{vert0_posm1.to_a}"
  puts "Face normal group1 cord 1st: #{face1.normal.to_a}"
  # and see the coordinates in model cord. system
  vert0_posg1 = face1.vertices[0].position.transform(tr_group1)
  puts "Vertices 0 model cord 1st: #{vert0_posg1.to_a}"
  puts "Face normal model cord 1st: #{face1.normal.transform(tr_group1).to_a}"
  puts
  UI.messagebox("Face created in group1.")
  
  # I want the group1 axes origin on one of the face vertices
  group_opoint = vert0_posm1
  # I want the group1 Z axis parallel to face1 normal
  group_z_axis = face1.normal
  # Creates a Transformation where origin is the new origin, and zaxis is the z axis.
  tr_orig_zax = Geom::Transformation.new(group_opoint, group_z_axis)
  
  # Transform the group1 INSTANCE with this transformation
  group1.transform!( tr_orig_zax )
  
  # Get the group1 current transformation 2nd stage
  tr_group2 = group1.transformation
  # and see the coordinates in group1 cord. system
  vert0_posm2 = face1.vertices[0].position
  puts "Vertices 0 group1 cord 2st: #{vert0_posm2.to_a}"
  puts "Face normal group1 cord 2st: #{face1.normal.to_a}"
  # and see the coordinates in model cord. system
  vert0_posg2 = face1.vertices[0].position.transform(tr_group2)
  puts "Vertices 0 model cord 2st: #{vert0_posg2.to_a}"
  puts "Face normal model cord 2st: #{face1.normal.transform(tr_group2).to_a}"
  puts
  UI.messagebox("group1 INSTANCE transformed by origin/axes")
  
  # Transform the group1 DEFINICION ENTITIES back with 
  # this transformation inverse
  myentities = group1.definition.entities
  myentities.transform_entities(tr_orig_zax.inverse, myentities.to_a)
  
  # Get the group1 current transformation 3rd stage
  tr_group3 = group1.transformation
  # and see the coordinates in group1 cord. system
  vert0_posm3 = face1.vertices[0].position
  puts "Vertices 0 group1 cord 3rd: #{vert0_posm3.to_a}"
  puts "Face normal group1 cord 3rd: #{face1.normal.to_a}"
  # and see the coordinates in model cord. system
  vert0_posg3 = face1.vertices[0].position.transform(tr_group3)
  puts "Vertices 0 model cord 3rd: #{vert0_posg3.to_a}"
  puts "Face normal model cord 3rd: #{face1.normal.transform(tr_group3).to_a}"
  puts
  UI.messagebox("group1 DEFINICION ENTITIES transformed 'back' by origin/axes")
  return "Do you see?"
end
create_group_and_transform_study

2 Likes

As I understand, to move group axes in ruby, first you move group to new point, you go to group edit mode and move all entities in group to original place. Very clever!!!

In this example, you made Z axes parallel to face1 normal. How can you make X or Y axes parallel to face1 normal?

Vector3d#axes-instance_method

 # I want the group1 X axis parallel to face1 normal
  group_x_axis = face1.normal.axes[0]
 # I want the group1 Y axis parallel to face1 normal
  group_y_axis = face1.normal.axes[1]

NOTICE THAT - If you read the link above - you will see the 3rd element of the .axes method will Returns the same as the original vector (the face1.normal)

 # I want the group1 Z axis parallel to face1 normal.
  group_z_axis = face1.normal.axes[2]

You may need to apply the following to the result vector (because of the right direction):
Vector3d.#reverse-instance_method for example to get opposite direction for y axis:

 group_y_axis = face1.normal.axes[1].reverse
2 Likes

Dear Friends,

Can you tell me what is problem of following codes. Sometimes face normal is parallel with axes sometimes not!!!

      def onLButtonDown(flags, x, y, view)
        @input.pick view, x, y
        get_pt = @input.position
        gface = @input.face
        grp = gface.parent.instances[0]
        grp_ents = grp.definition.entities
        vert = gface.vertices[0].position
        grp_ax = gface.normal.axes[1] # Vector3d
        tr_ax = Geom::Transformation.new(vert, grp_ax)
        grp_ents.transform_entities(tr_ax.inverse, grp_ents.to_a)       
      end

Thank you in advance.

Concretely?
Btw: You can not have all axes parallel to one vector.
axes: 2 or more axes
axis: 1 axis

Before click


if we choose X.AXIS and click on red side. Everything is good. Box move and rotate correctly.

Just change to Y.AXIS. Box move but no rotation.

My mistake or bug in software?

No bug. This is a behavior how the Vector3d#axes-instance_method creating the ‘x, y part’ of axes.

You can try to use Transformation#axes-class_method
to create the transformation what you whish.

1 Like

I wish to move group coordinate to group base on face that we selected (same as you do for triangle in this chat). Following codes can do it for me…

      def onLButtonDown(flags, x, y, view)
        @input.pick view, x, y
        get_pt = @input.position
        gface = @input.face
        grp = gface.parent.instances[0]
        tr = grp.transformation
        grp_ents = grp.definition.entities
        pt = gface.vertices[0].position
        grp_ax = gface.normal
        tr_pt = Geom::Transformation.new pt
        angle = grp_ax.angle_between [0, -1, 0]
        tr_rt = Geom::Transformation.rotation pt, [0, 0, 1], -angle
        grp_ents.transform_entities(tr_rt, grp_ents.to_a) 
        grp_ents.transform_entities(tr_pt.inverse, grp_ents.to_a)
        grp.transform! tr.inverse                
        tr_rt = Geom::Transformation.rotation [0, 0, 0], [0, 0, 1], angle  
        grp.transform! tr_rt
        grp.transform! tr
        grp.transform! tr_pt
      end

It works but need 6 steps to move and rotate coordinate. Can you show me a way that we do it better same as you did for triangle face? Thank you in advance.
Before


After
Screenshot 2020-09-28 193809

You can try with .axes transformation
(this apply to my original example)

tr_orig_zax = Geom::Transformation.new(group_opoint, group_z_axis)

  x_ax, y_ax, z_ax = group_z_axis.axes 
  tr_orig_zax = Geom::Transformation.axes(group_opoint, x_ax, y_ax, z_ax)
1 Like