FreeCAD

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

29
## \addtogroup draftmake
30
# @{
31
import FreeCAD as App
32
from draftobjects.layer import Layer, LayerContainer
33
from draftutils import utils
34
from draftutils.messages import _err
35
from draftutils.translate import translate
36

37
if App.GuiUp:
38
    from draftviewproviders.view_layer import (ViewProviderLayer,
39
                                               ViewProviderLayerContainer)
40

41

42
def get_layer_container():
43
    """Return a group object to put layers in.
44

45
    Returns
46
    -------
47
    App::DocumentObjectGroupPython
48
        The existing group object named `'LayerContainer'`
49
        of type `LayerContainer`.
50
        If it doesn't exist it will create it with this default Name.
51
    """
52
    found, doc = utils.find_doc(App.activeDocument())
53
    if not found:
54
        _err(translate("draft","No active document. Aborting."))
55
        return None
56

57
    for obj in doc.Objects:
58
        if obj.Name == "LayerContainer":
59
            return obj
60

61
    new_obj = doc.addObject("App::DocumentObjectGroupPython",
62
                            "LayerContainer")
63
    new_obj.Label = translate("draft", "Layers")
64

65
    LayerContainer(new_obj)
66

67
    if App.GuiUp:
68
        ViewProviderLayerContainer(new_obj.ViewObject)
69

70
    return new_obj
71

72

73
def getLayerContainer():
74
    """Get the Layer container. DEPRECATED. Use 'get_layer_container'."""
75
    utils.use_instead("get_layer_container")
76

77
    return get_layer_container()
78

79

80
def make_layer(name=None,
81
               line_color=(0.0, 0.0, 0.0),   # does not match default DefaultShapeLineColor
82
               shape_color=(0.8, 0.8, 0.8),  # matches default DefaultShapeColor
83
               line_width=2.0,
84
               draw_style="Solid",
85
               transparency=0):
86
    """Create a Layer object in the active document.
87

88
    If a layer container named `'LayerContainer'` does not exist, it is created.
89

90
    A layer controls the view properties of the objects inside the layer.
91
    All parameters except for `name` only apply if the graphical interface
92
    is up.
93

94
    All parameters that control view properties can be set to `None`. Their
95
    value, as set by the view provider (matching the current preferences), is
96
    then not changed.
97

98
    Parameters
99
    ----------
100
    name: str or `None`, optional
101
        It defaults to `None`.
102
        It is used to set the layer's `Label`. If it is `None` the `Label` is
103
        set to `'Layer'` or to its translation in the current language.
104

105
    line_color: tuple or `None`, optional
106
        It defaults to `(0.0, 0.0, 0.0)`.
107
        If it is given, it should be a tuple of three floating point values
108
        from 0.0 to 1.0.
109

110
    shape_color: tuple or `None`, optional
111
        It defaults to `(0.8, 0.8, 0.8)`.
112
        If it is given, it should be a tuple of three floating point values
113
        from 0.0 to 1.0.
114

115
    line_width: float or `None`, optional
116
        It defaults to 2.0.
117
        It determines the width of the edges of the objects contained in the layer.
118

119
    draw_style: str or `None`, optional
120
        It defaults to `'Solid'`.
121
        It determines the style of the edges of the objects contained in the layer.
122
        If it is given, it should be 'Solid', 'Dashed', 'Dotted' or 'Dashdot'.
123

124
    transparency: int or `None`, optional
125
        It defaults to 0.
126
        It should be an integer from 0 to 100.
127

128
    Return
129
    ------
130
    App::FeaturePython
131
        A scripted object of type `'Layer'`.
132
        This object does not have a `Shape` attribute.
133
        Modifying the view properties of this object will affect the objects
134
        inside of it.
135

136
    None
137
        If there is a problem it will return `None`.
138
    """
139
    _name = "make_layer"
140

141
    found, doc = utils.find_doc(App.activeDocument())
142
    if not found:
143
        _err(translate("draft","No active document. Aborting."))
144
        return None
145

146
    if name is not None:
147
        try:
148
            utils.type_check([(name, str)], name=_name)
149
        except TypeError:
150
            _err(translate("draft","Wrong input: it must be a string."))
151
            return None
152
    else:
153
        name = translate("draft", "Layer")
154

155
    if line_color is not None:
156
        try:
157
            utils.type_check([(line_color, tuple)], name=_name)
158
        except TypeError:
159
            _err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
160
            return None
161

162
        if not all(isinstance(color, (int, float)) for color in line_color):
163
            _err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
164
            return None
165

166
    if shape_color is not None:
167
        try:
168
            utils.type_check([(shape_color, tuple)], name=_name)
169
        except TypeError:
170
            _err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
171
            return None
172

173
        if not all(isinstance(color, (int, float)) for color in shape_color):
174
            _err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
175
            return None
176

177
    if line_width is not None:
178
        try:
179
            utils.type_check([(line_width, (int, float))], name=_name)
180
            line_width = float(abs(line_width))
181
        except TypeError:
182
            _err(translate("draft","Wrong input: must be a number."))
183
            return None
184

185
    if draw_style is not None:
186
        try:
187
            utils.type_check([(draw_style, str)], name=_name)
188
        except TypeError:
189
            _err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
190
            return None
191

192
        if draw_style not in ('Solid', 'Dashed', 'Dotted', 'Dashdot'):
193
            _err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
194
            return None
195

196
    if transparency is not None:
197
        try:
198
            utils.type_check([(transparency, (int, float))], name=_name)
199
            transparency = int(abs(transparency))
200
        except TypeError:
201
            _err(translate("draft","Wrong input: must be a number between 0 and 100."))
202
            return None
203

204
    new_obj = doc.addObject("App::FeaturePython", "Layer")
205
    Layer(new_obj)
206

207
    new_obj.Label = name
208

209
    if App.GuiUp:
210
        ViewProviderLayer(new_obj.ViewObject)
211
        if line_color is not None:
212
            new_obj.ViewObject.LineColor = line_color
213
        if shape_color is not None:
214
            new_obj.ViewObject.ShapeColor = shape_color
215
        if line_width is not None:
216
            new_obj.ViewObject.LineWidth = line_width
217
        if draw_style is not None:
218
            new_obj.ViewObject.DrawStyle = draw_style
219
        if transparency is not None:
220
            new_obj.ViewObject.Transparency = transparency
221

222
    container = get_layer_container()
223
    container.addObject(new_obj)
224

225
    return new_obj
226

227
## @}
228

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

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

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

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