FreeCAD

Форк
0
/
make_clone.py 
124 строки · 4.9 Кб
1
# ***************************************************************************
2
# *   Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net>        *
3
# *   Copyright (c) 2009, 2010 Ken Cline <cline@frii.com>                   *
4
# *   Copyright (c) 2020 FreeCAD Developers                                 *
5
# *                                                                         *
6
# *   This program is free software; you can redistribute it and/or modify  *
7
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
8
# *   as published by the Free Software Foundation; either version 2 of     *
9
# *   the License, or (at your option) any later version.                   *
10
# *   for detail see the LICENCE text file.                                 *
11
# *                                                                         *
12
# *   This program is distributed in the hope that it will be useful,       *
13
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
# *   GNU Library General Public License for more details.                  *
16
# *                                                                         *
17
# *   You should have received a copy of the GNU Library General Public     *
18
# *   License along with this program; if not, write to the Free Software   *
19
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
20
# *   USA                                                                   *
21
# *                                                                         *
22
# ***************************************************************************
23
"""Provides functions to create Clone objects."""
24
## @package make_clone
25
# \ingroup draftmake
26
# \brief Provides functions to create Clone objects.
27

28
## \addtogroup draftmake
29
# @{
30
import FreeCAD as App
31
from draftobjects.clone import Clone
32
from draftutils import params
33
from draftutils import utils
34
from draftutils import gui_utils
35

36
if App.GuiUp:
37
    from draftviewproviders.view_clone import ViewProviderClone
38

39

40
def make_clone(obj, delta=None, forcedraft=False):
41
    """clone(obj,[delta,forcedraft])
42

43
    Makes a clone of the given object(s).
44
    The clone is an exact, linked copy of the given object. If the original
45
    object changes, the final object changes too.
46

47
    Parameters
48
    ----------
49
    obj :
50

51
    delta : Base.Vector
52
        Delta Vector to move the clone from the original position.
53

54
    forcedraft : bool
55
        If forcedraft is True, the resulting object is a Draft clone
56
        even if the input object is an Arch object.
57

58
    """
59

60
    prefix = params.get_param("ClonePrefix")
61

62
    cl = None
63

64
    if prefix:
65
        prefix = prefix.strip() + " "
66

67
    if not isinstance(obj,list):
68
        obj = [obj]
69

70
    if (len(obj) == 1) and obj[0].isDerivedFrom("Part::Part2DObject"):
71
        cl = App.ActiveDocument.addObject("Part::Part2DObjectPython","Clone2D")
72
        cl.Label = prefix + obj[0].Label + " (2D)"
73
    elif (len(obj) == 1) and (hasattr(obj[0],"CloneOf") or (utils.get_type(obj[0]) == "BuildingPart")) and (not forcedraft):
74
        # arch objects can be clones
75
        import Arch
76
        if utils.get_type(obj[0]) == "BuildingPart":
77
            cl = Arch.makeComponent()
78
        else:
79
            try: # new-style make function
80
                cl = getattr(Arch, "make_" + obj[0].Proxy.Type.lower())()
81
            except Exception:
82
                try: # old-style make function
83
                    cl = getattr(Arch, "make" + obj[0].Proxy.Type)()
84
                except Exception:
85
                    pass # not a standard Arch object... Fall back to Draft mode
86
        if cl:
87
            base = utils.get_clone_base(obj[0])
88
            cl.Label = prefix + base.Label
89
            cl.CloneOf = base
90
            if utils.get_type(obj[0]) != "BuildingPart":
91
                cl.Placement = obj[0].Placement
92
            for prop in ("Description", "IfcType", "Material", "Subvolume", "Tag"):
93
                try:
94
                    setattr(cl, prop, getattr(base, prop))
95
                except Exception:
96
                    pass
97
            if App.GuiUp:
98
                gui_utils.format_object(cl, base)
99
                gui_utils.select(cl)
100
            return cl
101

102
    # fall back to Draft clone mode
103
    if not cl:
104
        cl = App.ActiveDocument.addObject("Part::FeaturePython","Clone")
105
        cl.addExtension("Part::AttachExtensionPython")
106
        cl.Label = prefix + obj[0].Label
107
    Clone(cl)
108
    cl.Objects = obj
109
    if delta:
110
        cl.Placement.move(delta)
111
    elif (len(obj) == 1) and hasattr(obj[0],"Placement"):
112
        cl.Placement = obj[0].Placement
113
    if hasattr(cl,"LongName") and hasattr(obj[0],"LongName"):
114
        cl.LongName = obj[0].LongName
115
    if App.GuiUp:
116
        ViewProviderClone(cl.ViewObject)
117
        gui_utils.format_object(cl, obj[0])
118
        gui_utils.select(cl)
119
    return cl
120

121

122
clone = make_clone
123

124
## @}
125

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

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

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

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