1
# ***************************************************************************
2
# * Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net> *
3
# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
5
# * This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
23
# ***************************************************************************
24
"""Provides functions to create Layer objects."""
27
# \brief Provides functions to create Layer objects.
29
## \addtogroup draftmake
32
from draftobjects.layer import Layer, LayerContainer
33
from draftutils import utils
34
from draftutils.messages import _err
35
from draftutils.translate import translate
38
from draftviewproviders.view_layer import (ViewProviderLayer,
39
ViewProviderLayerContainer)
42
def get_layer_container():
43
"""Return a group object to put layers in.
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.
52
found, doc = utils.find_doc(App.activeDocument())
54
_err(translate("draft","No active document. Aborting."))
57
for obj in doc.Objects:
58
if obj.Name == "LayerContainer":
61
new_obj = doc.addObject("App::DocumentObjectGroupPython",
63
new_obj.Label = translate("draft", "Layers")
65
LayerContainer(new_obj)
68
ViewProviderLayerContainer(new_obj.ViewObject)
73
def getLayerContainer():
74
"""Get the Layer container. DEPRECATED. Use 'get_layer_container'."""
75
utils.use_instead("get_layer_container")
77
return get_layer_container()
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
86
"""Create a Layer object in the active document.
88
If a layer container named `'LayerContainer'` does not exist, it is created.
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
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
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.
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
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
115
line_width: float or `None`, optional
117
It determines the width of the edges of the objects contained in the layer.
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'.
124
transparency: int or `None`, optional
126
It should be an integer from 0 to 100.
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
137
If there is a problem it will return `None`.
141
found, doc = utils.find_doc(App.activeDocument())
143
_err(translate("draft","No active document. Aborting."))
148
utils.type_check([(name, str)], name=_name)
150
_err(translate("draft","Wrong input: it must be a string."))
153
name = translate("draft", "Layer")
155
if line_color is not None:
157
utils.type_check([(line_color, tuple)], name=_name)
159
_err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
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."))
166
if shape_color is not None:
168
utils.type_check([(shape_color, tuple)], name=_name)
170
_err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
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."))
177
if line_width is not None:
179
utils.type_check([(line_width, (int, float))], name=_name)
180
line_width = float(abs(line_width))
182
_err(translate("draft","Wrong input: must be a number."))
185
if draw_style is not None:
187
utils.type_check([(draw_style, str)], name=_name)
189
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
192
if draw_style not in ('Solid', 'Dashed', 'Dotted', 'Dashdot'):
193
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
196
if transparency is not None:
198
utils.type_check([(transparency, (int, float))], name=_name)
199
transparency = int(abs(transparency))
201
_err(translate("draft","Wrong input: must be a number between 0 and 100."))
204
new_obj = doc.addObject("App::FeaturePython", "Layer")
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
222
container = get_layer_container()
223
container.addObject(new_obj)