How to read object placement angle

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

How to read object placement angle

Post by freedman »

My goal is to rotate an object a certain amount but first I need to read it's current angle so I can add to it. I have done this:

Code: Select all

Ang = App.ActiveDocument.Body001.Placement
print(Ang)
and get a print out:

Code: Select all

Placement[Pos=(0,0,0), Yaw-Pitch-Roll=(14,0,0)]
The angle is 14 degrees but how do access that variable (Yaw).
It comes down to how do I read this, the rot.angle:
"doc.getObject(self.partName).Placement = App.Placement(App.Vector(self.obj_x,self.obj_y,0.0),App.Rotation(App.Vector(0,0,0), self.rot_angle))
Thanks
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to read object placement angle

Post by openBrain »

Code: Select all

App.ActiveDocument.Body001.Placement.Rotation.Angle
for Angle/Axis representation

If you want eg. Euler's yaw :

Code: Select all

App.ActiveDocument.Body001.Placement.Rotation.toEuler()[0]
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to read object placement angle

Post by freedman »

openBrain, I had the "Rotation.Angle" printed and got .244346095279, the Euler is what I needed, it prints out at 14.0. Thank you.

I was trying to get that info from this long thread about angles but that's a tough read at midnight.
https://forum.freecadweb.org/viewtopic. ... read+angle
Thanks again.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: How to read object placement angle

Post by vocx »

freedman wrote: Tue Jan 21, 2020 8:31 am

Code: Select all

Placement[Pos=(0,0,0), Yaw-Pitch-Roll=(14,0,0)]
Placement. It's a bit old, but it should be helpful.

A Placement is composed of a Base, and a Rotation.

Code: Select all

Placement = App.Placement(Base, Rotation)
The Base is a simple point (Vector) indicating where the object is.

Code: Select all

Base = App.Vector(x, y, z)
The Rotation, is composed of an Axis (Vector) of rotation, and an Angle.

Code: Select all

Rotation = App.Rotation(App.Vector(x, y, z), Angle)
Once you have the Placement you can extract the components from the Python console.

Code: Select all

Placement.Base
Placement.Rotation
Placement.Rotation.Axis
Placement.Rotation.Angle
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to read object placement angle

Post by freedman »

Thanks all, I think I understand. Good explaination vocx.
Placement. It's a bit old, but it should be helpful.

I looked at that a bunch and there are plenty of examples of how to write to placement but just a couple lines showing how to read the variables. I don't remember seeing the Eular stuff in there at all.
mario52
Veteran
Posts: 4696
Joined: Wed May 16, 2012 2:13 pm

Re: How to read object placement angle

Post by mario52 »

hi

see here Code_snippets#Search_and_data_extraction

Code: Select all

##################################################################################

# give the rotation of the object selected toEuler (angle rotation in degrees)
sel = FreeCADGui.Selection.getSelection()                             # select object with getSelection()
angle   = sel[0].Shape.Placement.Rotation.toEuler()                   # angle Euler
App.Console.PrintMessage("Angle          : "+str(angle)+"\n")
Yaw   = sel[0].Shape.Placement.Rotation.toEuler()[0]                  # decode angle Euler Yaw (Z) lacet (alpha)
App.Console.PrintMessage("Yaw            : "+str(Yaw)+"\n")
Pitch = sel[0].Shape.Placement.Rotation.toEuler()[1]                  # decode angle Euler Pitch (Y) tangage (beta)
App.Console.PrintMessage("Pitch          : "+str(Pitch)+"\n")
Roll  = sel[0].Shape.Placement.Rotation.toEuler()[2]                  # decode angle Euler Roll (X) roulis (gamma)
App.Console.PrintMessage("Roll           : "+str(Roll)+"\n\n")
##################################################################################
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.
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to read object placement angle

Post by freedman »

Thanks Mario. It would be great if someone could add to the "Code snippets" how to read/write/search for linked objects and their params.
mario52
Veteran
Posts: 4696
Joined: Wed May 16, 2012 2:13 pm

Re: How to read object placement angle

Post by mario52 »

hi

give an example file to see ...

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.
freedman
Veteran
Posts: 3472
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to read object placement angle

Post by freedman »

This is a Part design file. The first Part is the original, the next two are links. Using 19.187
Attachments
linked poles.FCStd
(16.8 KiB) Downloaded 8 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to read object placement angle

Post by openBrain »

freedman wrote: Wed Jan 22, 2020 11:50 pm This is a Part design file. The first Part is the original, the next two are links. Using 19.187
Dont' know exactly what you want but Links are listed in the InList property of an object :

Code: Select all

App.ActiveDocument.Part.InList
I don't know dedicated method to get only the list of Links pointing to an object but maybe there is one.
However there is one in the other direction :

Code: Select all

App.ActiveDocument.Link.getLinkedObject()
Post Reply