How to get the model angle property data in python script?

Need help, or want to share a macro? Post here!
User avatar
freecadlzh
Posts: 98
Joined: Fri Mar 06, 2020 12:52 pm

How to get the model angle property data in python script?

Postby freecadlzh » Thu Sep 24, 2020 12:11 pm

1.png
1.png (29.56 KiB) Viewed 236 times
As shown above, how to get the data of angle property by python script?

Need help, Thanks a lot.
User avatar
freecadlzh
Posts: 98
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get the model angle property data in python script?

Postby freecadlzh » Thu Sep 24, 2020 9:29 pm

I have got the way. Thanks a lot.
chrisb
Posts: 27930
Joined: Tue Mar 17, 2015 9:14 am

Re: How to get the model angle property data in python script?

Postby chrisb » Thu Sep 24, 2020 10:08 pm

freecadlzh wrote:
Thu Sep 24, 2020 9:29 pm
I have got the way. Thanks a lot.
Care to share? I was not successful to get the single value when I gave it a try.
User avatar
freecadlzh
Posts: 98
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get the model angle property data in python script?

Postby freecadlzh » Fri Sep 25, 2020 12:29 am

chrisb wrote:
Thu Sep 24, 2020 10:08 pm
freecadlzh wrote:
Thu Sep 24, 2020 9:29 pm
I have got the way. Thanks a lot.
Care to share? I was not successful to get the single value when I gave it a try.
图片5.png
图片5.png (69.18 KiB) Viewed 173 times
chrisb
Posts: 27930
Joined: Tue Mar 17, 2015 9:14 am

Re: How to get the model angle property data in python script?

Postby chrisb » Fri Sep 25, 2020 12:48 am

Thanks!
mario52
Posts: 3141
Joined: Wed May 16, 2012 2:13 pm

Re: How to get the model angle property data in python script?

Postby mario52 » Fri Sep 25, 2020 10:02 am

hi

see Code_snippets section "Search and data extraction"

for many examples code

here for angle rotation.Q and angle to toEuler

Code: Select all

# give the rotation.Q of the object selected (angle rotation in Radian) for convert: math.degrees(angleInRadian)
sel = FreeCADGui.Selection.getSelection()                                 # select object with getSelection()
Rot   = sel[0].Placement.Rotation.Q                                       # Placement Rotation Q
App.Console.PrintMessage("Rot           : "+str(Rot)+ "\n")
 
Rot_0 = sel[0].Placement.Rotation.Q[0]                                    # decode Placement Rotation Q
App.Console.PrintMessage("Rot_0         : "+str(Rot_0)+ " rad ,  "+str(180 * Rot_0 / 3.1416)+" deg "+"\n") # or math.degrees(angle)
 
Rot_1 = sel[0].Placement.Rotation.Q[1]                                    # decode Placement Rotation 1
App.Console.PrintMessage("Rot_1         : "+str(Rot_1)+ " rad ,  "+str(180 * Rot_1 / 3.1416)+" deg "+"\n") # or math.degrees(angle)
 
Rot_2 = sel[0].Placement.Rotation.Q[2]                                    # decode Placement Rotation 2
App.Console.PrintMessage("Rot_2         : "+str(Rot_2)+ " rad ,  "+str(180 * Rot_2 / 3.1416)+" deg "+"\n") # or math.degrees(angle)

Rot_3 = sel[0].Placement.Rotation.Q[3]                                    # decode Placement Rotation 3
App.Console.PrintMessage("Rot_3         : "+str(Rot_3)+"\n\n")

Rot_Axis = sel[0].Placement.Rotation.Axis                                 # Placement Rotation .Axis
App.Console.PrintMessage("Rot_Axis      : "+str(Rot_Axis)+ "\n")
 
Rot_Angle = sel[0].Placement.Rotation.Angle                               # Placement Rotation .Angle
App.Console.PrintMessage("Rot_Angle     : "+str(Rot_Angle)+ "\n\n")
##################################################################################

# 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, Dialog creation. My macros on Gist.github.