From fd6d3da0be296e01bc30267c862585a136c95dc6 Mon Sep 17 00:00:00 2001 From: carlopav Date: Sun, 26 Apr 2020 14:04:32 +0200 Subject: [PATCH] Draft: split mirror from Draft.py . --- src/Mod/Draft/CMakeLists.txt | 1 + src/Mod/Draft/Draft.py | 45 +----------- src/Mod/Draft/draftfunctions/mirror.py | 97 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 43 deletions(-) create mode 100644 src/Mod/Draft/draftfunctions/mirror.py diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index ea9144d9a4..c2fb1bb221 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -61,6 +61,7 @@ SET(Draft_utilities SET(Draft_functions draftfunctions/__init__.py draftfunctions/join.py + draftfunctions/mirror.py draftfunctions/move.py draftfunctions/offset.py draftfunctions/rotate.py diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index 83dc50cb77..203955def3 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -196,6 +196,8 @@ from draftfunctions.split import split from draftfunctions.offset import offset +from draftfunctions.mirror import mirror + #--------------------------------------------------------------------------- # Draft objects #--------------------------------------------------------------------------- @@ -865,49 +867,6 @@ def makeDrawingView(obj,page,lwmod=None,tmod=None,otherProjection=None): return viewobj -def mirror(objlist, p1, p2): - """mirror(objlist, p1, p2) - creates a Part::Mirror of the given object(s), along a plane defined - by the 2 given points and the draft working plane normal. - """ - - if not objlist: - _err = "No object given" - FreeCAD.Console.PrintError(translate("draft", _err) + "\n") - return - if p1 == p2: - _err = "The two points are coincident" - FreeCAD.Console.PrintError(translate("draft", _err) + "\n") - return - if not isinstance(objlist,list): - objlist = [objlist] - - if hasattr(FreeCAD, "DraftWorkingPlane"): - norm = FreeCAD.DraftWorkingPlane.getNormal() - elif gui: - norm = FreeCADGui.ActiveDocument.ActiveView.getViewDirection().negative() - else: - norm = FreeCAD.Vector(0,0,1) - - pnorm = p2.sub(p1).cross(norm).normalize() - - result = [] - - for obj in objlist: - mir = FreeCAD.ActiveDocument.addObject("Part::Mirroring","mirror") - mir.Label = "Mirror of " + obj.Label - mir.Source = obj - mir.Base = p1 - mir.Normal = pnorm - formatObject(mir, obj) - result.append(mir) - - if len(result) == 1: - result = result[0] - select(result) - - return result - def heal(objlist=None,delete=True,reparent=True): """heal([objlist],[delete],[reparent]) - recreates Draft objects that are damaged, diff --git a/src/Mod/Draft/draftfunctions/mirror.py b/src/Mod/Draft/draftfunctions/mirror.py new file mode 100644 index 0000000000..b9d0b4e9bd --- /dev/null +++ b/src/Mod/Draft/draftfunctions/mirror.py @@ -0,0 +1,97 @@ +# *************************************************************************** +# * Copyright (c) 2009, 2010 Yorik van Havre * +# * Copyright (c) 2009, 2010 Ken Cline * +# * Copyright (c) 2020 FreeCAD Developers * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * 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 this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** +"""This module provides the code for Draft mirror function. +""" +## @package mirror +# \ingroup DRAFT +# \brief This module provides the code for Draft mirror function. + +import math + +import FreeCAD as App + +import DraftVecUtils + +import draftutils.gui_utils as gui_utils +import draftutils.utils as utils + +from draftutils.translate import _tr + + +def mirror(objlist, p1, p2): + """mirror(objlist, p1, p2) + + Create a Part::Mirror of the given object(s) along a plane defined + by the 2 given points and the draft working plane normal. + + TODO: Implement a proper Draft mirror tool that do not create a + Part object but works similar to offset + + Parameters + ---------- + objlist : + + p1 : Base.Vector + Point 1 of the mirror plane + + p2 : Base.Vector + Point 1 of the mirror plane + + """ + + if not objlist: + _err = "No object given" + App.Console.PrintError(_tr(_err) + "\n") + return + if p1 == p2: + _err = "The two points are coincident" + App.Console.PrintError(_tr(_err) + "\n") + return + if not isinstance(objlist,list): + objlist = [objlist] + + if hasattr(App, "DraftWorkingPlane"): + norm = App.DraftWorkingPlane.getNormal() + elif App.GuiUp: + norm = FreeCADGui.ActiveDocument.ActiveView.getViewDirection().negative() + else: + norm = App.Vector(0,0,1) + + pnorm = p2.sub(p1).cross(norm).normalize() + + result = [] + + for obj in objlist: + mir = App.ActiveDocument.addObject("Part::Mirroring","mirror") + mir.Label = "Mirror of " + obj.Label + mir.Source = obj + mir.Base = p1 + mir.Normal = pnorm + gui_utils.format_object(mir, obj) + result.append(mir) + + if len(result) == 1: + result = result[0] + gui_utils.select(result) + + return result