Draft: clean up code, PEP8, and docstrings for PointArray

Test the inputs to the `make_point_array` function
and return `None` if there is a problem.

Now the make function accepts as input a `"String"` which must be
the `Label` of an object in the document, so it is easier to create
arrays quickly from the Python console.

Add a message deprecating the older call `makePointArray`.

Adjust the GuiCommand accordingly. Now it uses the commit
mechanism of the parent `Modifier` class so that the executed
functions are recorded in the Python console.

Clean up the `PointArray` class as well.
This commit is contained in:
vocx-fc
2020-05-14 22:53:14 -05:00
committed by Yorik van Havre
parent 6ace3e96e3
commit 4b65440112
3 changed files with 323 additions and 123 deletions

View File

@@ -1,7 +1,9 @@
# ***************************************************************************
# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * Copyright (c) 2020 FreeCAD Developers *
# * Copyright (c) 2018 Benjamin Alterauge (ageeye) *
# * Copyright (c) 2020 Carlo Pavan <carlopav@gmail.com> *
# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
@@ -20,88 +22,171 @@
# * USA *
# * *
# ***************************************************************************
"""This module provides the object code for the Draft PointArray object.
"""
"""Provides the object code for the Draft PointArray object."""
## @package pointarray
# \ingroup DRAFT
# \brief This module provides the object code for the Draft PointArray object.
# \brief Provides the object code for the Draft PointArray object.
import math
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import DraftVecUtils
import draftutils.utils as utils
import lazy_loader.lazy_loader as lz
from draftutils.messages import _err
from draftutils.translate import translate, _tr
from draftobjects.base import DraftObject
# Delay import of module until first use because it is heavy
Part = lz.LazyLoader("Part", globals(), "Part")
class PointArray(DraftObject):
"""The Draft Point Array object"""
"""The Draft Point Array object."""
def __init__(self, obj, bobj, ptlst):
def __init__(self, obj):
super(PointArray, self).__init__(obj, "PointArray")
_tip = "Base object"
obj.addProperty("App::PropertyLink", "Base",
"Objects", QT_TRANSLATE_NOOP("App::Property", _tip))
_tip = "List of points used to distribute the base object"
obj.addProperty("App::PropertyLink", "PointList",
"Objects", QT_TRANSLATE_NOOP("App::Property", _tip))
_tip = "Number of copies" # TODO: verify description of the tooltip
obj.addProperty("App::PropertyInteger", "Count",
"Parameters", QT_TRANSLATE_NOOP("App::Property", _tip))
self.set_properties(obj)
obj.Base = bobj
obj.PointList = ptlst
obj.Count = 0
def set_properties(self, obj):
"""Set properties only if they don't exist."""
properties = obj.PropertiesList
obj.setEditorMode("Count", 1)
if "Base" not in properties:
_tip = "Base object that will be duplicated"
obj.addProperty("App::PropertyLink",
"Base",
"Objects",
QT_TRANSLATE_NOOP("App::Property", _tip))
obj.Base = None
# TODO: this isn't a list, it should be renamed to PointObject
if "PointList" not in properties:
_tip = ("Object containing points used to distribute "
"the base object, for example, a sketch or "
"a Part compound.\n"
"The sketch or compound must contain at least "
"one explicit point or vertex object.")
obj.addProperty("App::PropertyLink",
"PointList",
"Objects",
QT_TRANSLATE_NOOP("App::Property", _tip))
obj.PointList = None
if "Count" not in properties:
_tip = ("Total number of elements in the array.\n"
"This property is read-only, as the number depends "
"on the points contained within 'Point Object'.")
obj.addProperty("App::PropertyInteger",
"Count",
"Objects",
QT_TRANSLATE_NOOP("App::Property", _tip))
obj.Count = 0
obj.setEditorMode("Count", 1) # Read only
def execute(self, obj):
import Part
pls = []
opl = obj.PointList
while utils.get_type(opl) == 'Clone':
opl = opl.Objects[0]
if hasattr(opl, 'Geometry'):
place = opl.Placement
for pts in opl.Geometry:
if hasattr(pts, 'X') and hasattr(pts, 'Y') and hasattr(pts, 'Z'):
pn = pts.copy()
pn.translate(place.Base)
pn.rotate(place)
pls.append(pn)
elif hasattr(opl, 'Links'):
pls = opl.Links
elif hasattr(opl, 'Components'):
pls = opl.Components
"""Run when the object is created or recomputed."""
if not hasattr(obj.Base, 'Shape'):
_err(_tr("Base object doesn't have a 'Shape', "
"it cannot be used for an array."))
obj.Count = 0
return
base = []
i = 0
if hasattr(obj.Base, 'Shape'):
for pts in pls:
#print pts # inspect the objects
if hasattr(pts, 'X') and hasattr(pts, 'Y') and hasattr(pts, 'Z'):
nshape = obj.Base.Shape.copy()
if hasattr(pts, 'Placement'):
place = pts.Placement
nshape.translate(place.Base)
nshape.rotate(place.Base, place.Rotation.Axis, place.Rotation.Angle * 180 / math.pi )
else:
nshape.translate(App.Vector(pts.X,pts.Y,pts.Z))
i += 1
base.append(nshape)
obj.Count = i
if i > 0:
obj.Shape = Part.makeCompound(base)
pt_list, count = get_point_list(obj.PointList)
shape = build_copies(obj.Base, pt_list)
obj.Shape = shape
obj.Count = count
def get_point_list(point_object):
"""Extract a list of points from a point object.
Returns
-------
list, int
A list of points that have `X`, `Y`, `Z` coordinates;
the second element is the number of elements.
If the list is empty, the second element is zero.
"""
# If its a clone, extract the real object
while utils.get_type(point_object) == 'Clone':
point_object = point_object.Objects[0]
# If the point object doesn't have actual points
# the point list will remain empty
pt_list = list()
if hasattr(point_object, 'Geometry'):
# Intended for a Sketcher::SketchObject, which has this property
place = point_object.Placement
for geo in point_object.Geometry:
# It must contain at least one Part::GeomPoint.
if (hasattr(geo, 'X')
and hasattr(geo, 'Y') and hasattr(geo, 'Z')):
point = geo.copy()
point.translate(place.Base)
point.rotate(place)
pt_list.append(point)
count = len(pt_list)
return pt_list, count
obj_list = list()
if hasattr(point_object, 'Links'):
# Intended for a Part::Compound, which has this property
obj_list = point_object.Links
elif hasattr(point_object, 'Components'):
# Intended for a Draft Block, which has this property
obj_list = point_object.Components
# These compounds should have at least one discrete point object
# like a Draft Point or a Part::Vertex
for _obj in obj_list:
if hasattr(_obj, 'X') and hasattr(_obj, 'Y') and hasattr(_obj, 'Z'):
pt_list.append(_obj)
count = len(pt_list)
return pt_list, count
def build_copies(base_object, pt_list=None):
"""Build a compound of copies from the base object and list of points.
Returns
-------
Part::TopoShape
The compound shape created by `Part.makeCompound`.
"""
if not pt_list:
_err(translate("Draft",
"Point object doesn't have a discrete point, "
"it cannot be used for an array."))
shape = base_object.Shape.copy()
return shape
copies = list()
for point in pt_list:
new_shape = base_object.Shape.copy()
# If the point object has a placement, use it
# to displace the copy of the shape. Otherwise
# translate by the X, Y, Z coordinates.
if hasattr(point, 'Placement'):
place = point.Placement
new_shape.translate(place.Base)
new_shape.rotate(place.Base,
place.Rotation.Axis,
place.Rotation.Angle * 180 / math.pi)
else:
App.Console.PrintError(QT_TRANSLATE_NOOP("draft","No point found\n"))
obj.Shape = obj.Base.Shape.copy()
new_shape.translate(App.Vector(point.X, point.Y, point.Z))
copies.append(new_shape)
shape = Part.makeCompound(copies)
return shape
_PointArray = PointArray