FreeCAD

Форк
0
360 строк · 14.8 Кб
1
# ***************************************************************************
2
# *   (c) 2009 Yorik van Havre <yorik@uncreated.net>                        *
3
# *   (c) 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 create straight Line and Wire objects.
26

27
The Line class is used by other Gui Commands that behave in a similar way
28
like Wire, BSpline, and BezCurve.
29
"""
30
## @package gui_lines
31
# \ingroup draftguitools
32
# \brief Provides GUI tools to create straight Line and Wire objects.
33

34
## \addtogroup draftguitools
35
# @{
36
from PySide.QtCore import QT_TRANSLATE_NOOP
37

38
import FreeCAD as App
39
import FreeCADGui as Gui
40
import DraftVecUtils
41
from draftguitools import gui_base_original
42
from draftguitools import gui_tool_utils
43
from draftutils import gui_utils
44
from draftutils import params
45
from draftutils import utils
46
from draftutils import todo
47
from draftutils.messages import _err, _toolmsg
48
from draftutils.translate import translate
49

50

51
class Line(gui_base_original.Creator):
52
    """Gui command for the Line tool."""
53

54
    def __init__(self, wiremode=False):
55
        super().__init__()
56
        self.isWire = wiremode
57

58
    def GetResources(self):
59
        """Set icon, menu and tooltip."""
60

61
        return {'Pixmap': 'Draft_Line',
62
                'Accel': "L,I",
63
                'MenuText': QT_TRANSLATE_NOOP("Draft_Line", "Line"),
64
                'ToolTip': QT_TRANSLATE_NOOP("Draft_Line", "Creates a 2-point line. CTRL to snap, SHIFT to constrain.")}
65

66
    def Activated(self, name=QT_TRANSLATE_NOOP("draft", "Line"), icon="Draft_Line", task_title=None):
67
        """Execute when the command is called."""
68
        super().Activated(name)
69
        if task_title is None:
70
            title = translate("draft", name)
71
        else:
72
            title = task_title
73
        if self.isWire:
74
            self.ui.wireUi(title=title, icon=icon)
75
        else:
76
            self.ui.lineUi(title=title, icon=icon)
77

78
        self.obj = self.doc.addObject("Part::Feature", self.featureName)
79
        gui_utils.format_object(self.obj)
80

81
        self.call = self.view.addEventCallback("SoEvent", self.action)
82
        _toolmsg(translate("draft", "Pick first point"))
83

84
    def action(self, arg):
85
        """Handle the 3D scene events.
86

87
        This is installed as an EventCallback in the Inventor view.
88

89
        Parameters
90
        ----------
91
        arg: dict
92
            Dictionary with strings that indicates the type of event received
93
            from the 3D view.
94
        """
95
        if arg["Type"] == "SoKeyboardEvent" and arg["Key"] == "ESCAPE":
96
            self.finish()
97
        elif arg["Type"] == "SoLocation2Event":
98
            self.point, ctrlPoint, info = gui_tool_utils.getPoint(self, arg)
99
            gui_tool_utils.redraw3DView()
100
        elif (arg["Type"] == "SoMouseButtonEvent"
101
              and arg["State"] == "DOWN"
102
              and arg["Button"] == "BUTTON1"):
103
            if arg["Position"] == self.pos:
104
                self.finish(cont=None)
105
                return
106
            if (not self.node) and (not self.support):
107
                gui_tool_utils.getSupport(arg)
108
                (self.point,
109
                 ctrlPoint, info) = gui_tool_utils.getPoint(self, arg)
110
            if self.point:
111
                self.ui.redraw()
112
                self.pos = arg["Position"]
113
                self.node.append(self.point)
114
                self.drawSegment(self.point)
115
                if not self.isWire and len(self.node) == 2:
116
                    self.finish(cont=None, closed=False)
117
                if len(self.node) > 2:
118
                    # The wire is closed
119
                    if (self.point - self.node[0]).Length < utils.tolerance():
120
                        self.undolast()
121
                        if len(self.node) > 2:
122
                            self.finish(cont=None, closed=True)
123
                        else:
124
                            self.finish(cont=None, closed=False)
125

126
    def finish(self, cont=False, closed=False):
127
        """Terminate the operation and close the polyline if asked.
128

129
        Parameters
130
        ----------
131
        cont: bool or None, optional
132
            Restart (continue) the command if `True`, or if `None` and
133
            `ui.continueMode` is `True`.
134
        closed: bool, optional
135
            Close the line if `True`.
136
        """
137
        self.end_callbacks(self.call)
138
        self.removeTemporaryObject()
139

140
        if len(self.node) > 1:
141
            Gui.addModule("Draft")
142
            # The command to run is built as a series of text strings
143
            # to be committed through the `draftutils.todo.ToDo` class.
144
            if (len(self.node) == 2
145
                    and params.get_param("UsePartPrimitives")):
146
                # Insert a Part::Primitive object
147
                p1 = self.node[0]
148
                p2 = self.node[-1]
149

150
                _cmd = 'FreeCAD.ActiveDocument.'
151
                _cmd += 'addObject("Part::Line", "Line")'
152
                _cmd_list = ['line = ' + _cmd,
153
                             'line.X1 = ' + str(p1.x),
154
                             'line.Y1 = ' + str(p1.y),
155
                             'line.Z1 = ' + str(p1.z),
156
                             'line.X2 = ' + str(p2.x),
157
                             'line.Y2 = ' + str(p2.y),
158
                             'line.Z2 = ' + str(p2.z),
159
                             'Draft.autogroup(line)',
160
                             'Draft.select(line)',
161
                             'FreeCAD.ActiveDocument.recompute()']
162
                self.commit(translate("draft", "Create Line"),
163
                            _cmd_list)
164
            else:
165
                # Insert a Draft line
166
                rot, sup, pts, fil = self.getStrings()
167

168
                _base = DraftVecUtils.toString(self.node[0])
169
                _cmd = 'Draft.make_wire'
170
                _cmd += '('
171
                _cmd += 'points, '
172
                _cmd += 'placement=pl, '
173
                _cmd += 'closed=' + str(closed) + ', '
174
                _cmd += 'face=' + fil + ', '
175
                _cmd += 'support=' + sup
176
                _cmd += ')'
177
                _cmd_list = ['pl = FreeCAD.Placement()',
178
                             'pl.Rotation.Q = ' + rot,
179
                             'pl.Base = ' + _base,
180
                             'points = ' + pts,
181
                             'line = ' + _cmd,
182
                             'Draft.autogroup(line)',
183
                             'FreeCAD.ActiveDocument.recompute()']
184
                self.commit(translate("draft", "Create Wire"),
185
                            _cmd_list)
186
        super().finish()
187
        if cont or (cont is None and self.ui and self.ui.continueMode):
188
            self.Activated()
189

190
    def removeTemporaryObject(self):
191
        """Remove temporary object created."""
192
        if self.obj:
193
            try:
194
                old = self.obj.Name
195
            except ReferenceError:
196
                # object already deleted, for some reason
197
                pass
198
            else:
199
                todo.ToDo.delay(self.doc.removeObject, old)
200
        self.obj = None
201

202
    def undolast(self):
203
        """Undoes last line segment."""
204
        import Part
205
        if len(self.node) > 1:
206
            self.node.pop()
207
            # last = self.node[-1]
208
            if self.obj.Shape.Edges:
209
                edges = self.obj.Shape.Edges
210
                if len(edges) > 1:
211
                    newshape = Part.makePolygon(self.node)
212
                    self.obj.Shape = newshape
213
                else:
214
                    self.obj.ViewObject.hide()
215
                # DNC: report on removal
216
                # _toolmsg(translate("draft", "Removing last point"))
217
                _toolmsg(translate("draft", "Pick next point"))
218

219
    def drawSegment(self, point):
220
        """Draws new line segment."""
221
        import Part
222
        if self.planetrack and self.node:
223
            self.planetrack.set(self.node[-1])
224
        if len(self.node) == 1:
225
            _toolmsg(translate("draft", "Pick next point"))
226
        elif len(self.node) == 2:
227
            last = self.node[len(self.node) - 2]
228
            newseg = Part.LineSegment(last, point).toShape()
229
            self.obj.Shape = newseg
230
            self.obj.ViewObject.Visibility = True
231
            if self.isWire:
232
                _toolmsg(translate("draft", "Pick next point"))
233
        else:
234
            currentshape = self.obj.Shape.copy()
235
            last = self.node[len(self.node) - 2]
236
            if not DraftVecUtils.equals(last, point):
237
                newseg = Part.LineSegment(last, point).toShape()
238
                newshape = currentshape.fuse(newseg)
239
                self.obj.Shape = newshape
240
            _toolmsg(translate("draft", "Pick next point"))
241

242
    def wipe(self):
243
        """Remove all previous segments and starts from last point."""
244
        if len(self.node) > 1:
245
            # self.obj.Shape.nullify()  # For some reason this fails
246
            self.obj.ViewObject.Visibility = False
247
            self.node = [self.node[-1]]
248
            if self.planetrack:
249
                self.planetrack.set(self.node[0])
250
            _toolmsg(translate("draft", "Pick next point"))
251

252
    def orientWP(self):
253
        """Orient the working plane."""
254
        if len(self.node) > 1 and self.obj:
255
            import DraftGeomUtils
256
            n = DraftGeomUtils.getNormal(self.obj.Shape)
257
            if not n:
258
                n = self.wp.axis
259
            p = self.node[-1]
260
            v = self.node[-1].sub(self.node[-2])
261
            self.wp.align_to_point_and_axis(p, n, upvec=v, _hist_add=False)
262
            if self.planetrack:
263
                self.planetrack.set(self.node[-1])
264

265
    def numericInput(self, numx, numy, numz):
266
        """Validate the entry fields in the user interface.
267

268
        This function is called by the toolbar or taskpanel interface
269
        when valid x, y, and z have been entered in the input fields.
270
        """
271
        self.point = App.Vector(numx, numy, numz)
272
        self.node.append(self.point)
273
        self.drawSegment(self.point)
274
        if not self.isWire and len(self.node) == 2:
275
            self.finish(cont=None, closed=False)
276
        self.ui.setNextFocus()
277

278

279
Gui.addCommand('Draft_Line', Line())
280

281

282
class Wire(Line):
283
    """Gui command for the Wire or Polyline tool.
284

285
    It inherits the `Line` class, and calls essentially the same code,
286
    only this time the `wiremode` is set to `True`,
287
    so we are allowed to place more than two points.
288
    """
289

290
    def __init__(self):
291
        super().__init__(wiremode=True)
292

293
    def GetResources(self):
294
        """Set icon, menu and tooltip."""
295

296
        return {'Pixmap': 'Draft_Wire',
297
                'Accel': "P, L",
298
                'MenuText': QT_TRANSLATE_NOOP("Draft_Wire", "Polyline"),
299
                'ToolTip': QT_TRANSLATE_NOOP("Draft_Wire", "Creates a multiple-points line (polyline). CTRL to snap, SHIFT to constrain.")}
300

301
    def Activated(self):
302
        """Execute when the command is called."""
303
        import Part
304

305
        # If there is a selection, and this selection contains various
306
        # two-point lines, their shapes are extracted, and we attempt
307
        # to join them into a single Wire (polyline),
308
        # then the old lines are removed.
309
        if len(Gui.Selection.getSelection()) > 1:
310
            edges = []
311
            for o in Gui.Selection.getSelection():
312
                if utils.get_type(o) != "Wire":
313
                    edges = []
314
                    break
315
                edges.extend(o.Shape.Edges)
316
            if edges:
317
                try:
318
                    w = Part.Wire(Part.__sortEdges__(edges))
319
                except Exception:
320
                    _err(translate("draft",
321
                                   "Unable to create a Wire "
322
                                   "from selected objects"))
323
                else:
324
                    # Points of the new fused Wire in string form
325
                    # 'FreeCAD.Vector(x,y,z), FreeCAD.Vector(x1,y1,z1), ...'
326
                    pts = ", ".join([str(v.Point) for v in w.Vertexes])
327
                    pts = pts.replace("Vector ", "FreeCAD.Vector")
328

329
                    # List of commands to remove the old objects
330
                    rems = list()
331
                    for o in Gui.Selection.getSelection():
332
                        rems.append('FreeCAD.ActiveDocument.'
333
                                    'removeObject("' + o.Name + '")')
334

335
                    Gui.addModule("Draft")
336
                    # The command to run is built as a series of text strings
337
                    # to be committed through the `draftutils.todo.ToDo` class
338
                    _cmd = 'wire = Draft.make_wire('
339
                    _cmd += '[' + pts + '], closed=' + str(w.isClosed())
340
                    _cmd += ')'
341
                    _cmd_list = [_cmd]
342
                    _cmd_list.extend(rems)
343
                    _cmd_list.append('Draft.autogroup(wire)')
344
                    _cmd_list.append('FreeCAD.ActiveDocument.recompute()')
345

346
                    _op_name = translate("draft", "Convert to Wire")
347
                    todo.ToDo.delayCommit([(_op_name, _cmd_list)])
348
                    return
349

350
        # If there was no selection or the selection was just one object
351
        # then we proceed with the normal line creation functions,
352
        # only this time we will be able to input more than two points
353
        super().Activated(name="Polyline",
354
                          icon="Draft_Wire",
355
                          task_title=translate("draft", "Polyline"))
356

357

358
Gui.addCommand('Draft_Wire', Wire())
359

360
## @}
361

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

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

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

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