Rotating a sketch around global axes

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ondulation
Posts: 1
Joined: Sat Apr 10, 2021 7:04 pm

Rotating a sketch around global axes

Post by ondulation »

Hi,

I need help with setting the orientation of a sketch based on spreadsheet input that defines the rotation in global coordinates.

Background: I have just built my first FreeCad model to visualize how the teeth of a saw are shaped depending on how the sharpening file is oriented.

Saw teeth are sharpened with a triangular files where the corners are cut-off. If the saw blade is in the xz-plane, the file position is traditionally defined by three angles: rotation around its own length (rake), around the global z-axis (fleam) and around the global x-axis (slope). For every other tooth, the position is mirrored in the xz-plane to give an overall symmetry to the teeth. See this page for a detailed explanation.

In my model, rotation around the file long axis (rake) is setup with an angle constraint. Then I've placed the file with Euler angles to control fleam and slope. It works as a proof of concept but is not really what I want as the rotation of the file sketch is done in local coordinates and not relative to the global coordinate system as desired.

From what I've read, it seems quite straight-forward to convert rotations in the global x- and z-plane to the local coordinate systems of the file using Placements. While I have a decent understanding of coordinate systems and quaternions I am totally lost on how to get started with this script.

What is the best way to rotate a sketch, based on angles of rotation around the global axes?
How can I trigger a script to update the model when new data is entered in the spreadsheet?

Many thanks!
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Rotating a sketch around global axes

Post by edwilliams16 »

You can construct rotations in a number of different ways:

Code: Select all

App.Rotation(0.)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: Rotation constructor accepts:
-- empty parameter list
-- Rotation object-- four floats (a quaternion)
-- three floats (yaw, pitch, roll)-- Vector (rotation axis) and float (rotation angle)
-- two Vectors (two axes)
-- Matrix object
-- 16 floats (4x4 matrix)
-- 9 floats (3x3 matrix)
-- 3 vectors + optional string
whichever is more convenient. A Placement is a Placement.Rotation followed by a translation by Placement.Base
Here I construct a Placement with a rotation of 30 degrees about the (1,1,1) axis, followed by a translation of 100 in z.

Code: Select all

>>> r =App.Rotation(App.Vector(1,1,1),30)
>>> d = App.Vector(0,0,100)
>>> p = App.Placement(d,r)
>>> p
Placement [Pos=(0,0,100), Yaw-Pitch-Roll=(20.1039,14.1237,20.1039)]
>>> p.Rotation.Axis
Vector (0.5773502691896258, 0.5773502691896258, 0.5773502691896258)
>>> p.Rotation.Angle
0.5235987755982988
The constructor normalizes the Axis to a unit vector.

What you need to do is then set the Placement of your sketch with one you calculate with something like

Code: Select all

App.ActiveDocument.Sketch.Placement = p
"Sketch" is the (internal) name of the sketch in question - might be Sketch001 for instance.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Rotating a sketch around global axes

Post by edwilliams16 »

An alternative to using a macro would be to link the placement property of the sketch to the spreadsheet using Expressions. https://wiki.freecadweb.org/Expressions
You'd have to translate to Angle/Axis representation in the spreadsheet by hand. It does support trig functions.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Rotating a sketch around global axes

Post by onekk »

Why using a spreadsheet?

python has a decent powerful mathematic engine to do much more than a spreadsheets, like nested if or cycles (for, do \ while, an so on).

A decent taylored script could do many more, than a Gui or a Macro, as the Macro is using an existent drawing and derive some measurements, that maybe you could have in the creation code of the "primitive" and reuse simply referring to a variable name.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Rotating a sketch around global axes

Post by mario52 »

hi

see Placement

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply