File size: 5,219 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
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net>     *
# *                                                                         *
# *   This file is part of FreeCAD.                                         *
# *                                                                         *
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
# *   under the terms of the GNU Lesser General Public License as           *
# *   published by the Free Software Foundation, either version 2.1 of the  *
# *   License, or (at your option) any later version.                       *
# *                                                                         *
# *   FreeCAD 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      *
# *   Lesser General Public License for more details.                       *
# *                                                                         *
# *   You should have received a copy of the GNU Lesser General Public      *
# *   License along with FreeCAD. If not, see                               *
# *   <https://www.gnu.org/licenses/>.                                      *
# *                                                                         *
# ***************************************************************************

__title__ = "BOPTools.BOPFeatures module"
__author__ = "Werner Mayer"
__url__ = "https://www.freecad.org"
__doc__ = "Helper class to create the features for Boolean operations."

import FreeCAD
import Part


class BOPFeatures:
    def __init__(self, doc):
        self.doc = doc

    def make_section(self, inputNames):
        obj = self.doc.addObject("Part::Section", "Section")
        obj.Base = self.doc.getObject(inputNames[0])
        obj.Tool = self.doc.getObject(inputNames[1])
        self.copy_visual_attributes(obj, obj.Base)
        target = self.move_input_objects([obj.Base, obj.Tool])
        if target:
            target.addObject(obj)
        return obj

    def make_cut(self, inputNames):
        obj = self.doc.addObject("Part::Cut", "Cut")
        obj.Base = self.doc.getObject(inputNames[0])
        obj.Tool = self.doc.getObject(inputNames[1])
        self.copy_visual_attributes(obj, obj.Base)
        target = self.move_input_objects([obj.Base, obj.Tool])
        if target:
            target.addObject(obj)
        return obj

    def make_common(self, inputNames):
        obj = self.doc.addObject("Part::Common", "Common")
        obj.Base = self.doc.getObject(inputNames[0])
        obj.Tool = self.doc.getObject(inputNames[1])
        self.copy_visual_attributes(obj, obj.Base)
        target = self.move_input_objects([obj.Base, obj.Tool])
        if target:
            target.addObject(obj)
        return obj

    def make_multi_common(self, inputNames):
        obj = self.doc.addObject("Part::MultiCommon", "Common")
        obj.Shapes = [self.doc.getObject(name) for name in inputNames]
        self.copy_visual_attributes(obj, obj.Shapes[0])
        target = self.move_input_objects(obj.Shapes)
        if target:
            target.addObject(obj)
        return obj

    def make_fuse(self, inputNames):
        obj = self.doc.addObject("Part::Fuse", "Fusion")
        obj.Base = self.doc.getObject(inputNames[0])
        obj.Tool = self.doc.getObject(inputNames[1])
        self.copy_visual_attributes(obj, obj.Base)
        target = self.move_input_objects([obj.Base, obj.Tool])
        if target:
            target.addObject(obj)
        return obj

    def make_multi_fuse(self, inputNames):
        obj = self.doc.addObject("Part::MultiFuse", "Fusion")
        obj.Shapes = [self.doc.getObject(name) for name in inputNames]
        self.copy_visual_attributes(obj, obj.Shapes[0])
        target = self.move_input_objects(obj.Shapes)
        if target:
            target.addObject(obj)
        return obj

    def move_input_objects(self, objects):
        targetGroup = None
        for obj in objects:
            obj.Visibility = False
            parent = obj.getParent()
            if parent:
                parent.removeObject(obj)
                targetGroup = parent
        return targetGroup

    def copy_visual_attributes(self, target, source):
        if target.ViewObject:
            displayMode = source.ViewObject.DisplayMode
            src = source
            while displayMode == "Link":
                if getattr(src, "LinkedObject", None):
                    src = src.LinkedObject
                elif getattr(src, "Base", None):
                    # Draft Link array
                    src = src.Base
                else:
                    break
                if not hasattr(src, "ViewObject"):
                    break
                displayMode = src.ViewObject.DisplayMode
            if displayMode in target.ViewObject.getEnumerationsOfProperty("DisplayMode"):
                target.ViewObject.DisplayMode = displayMode