import FreeCAD as App
import draftutils.utils as utils
import draftutils.gui_utils as gui_utils
from draftutils.messages import _err
from draftutils.translate import translate
from draftobjects.patharray import PathArray
from draftobjects.pathtwistedarray import PathTwistedArray
if App.GuiUp:
from draftviewproviders.view_array import ViewProviderDraftArray
from draftviewproviders.view_draftlink import ViewProviderDraftLink
def make_path_array(
base_object,
path_object,
count=4,
extra=App.Vector(0, 0, 0),
subelements=None,
align=False,
align_mode="Original",
tan_vector=App.Vector(1, 0, 0),
force_vertical=False,
vertical_vector=App.Vector(0, 0, 1),
start_offset=0.0,
end_offset=0.0,
use_link=True,
):
_name = "make_path_array"
found, doc = utils.find_doc(App.activeDocument())
if not found:
_err(translate("draft", "No active document. Aborting."))
return None
found, base_object = utils.find_object(base_object, doc)
if not found:
_err(translate("draft", "Wrong input: base_object not in document."))
return None
found, path_object = utils.find_object(path_object, doc)
if not found:
_err(translate("draft", "Wrong input: path_object not in document."))
return None
try:
utils.type_check([(count, (int, float))], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a number."))
return None
count = int(count)
try:
utils.type_check([(extra, App.Vector)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a vector."))
return None
if subelements:
try:
if isinstance(subelements, str):
subelements = [subelements]
utils.type_check([(subelements, (list, tuple, str))], name=_name)
except TypeError:
_err(
translate(
"draft", "Wrong input: must be a list or tuple of strings, or a single string."
)
)
return None
sub_list = list()
for sub in subelements:
sub_list.append((path_object, sub))
else:
sub_list = None
align = bool(align)
try:
utils.type_check([(align_mode, str)], name=_name)
if align_mode not in ("Original", "Frenet", "Tangent"):
raise TypeError
except TypeError:
_err(translate("draft", "Wrong input: must be 'Original', 'Frenet', or 'Tangent'."))
return None
try:
utils.type_check([(tan_vector, App.Vector)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a vector."))
return None
force_vertical = bool(force_vertical)
try:
utils.type_check([(vertical_vector, App.Vector)], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a vector."))
return None
try:
utils.type_check([(start_offset, (int, float))], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a number."))
return None
start_offset = float(start_offset)
try:
utils.type_check([(end_offset, (int, float))], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a number."))
return None
end_offset = float(end_offset)
use_link = bool(use_link)
if use_link:
new_obj = doc.addObject("Part::FeaturePython", "PathArray", PathArray(None), None, True)
else:
new_obj = doc.addObject("Part::FeaturePython", "PathArray")
PathArray(new_obj)
new_obj.Base = base_object
new_obj.PathObject = path_object
new_obj.Count = count
new_obj.ExtraTranslation = extra
new_obj.PathSubelements = sub_list
new_obj.Align = align
new_obj.AlignMode = align_mode
new_obj.TangentVector = tan_vector
new_obj.ForceVertical = force_vertical
new_obj.VerticalVector = vertical_vector
new_obj.StartOffset = start_offset
new_obj.EndOffset = end_offset
if App.GuiUp:
if use_link:
ViewProviderDraftLink(new_obj.ViewObject)
else:
ViewProviderDraftArray(new_obj.ViewObject)
gui_utils.formatObject(new_obj, new_obj.Base)
new_obj.ViewObject.Proxy.resetColors(new_obj.ViewObject)
new_obj.Base.ViewObject.hide()
gui_utils.select(new_obj)
return new_obj
def makePathArray(
baseobject, pathobject, count, xlate=None, align=False, pathobjsubs=[], use_link=False
):
utils.use_instead("make_path_array")
return make_path_array(baseobject, pathobject, count, xlate, pathobjsubs, align, use_link)
def make_path_twisted_array(base_object, path_object, count=15, rot_factor=0.25, use_link=True):
_name = "make_path_twisted_array"
found, doc = utils.find_doc(App.activeDocument())
if not found:
_err(translate("draft", "No active document. Aborting."))
return None
found, base_object = utils.find_object(base_object, doc)
if not found:
_err(translate("draft", "Wrong input: base_object not in document."))
return None
found, path_object = utils.find_object(path_object, doc)
if not found:
_err(translate("draft", "Wrong input: path_object not in document."))
return None
try:
utils.type_check([(count, (int, float))], name=_name)
except TypeError:
_err(translate("draft", "Wrong input: must be a number."))
return None
count = int(count)
use_link = bool(use_link)
if use_link:
new_obj = doc.addObject(
"Part::FeaturePython", "PathTwistedArray", PathTwistedArray(None), None, True
)
else:
new_obj = doc.addObject("Part::FeaturePython", "PathTwistedArray")
PathTwistedArray(new_obj)
new_obj.Base = base_object
new_obj.PathObject = path_object
new_obj.Count = count
new_obj.RotationFactor = rot_factor
if App.GuiUp:
if use_link:
ViewProviderDraftLink(new_obj.ViewObject)
else:
ViewProviderDraftArray(new_obj.ViewObject)
gui_utils.formatObject(new_obj, new_obj.Base)
new_obj.ViewObject.Proxy.resetColors(new_obj.ViewObject)
new_obj.Base.ViewObject.hide()
gui_utils.select(new_obj)
return new_obj