https://forum.freecadweb.org/viewtopic.php?f=22&t=72362
and Thanks to @mario52 invaluable help:
https://forum.freecadweb.org/viewtopic. ... 02#p631002
I have elaborated some more the topic:
Code: Select all
import FreeCAD
from FreeCAD import Placement, Rotation, Vector
import Part
ROT0 = Rotation(0,0,0)
VEC0 = Vector(0,0,0)
DOC = FreeCAD.activeDocument
box = Part.makeBox(10,15,20)
box.Placement = Placement(Vector(10,10,10), Rotation(0, 10 ,10))
box_do = Part.show(box, "or_box")
# copy the Box.shape
box1_sh = box_do.Shape
# retain the original box placement
pl1 = box.Placement
# choose an edge to rotate from
# I know is a cheat, but...
edge = box1_sh.Edges[3]
# obtain starting and ending point of the edge
# it should work even for a cruved edge
p1 = edge.valueAt(0)
p2 = edge.valueAt(1.0)
print(f"p1: {p1}, p2: {p2}")
axis = p2.sub(p1)
angle = 60
rot_p = Placement(VEC0, Rotation(axis, angle), p1)
print(f"axis: {axis}, angle: {angle}")
box_rot = Part.show(box1_sh, "rot_box")
# multiply the two placement
box_rot.Placement = rot_p.multiply(pl1)
Part.show(edge, "line")
Code should work even on "arbitrary shapes" other than a cube if you find a suitable way to obtain an edge and calculate a start and ending point. (It will not work as example for a cilynder, or a spehre, and probably not even for cones, torus and so on.)
Some caveats:
- I have used a copy of the object, obtained using .Shape that will make a copy of the Shape alone
Code: Select all
box1_sh = box_do.Shape
- I have "retained" the placement as it is not copied
Code: Select all
# retain the original box placement pl1 = box.Placement
- To make the correct placement you have to multiply them but remember that the "order" is important
Code: Select all
# multiply the two placement box_rot.Placement = rot_p.multiply(pl1)
- Rotation is done using the form (axis, angle) and specifing a center as third paramter of Placement.
Code: Select all
rot_p = Placement(VEC0, Rotation(axis, angle), p1)
- Better to not make too much work on placement, and if you need further moves, use:
Remember that order matters.Code: Select all
pl2.multiply(pl1)
This is one of the most problematic things when dealing with FreeCAD, as there is behind some complicated "Vector Math", that is not easy to sort out if you are not a "vector math guy". (I'm "math impaired").
So take not of it and "keep under your cushion".
Hope it helps
Carlo D.