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?
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
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!!!
# 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:
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