Draft: clean up code, PEP8, and docstrings in PathArray
Test the inputs to the `make_path_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 the new parameters to the make function, `align_mode`, `tan_vector`, `force_vertical`, and `vertical_vector`. These properties were added to the proxy object in 0db11da9cf. Add message deprecating the older call `makePathArray`. 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 `PathArray` class as well.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
# ***************************************************************************
|
||||
# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
|
||||
# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
|
||||
# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
|
||||
# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
|
||||
# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
|
||||
# * Copyright (c) 2013 Wandererfan <wandererfan@gmail.com> *
|
||||
# * Copyright (c) 2019 Zheng, Lei (realthunder)<realthunder.dev@gmail.com>*
|
||||
# * Copyright (c) 2020 Carlo Pavan <carlopav@gmail.com> *
|
||||
# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
|
||||
# * *
|
||||
# * This file is part of the FreeCAD CAx development system. *
|
||||
# * *
|
||||
@@ -11,13 +14,13 @@
|
||||
# * the License, or (at your option) any later version. *
|
||||
# * for detail see the LICENCE text file. *
|
||||
# * *
|
||||
# * FreeCAD is distributed in the hope that it will be useful, *
|
||||
# * This program is distributed in the hope that it will be useful, *
|
||||
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
# * GNU Library General Public License for more details. *
|
||||
# * *
|
||||
# * You should have received a copy of the GNU Library General Public *
|
||||
# * License along with FreeCAD; if not, write to the Free Software *
|
||||
# * License along with this program; if not, write to the Free Software *
|
||||
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
|
||||
# * USA *
|
||||
# * *
|
||||
@@ -35,12 +38,12 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
|
||||
|
||||
import FreeCAD as App
|
||||
import FreeCADGui as Gui
|
||||
import Draft
|
||||
import Draft_rc
|
||||
import DraftVecUtils
|
||||
import draftguitools.gui_base_original as gui_base_original
|
||||
import draftguitools.gui_tool_utils as gui_tool_utils
|
||||
from draftutils.messages import _msg
|
||||
from draftutils.translate import translate, _tr
|
||||
|
||||
from draftutils.messages import _err
|
||||
from draftutils.translate import _tr
|
||||
|
||||
# The module is used to prevent complaints from code checkers (flake8)
|
||||
True if Draft_rc.__name__ else False
|
||||
@@ -59,13 +62,16 @@ class PathArray(gui_base_original.Modifier):
|
||||
def __init__(self, use_link=False):
|
||||
super(PathArray, self).__init__()
|
||||
self.use_link = use_link
|
||||
self.call = None
|
||||
|
||||
def GetResources(self):
|
||||
"""Set icon, menu and tooltip."""
|
||||
_menu = "Path array"
|
||||
_tip = ("Creates copies of a selected object along a selected path.\n"
|
||||
_tip = ("Creates copies of the selected object "
|
||||
"along a selected path.\n"
|
||||
"First select the object, and then select the path.\n"
|
||||
"The path can be a polyline, B-spline or Bezier curve.")
|
||||
"The path can be a polyline, B-spline, Bezier curve, "
|
||||
"or even edges from other objects.")
|
||||
|
||||
return {'Pixmap': 'Draft_PathArray',
|
||||
'MenuText': QT_TRANSLATE_NOOP("Draft_PathArray", _menu),
|
||||
@@ -74,15 +80,24 @@ class PathArray(gui_base_original.Modifier):
|
||||
def Activated(self, name=_tr("Path array")):
|
||||
"""Execute when the command is called."""
|
||||
super(PathArray, self).Activated(name=name)
|
||||
if not Gui.Selection.getSelectionEx():
|
||||
if self.ui:
|
||||
self.ui.selectUi()
|
||||
_msg(translate("draft", "Please select base and path objects"))
|
||||
self.call = \
|
||||
self.view.addEventCallback("SoEvent",
|
||||
gui_tool_utils.selectObject)
|
||||
else:
|
||||
self.proceed()
|
||||
self.name = name
|
||||
# This was deactivated becuase it doesn't work correctly;
|
||||
# the selection needs to be made on two objects, but currently
|
||||
# it only selects one.
|
||||
|
||||
# if not Gui.Selection.getSelectionEx():
|
||||
# if self.ui:
|
||||
# self.ui.selectUi()
|
||||
# _msg(translate("draft",
|
||||
# "Please select exactly two objects, "
|
||||
# "the base object and the path object, "
|
||||
# "before calling this command."))
|
||||
# self.call = \
|
||||
# self.view.addEventCallback("SoEvent",
|
||||
# gui_tool_utils.selectObject)
|
||||
# else:
|
||||
# self.proceed()
|
||||
self.proceed()
|
||||
|
||||
def proceed(self):
|
||||
"""Proceed with the command if one object was selected."""
|
||||
@@ -90,21 +105,54 @@ class PathArray(gui_base_original.Modifier):
|
||||
self.view.removeEventCallback("SoEvent", self.call)
|
||||
|
||||
sel = Gui.Selection.getSelectionEx()
|
||||
if sel:
|
||||
base = sel[0].Object
|
||||
path = sel[1].Object
|
||||
if len(sel) != 2:
|
||||
_err(_tr("Please select exactly two objects, "
|
||||
"the base object and the path object, "
|
||||
"before calling this command."))
|
||||
else:
|
||||
base_object = sel[0].Object
|
||||
path_object = sel[1].Object
|
||||
|
||||
defCount = 4
|
||||
defXlate = App.Vector(0, 0, 0)
|
||||
defAlign = False
|
||||
pathsubs = list(sel[1].SubElementNames)
|
||||
count = 4
|
||||
xlate = App.Vector(0, 0, 0)
|
||||
subelements = list(sel[1].SubElementNames)
|
||||
align = False
|
||||
align_mode = "Original"
|
||||
tan_vector = App.Vector(1, 0, 0)
|
||||
force_vertical = False
|
||||
vertical_vector = App.Vector(0, 0, 1)
|
||||
use_link = self.use_link
|
||||
|
||||
App.ActiveDocument.openTransaction("PathArray")
|
||||
Draft.makePathArray(base, path,
|
||||
defCount, defXlate, defAlign, pathsubs,
|
||||
use_link=self.use_link)
|
||||
App.ActiveDocument.commitTransaction()
|
||||
App.ActiveDocument.recompute()
|
||||
_edge_list_str = list()
|
||||
_edge_list_str = ["'" + edge + "'" for edge in subelements]
|
||||
_sub_str = ", ".join(_edge_list_str)
|
||||
subelements_list_str = "[" + _sub_str + "]"
|
||||
|
||||
vertical_vector_str = DraftVecUtils.toString(vertical_vector)
|
||||
|
||||
Gui.addModule("Draft")
|
||||
_cmd = "Draft.make_path_array"
|
||||
_cmd += "("
|
||||
_cmd += "App.ActiveDocument." + base_object.Name + ", "
|
||||
_cmd += "App.ActiveDocument." + path_object.Name + ", "
|
||||
_cmd += "count=" + str(count) + ", "
|
||||
_cmd += "xlate=" + DraftVecUtils.toString(xlate) + ", "
|
||||
_cmd += "subelements=" + subelements_list_str + ", "
|
||||
_cmd += "align=" + str(align) + ", "
|
||||
_cmd += "align_mode=" + "'" + align_mode + "', "
|
||||
_cmd += "tan_vector=" + DraftVecUtils.toString(tan_vector) + ", "
|
||||
_cmd += "force_vertical=" + str(force_vertical) + ", "
|
||||
_cmd += "vertical_vector=" + vertical_vector_str + ", "
|
||||
_cmd += "use_link=" + str(use_link)
|
||||
_cmd += ")"
|
||||
|
||||
_cmd_list = ["_obj_ = " + _cmd,
|
||||
"Draft.autogroup(_obj_)",
|
||||
"App.ActiveDocument.recompute()"]
|
||||
self.commit(_tr(self.name), _cmd_list)
|
||||
|
||||
# Commit the transaction and execute the commands
|
||||
# through the parent class
|
||||
self.finish()
|
||||
|
||||
|
||||
@@ -131,7 +179,7 @@ class PathLinkArray(PathArray):
|
||||
|
||||
def Activated(self):
|
||||
"""Execute when the command is called."""
|
||||
super(PathLinkArray, self).Activated(name=_tr("Link path array"))
|
||||
super(PathLinkArray, self).Activated(name=_tr("Path link array"))
|
||||
|
||||
|
||||
Gui.addCommand('Draft_PathLinkArray', PathLinkArray())
|
||||
|
||||
Reference in New Issue
Block a user