import FreeCAD as App
from draftobjects.layer import Layer, LayerContainer
from draftutils import utils
from draftutils.messages import _err
from draftutils.translate import translate
if App.GuiUp:
from draftviewproviders.view_layer import ViewProviderLayer, ViewProviderLayerContainer
def get_layer_container():
found, doc = utils.find_doc(App.activeDocument())
if not found:
_err(translate("draft", "No active document. Aborting."))
return None
for obj in doc.Objects:
if obj.Name == "LayerContainer":
return obj
obj = doc.addObject("App::DocumentObjectGroupPython", "LayerContainer")
obj.Label = translate("draft", "Layers")
LayerContainer(obj)
if App.GuiUp:
ViewProviderLayerContainer(obj.ViewObject)
return obj
def getLayerContainer():
utils.use_instead("get_layer_container")
return get_layer_container()
def make_layer(
name=None,
line_color=(0.0, 0.0, 0.0), shape_color=(0.8, 0.8, 0.8), line_width=2.0,
draw_style="Solid",
transparency=0,
):
_name = "make_layer"
found, doc = utils.find_doc(App.activeDocument())
if not found:
_err(translate("draft", "No active document. Aborting."))
return None
if name is not None:
try:
utils.type_check([(name, str)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: it must be a string."))
return None
else:
name = translate("draft", "Layer")
if line_color is not None:
try:
utils.type_check([(line_color, tuple)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
if not all(isinstance(color, (int, float)) for color in line_color):
_err(translate("draft", "Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
if shape_color is not None:
try:
utils.type_check([(shape_color, tuple)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
if not all(isinstance(color, (int, float)) for color in shape_color):
_err(translate("draft", "Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
if line_width is not None:
try:
utils.type_check([(line_width, (int, float))], name=_name)
line_width = float(abs(line_width))
except TypeError:
_err(translate("draft", "Wrong input: must be a number."))
return None
if draw_style is not None:
try:
utils.type_check([(draw_style, str)], name=_name)
except TypeError:
_err(
translate(
"draft", "Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."
)
)
return None
if draw_style not in ("Solid", "Dashed", "Dotted", "Dashdot"):
_err(
translate(
"draft", "Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."
)
)
return None
if transparency is not None:
try:
utils.type_check([(transparency, (int, float))], name=_name)
transparency = int(abs(transparency))
except TypeError:
_err(translate("draft", "Wrong input: must be a number between 0 and 100."))
return None
obj = doc.addObject("App::FeaturePython", "Layer")
Layer(obj)
obj.Label = name
if App.GuiUp:
vobj = obj.ViewObject
ViewProviderLayer(vobj)
if line_color is not None:
vobj.LineColor = line_color
if shape_color is not None:
vobj.ShapeColor = shape_color
if line_width is not None:
vobj.LineWidth = line_width
if draw_style is not None:
vobj.DrawStyle = draw_style
if transparency is not None:
vobj.Transparency = transparency
container = get_layer_container()
container.addObject(obj)
return obj