File size: 11,001 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# SPDX-License-Identifier: LGPL-2.1-or-later

# /***************************************************************************
# *   Copyright (c) 2018 Victor Titov (DeepSOIC) <vv.titov@gmail.com>       *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This library is free software; you can redistribute it and/or         *
# *   modify it under the terms of the GNU Library General Public           *
# *   License as published by the Free Software Foundation; either          *
# *   version 2 of the License, or (at your option) any later version.      *
# *                                                                         *
# *   This library  is distributed in the hope that it will be useful,      *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this library; see the file COPYING.LIB. If not,    *
# *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
# *   Suite 330, Boston, MA  02111-1307, USA                                *
# *                                                                         *
# ***************************************************************************/

# This is a temporary replacement for C++-powered Container class that should be eventually introduced into FreeCAD


class Container(object):
    """Container class: a unified interface for container objects, such as Group, Part, Body, or Document.
    This is a temporary implementation."""

    Object = None  # DocumentObject or Document, the actual container

    def __init__(self, obj):
        self.Object = obj

    def self_check(self):
        if self.Object is None:
            raise ValueError("Null!")
        if not isAContainer(self.Object, links_too=True):
            raise NotAContainerError(self.Object)

    def getAllChildren(self):
        """Returns all objects directly contained by the container. all = static + dynamic."""
        return self.getStaticChildren() + self.getDynamicChildren()

    def getStaticChildren(self):
        """Returns children tightly bound to the container, such as Origin. The key thing
        about them is that they are not supposed to be removed or added from/to the container."""

        self.self_check()
        container = self.Object
        if container.isDerivedFrom("App::Document"):
            return []
        elif container.hasExtension("App::OriginGroupExtension"):
            if container.Origin is not None:
                return [container.Origin]
            else:
                return []
        elif container.isDerivedFrom("App::Origin"):
            return container.OriginFeatures
        elif container.hasExtension("App::GroupExtension"):
            return []
        elif container.hasChildElement():  # Link
            return []
        raise RuntimeError("getStaticChildren: unexpected container type!")

    def getDynamicChildren(self):
        """Returns dynamic children, i.e. the stuff that can be removed from the container."""
        self.self_check()
        container = self.Object

        if container.isDerivedFrom("App::Document"):
            # find all objects not contained by any Part or Body
            result = set(container.Objects)
            for obj in container.Objects:
                if isAContainer(obj):
                    children = set(Container(obj).getAllChildren())
                    result = result - children
            return list(result)
        elif container.hasExtension("App::GroupExtension"):
            result = container.Group
            if container.hasExtension("App::GeoFeatureGroupExtension"):
                # geofeaturegroup's group contains all objects within the CS, we don't want that
                result = [obj for obj in result if obj.getParentGroup() is not container]
            return result
        elif container.isDerivedFrom("App::Origin"):
            return []
        elif container.hasChildElement():
            result = []
            for sub in container.getSubObjects(1):
                sobj = container.getSubObject(sub, retType=1)
                if sobj:
                    result.append(sobj)
            return result

        raise RuntimeError("getDynamicChildren: unexpected container type!")

    def isACS(self):
        """isACS(): returns true if the container forms internal coordinate system."""
        self.self_check()
        container = self.Object

        if container.isDerivedFrom("App::Document"):
            return True  # Document is a special thing... is it a CS or not is a matter of coding convenience.
        elif container.hasExtension("App::GeoFeatureGroupExtension"):
            return True
        elif container.hasChildElement():  # Link
            return True
        else:
            return False

    def isAVisGroup(self):
        """isAVisGroup(): returns True if the container consumes viewproviders of children, and thus affects their visibility."""
        self.self_check()
        container = self.Object

        if container.isDerivedFrom("App::Document"):
            return True  # Document is a special thing... Return value is a matter of coding convenience.
        elif container.hasExtension("App::GeoFeatureGroupExtension"):
            return True
        elif container.isDerivedFrom("App::Origin"):
            return True
        elif container.hasChildElement():  # Link
            return True
        else:
            return False

    def getCSChildren(self):
        if not self.isACS():
            raise TypeError("Container is not a coordinate system")
        container = self.Object
        return _getMetacontainerChildren(self, Container.isACS)

    def getVisGroupChildren(self):
        if not self.isAVisGroup():
            raise TypeError("Container is not a visibility group")
        container = self.Object
        return _getMetacontainerChildren(self, Container.isAVisGroup)

    def isChildVisible(self, obj):
        container = self.Object
        isElementVisible = getattr(container, "isElementVisible", None)
        if not isElementVisible:
            return obj.Visibility
        vis = isElementVisible(obj.Name)
        if vis < 0:
            return obj.Visibility
        return vis > 0

    def hasObject(self, obj):
        """Returns True if the container contains specified object directly."""
        return obj in self.getAllChildren()

    def hasObjectRecursive(self, obj):
        return self.Object in ContainerChain(obj)


def _getMetacontainerChildren(container, isrightcontainer_func):
    """Gathers up children of metacontainer - a container structure formed by containers of specific type.
    For example, coordinate systems form a kind of container structure.

    container: instance of Container class
    isrightcontainer_func: a function f(cnt)->bool, where cnt is a Container object."""

    result = []
    list_traversing_now = [container]  # list of Container instances
    list_to_be_traversed_next = []  # list of Container instances
    visited_containers = set([container.Object])  # set of DocumentObjects

    while len(list_traversing_now) > 0:
        list_to_be_traversed_next = []
        for itcnt in list_traversing_now:
            children = itcnt.getAllChildren()
            result.extend(children)
            for child in children:
                if isAContainer(child):
                    newcnt = Container(child)
                    if not isrightcontainer_func(newcnt):
                        list_to_be_traversed_next.append(newcnt)
        list_traversing_now = list_to_be_traversed_next

    return result


def isAContainer(obj, links_too=False):
    """isAContainer(obj, links_too): returns True if obj is an object container, such as
    Group, Part, Body. The important characteristic of an object being a
    container is that it can be activated to receive new objects. Documents
    are considered containers, too.
    If links_too, App::Link objects are considered containers, too. Then, container tree
    isn't necessarily a tree."""

    if obj.isDerivedFrom("App::Document"):
        return True
    if obj.hasExtension("App::GroupExtension"):
        return True
    if obj.isDerivedFrom("App::Origin"):
        return True
    if obj.hasChildElement():
        return True if links_too else False
    return False


# from Part-o-magic...
def ContainerOf(obj):
    """ContainerOf(obj): returns the container that immediately has obj."""
    cnt = None
    for dep in obj.InList:
        if isAContainer(dep):
            if Container(dep).hasObject(obj):
                if cnt is not None and dep is not cnt:
                    raise ContainerTreeError("Container tree is not a tree")
                cnt = dep
    if cnt is None:
        return obj.Document
    return cnt


def getVisGroupOf(obj):
    chain = VisGroupChain(obj)
    return chain[-1]


# from Part-o-magic... over-engineered, but proven to work
def ContainerChain(feat):
    """ContainerChain(feat): container path to feat (not including feat itself).
    Last container directly contains the feature.
    Example of output:  [<document>,<SuperPart>,<Part>,<Body>]"""

    if feat.isDerivedFrom("App::Document"):
        return []

    list_traversing_now = [feat]
    set_of_deps = set()
    list_of_deps = []

    while len(list_traversing_now) > 0:
        list_to_be_traversed_next = []
        for feat in list_traversing_now:
            for dep in feat.InList:
                if isAContainer(dep) and Container(dep).hasObject(feat):
                    if not (dep in set_of_deps):
                        set_of_deps.add(dep)
                        list_of_deps.append(dep)
                        list_to_be_traversed_next.append(dep)
        if len(list_to_be_traversed_next) > 1:
            raise ContainerTreeError("Container tree is not a tree")
        list_traversing_now = list_to_be_traversed_next

    return [feat.Document] + list_of_deps[::-1]


def CSChain(feat):
    cnt_chain = ContainerChain(feat)
    return [cnt for cnt in cnt_chain if Container(cnt).isACS()]


def VisGroupChain(feat):
    cnt_chain = ContainerChain(feat)
    return [cnt for cnt in cnt_chain if Container(cnt).isAVisGroup()]


class ContainerError(RuntimeError):
    pass


class NotAContainerError(ContainerError):
    def __init__(self, name="None"):
        ContainerError.__init__(self, "'{}' is not recognized as container".format(name))


class ContainerTreeError(ContainerError):
    pass