1
# ***************************************************************************
2
# * Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net> *
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. *
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. *
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 *
20
# ***************************************************************************
21
"""Initialization of the Draft workbench (graphical interface)."""
28
__title__ = "FreeCAD Draft Workbench - Init file"
29
__author__ = "Yorik van Havre <yorik@uncreated.net>"
30
__url__ = "https://www.freecad.org"
33
class DraftWorkbench(FreeCADGui.Workbench):
34
"""The Draft Workbench definition."""
37
def QT_TRANSLATE_NOOP(context, text):
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__,
45
self.__class__.MenuText = QT_TRANSLATE_NOOP("draft", "Draft")
46
self.__class__.ToolTip = QT_TRANSLATE_NOOP("draft", _tooltip)
49
"""When the workbench is first loaded."""
51
def QT_TRANSLATE_NOOP(context, text):
55
dependencies_OK = False
58
if FreeCADGui.getSoDBVersion() != coin.SoDB.getVersion():
59
raise AssertionError("FreeCAD and Pivy use different versions "
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 "
68
FreeCAD.Console.PrintWarning("Error: Pivy not found, "
69
"Draft Workbench will be disabled.\n")
71
FreeCAD.Console.PrintWarning("Error: Unknown error "
72
"while trying to load Pivy.\n")
74
dependencies_OK = True
76
if not dependencies_OK:
79
# Import Draft tools, icons
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")
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()
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())
120
[QT_TRANSLATE_NOOP("Workbench", "&Drafting")],
121
self.drawing_commands)
123
[QT_TRANSLATE_NOOP("Workbench", "&Annotation")],
124
self.annotation_commands)
126
[QT_TRANSLATE_NOOP("Workbench", "&Modification")],
127
self.modification_commands)
129
[QT_TRANSLATE_NOOP("Workbench", "&Utilities")],
130
self.utility_commands_menu)
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
144
FreeCADGui.getMainWindow().mainWindowClosed.connect(self.Deactivated)
146
FreeCAD.Console.PrintLog('Loading Draft workbench, done.\n')
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()
157
WorkingPlane._view_observer_start() # Updates the draftToolBar when switching views.
158
FreeCAD.Console.PrintLog("Draft workbench activated.\n")
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()
169
WorkingPlane._view_observer_stop()
170
FreeCAD.Console.PrintLog("Draft workbench deactivated.\n")
172
def ContextMenu(self, recipient):
173
"""Define an optional custom context menu."""
175
for o in FreeCADGui.Selection.getCompleteSelection():
176
if hasattr(o.Object, "Text"):
182
from draftguitools import gui_hyperlink
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"])
188
self.appendContextMenu("Utilities", self.context_commands)
190
def GetClassName(self):
191
"""Type of workbench."""
192
return "Gui::PythonWorkbench"
195
FreeCADGui.addWorkbench(DraftWorkbench)
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
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"))
206
FreeCAD.__unit_test__ += ["TestDraftGui"]