FreeCAD

Форк
0
/
expandplacements.py 
117 строк · 5.8 Кб
1
#***************************************************************************
2
#*   Copyright (c) 2012 Sebastian Hoogen <github@sebastianhoogen.de>       *
3
#*                                                                         *
4
#*   This program is free software; you can redistribute it and/or modify  *
5
#*   it under the terms of the GNU Lesser General Public License (LGPL)    *
6
#*   as published by the Free Software Foundation; either version 2 of     *
7
#*   the License, or (at your option) any later version.                   *
8
#*   for detail see the LICENCE text file.                                 *
9
#*                                                                         *
10
#*   This program is distributed in the hope that it will be useful,       *
11
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
#*   GNU Library General Public License for more details.                  *
14
#*                                                                         *
15
#*   You should have received a copy of the GNU Library General Public     *
16
#*   License along with this program; if not, write to the Free Software   *
17
#*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
18
#*   USA                                                                   *
19
#*                                                                         *
20
#***************************************************************************
21

22
__title__ = "FreeCAD OpenSCAD Workbench - expand placements and matrices functions"
23
__author__ = "Sebastian Hoogen"
24
__url__ = ["https://www.freecad.org"]
25

26
'''
27
This Script includes python functions to shift all placements down the
28
feature tree to the most basic objects
29
'''
30

31
import FreeCAD
32
from OpenSCADFeatures import *
33
from OpenSCADUtils import isspecialorthogonal
34
import replaceobj
35

36

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')
43

44

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): #this matrix is needed
52
            obj.Placement = FreeCAD.Placement()
53
            obj.Matrix = innermatrix
54
        else:  #the inner object is not a primitive
55
            expandplacementsmatrix(obj.Base, innermatrix)
56
            #remove the matrix object
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):
61
        #if isspecialorthogonalpython(fcsubmatrix(ownmatrix)):
62
        if isspecialorthogonal(ownmatrix):
63
            obj.Placement = FreeCAD.Placement()
64
            #this should never happen unless matrices cancel out
65
            obj.Placement = FreeCAD.Placement(ownmatrix)
66
        else:
67
            newobj = doc.addObject("Part::FeaturePython", 'exp_trans')
68
            MatrixTransform(newobj,ownmatrix,obj) #This object is not mutable GUI
69
            ViewProviderTree(newobj.ViewObject)
70
            for parent in obj.InList:
71
                replaceobj.replaceobj(parent, obj, newobj) # register the new object in the feature tree
72
            obj.Placement=FreeCAD.Placement()
73
    else: #not a primitive
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): #child of is like primtitive
80
                    outobj.Matrix = newmatrix
81
                    outobj.Base.Placement=FreeCAD.Placement()
82
                else: #remove the MatrixTransformation
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)
88
            else:
89
                expandplacementsmatrix(outobj, ownmatrix)
90
        obj.Placement = FreeCAD.Placement()
91

92

93
def expandplacements(obj,placement):
94
    ownplacement = placement.multiply(obj.Placement)
95
    if obj.isDerivedFrom('Part::FeaturePython') and isinstance(obj.Proxy, MatrixTransform):
96
        #expandplacementsmatrix(obj,ownplacement.toMatrix())
97
        expandplacementsmatrix(obj,placement.toMatrix())
98
    elif likeprimitive(obj, False):
99
        obj.Placement = ownplacement
100
    elif obj.isDerivedFrom('Part::Mirroring'):
101
        import OpenSCADUtils
102
        mm  = OpenSCADUtils.mirror2mat(obj.Normal,obj.Base)
103
        #TODO: set the base to 0,0,0
104
        innerp = FreeCAD.Placement(mm * ownplacement.toMatrix() *mm)
105
        expandplacements(obj.Source, innerp)
106
        obj.Placement = FreeCAD.Placement()
107
    else:
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)
113
                #obj.Base=ownplacement.Rotation.multVec(obj.Base)
114
            expandplacements(outobj, ownplacement)
115
        obj.Placement = FreeCAD.Placement()
116

117
#expandplacements(rootobj,FreeCAD.Placement())
118

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.