FreeCAD

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

27
The highlighting can be used to manipulate shapes with other tools
28
such 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
# @{
36
import pivy.coin as coin
37
from PySide.QtCore import QT_TRANSLATE_NOOP
38

39
import FreeCADGui as Gui
40
import draftguitools.gui_base_original as gui_base_original
41
import draftguitools.gui_tool_utils as gui_tool_utils
42

43
from draftutils.messages import _msg
44
from draftutils.translate import translate
45

46

47
class SubelementHighlight(gui_base_original.Modifier):
48
    """Gui Command for the SubelementHighlight tool."""
49

50
    def __init__(self):
51
        super().__init__()
52
        self.is_running = False
53
        self.editable_objects = []
54
        self.original_view_settings = {}
55

56
    def GetResources(self):
57
        """Set icon, menu and tooltip."""
58

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

64
    def Activated(self):
65
        """Execute when the command is called."""
66
        if self.is_running:
67
            return self.finish()
68
        self.is_running = True
69
        super().Activated(name="Subelement highlight")
70
        self.get_selection()
71

72
    def proceed(self):
73
        """Continue with the command."""
74
        if self.call:
75
            self.view.removeEventCallback("SoEvent", self.call)
76
        self.get_editable_objects_from_selection()
77
        if not self.editable_objects:
78
            return self.finish()
79
        self.call = self.view.addEventCallback("SoEvent", self.action)
80
        self.highlight_editable_objects()
81

82
    def finish(self):
83
        """Terminate the operation.
84

85
        Re-initialize by running __init__ again at the end.
86
        """
87
        self.end_callbacks(self.call)
88
        self.restore_editable_objects_graphics()
89
        super().finish()
90
        self.__init__()
91

92
    def action(self, event):
93
        """Handle the 3D scene events.
94

95
        This is installed as an EventCallback in the Inventor view.
96

97
        Parameters
98
        ----------
99
        event: dict
100
            Dictionary with strings that indicates the type of event received
101
            from the 3D view.
102
        """
103
        if event["Type"] == "SoKeyboardEvent" and event["Key"] == "ESCAPE":
104
            self.finish()
105

106
    def get_selection(self):
107
        """Get the selection."""
108
        if not Gui.Selection.getSelection() and self.ui:
109
            _msg(translate("draft", "Select an object to edit"))
110
            self.call = self.view.addEventCallback("SoEvent",
111
                                                   gui_tool_utils.selectObject)
112
        else:
113
            self.proceed()
114

115
    def get_editable_objects_from_selection(self):
116
        """Get editable Draft objects for the selection."""
117
        for obj in Gui.Selection.getSelection():
118
            if obj.isDerivedFrom("Part::Part2DObject"):
119
                self.editable_objects.append(obj)
120
            elif (hasattr(obj, "Base")
121
                  and obj.Base.isDerivedFrom("Part::Part2DObject")):
122
                self.editable_objects.append(obj.Base)
123

124
    def highlight_editable_objects(self):
125
        """Highlight editable Draft objects from the selection."""
126
        for obj in self.editable_objects:
127
            self.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}
132
            obj.ViewObject.Visibility = True
133
            obj.ViewObject.PointSize = 10
134
            obj.ViewObject.PointColor = (1.0, 0.0, 0.0)
135
            obj.ViewObject.LineColor = (1.0, 0.0, 0.0)
136
            xray = coin.SoAnnotation()
137
            if obj.ViewObject.RootNode.getNumChildren() > 2:
138
                xray.addChild(obj.ViewObject.RootNode.getChild(2).getChild(0))
139
                xray.setName("xray")
140
                obj.ViewObject.RootNode.addChild(xray)
141

142
    def restore_editable_objects_graphics(self):
143
        """Restore the editable objects' appearance."""
144
        for obj in self.editable_objects:
145
            try:
146
                for attribute, value in self.original_view_settings[obj.Name].items():
147
                    vobj = obj.ViewObject
148
                    setattr(vobj, attribute, value)
149
                    vobj.RootNode.removeChild(vobj.RootNode.getByName("xray"))
150
            except Exception:
151
                # This can occur if objects have had graph changing operations
152
                pass
153

154

155
Gui.addCommand('Draft_SubelementHighlight', SubelementHighlight())
156

157
## @}
158

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

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

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

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