[SOLVED] Script for rotating a cube around a cylinder
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[SOLVED] Script for rotating a cube around a cylinder
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
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
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
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.
Re: Script for rotating a cube around a cylinder
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
Re: Script for rotating a cube around a cylinder
Thanks a million!
Works perfectly
cheers,
Dorin
Works perfectly
cheers,
Dorin
Re: Script for rotating a cube around a cylinder
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
it rotates this way
added the single line above
it helped for some simple position but not for any
also tried
appreciate any hint.
cheers,
Dorin
It rotates but not around cylinder, I guess the position reading may be needed
it rotates this way
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
Code: Select all
NewPla = FreeCAD.getDocument("Unnamed").Box.Placement
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
cheers,
Dorin
Re: Script for rotating a cube around a cylinder
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
Re: Script for rotating a cube around a cylinder
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
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
Re: Script for rotating a cube around a cylinder
Sorry for the problems, now the code is tested.
The issue was: Placement stores absolute values
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
Re: Script for rotating a cube around a cylinder
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.
cheers,
Dorin
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.
cheers,
Dorin