From 60f3155651f5f4c1aa01f140368f08f5de3a9b1a Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Thu, 19 Mar 2020 18:21:29 -0600 Subject: [PATCH] Draft: move Draft_Slope to gui_lineslope module The class name was renamed to `LineSlope` as it is fundamentally inteded to control slopes of lines. --- src/Mod/Draft/CMakeLists.txt | 1 + src/Mod/Draft/DraftTools.py | 56 +------ src/Mod/Draft/draftguitools/gui_lineslope.py | 156 +++++++++++++++++++ src/Mod/Draft/draftutils/init_tools.py | 2 +- 4 files changed, 159 insertions(+), 56 deletions(-) create mode 100644 src/Mod/Draft/draftguitools/gui_lineslope.py diff --git a/src/Mod/Draft/CMakeLists.txt b/src/Mod/Draft/CMakeLists.txt index d7ada4c5ce..26edb6e83c 100644 --- a/src/Mod/Draft/CMakeLists.txt +++ b/src/Mod/Draft/CMakeLists.txt @@ -102,6 +102,7 @@ SET(Draft_GUI_tools draftguitools/gui_grid.py draftguitools/gui_heal.py draftguitools/gui_dimension_ops.py + draftguitools/gui_lineslope.py draftguitools/README.md ) diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 9ae7836ff2..7756e64550 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -88,6 +88,7 @@ from draftguitools.gui_groups import SelectGroup from draftguitools.gui_grid import ToggleGrid from draftguitools.gui_heal import Heal from draftguitools.gui_dimension_ops import Draft_FlipDimension +from draftguitools.gui_lineslope import Draft_Slope # import DraftFillet import drafttaskpanels.task_shapestring as task_shapestring import drafttaskpanels.task_scale as task_scale @@ -4865,60 +4866,6 @@ class Mirror(Modifier): self.finish() -class Draft_Slope(): - - def GetResources(self): - return {'Pixmap' : 'Draft_Slope', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_Slope", "Set Slope"), - 'ToolTip' : QtCore.QT_TRANSLATE_NOOP("Draft_Slope", "Sets the slope of a selected Line or Wire")} - - def Activated(self): - if not FreeCADGui.Selection.getSelection(): - return - for obj in FreeCADGui.Selection.getSelection(): - if Draft.getType(obj) != "Wire": - FreeCAD.Console.PrintMessage(translate("draft", "This tool only works with Wires and Lines")+"\n") - return - w = QtGui.QWidget() - w.setWindowTitle(translate("Draft","Slope")) - layout = QtGui.QHBoxLayout(w) - label = QtGui.QLabel(w) - label.setText(translate("Draft", "Slope")+":") - layout.addWidget(label) - self.spinbox = QtGui.QDoubleSpinBox(w) - self.spinbox.setMinimum(-9999.99) - self.spinbox.setMaximum(9999.99) - self.spinbox.setSingleStep(0.01) - self.spinbox.setToolTip(translate("Draft", "Slope to give selected Wires/Lines: 0 = horizontal, 1 = 45deg up, -1 = 45deg down")) - layout.addWidget(self.spinbox) - taskwidget = QtGui.QWidget() - taskwidget.form = w - taskwidget.accept = self.accept - FreeCADGui.Control.showDialog(taskwidget) - - def accept(self): - if hasattr(self,"spinbox"): - pc = self.spinbox.value() - FreeCAD.ActiveDocument.openTransaction("Change slope") - for obj in FreeCADGui.Selection.getSelection(): - if Draft.getType(obj) == "Wire": - if len(obj.Points) > 1: - lp = None - np = [] - for p in obj.Points: - if not lp: - lp = p - else: - v = p.sub(lp) - z = pc*FreeCAD.Vector(v.x,v.y,0).Length - lp = FreeCAD.Vector(p.x,p.y,lp.z+z) - np.append(lp) - obj.Points = np - FreeCAD.ActiveDocument.commitTransaction() - FreeCADGui.Control.closeDialog() - FreeCAD.ActiveDocument.recompute() - - class SetAutoGroup(): """The SetAutoGroup FreeCAD command definition""" @@ -5283,7 +5230,6 @@ FreeCADGui.addCommand('Draft_PathArray',PathArray()) FreeCADGui.addCommand('Draft_PathLinkArray',PathLinkArray()) FreeCADGui.addCommand('Draft_PointArray',PointArray()) FreeCADGui.addCommand('Draft_Mirror',Mirror()) -FreeCADGui.addCommand('Draft_Slope',Draft_Slope()) FreeCADGui.addCommand('Draft_Stretch',Stretch()) # context commands diff --git a/src/Mod/Draft/draftguitools/gui_lineslope.py b/src/Mod/Draft/draftguitools/gui_lineslope.py new file mode 100644 index 0000000000..3613c76444 --- /dev/null +++ b/src/Mod/Draft/draftguitools/gui_lineslope.py @@ -0,0 +1,156 @@ +# *************************************************************************** +# * (c) 2009, 2010 Yorik van Havre * +# * (c) 2009, 2010 Ken Cline * +# * (c) 2020 Eliud Cabrera Castillo * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * 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. * +# * * +# * FreeCAD 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 * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** +"""Provides tools to change the slope of a line over the working plane. + +It currently only works for a line in the XY plane, it changes the height +of one of its points in the Z direction to create a sloped line. +""" +## @package gui_lineslope +# \ingroup DRAFT +# \brief Provides tools to change the slope of a line over the working plane. + +import PySide.QtGui as QtGui +from PySide.QtCore import QT_TRANSLATE_NOOP + +import FreeCAD as App +import FreeCADGui as Gui +import Draft_rc +import draftutils.utils as utils +import draftguitools.gui_base as gui_base +from draftutils.translate import _tr, translate + +# The module is used to prevent complaints from code checkers (flake8) +True if Draft_rc.__name__ else False + + +class LineSlope(gui_base.GuiCommandNeedsSelection): + """Gui Command for the Line slope tool. + + For a line in the XY plane, it changes the height of one of its points + to create a sloped line. + + To Do + ----- + Make it work also with lines lying on the YZ and XZ planes, + or in an arbitrary plane, for which the normal is known. + """ + + def __init__(self): + super().__init__(name=_tr("Change slope")) + + def GetResources(self): + """Set icon, menu and tooltip.""" + _menu = "Set slope" + _tip = ("Sets the slope of the selected line " + "by changing the value of the Z value of one of its points.\n" + "If a polyline is selected, it will apply the slope " + "transformation to each of its segments.\n\n" + "The slope will always change the Z value, therefore " + "this command only works well for\n" + "straight Draft lines that are drawn in the XY plane. " + "Selected objects that aren't single lines will be ignored.") + + return {'Pixmap': 'Draft_Slope', + 'MenuText': QT_TRANSLATE_NOOP("Draft_Slope", _menu), + 'ToolTip': QT_TRANSLATE_NOOP("Draft_Slope", _tip)} + + def Activated(self): + """Execute when the command is called.""" + super().Activated() + + # for obj in Gui.Selection.getSelection(): + # if utils.get_type(obj) != "Wire": + # _msg(translate("draft", + # "This tool only works with " + # "Draft Lines and Wires")) + # return + + # TODO: create a .ui file with QtCreator and import it here + # instead of creating the interface programmatically, + # see the `gui_othoarray` module for an example. + w = QtGui.QWidget() + w.setWindowTitle(translate("Draft", "Slope")) + layout = QtGui.QHBoxLayout(w) + label = QtGui.QLabel(w) + label.setText(translate("Draft", "Slope")+":") + layout.addWidget(label) + self.spinbox = QtGui.QDoubleSpinBox(w) + self.spinbox.setMinimum(-9999.99) + self.spinbox.setMaximum(9999.99) + self.spinbox.setSingleStep(0.01) + _tip = ("New slope of the selected lines.\n" + "This is the tangent of the horizontal angle:\n" + "0 = horizontal\n" + "1 = 45 deg up\n" + "-1 = 45deg down\n") + label.setToolTip(translate("Draft", _tip)) + self.spinbox.setToolTip(translate("Draft", _tip)) + layout.addWidget(self.spinbox) + + # In order to display our interface inside the task panel + # we must contain our interface inside a parent widget. + # Then our interface must be installed in this parent widget + # inside the attribute called "form". + taskwidget = QtGui.QWidget() + taskwidget.form = w + + # The "accept" attribute of the parent widget + # should also contain a reference to a function that will be called + # when we press the "OK" button. + # Then we must show the container widget. + taskwidget.accept = self.accept + Gui.Control.showDialog(taskwidget) + + def accept(self): + """Execute when clicking the OK button or pressing Enter key. + + It changes the slope of the line that lies on the XY plane. + + TODO: make it work also with lines lying on the YZ and XZ planes. + """ + if hasattr(self, "spinbox"): + pc = self.spinbox.value() + self.doc.openTransaction("Change slope") + for obj in Gui.Selection.getSelection(): + if utils.get_type(obj) == "Wire": + if len(obj.Points) > 1: + lp = None + np = [] + for p in obj.Points: + if not lp: + lp = p + else: + v = p.sub(lp) + z = pc * App.Vector(v.x, v.y, 0).Length + lp = App.Vector(p.x, p.y, lp.z + z) + np.append(lp) + obj.Points = np + self.doc.commitTransaction() + Gui.Control.closeDialog() + self.doc.recompute() + + +Draft_Slope = LineSlope +Gui.addCommand('Draft_Slope', LineSlope()) diff --git a/src/Mod/Draft/draftutils/init_tools.py b/src/Mod/Draft/draftutils/init_tools.py index 5ec19ec8d0..eb6e881daf 100644 --- a/src/Mod/Draft/draftutils/init_tools.py +++ b/src/Mod/Draft/draftutils/init_tools.py @@ -83,7 +83,7 @@ def get_draft_modification_commands(): "Draft_Upgrade", "Draft_Downgrade", "Separator", "Draft_WireToBSpline", "Draft_Draft2Sketch", - "Draft_FlipDimension", + "Draft_Slope", "Draft_FlipDimension", "Separator", "Draft_Shape2DView", "Draft_Drawing"] return lst