| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| __title__ = "FreeCAD OpenSCAD Workbench - expand placements and matrices functions" |
| __author__ = "Sebastian Hoogen" |
| __url__ = ["https://www.freecad.org"] |
|
|
| ''' |
| This Script includes python functions to shift all placements down the |
| feature tree to the most basic objects |
| ''' |
|
|
| import FreeCAD |
| from OpenSCADFeatures import * |
| from OpenSCADUtils import isspecialorthogonal |
| import replaceobj |
|
|
|
|
| def likeprimitive(obj,extrusion=False): |
| '''we can't push the matrix transformation further down''' |
| return not obj.OutList or obj.isDerivedFrom('Part::Extrusion')\ |
| or extrusion and (obj.isDerivedFrom('Part::Revolution') \ |
| or obj.isDerivedFrom('Part::FeaturePython')) or \ |
| not obj.isDerivedFrom('Part::Feature') |
|
|
|
|
| def expandplacementsmatrix(obj,matrix): |
| '''expand afine transformation down the feature tree''' |
| ownmatrix = matrix.multiply(obj.Placement.toMatrix()) |
| if obj.isDerivedFrom('Part::Feature') and \ |
| isinstance(obj.Proxy, MatrixTransform): |
| innermatrix = ownmatrix.multiply(obj.Matrix) |
| if likeprimitive(obj.Base,True): |
| obj.Placement = FreeCAD.Placement() |
| obj.Matrix = innermatrix |
| else: |
| expandplacementsmatrix(obj.Base, innermatrix) |
| |
| for parent in obj.Base.InList: |
| replaceobj.replaceobj(parent, obj, obj.Base) |
| out.Document.removeObject(obj.Name) |
| elif likeprimitive(obj, True): |
| if isspecialorthogonal(ownmatrix): |
| obj.Placement = FreeCAD.Placement() |
| |
| obj.Placement = FreeCAD.Placement(ownmatrix) |
| else: |
| newobj = doc.addObject("Part::FeaturePython", 'exp_trans') |
| MatrixTransform(newobj,ownmatrix,obj) |
| ViewProviderTree(newobj.ViewObject) |
| for parent in obj.InList: |
| replaceobj.replaceobj(parent, obj, newobj) |
| obj.Placement=FreeCAD.Placement() |
| else: |
| for outobj in obj.OutList: |
| if outobj.isDerivedFrom('Part::Feature') and \ |
| isinstance(obj.Proxy,MatrixTransform): |
| newmatrix = ownmatrix.multiply(obj.Matrix).multiply(\ |
| outobj.Base.Placement.toMatrix()) |
| if likeprimitive(outobj.Base,True): |
| outobj.Matrix = newmatrix |
| outobj.Base.Placement=FreeCAD.Placement() |
| else: |
| plainobj = outobj.Base |
| for parent in outobj.InList: |
| replaceobj.replaceobj(parent, outobj, plainobj) |
| outobj.Document.removeObject(outobj.Name) |
| expandplacementsmatrix(outobj,newmatrix) |
| else: |
| expandplacementsmatrix(outobj, ownmatrix) |
| obj.Placement = FreeCAD.Placement() |
|
|
|
|
| def expandplacements(obj,placement): |
| ownplacement = placement.multiply(obj.Placement) |
| if obj.isDerivedFrom('Part::FeaturePython') and isinstance(obj.Proxy, MatrixTransform): |
| expandplacementsmatrix(obj,placement.toMatrix()) |
| elif likeprimitive(obj, False): |
| obj.Placement = ownplacement |
| elif obj.isDerivedFrom('Part::Mirroring'): |
| import OpenSCADUtils |
| mm = OpenSCADUtils.mirror2mat(obj.Normal,obj.Base) |
| |
| innerp = FreeCAD.Placement(mm * ownplacement.toMatrix() *mm) |
| expandplacements(obj.Source, innerp) |
| obj.Placement = FreeCAD.Placement() |
| else: |
| for outobj in obj.OutList: |
| if obj.isDerivedFrom('Part::Extrusion'): |
| obj.Dir = ownplacement.Rotation.multVec(obj.Dir) |
| elif obj.isDerivedFrom('Part::Revolution'): |
| obj.Axis = ownplacement.Rotation.multVec(obj.Axis) |
| expandplacements(outobj, ownplacement) |
| obj.Placement = FreeCAD.Placement() |
|
|
| |
|
|