[SOLVED] Script for rotating a cube around a cylinder

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

[SOLVED] Script for rotating a cube around a cylinder

Post by DorinDXN »

Hi all,

I appreciate some direction to where to look at to manage to rotate the blue cube around the axis of a cylinder.

like in the image
blue_cube_red_cyl.png
blue_cube_red_cyl.png (18.61 KiB) Viewed 736 times
I have the cylinder's position, and rotation, in this case the rotation is
45° [0,1,0]

The cylinder I rotated with
FreeCAD.getDocument("Unnamed").getObject("Cylinder").Placement = App.Placement(App.Vector(0,0,0),App.Rotation(App.Vector(0,1,0),45))

Now the question is how I can rotate the cube around the cylinder's axis

I managed in the past to compute the quaternions, I guess, I'll be able to do again if needed, to pass them to some function.

thanks in advance,

cheers,
Dorin
Last edited by DorinDXN on Fri Dec 04, 2020 4:53 pm, edited 1 time in total.
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Script for rotating a cube around a cylinder

Post by edi »

Assuming the cylinder object is Cyl and the cube object is Cube the following code (not really tested) should rotate Cube arround the axis of Cyl by an angle Angle.

Code: Select all

CylAxisRot = Cyl.Placement.Rotation # rotation of cylinder
ZAxis = App.Vector(0,0,1) # the z-axis
CylAxisVec = CylAxisRot.multVec(ZAxis) # cylinder axis vector
MyRot = App.Rotation(CylAxisVec,Angle) # rotation around CylAxisVec by Angle in degree
NewPla = App.Placement() #create a new Placement
NewPla.Rotation = MyRot
Cube.Placement = NewPla
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: Script for rotating a cube around a cylinder

Post by DorinDXN »

Thanks a million!

Works perfectly :)

cheers,
Dorin
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: Script for rotating a cube around a cylinder

Post by DorinDXN »

It turns out it didn't work if the cube and the cylinder are not in origin 0,0,0

It rotates but not around cylinder, I guess the position reading may be needed :roll:
how_rotate_cube_around_cyl.FCStd
(4.92 KiB) Downloaded 27 times
it rotates this way
blue_cube_red_cyl_2.png
blue_cube_red_cyl_2.png (35.78 KiB) Viewed 703 times

Code: Select all

CylAxisRot = FreeCAD.getDocument("how_rotate_cube_around_cyld").getObject("Cylinder").Placement.Rotation # rotation of cylinder
ZAxis = App.Vector(0,0,1) # the z-axis
CylAxisVec = CylAxisRot.multVec(ZAxis) # cylinder axis vector
MyRot = App.Rotation(CylAxisVec,45) # rotation around CylAxisVec by Angle in degree
NewPla = App.Placement() #create a new Placement


NewPla = FreeCAD.getDocument("how_rotate_cube_around_cyl").Box.Placement # Added this but not help for any positions


NewPla.Rotation = MyRot
FreeCAD.getDocument("how_rotate_cube_around_cyl").Box.Placement = NewPla
added the single line above

Code: Select all

NewPla = FreeCAD.getDocument("Unnamed").Box.Placement 
it helped for some simple position but not for any

also tried

Code: Select all

CylAxisRot = FreeCAD.getDocument("how_rotate_cube_around_cyl").getObject("Cylinder").Placement.Rotation # rotation of cylinder
ZAxis = App.Vector(0.26,0.73,0.62) # the z-axis
CylAxisVec = CylAxisRot.multVec(ZAxis) # cylinder axis vector
MyRot = App.Rotation(CylAxisVec,45) # rotation around CylAxisVec by Angle in degree
NewPla = App.Placement() #create a new Placement
NewPla = FreeCAD.getDocument("how_rotate_cube_around_cyl").Box.Placement # Added this but seems to not help
NewPla.Rotation = MyRot
FreeCAD.getDocument("how_rotate_cube_around_cyl").Box.Placement = NewPla
appreciate any hint.
cheers,
Dorin
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Script for rotating a cube around a cylinder

Post by edi »

Hope the following code works (did not test)

Code: Select all

CylAxisRot = Cyl.Placement.Rotation # rotation of cylinder axis
ZAxis = App.Vector(0,0,1) # the z-axis
CylAxisVec = CylAxisRot.multVec(ZAxis) # cylinder axis vector
MyRot = App.Rotation(CylAxisVec,Angle) # rotation around CylAxisVec by Angle in degree
BoxBase = Box.Placement.Base # current Box base position
CylBase = Cyl.Placement.Base # current Cyl base position
DistVector = BoxBase.sub(CylBase) # distance vector between base positions
NewBoxBase = CylBase+MyRot.multVec(DistVector) # new Box base
NewPla = App.Placement(NewBoxBase,MyRot) #create a new Placement for Box
Box.Placement = NewPla
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: Script for rotating a cube around a cylinder

Post by DorinDXN »

Thanks again,

I think I'll manage to use,

It works in simple situation but when I apply the script to a different set of objects works once only,
e.g.
I rotate with 45 - it does fine
next rotation doesn't work regardless of the angle, (it rotates after something else)
but with the cube and cyl works, at least what I tried.

So far, the work around for complex objects is like this
I store the old placement in OldPla

then before each rotation I restore the old placement first, then rotate to new
this seems to work in any situations I tried.

so again, thanks a lot, I'll study why it works only once, if will not find why then restore the old before the new will be the way :)

cheers,
Dorin
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Script for rotating a cube around a cylinder

Post by edi »

Sorry for the problems, now the code is tested.

Code: Select all

import math
CylAxisRot = Cyl.Placement.Rotation # rotation of cylinder axis
ZAxis = App.Vector(0,0,1) # the z-axis
CylAxisVec = CylAxisRot.multVec(ZAxis) # cylinder axis vector
OldAngle=math.degrees(Box.Placement.Rotation.Angle) # old Box rotation angle
BaseRot = App.Rotation(CylAxisVec,Angle) # rotation of Base
BoxRot = App.Rotation(CylAxisVec,Angle+OldAngle) # rotation of Box, value of angle is absolute
BoxPos = Box.Placement.Base # current Box position
CylPos = Cyl.Placement.Base # current Cyl position
DistVector = BoxPos.sub(CylPos) # distance vector between positions
NewBoxPos = CylPos+BaseRot.multVec(DistVector) # new Box position
NewPla = App.Placement(NewBoxPos,BoxRot) #create a new Placement for Box
Box.Placement = NewPla
The issue was: Placement stores absolute values
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: Script for rotating a cube around a cylinder

Post by DorinDXN »

Thanks again,

No need to be sorry for :)

I managed to use first code, could be useful when I want to be sure no problems caused by rounding are due to added rotations.

Also tested and used the final version.
Here is a quick application on a complex object using the final version,
the axis of rotation of the propellers are a bit tilted 1.5.. 2 deg from Z axis,
nothing precise, the "cylinders" for rotations were added manually only to show that it works.

phpBB [video]


cheers,
Dorin
Post Reply