FreeCAD
152 строки · 6.7 Кб
1# ***************************************************************************
2# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
3# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
4# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
5# * *
6# * This file is part of the FreeCAD CAx development system. *
7# * *
8# * This program is free software; you can redistribute it and/or modify *
9# * it under the terms of the GNU Lesser General Public License (LGPL) *
10# * as published by the Free Software Foundation; either version 2 of *
11# * the License, or (at your option) any later version. *
12# * for detail see the LICENCE text file. *
13# * *
14# * FreeCAD is distributed in the hope that it will be useful, *
15# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17# * GNU Library General Public License for more details. *
18# * *
19# * You should have received a copy of the GNU Library General Public *
20# * License along with FreeCAD; if not, write to the Free Software *
21# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22# * USA *
23# * *
24# ***************************************************************************
25"""Provides GUI tools to change the slope of a line.
26
27It currently only works for a line in the XY plane, it changes the height
28of one of its points in the Z direction to create a sloped line.
29"""
30## @package gui_lineslope
31# \ingroup draftguitools
32# \brief Provides GUI tools to change the slope of a line.
33
34## \addtogroup draftguitools
35# @{
36import PySide.QtWidgets as QtWidgets
37from PySide.QtCore import QT_TRANSLATE_NOOP
38
39import FreeCAD as App
40import FreeCADGui as Gui
41import Draft_rc
42import draftutils.utils as utils
43import draftguitools.gui_base as gui_base
44
45from draftutils.translate import translate
46
47# The module is used to prevent complaints from code checkers (flake8)
48True if Draft_rc.__name__ else False
49
50
51class LineSlope(gui_base.GuiCommandNeedsSelection):
52"""Gui Command for the Line slope tool.
53
54For a line in the XY plane, it changes the height of one of its points
55to create a sloped line.
56
57To Do
58-----
59Make it work also with lines lying on the YZ and XZ planes,
60or in an arbitrary plane, for which the normal is known.
61"""
62
63def __init__(self):
64super(LineSlope, self).__init__(name=translate("draft","Change slope"))
65
66def GetResources(self):
67"""Set icon, menu and tooltip."""
68
69return {'Pixmap': 'Draft_Slope',
70'MenuText': QT_TRANSLATE_NOOP("Draft_Slope", "Set slope"),
71'ToolTip': QT_TRANSLATE_NOOP("Draft_Slope", "Sets the slope of the selected line by changing the value of the Z value of one of its points.\nIf a polyline is selected, it will apply the slope transformation to each of its segments.\n\nThe slope will always change the Z value, therefore this command only works well for\nstraight Draft lines that are drawn in the XY plane. Selected objects that aren't single lines will be ignored.")}
72
73def Activated(self):
74"""Execute when the command is called."""
75super(LineSlope, self).Activated()
76
77# for obj in Gui.Selection.getSelection():
78# if utils.get_type(obj) != "Wire":
79# _msg(translate("draft",
80# "This tool only works with "
81# "Draft Lines and Wires"))
82# return
83
84# TODO: create a .ui file with QtCreator and import it here
85# instead of creating the interface programmatically,
86# see the `gui_othoarray` module for an example.
87w = QtWidgets.QWidget()
88w.setWindowTitle(translate("Draft", "Slope"))
89layout = QtWidgets.QHBoxLayout(w)
90label = QtWidgets.QLabel(w)
91label.setText(translate("Draft", "Slope")+":")
92layout.addWidget(label)
93self.spinbox = QtWidgets.QDoubleSpinBox(w)
94self.spinbox.setMinimum(-9999.99)
95self.spinbox.setMaximum(9999.99)
96self.spinbox.setSingleStep(0.01)
97_tip = ("New slope of the selected lines.\n"
98"This is the tangent of the horizontal angle:\n"
99"0 = horizontal\n"
100"1 = 45 deg up\n"
101"-1 = 45deg down\n")
102label.setToolTip(translate("Draft", _tip))
103self.spinbox.setToolTip(translate("Draft", _tip))
104layout.addWidget(self.spinbox)
105
106# In order to display our interface inside the task panel
107# we must contain our interface inside a parent widget.
108# Then our interface must be installed in this parent widget
109# inside the attribute called "form".
110taskwidget = QtWidgets.QWidget()
111taskwidget.form = w
112
113# The "accept" attribute of the parent widget
114# should also contain a reference to a function that will be called
115# when we press the "OK" button.
116# Then we must show the container widget.
117taskwidget.accept = self.accept
118Gui.Control.showDialog(taskwidget)
119
120def accept(self):
121"""Execute when clicking the OK button or pressing Enter key.
122
123It changes the slope of the line that lies on the XY plane.
124
125TODO: make it work also with lines lying on the YZ and XZ planes.
126"""
127if hasattr(self, "spinbox"):
128pc = self.spinbox.value()
129self.doc.openTransaction("Change slope")
130for obj in Gui.Selection.getSelection():
131if utils.get_type(obj) == "Wire":
132if len(obj.Points) > 1:
133lp = None
134np = []
135for p in obj.Points:
136if not lp:
137lp = p
138else:
139v = p.sub(lp)
140z = pc * App.Vector(v.x, v.y, 0).Length
141lp = App.Vector(p.x, p.y, lp.z + z)
142np.append(lp)
143obj.Points = np
144self.doc.commitTransaction()
145Gui.Control.closeDialog()
146self.doc.recompute()
147
148
149Draft_Slope = LineSlope
150Gui.addCommand('Draft_Slope', LineSlope())
151
152## @}
153