Hello everyone,
When we use the Axes tool in SketchUp, we can rotate and move the axes inside a group — the axes change, but the group’s entities stay in place.
How can we achieve the same effect through code?
I have the axes (transformation) of an old group, and I want to apply them to a newly created group that currently has its own axes — but I don’t want to move or reposition the entities inside it.
You need to apply the old group transformation onto the new group instance - considering the new group instance own transformation ( the add_group method is creating it with IDENTITY )
Then you need to apply the old group transformation inverse onto the new group (definition) entities.
If you have such a simple model, where the new group you created have its origin in an ORIGIN, but geometries in a “same location” than the old group geometries
def transform_obj_axes(obj, tr_ax)
obj.definition.entities.transform_entities( tr_ax.inverse, obj.definition.entities.to_a )
obj.transformation = obj.transformation * tr_ax
end
model = Sketchup.active_model
entities = model.active_entities
old_group = entities.find{|e| e.is_a?(Sketchup::Group) && e.name == "old_group"}
new_group = entities.find{|e| e.is_a?(Sketchup::Group) && e.name == "new_group"}
tr_o = old_group.transformation
#this may not needed,
# you can use 'tr_o' directly as parameter for transform_obj_axes:
tr_ax = Geom::Transformation.axes(tr_o.origin, tr_o.xaxis, tr_o.yaxis, tr_o.zaxis)
model.start_operation("tr obj axes", true)
#just to make sure you will do as the UI does:
new_group.make_unique
transform_obj_axes(new_group, tr_ax)
model.commit_operation
Unfortunately, I don’t understand anything about what the pictures mean. Which one is the old group, which one is the new group? What does it mean and where does grp_info[:transformation] come from? How did you calculate it?
What are those guide-lines and the window-like thing in the third picture?
I have no idea what your model structure (nested level) is and what else in it… Etc, Etc…
Without a clear description, I’m unable to figure out what you have in mind and what you really want to achieve.
My example handles the simple case when both groups are at the root of the model. If the groups are in different editing contexts, then the combined transformations must be taken into account. If the model does not use the world axis, then that must also be taken into account.
When you quote a comment from me, please don’t put a sentence in it that I didn’t write!
Because the Axes tool changes the Drawing Axes only, not the model’s axes nor the axes of the group’s or component’s entities collection. (This is like the UCS in AutoCAD.)
(1) You adjust the drawing axes via the singleton Sketchup::Axes class and it’s #set method.
(You should probably first save the current drawing axes and restore it afterward.)
(2) The native tools honor the drawing axes, but Ruby tools must explicitly use the drawing axes properties if they are to honor them.
(3) Be careful. When the edit context is not at the model’s top level, but within a group or component, the API methods expect and return world coordinates.