Get Current Axes Position/Orientation
-
Hello everyone,
is it possible to get the model's axes/orientation through the Ruby API? I used the axes button (under the construction toolbar) and moved the axes so that:
- They are in the dead center of the model (vs the corner)
- Rotated 90 degrees clockwise
All I need to do is get this info into Ruby so I can push it out into some XML that I'm writing, is there any way to do this?
Thanks
Dan
-
Although you can temporarily reset axes manually - useful for manual-drafting - there is no direct access to the axes in the API.
The model's axes are fixed and everything is inserted around them.
In code you can't move the real model's axes but you can move objects...To put this another way... what you want to do is relocate all of the model.entities so they are located centered on the origin...
You know theSketchup.active_model.bounds.center
You know theORIGIN
You can get the vector between them...
vector=Sketchup.active_model.bounds.center.vector_to(ORIGIN)
From that you can make a translation transformation...
tr=Geom::Transformation.translation(vector)
You can now apply that to the model's entities...
Sketchup.active_model.entities.transform_entities(tr, Sketchup.active_model.entities.to_a)
All of the model should move to be centered... you might want to think about non-visible and/or locked objects - as without testing it I unsure if they'd move too...
Once you have everything relocated use a rotation transformation
tr=Geom::Transformation.rotation(ORIGIN, Z_AXIS, -90.degrees)
and
Sketchup.active_model.entities.transform_entities(tr, Sketchup.active_model.entities.to_a)
to do a cw rotation of the model [-ve=cw/+ve=ccw] - BUT if you mean 'relatively' rotate the axes 90 cw then you reverse the tr so its 90.degrees rather than -90...
If you do all of this after aSketchup.active_model.start_operation("XML")
, do the changes and the export and then finally callSketchup.active_model.abort_operation
ALL of the changes are then undone, BUT the exported XML code be unaffected as it's outside of the SKP, and it will reflect these temporary relationships of axes/geometry... -
I use this hack:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># SU7 only - incorrect in older versions.<br /># A hack to get the current model axis.<br />#<br /># @return [Boolean]<br /># @since 1.0.0<br /></span><span class="syntaxdefault">def get_model_axis<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Get Model Axis (Hack)'</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">entities </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br /> t </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">transformation<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">xaxis </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">t</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">xaxis<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">yaxis </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">t</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">yaxis<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">zaxis </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">t</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zaxis<br />ensure<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">abort_operation<br />end<br /></span>
NOTE: Since this creates temp geometry and temp operation it cannot be used at any time. You must be careful. It will interrupt any started operations - so use it right before your own .start_operations.
-
To recap.
The Model's axes are fixed; however, the axes inside a group/component edit context [active_entities versus entities] can differ.
So thomthom's 'hack' will return the axes of that context.
If you create a temporary empty group and get its transformation axes as tt showed I don't think you need to abort the operation because an empty group will not survive the rest of the code's processing anyway...
Alternatively you could make a reference to the temporary group [tgp=...] and then tgp.erase! it immediately after you have got tgp.transformation axes into the three @variables.
A converse issue when making a new group that you want to keep - you need to add entities t it quickly or else you'll find in no longer exists -
@tig said:
To recap.
The Model's axes are fixed;So the model's origin will always equal
ORIGIN
(unless some dumb coder changes this constant.)
So the model's transformation will always equal:
Geom::Transformation.new(ORIGIN)
and it's axes will always be the unit vectors:
xvec = Geom::Transformation.new(ORIGIN).xaxis() yvec = Geom::Transformation.new(ORIGIN).yaxis() zvec = Geom::Transformation.new(ORIGIN).zaxis()
Note: Changing the NorthAngle does not affect the x or y axis vector.
@tig said:
however, the axes inside a group/component edit context [active_entities versus entities] can differ.
So thomthom's 'hack' will return the axes of that context.But.. an edit context of a group or component, is not actually a UCS (user Co-ordinate System.) When you move the axis indicators with the AxisTool, you are setting a temporary UCS. The API does NOT expose this "Axes Object" either for setting it, saving it, or getting it.
This IS one of the Feature Requests that I remember filing (both for UI integration and API integration.)
What I had proposed (from memory,) was aGeom::Axes
(orGeom::AxesSet
,) object, that I think would be anArray
of 3Geom::Vector3d
objects (x, y and z.) For the UI, I requested that the user be able to save and restore named AxesSets (just as is possible in AutoCAD.)Unfortunately.. we cannot even "hack" the current AxisTool on Windows, because the tool does not use the VCB ("Measurements") box.
-
@tig said:
The Model's axes are fixed;
You can change the axis of the root context... I've always called that the model axis..
-
Just a note, that in playing around (I did not realize this before,) that custom "User" Axes can be saved within a ScenePage. (I and others, have been after the Developemnt Team to allow saving a UCS like we can in AutoCAD.)
Although this not "exactly" what we have been asking for... it can work for now.
1) Add a new ScenePage, .. name it what ever you want.
2) Use the AxisTool to set a custom axes.
3) Click the update button on the Scenes Inspector toolbar (or right-click the SceneTab > choose "Update", if you have Scenetabs on.)You can now switch back to this page anytime you wish to work with in this custom axes.
If you want the scenepage to be "Axis-Aligned",.. then with it the active page,...
1) Right click one of the axis, choose "Align View"
2) Choose on the menu: "Camera" > "Parallel Projection"
3) Update the scenepage.The custom axes scenepage will now always initially display in "aligned mode"... but can temporarily change it to Iso (click the "Iso" button,) or just use the Orbit / Zoom tools to change the camera location. Just do not update the page.
Anytime you wish to return the the "axis aligned" camera, just double-click the scenetab (if you have Scenetabs on.) or double-click the scene's title in the list within the Scenes inspector. -
-
-
@dan rathbun said:
Just a note, that in playing around (I did not realize this before,) that custom "User" Axes can be saved within a ScenePage.
Yea, I often set up Scenes that adjust just the axis. All though, with SU8 now automatically adjusting the model axis to match component axis there's less need of it for my use. Works well when you have a building with axis in various directions.
Advertisement