FreeCAD
207 строк · 8.7 Кб
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 Rectangle objects."""
26## @package gui_rectangles
27# \ingroup draftguitools
28# \brief Provides GUI tools to create Rectangle objects.
29
30## \addtogroup draftguitools
31# @{
32from PySide.QtCore import QT_TRANSLATE_NOOP
33
34import FreeCAD as App
35import FreeCADGui as Gui
36import DraftVecUtils
37from draftguitools import gui_base_original
38from draftguitools import gui_tool_utils
39from draftguitools import gui_trackers as trackers
40from draftutils import params
41from draftutils import utils
42from draftutils.messages import _err, _toolmsg
43from draftutils.translate import translate
44
45
46class Rectangle(gui_base_original.Creator):
47"""Gui command for the Rectangle tool."""
48
49def GetResources(self):
50"""Set icon, menu and tooltip."""
51
52return {'Pixmap': 'Draft_Rectangle',
53'Accel': "R, E",
54'MenuText': QT_TRANSLATE_NOOP("Draft_Rectangle", "Rectangle"),
55'ToolTip': QT_TRANSLATE_NOOP("Draft_Rectangle", "Creates a 2-point rectangle. CTRL to snap.")}
56
57def Activated(self):
58"""Execute when the command is called."""
59super().Activated(name="Rectangle")
60if self.ui:
61self.refpoint = None
62self.ui.pointUi(title=translate("draft", "Rectangle"), icon="Draft_Rectangle")
63self.ui.extUi()
64self.call = self.view.addEventCallback("SoEvent", self.action)
65self.rect = trackers.rectangleTracker()
66_toolmsg(translate("draft", "Pick first point"))
67
68def finish(self, cont=False):
69"""Terminate the operation.
70
71Parameters
72----------
73cont: bool or None, optional
74Restart (continue) the command if `True`, or if `None` and
75`ui.continueMode` is `True`.
76"""
77self.end_callbacks(self.call)
78if self.ui:
79self.rect.off()
80self.rect.finalize()
81super().finish()
82if cont or (cont is None and self.ui and self.ui.continueMode):
83self.Activated()
84
85def createObject(self):
86"""Create the final object in the current document."""
87p1 = self.node[0]
88p3 = self.node[-1]
89diagonal = p3.sub(p1)
90p2 = p1.add(DraftVecUtils.project(diagonal, self.wp.v))
91p4 = p1.add(DraftVecUtils.project(diagonal, self.wp.u))
92length = p4.sub(p1).Length
93if abs(DraftVecUtils.angle(p4.sub(p1), self.wp.u, self.wp.axis)) > 1:
94length = -length
95height = p2.sub(p1).Length
96if abs(DraftVecUtils.angle(p2.sub(p1), self.wp.v, self.wp.axis)) > 1:
97height = -height
98try:
99# The command to run is built as a series of text strings
100# to be committed through the `draftutils.todo.ToDo` class.
101rot, sup, pts, fil = self.getStrings()
102base = p1
103if length < 0:
104length = -length
105base = base.add((p1.sub(p4)).negative())
106if height < 0:
107height = -height
108base = base.add((p1.sub(p2)).negative())
109Gui.addModule("Draft")
110if params.get_param("UsePartPrimitives"):
111# Insert a Part::Primitive object
112_cmd = 'FreeCAD.ActiveDocument.'
113_cmd += 'addObject("Part::Plane", "Plane")'
114_cmd_list = ['plane = ' + _cmd,
115'plane.Length = ' + str(length),
116'plane.Width = ' + str(height),
117'pl = FreeCAD.Placement()',
118'pl.Rotation.Q=' + rot,
119'pl.Base = ' + DraftVecUtils.toString(base),
120'plane.Placement = pl',
121'Draft.autogroup(plane)',
122'Draft.select(plane)',
123'FreeCAD.ActiveDocument.recompute()']
124self.commit(translate("draft", "Create Plane"),
125_cmd_list)
126else:
127_cmd = 'Draft.make_rectangle'
128_cmd += '('
129_cmd += 'length=' + str(length) + ', '
130_cmd += 'height=' + str(height) + ', '
131_cmd += 'placement=pl, '
132_cmd += 'face=' + fil + ', '
133_cmd += 'support=' + sup
134_cmd += ')'
135_cmd_list = ['pl = FreeCAD.Placement()',
136'pl.Rotation.Q = ' + rot,
137'pl.Base = ' + DraftVecUtils.toString(base),
138'rec = ' + _cmd,
139'Draft.autogroup(rec)',
140'FreeCAD.ActiveDocument.recompute()']
141self.commit(translate("draft", "Create Rectangle"),
142_cmd_list)
143except Exception:
144_err("Draft: error delaying commit")
145self.finish(cont=None)
146
147def action(self, arg):
148"""Handle the 3D scene events.
149
150This is installed as an EventCallback in the Inventor view.
151
152Parameters
153----------
154arg: dict
155Dictionary with strings that indicates the type of event received
156from the 3D view.
157"""
158if arg["Type"] == "SoKeyboardEvent":
159if arg["Key"] == "ESCAPE":
160self.finish()
161elif arg["Type"] == "SoLocation2Event": # mouse movement detection
162self.point, ctrlPoint, info = gui_tool_utils.getPoint(self, arg, noTracker=True)
163self.rect.update(self.point)
164gui_tool_utils.redraw3DView()
165elif (arg["Type"] == "SoMouseButtonEvent"
166and arg["State"] == "DOWN"
167and arg["Button"] == "BUTTON1"):
168
169if arg["Position"] == self.pos:
170self.finish(cont=None)
171return
172
173if (not self.node) and (not self.support):
174gui_tool_utils.getSupport(arg)
175self.point, ctrlPoint, info = gui_tool_utils.getPoint(self, arg, noTracker=True)
176if self.point:
177self.ui.redraw()
178self.pos = arg["Position"]
179self.appendPoint(self.point)
180
181def numericInput(self, numx, numy, numz):
182"""Validate the entry fields in the user interface.
183
184This function is called by the toolbar or taskpanel interface
185when valid x, y, and z have been entered in the input fields.
186"""
187self.point = App.Vector(numx, numy, numz)
188self.appendPoint(self.point)
189
190def appendPoint(self, point):
191"""Append a point to the list of nodes."""
192self.node.append(point)
193if len(self.node) > 1:
194self.rect.update(point)
195self.createObject()
196else:
197_toolmsg(translate("draft", "Pick opposite point"))
198self.ui.setRelative()
199self.rect.setorigin(point)
200self.rect.on()
201if self.planetrack:
202self.planetrack.set(point)
203
204
205Gui.addCommand('Draft_Rectangle', Rectangle())
206
207## @}
208