FreeCAD
157 строк · 6.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 highlight subelements of objects.
26
27The highlighting can be used to manipulate shapes with other tools
28such as Move, Rotate, and Scale.
29"""
30## @package gui_subelements
31# \ingroup draftguitools
32# \brief Provides GUI tools to highlight subelements of objects.
33
34## \addtogroup draftguitools
35# @{
36import pivy.coin as coin
37from PySide.QtCore import QT_TRANSLATE_NOOP
38
39import FreeCADGui as Gui
40import draftguitools.gui_base_original as gui_base_original
41import draftguitools.gui_tool_utils as gui_tool_utils
42
43from draftutils.messages import _msg
44from draftutils.translate import translate
45
46
47class SubelementHighlight(gui_base_original.Modifier):
48"""Gui Command for the SubelementHighlight tool."""
49
50def __init__(self):
51super().__init__()
52self.is_running = False
53self.editable_objects = []
54self.original_view_settings = {}
55
56def GetResources(self):
57"""Set icon, menu and tooltip."""
58
59return {'Pixmap': 'Draft_SubelementHighlight',
60'Accel': "H, S",
61'MenuText': QT_TRANSLATE_NOOP("Draft_SubelementHighlight","Subelement highlight"),
62'ToolTip': QT_TRANSLATE_NOOP("Draft_SubelementHighlight","Highlight the subelements of the selected objects, so that they can then be edited with the move, rotate, and scale tools.")}
63
64def Activated(self):
65"""Execute when the command is called."""
66if self.is_running:
67return self.finish()
68self.is_running = True
69super().Activated(name="Subelement highlight")
70self.get_selection()
71
72def proceed(self):
73"""Continue with the command."""
74if self.call:
75self.view.removeEventCallback("SoEvent", self.call)
76self.get_editable_objects_from_selection()
77if not self.editable_objects:
78return self.finish()
79self.call = self.view.addEventCallback("SoEvent", self.action)
80self.highlight_editable_objects()
81
82def finish(self):
83"""Terminate the operation.
84
85Re-initialize by running __init__ again at the end.
86"""
87self.end_callbacks(self.call)
88self.restore_editable_objects_graphics()
89super().finish()
90self.__init__()
91
92def action(self, event):
93"""Handle the 3D scene events.
94
95This is installed as an EventCallback in the Inventor view.
96
97Parameters
98----------
99event: dict
100Dictionary with strings that indicates the type of event received
101from the 3D view.
102"""
103if event["Type"] == "SoKeyboardEvent" and event["Key"] == "ESCAPE":
104self.finish()
105
106def get_selection(self):
107"""Get the selection."""
108if not Gui.Selection.getSelection() and self.ui:
109_msg(translate("draft", "Select an object to edit"))
110self.call = self.view.addEventCallback("SoEvent",
111gui_tool_utils.selectObject)
112else:
113self.proceed()
114
115def get_editable_objects_from_selection(self):
116"""Get editable Draft objects for the selection."""
117for obj in Gui.Selection.getSelection():
118if obj.isDerivedFrom("Part::Part2DObject"):
119self.editable_objects.append(obj)
120elif (hasattr(obj, "Base")
121and obj.Base.isDerivedFrom("Part::Part2DObject")):
122self.editable_objects.append(obj.Base)
123
124def highlight_editable_objects(self):
125"""Highlight editable Draft objects from the selection."""
126for obj in self.editable_objects:
127self.original_view_settings[obj.Name] = {
128'Visibility': obj.ViewObject.Visibility,
129'PointSize': obj.ViewObject.PointSize,
130'PointColor': obj.ViewObject.PointColor,
131'LineColor': obj.ViewObject.LineColor}
132obj.ViewObject.Visibility = True
133obj.ViewObject.PointSize = 10
134obj.ViewObject.PointColor = (1.0, 0.0, 0.0)
135obj.ViewObject.LineColor = (1.0, 0.0, 0.0)
136xray = coin.SoAnnotation()
137if obj.ViewObject.RootNode.getNumChildren() > 2:
138xray.addChild(obj.ViewObject.RootNode.getChild(2).getChild(0))
139xray.setName("xray")
140obj.ViewObject.RootNode.addChild(xray)
141
142def restore_editable_objects_graphics(self):
143"""Restore the editable objects' appearance."""
144for obj in self.editable_objects:
145try:
146for attribute, value in self.original_view_settings[obj.Name].items():
147vobj = obj.ViewObject
148setattr(vobj, attribute, value)
149vobj.RootNode.removeChild(vobj.RootNode.getByName("xray"))
150except Exception:
151# This can occur if objects have had graph changing operations
152pass
153
154
155Gui.addCommand('Draft_SubelementHighlight', SubelementHighlight())
156
157## @}
158