From 33d3fb1fa9308409d2ac8b13c2f75c3e50222c26 Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Sun, 24 May 2020 21:29:18 -0500 Subject: [PATCH] Draft: clean up PolarArray code Avoid `Draft.py` in the `make_polar_array` function because it creates a circular dependency. Use function to find the object in `make_polar_rarray`. 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 interactively from the Python console. Clean up the GuiCommand and task panel code, and avoid printing messages to the terminal, as this is already done by the make function. --- src/Mod/Draft/draftguitools/gui_polararray.py | 14 +++-- src/Mod/Draft/draftmake/make_polararray.py | 60 ++++++++++++------- .../Draft/drafttaskpanels/task_polararray.py | 17 +++--- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/Mod/Draft/draftguitools/gui_polararray.py b/src/Mod/Draft/draftguitools/gui_polararray.py index c823c47178..ae81831374 100644 --- a/src/Mod/Draft/draftguitools/gui_polararray.py +++ b/src/Mod/Draft/draftguitools/gui_polararray.py @@ -32,11 +32,12 @@ import FreeCAD as App import FreeCADGui as Gui import Draft import Draft_rc # include resources, icons, ui files +import draftutils.todo as todo + from draftutils.messages import _msg, _log from draftutils.translate import _tr from draftguitools import gui_base from drafttaskpanels import task_polararray -import draftutils.todo as todo # The module is used to prevent complaints from code checkers (flake8) bool(Draft_rc.__name__) @@ -58,11 +59,12 @@ class PolarArray(gui_base.GuiCommandBase): def GetResources(self): """Set icon, menu and tooltip.""" - _tip = ("Creates copies of a selected object, " - "and places the copies in a polar pattern.\n" - "The properties of the array can be further modified after " - "the new object is created, including turning it into " - "a different type of array.") + _tip = ("Creates copies of the selected object, " + "and places the copies in a polar pattern\n" + "defined by a center of rotation and its angle.\n" + "\n" + "The array can be turned into an orthogonal " + "or a circular array by changing its type.") d = {'Pixmap': 'Draft_PolarArray', 'MenuText': QT_TRANSLATE_NOOP("Draft", "Polar array"), diff --git a/src/Mod/Draft/draftmake/make_polararray.py b/src/Mod/Draft/draftmake/make_polararray.py index d5d7f67c84..82d2cc7e2f 100644 --- a/src/Mod/Draft/draftmake/make_polararray.py +++ b/src/Mod/Draft/draftmake/make_polararray.py @@ -26,29 +26,31 @@ # \brief Provides functions for creating polar arrays in a plane. import FreeCAD as App -import Draft -# import draftmake.make_array as make_array + +import draftmake.make_array as make_array import draftutils.utils as utils + from draftutils.messages import _msg, _err from draftutils.translate import _tr -def make_polar_array(obj, - number=4, angle=360, center=App.Vector(0, 0, 0), +def make_polar_array(base_object, + number=5, angle=360, center=App.Vector(0, 0, 0), use_link=True): """Create a polar array from the given object. Parameters ---------- - obj: Part::Feature - Any type of object that has a `Part::TopoShape` - that can be duplicated. - This means most 2D and 3D objects produced - with any workbench. + base_object: Part::Feature or str + Any of object that has a `Part::TopoShape` that can be duplicated. + This means most 2D and 3D objects produced with any workbench. + If it is a string, it must be the `Label` of that object. + Since a label is not guaranteed to be unique in a document, + it will use the first object found with this label. number: int, optional - It defaults to 4. - The number of copies produced in the circular pattern. + It defaults to 5. + The number of copies produced in the polar pattern. angle: float, optional It defaults to 360. @@ -77,38 +79,56 @@ def make_polar_array(obj, Returns ------- Part::FeaturePython - A scripted object with `Proxy.Type='Array'`. + A scripted object of type `'Array'`. Its `Shape` is a compound of the copies of the original object. + + None + If there is a problem it will return `None`. + + See Also + -------- + make_ortho_array, make_circular_array, make_path_array, make_point_array """ _name = "make_polar_array" utils.print_header(_name, _tr("Polar array")) - _msg("Number: {}".format(number)) - _msg("Angle: {}".format(angle)) - _msg("Center: {}".format(center)) + if isinstance(base_object, str): + base_object_str = base_object + found, base_object = utils.find_object(base_object, + doc=App.activeDocument()) + if not found: + _msg("base_object: {}".format(base_object_str)) + _err(_tr("Wrong input: object not in document.")) + return None + + _msg("base_object: {}".format(base_object.Label)) + + _msg("number: {}".format(number)) try: utils.type_check([(number, int)], name=_name) except TypeError: _err(_tr("Wrong input: must be an integer number.")) return None + _msg("angle: {}".format(angle)) try: utils.type_check([(angle, (int, float))], name=_name) except TypeError: _err(_tr("Wrong input: must be a number.")) return None + _msg("center: {}".format(center)) try: utils.type_check([(center, App.Vector)], name=_name) except TypeError: _err(_tr("Wrong input: must be a vector.")) return None - _msg("use_link: {}".format(bool(use_link))) + use_link = bool(use_link) + _msg("use_link: {}".format(use_link)) - # new_obj = make_array.make_array() - new_obj = Draft.makeArray(obj, - arg1=center, arg2=angle, arg3=number, - use_link=use_link) + new_obj = make_array.make_array(base_object, + arg1=center, arg2=angle, arg3=number, + use_link=use_link) return new_obj diff --git a/src/Mod/Draft/drafttaskpanels/task_polararray.py b/src/Mod/Draft/drafttaskpanels/task_polararray.py index 539163eccd..70e8f513f6 100644 --- a/src/Mod/Draft/drafttaskpanels/task_polararray.py +++ b/src/Mod/Draft/drafttaskpanels/task_polararray.py @@ -33,9 +33,10 @@ import FreeCADGui as Gui import Draft_rc # include resources, icons, ui files import DraftVecUtils import draftutils.utils as utils + +from FreeCAD import Units as U from draftutils.messages import _msg, _wrn, _err, _log from draftutils.translate import _tr -from FreeCAD import Units as U # The module is used to prevent complaints from code checkers (flake8) bool(Draft_rc.__name__) @@ -173,7 +174,8 @@ class TaskPanelPolarArray: self.center) if self.valid_input: self.create_object() - self.print_messages() + # The internal function already displays messages + # self.print_messages() self.finish() def validate_input(self, selection, @@ -232,13 +234,11 @@ class TaskPanelPolarArray: sel_obj = self.selection[0] # This creates the object immediately - # obj = Draft.makeArray(sel_obj, - # self.center, self.angle, self.number, - # self.use_link) - # if obj: - # obj.Fuse = self.fuse + # obj = Draft.make_polar_array(sel_obj, + # self.number, self.angle, self.center, + # self.use_link) - # Instead, we build the commands to execute through the parent + # Instead, we build the commands to execute through the caller # of this class, the GuiCommand. # This is needed to schedule geometry manipulation # that would crash Coin3D if done in the event callback. @@ -252,7 +252,6 @@ class TaskPanelPolarArray: _cmd += ")" Gui.addModule('Draft') - Gui.addModule('draftmake.make_polararray') _cmd_list = ["_obj_ = " + _cmd, "_obj_.Fuse = " + str(self.fuse),