FreeCAD

Форк
0
/
InitGui.py 
206 строк · 9.5 Кб
1
# ***************************************************************************
2
# *   Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net>              *
3
# *                                                                         *
4
# *   This program is free software; you can redistribute it and/or modify  *
5
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
6
# *   as published by the Free Software Foundation; either version 2 of     *
7
# *   the License, or (at your option) any later version.                   *
8
# *   for detail see the LICENCE text file.                                 *
9
# *                                                                         *
10
# *   This program is distributed in the hope that it will be useful,       *
11
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
# *   GNU Library General Public License for more details.                  *
14
# *                                                                         *
15
# *   You should have received a copy of the GNU Library General Public     *
16
# *   License along with this program; if not, write to the Free Software   *
17
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
18
# *   USA                                                                   *
19
# *                                                                         *
20
# ***************************************************************************
21
"""Initialization of the Draft workbench (graphical interface)."""
22

23
import os
24

25
import FreeCAD
26
import FreeCADGui
27

28
__title__ = "FreeCAD Draft Workbench - Init file"
29
__author__ = "Yorik van Havre <yorik@uncreated.net>"
30
__url__ = "https://www.freecad.org"
31

32

33
class DraftWorkbench(FreeCADGui.Workbench):
34
    """The Draft Workbench definition."""
35

36
    def __init__(self):
37
        def QT_TRANSLATE_NOOP(context, text):
38
            return text
39

40
        __dirname__ = os.path.join(FreeCAD.getResourceDir(), "Mod", "Draft")
41
        _tooltip = "The Draft workbench is used for 2D drafting on a grid"
42
        self.__class__.Icon = os.path.join(__dirname__,
43
                                           "Resources", "icons",
44
                                           "DraftWorkbench.svg")
45
        self.__class__.MenuText = QT_TRANSLATE_NOOP("draft", "Draft")
46
        self.__class__.ToolTip = QT_TRANSLATE_NOOP("draft", _tooltip)
47

48
    def Initialize(self):
49
        """When the workbench is first loaded."""
50

51
        def QT_TRANSLATE_NOOP(context, text):
52
            return text
53

54
        # Run self-tests
55
        dependencies_OK = False
56
        try:
57
            from pivy import coin
58
            if FreeCADGui.getSoDBVersion() != coin.SoDB.getVersion():
59
                raise AssertionError("FreeCAD and Pivy use different versions "
60
                                     "of Coin. "
61
                                     "This will lead to unexpected behaviour.")
62
        except AssertionError:
63
            FreeCAD.Console.PrintWarning("Error: FreeCAD and Pivy "
64
                                         "use different versions of Coin. "
65
                                         "This will lead to unexpected "
66
                                         "behaviour.\n")
67
        except ImportError:
68
            FreeCAD.Console.PrintWarning("Error: Pivy not found, "
69
                                         "Draft Workbench will be disabled.\n")
70
        except Exception:
71
            FreeCAD.Console.PrintWarning("Error: Unknown error "
72
                                         "while trying to load Pivy.\n")
73
        else:
74
            dependencies_OK = True
75

76
        if not dependencies_OK:
77
            return
78

79
        # Import Draft tools, icons
80
        try:
81
            import Draft_rc
82
            import DraftTools
83
            import DraftGui
84
            FreeCADGui.addLanguagePath(":/translations")
85
            FreeCADGui.addIconPath(":/icons")
86
        except Exception as exc:
87
            FreeCAD.Console.PrintError(exc)
88
            FreeCAD.Console.PrintError("Error: Initializing one or more "
89
                                       "of the Draft modules failed, "
90
                                       "Draft will not work as expected.\n")
91

92
        # Set up command lists
93
        import draftutils.init_tools as it
94
        self.drawing_commands = it.get_draft_drawing_commands()
95
        self.annotation_commands = it.get_draft_annotation_commands()
96
        self.modification_commands = it.get_draft_modification_commands()
97
        self.utility_commands_menu = it.get_draft_utility_commands_menu()
98
        self.utility_commands_toolbar = it.get_draft_utility_commands_toolbar()
99
        self.context_commands = it.get_draft_context_commands()
100

101
        # Set up toolbars
102
        it.init_toolbar(self,
103
                        QT_TRANSLATE_NOOP("Workbench", "Draft creation tools"),
104
                        self.drawing_commands)
105
        it.init_toolbar(self,
106
                        QT_TRANSLATE_NOOP("Workbench", "Draft annotation tools"),
107
                        self.annotation_commands)
108
        it.init_toolbar(self,
109
                        QT_TRANSLATE_NOOP("Workbench", "Draft modification tools"),
110
                        self.modification_commands)
111
        it.init_toolbar(self,
112
                        QT_TRANSLATE_NOOP("Workbench", "Draft utility tools"),
113
                        self.utility_commands_toolbar)
114
        it.init_toolbar(self,
115
                        QT_TRANSLATE_NOOP("Workbench", "Draft snap"),
116
                        it.get_draft_snap_commands())
117

118
        # Set up menus
119
        it.init_menu(self,
120
                     [QT_TRANSLATE_NOOP("Workbench", "&Drafting")],
121
                     self.drawing_commands)
122
        it.init_menu(self,
123
                     [QT_TRANSLATE_NOOP("Workbench", "&Annotation")],
124
                     self.annotation_commands)
125
        it.init_menu(self,
126
                     [QT_TRANSLATE_NOOP("Workbench", "&Modification")],
127
                     self.modification_commands)
128
        it.init_menu(self,
129
                     [QT_TRANSLATE_NOOP("Workbench", "&Utilities")],
130
                     self.utility_commands_menu)
131

132
        # Set up preferences pages
133
        if hasattr(FreeCADGui, "draftToolBar"):
134
            if not hasattr(FreeCADGui.draftToolBar, "loadedPreferences"):
135
                from draftutils import params
136
                params._param_observer_start()
137
                FreeCADGui.addPreferencePage(":/ui/preferences-draft.ui", QT_TRANSLATE_NOOP("QObject", "Draft"))
138
                FreeCADGui.addPreferencePage(":/ui/preferences-draftinterface.ui", QT_TRANSLATE_NOOP("QObject", "Draft"))
139
                FreeCADGui.addPreferencePage(":/ui/preferences-draftsnap.ui", QT_TRANSLATE_NOOP("QObject", "Draft"))
140
                FreeCADGui.addPreferencePage(":/ui/preferences-draftvisual.ui", QT_TRANSLATE_NOOP("QObject", "Draft"))
141
                FreeCADGui.addPreferencePage(":/ui/preferences-drafttexts.ui", QT_TRANSLATE_NOOP("QObject", "Draft"))
142
                FreeCADGui.draftToolBar.loadedPreferences = True
143

144
        FreeCADGui.getMainWindow().mainWindowClosed.connect(self.Deactivated)
145

146
        FreeCAD.Console.PrintLog('Loading Draft workbench, done.\n')
147

148
    def Activated(self):
149
        """When entering the workbench."""
150
        if hasattr(FreeCADGui, "draftToolBar"):
151
            FreeCADGui.draftToolBar.Activated()
152
        if hasattr(FreeCADGui, "Snapper"):
153
            FreeCADGui.Snapper.show()
154
            import draftutils.init_draft_statusbar as dsb
155
            dsb.show_draft_statusbar()
156
        import WorkingPlane
157
        WorkingPlane._view_observer_start()  # Updates the draftToolBar when switching views.
158
        FreeCAD.Console.PrintLog("Draft workbench activated.\n")
159

160
    def Deactivated(self):
161
        """When quitting the workbench."""
162
        if hasattr(FreeCADGui, "draftToolBar"):
163
            FreeCADGui.draftToolBar.Deactivated()
164
        if hasattr(FreeCADGui, "Snapper"):
165
            FreeCADGui.Snapper.hide()
166
            import draftutils.init_draft_statusbar as dsb
167
            dsb.hide_draft_statusbar()
168
        import WorkingPlane
169
        WorkingPlane._view_observer_stop()
170
        FreeCAD.Console.PrintLog("Draft workbench deactivated.\n")
171

172
    def ContextMenu(self, recipient):
173
        """Define an optional custom context menu."""
174
        has_text = False
175
        for o in FreeCADGui.Selection.getCompleteSelection():
176
            if hasattr(o.Object, "Text"):
177
                has_text = True
178
                break
179

180
        if has_text:
181
            import sys
182
            from draftguitools import gui_hyperlink
183

184
            hyperlinks_search = gui_hyperlink.Draft_Hyperlink()
185
            if hyperlinks_search.has_hyperlinks() and sys.platform in ["win32", "cygwin", "darwin", "linux"]:
186
                self.appendContextMenu("", ["Draft_Hyperlink"])
187

188
        self.appendContextMenu("Utilities", self.context_commands)
189

190
    def GetClassName(self):
191
        """Type of workbench."""
192
        return "Gui::PythonWorkbench"
193

194

195
FreeCADGui.addWorkbench(DraftWorkbench)
196

197
# Preference pages for importing and exporting various file formats
198
# are independent of the loading of the workbench and can be loaded at startup
199
import Draft_rc
200
from PySide.QtCore import QT_TRANSLATE_NOOP
201
FreeCADGui.addPreferencePage(":/ui/preferences-dxf.ui", QT_TRANSLATE_NOOP("QObject", "Import-Export"))
202
FreeCADGui.addPreferencePage(":/ui/preferences-dwg.ui", QT_TRANSLATE_NOOP("QObject", "Import-Export"))
203
FreeCADGui.addPreferencePage(":/ui/preferences-svg.ui", QT_TRANSLATE_NOOP("QObject", "Import-Export"))
204
FreeCADGui.addPreferencePage(":/ui/preferences-oca.ui", QT_TRANSLATE_NOOP("QObject", "Import-Export"))
205

206
FreeCAD.__unit_test__ += ["TestDraftGui"]
207

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

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

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

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