FreeCAD
292 строки · 10.4 Кб
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 the base classes for most old Draft Gui Commands.
26
27This class is used by Gui Commands to set up some properties
28of the DraftToolBar, the Snapper, and the working plane.
29"""
30## @package gui_base_original
31# \ingroup draftguitools
32# \brief Provides the base classes for most old Draft Gui Commands.
33
34## \addtogroup draftguitools
35# @{
36
37from PySide import QtCore38
39import FreeCAD as App40import FreeCADGui as Gui41import DraftVecUtils42import WorkingPlane43from draftguitools import gui_tool_utils44from draftguitools import gui_trackers as trackers45from draftutils import gui_utils46from draftutils import params47from draftutils import todo48from draftutils import utils49from draftutils.messages import _log, _toolmsg50
51
52class DraftTool:53"""The base class of all Draft Tools.54
55This is the original class that was defined in `DraftTools.py`
56before any re-organization of the code.
57
58This class is subclassed by `Creator` and `Modifier`
59to set up a few additional properties of these two types.
60
61This class connects with the `draftToolBar` and `Snapper` classes
62that are installed in the `FreeCADGui` namespace in order to set up some
63properties of the running tools such as the task panel, the snapping
64functions, and the grid trackers.
65
66It also connects with the `WorkingPlane` class.
67
68This class is intended to be replaced by newer classes inside the
69`gui_base` module, in particular, `GuiCommandBase`.
70"""
71
72def __init__(self):73self.commitList = []74
75def IsActive(self):76"""Return True when this command should be available.77
78It is `True` when there is a document.
79"""
80if Gui.ActiveDocument:81return True82else:83return False84
85def Activated(self, name="None", is_subtool=False):86"""Execute when the command is called.87
88If an active Gui Command exists, it will call the `finish` method
89of it to terminate it.
90
91If no active Gui Command exists, it will proceed with configuration
92of the tool. The child class that subclasses this class
93then should continue with its own Activated method.
94
95Parameters
96----------
97name: str, optional
98It defaults to `'None'`.
99It is the `featureName` of the object, to know what is being run.
100
101is_subtool: bool, optional
102It defaults to `False`.
103This is set to `True` when we want to modify an object
104by using the mechanism of a `subtool`, introduced
105through the `Draft_SubelementHighlight` command.
106That is, first we run `Draft_SubelementHighlight`
107then we can use `Draft_Move` while setting `is_subtool` to `True`.
108"""
109if App.activeDraftCommand and not is_subtool:110App.activeDraftCommand.finish()111App.activeDraftCommand = self112
113# The Part module is first initialized when using any Gui Command114# for the first time.115global Part, DraftGeomUtils116import Part117import DraftGeomUtils118
119self.call = None120self.commitList = []121self.constrain = None122self.doc = App.ActiveDocument123self.extendedCopy = False124self.featureName = name125self.node = []126self.obj = None127self.point = None128self.pos = []129self.support = None130self.ui = Gui.draftToolBar131self.ui.mouse = True # reset mouse movement132self.ui.sourceCmd = self133self.view = gui_utils.get_3d_view()134self.wp = WorkingPlane.get_working_plane()135
136self.planetrack = None137if params.get_param("showPlaneTracker"):138self.planetrack = trackers.PlaneTracker()139if hasattr(Gui, "Snapper"):140Gui.Snapper.setTrackers()141
142_toolmsg("{}".format(16*"-"))143_toolmsg("GuiCommand: {}".format(self.featureName))144
145def end_callbacks(self, call):146try:147self.view.removeEventCallback("SoEvent", call)148gui_utils.end_all_events()149except RuntimeError:150# the view has been deleted already151pass152call = None153
154def finish(self, cont=False):155"""Finish the current command.156
157These are general cleaning tasks that are performed
158when terminating all commands.
159
160These include setting the node list to empty,
161setting to `None` the active command,
162turning off the graphical interface (task panel),
163finishing the plane tracker, restoring the working plane,
164turning off the snapper.
165
166If a callback is installed in the 3D view, the callback is removed,
167and set to `None`.
168
169If the commit list is non-empty it will commit the instructions on
170the list with `draftutils.todo.ToDo.delayCommit`,
171and the list will be set to empty.
172"""
173self.node = []174App.activeDraftCommand = None175if self.ui:176self.ui.offUi()177self.ui.sourceCmd = None178if hasattr(Gui, "Snapper"):179Gui.Snapper.off()180if self.planetrack:181self.planetrack.finalize()182self.wp._restore()183if self.commitList:184last_cmd = self.commitList[-1][1][-1]185if last_cmd.find("recompute") >= 0:186self.commitList[-1] = (self.commitList[-1][0], self.commitList[-1][1][:-1])187todo.ToDo.delayCommit(self.commitList)188todo.ToDo.delayAfter(Gui.doCommand, last_cmd)189else:190todo.ToDo.delayCommit(self.commitList)191self.commitList = []192
193def commit(self, name, func):194"""Store actions in the commit list to be run later.195
196Parameters
197----------
198name: str
199An arbitrary string that indicates the name of the operation
200to run.
201
202func: list of str
203Each element of the list is a string that will be run by
204`Gui.doCommand`.
205
206See the complete information in the `draftutils.todo.ToDo` class.
207"""
208self.commitList.append((name, func))209
210def getStrings(self, addrot=None):211"""Return useful strings that will be used to build commands.212
213Returns
214-------
215str, str, str, str
216A tuple of four strings that represent useful information.
217
218* the current working plane rotation quaternion as a string
219* the support object if available as a string
220* the list of nodes inside the `node` attribute as a string
221* the string `'True'` or `'False'` depending on the fill mode
222of the current tool
223"""
224# Current plane rotation as a string225qr = self.wp.get_placement().Rotation.Q226qr = "({0}, {1}, {2}, {3})".format(qr[0], qr[1], qr[2], qr[3])227
228# Support object229if self.support and params.get_param("useSupport"):230sup = 'FreeCAD.ActiveDocument.getObject'231sup += '("{}")'.format(self.support.Name)232else:233sup = 'None'234
235# Contents of self.node236points = '['237for n in self.node:238if len(points) > 1:239points += ', '240points += DraftVecUtils.toString(n)241points += ']'242
243# Fill mode244if self.ui:245fil = str(bool(self.ui.fillmode))246else:247fil = "True"248
249return qr, sup, points, fil250
251
252class Creator(DraftTool):253"""A generic Creator tool, used by creation tools such as line or arc.254
255It inherits `DraftTool`, which sets up the majority of the behavior
256of this class.
257"""
258
259def Activated(self, name="None"):260"""Execute when the command is called.261
262Parameters
263----------
264name: str, optional
265It defaults to `'None'`.
266It is the `featureName` of the object, to know what is being run.
267"""
268super().Activated(name)269# call _save to sync with _restore called in finish method270self.wp._save()271self.support = gui_tool_utils.get_support()272
273
274class Modifier(DraftTool):275"""A generic Modifier tool, used by modification tools such as move.276
277After initializing the parent class, it sets the `copymode` attribute
278to `False`.
279
280It inherits `DraftTool`, which sets up the majority of the behavior
281of this class.
282"""
283
284def __init__(self):285super().__init__()286self.copymode = False287
288def Activated(self, name="None", is_subtool=False):289super().Activated(name, is_subtool)290# call _save to sync with _restore called in finish method291self.wp._save()292## @}
293