22
__title__ = "FreeCAD OpenSCAD Workbench - expand placements and matrices functions"
23
__author__ = "Sebastian Hoogen"
24
__url__ = ["https://www.freecad.org"]
27
This Script includes python functions to shift all placements down the
28
feature tree to the most basic objects
32
from OpenSCADFeatures import *
33
from OpenSCADUtils import isspecialorthogonal
37
def likeprimitive(obj,extrusion=False):
38
'''we can't push the matrix transformation further down'''
39
return not obj.OutList or obj.isDerivedFrom('Part::Extrusion')\
40
or extrusion and (obj.isDerivedFrom('Part::Revolution') \
41
or obj.isDerivedFrom('Part::FeaturePython')) or \
42
not obj.isDerivedFrom('Part::Feature')
45
def expandplacementsmatrix(obj,matrix):
46
'''expand afine transformation down the feature tree'''
47
ownmatrix = matrix.multiply(obj.Placement.toMatrix())
48
if obj.isDerivedFrom('Part::Feature') and \
49
isinstance(obj.Proxy, MatrixTransform):
50
innermatrix = ownmatrix.multiply(obj.Matrix)
51
if likeprimitive(obj.Base,True):
52
obj.Placement = FreeCAD.Placement()
53
obj.Matrix = innermatrix
55
expandplacementsmatrix(obj.Base, innermatrix)
57
for parent in obj.Base.InList:
58
replaceobj.replaceobj(parent, obj, obj.Base)
59
out.Document.removeObject(obj.Name)
60
elif likeprimitive(obj, True):
62
if isspecialorthogonal(ownmatrix):
63
obj.Placement = FreeCAD.Placement()
65
obj.Placement = FreeCAD.Placement(ownmatrix)
67
newobj = doc.addObject("Part::FeaturePython", 'exp_trans')
68
MatrixTransform(newobj,ownmatrix,obj)
69
ViewProviderTree(newobj.ViewObject)
70
for parent in obj.InList:
71
replaceobj.replaceobj(parent, obj, newobj)
72
obj.Placement=FreeCAD.Placement()
74
for outobj in obj.OutList:
75
if outobj.isDerivedFrom('Part::Feature') and \
76
isinstance(obj.Proxy,MatrixTransform):
77
newmatrix = ownmatrix.multiply(obj.Matrix).multiply(\
78
outobj.Base.Placement.toMatrix())
79
if likeprimitive(outobj.Base,True):
80
outobj.Matrix = newmatrix
81
outobj.Base.Placement=FreeCAD.Placement()
83
plainobj = outobj.Base
84
for parent in outobj.InList:
85
replaceobj.replaceobj(parent, outobj, plainobj)
86
outobj.Document.removeObject(outobj.Name)
87
expandplacementsmatrix(outobj,newmatrix)
89
expandplacementsmatrix(outobj, ownmatrix)
90
obj.Placement = FreeCAD.Placement()
93
def expandplacements(obj,placement):
94
ownplacement = placement.multiply(obj.Placement)
95
if obj.isDerivedFrom('Part::FeaturePython') and isinstance(obj.Proxy, MatrixTransform):
97
expandplacementsmatrix(obj,placement.toMatrix())
98
elif likeprimitive(obj, False):
99
obj.Placement = ownplacement
100
elif obj.isDerivedFrom('Part::Mirroring'):
102
mm = OpenSCADUtils.mirror2mat(obj.Normal,obj.Base)
104
innerp = FreeCAD.Placement(mm * ownplacement.toMatrix() *mm)
105
expandplacements(obj.Source, innerp)
106
obj.Placement = FreeCAD.Placement()
108
for outobj in obj.OutList:
109
if obj.isDerivedFrom('Part::Extrusion'):
110
obj.Dir = ownplacement.Rotation.multVec(obj.Dir)
111
elif obj.isDerivedFrom('Part::Revolution'):
112
obj.Axis = ownplacement.Rotation.multVec(obj.Axis)
114
expandplacements(outobj, ownplacement)
115
obj.Placement = FreeCAD.Placement()