FreeCAD

Форк
0
/
gui_lineslope.py 
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

27
It currently only works for a line in the XY plane, it changes the height
28
of 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
# @{
36
import PySide.QtWidgets as QtWidgets
37
from PySide.QtCore import QT_TRANSLATE_NOOP
38

39
import FreeCAD as App
40
import FreeCADGui as Gui
41
import Draft_rc
42
import draftutils.utils as utils
43
import draftguitools.gui_base as gui_base
44

45
from draftutils.translate import translate
46

47
# The module is used to prevent complaints from code checkers (flake8)
48
True if Draft_rc.__name__ else False
49

50

51
class LineSlope(gui_base.GuiCommandNeedsSelection):
52
    """Gui Command for the Line slope tool.
53

54
    For a line in the XY plane, it changes the height of one of its points
55
    to create a sloped line.
56

57
    To Do
58
    -----
59
    Make it work also with lines lying on the YZ and XZ planes,
60
    or in an arbitrary plane, for which the normal is known.
61
    """
62

63
    def __init__(self):
64
        super(LineSlope, self).__init__(name=translate("draft","Change slope"))
65

66
    def GetResources(self):
67
        """Set icon, menu and tooltip."""
68

69
        return {'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

73
    def Activated(self):
74
        """Execute when the command is called."""
75
        super(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.
87
        w = QtWidgets.QWidget()
88
        w.setWindowTitle(translate("Draft", "Slope"))
89
        layout = QtWidgets.QHBoxLayout(w)
90
        label = QtWidgets.QLabel(w)
91
        label.setText(translate("Draft", "Slope")+":")
92
        layout.addWidget(label)
93
        self.spinbox = QtWidgets.QDoubleSpinBox(w)
94
        self.spinbox.setMinimum(-9999.99)
95
        self.spinbox.setMaximum(9999.99)
96
        self.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")
102
        label.setToolTip(translate("Draft", _tip))
103
        self.spinbox.setToolTip(translate("Draft", _tip))
104
        layout.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".
110
        taskwidget = QtWidgets.QWidget()
111
        taskwidget.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.
117
        taskwidget.accept = self.accept
118
        Gui.Control.showDialog(taskwidget)
119

120
    def accept(self):
121
        """Execute when clicking the OK button or pressing Enter key.
122

123
        It changes the slope of the line that lies on the XY plane.
124

125
        TODO: make it work also with lines lying on the YZ and XZ planes.
126
        """
127
        if hasattr(self, "spinbox"):
128
            pc = self.spinbox.value()
129
            self.doc.openTransaction("Change slope")
130
            for obj in Gui.Selection.getSelection():
131
                if utils.get_type(obj) == "Wire":
132
                    if len(obj.Points) > 1:
133
                        lp = None
134
                        np = []
135
                        for p in obj.Points:
136
                            if not lp:
137
                                lp = p
138
                            else:
139
                                v = p.sub(lp)
140
                                z = pc * App.Vector(v.x, v.y, 0).Length
141
                                lp = App.Vector(p.x, p.y, lp.z + z)
142
                            np.append(lp)
143
                        obj.Points = np
144
            self.doc.commitTransaction()
145
        Gui.Control.closeDialog()
146
        self.doc.recompute()
147

148

149
Draft_Slope = LineSlope
150
Gui.addCommand('Draft_Slope', LineSlope())
151

152
## @}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.