-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
FreeCADGuiInit.py
210 lines (184 loc) · 7.85 KB
/
FreeCADGuiInit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# FreeCAD gui init module
# (c) 2003 Juergen Riegel
#
# Gathering all the information to start FreeCAD
# This is the second one of three init scripts, the third one
# runs when the gui is up
#***************************************************************************
#* (c) Juergen Riegel (juergen.riegel@web.de) 2002 *
#* *
#* This file is part of the FreeCAD CAx development system. *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* FreeCAD is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Lesser General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with FreeCAD; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#* Juergen Riegel 2002 *
#***************************************************************************/
# imports the one and only
import FreeCAD, FreeCADGui
# shortcuts
Gui = FreeCADGui
# Important definitions
class Workbench:
"""The workbench base class."""
MenuText = ""
ToolTip = ""
def Initialize(self):
"""Initializes this workbench."""
App.PrintWarning(str(self) + ": Workbench.Initialize() not implemented in subclass!")
def ContextMenu(self, recipient):
pass
def appendToolbar(self,name,cmds):
self.__Workbench__.appendToolbar(name, cmds)
def removeToolbar(self,name):
self.__Workbench__.removeToolbar(name)
def appendCommandbar(self,name,cmds):
self.__Workbench__.appendCommandbar(name, cmds)
def removeCommandbar(self,name):
self.__Workbench__.removeCommandbar(name)
def appendMenu(self,name,cmds):
self.__Workbench__.appendMenu(name, cmds)
def removeMenu(self,name):
self.__Workbench__.removeMenu(name)
def listMenus(self):
return self.__Workbench__.listMenus()
def appendContextMenu(self,name,cmds):
self.__Workbench__.appendContextMenu(name, cmds)
def removeContextMenu(self,name):
self.__Workbench__.removeContextMenu(name)
def name(self):
return self.__Workbench__.name()
def GetClassName(self):
"""Return the name of the associated C++ class."""
# as default use this to simplify writing workbenches in Python
return "Gui::PythonWorkbench"
class StandardWorkbench ( Workbench ):
"""A workbench defines the tool bars, command bars, menus,
context menu and dockable windows of the main window.
"""
def Initialize(self):
"""Initialize this workbench."""
# load the module
Log ('Init: Loading FreeCAD GUI\n')
def GetClassName(self):
"""Return the name of the associated C++ class."""
return "Gui::StdWorkbench"
class NoneWorkbench ( Workbench ):
"""An empty workbench."""
MenuText = "<none>"
ToolTip = "The default empty workbench"
def Initialize(self):
"""Initialize this workbench."""
# load the module
Log ('Init: Loading FreeCAD GUI\n')
def GetClassName(self):
"""Return the name of the associated C++ class."""
return "Gui::NoneWorkbench"
def InitApplications():
import sys,os,traceback
try:
# Python3
import io as cStringIO
except ImportError:
# Python2
import cStringIO
# Searching modules dirs +++++++++++++++++++++++++++++++++++++++++++++++++++
# (additional module paths are already cached)
ModDirs = FreeCAD.__ModDirs__
#print ModDirs
Log('Init: Searching modules...\n')
for Dir in ModDirs:
if ((Dir != '') & (Dir != 'CVS') & (Dir != '__init__.py')):
InstallFile = os.path.join(Dir,"InitGui.py")
if (os.path.exists(InstallFile)):
try:
# XXX: This looks scary securitywise...
with open(InstallFile) as f:
exec(f.read())
except Exception as inst:
Log('Init: Initializing ' + Dir + '... failed\n')
Log('-'*100+'\n')
Log(traceback.format_exc())
Log('-'*100+'\n')
Err('During initialization the error ' + str(inst) + ' occurred in ' + InstallFile + '\n')
Err('Please look into the log file for further information\n')
else:
Log('Init: Initializing ' + Dir + '... done\n')
else:
Log('Init: Initializing ' + Dir + '(InitGui.py not found)... ignore\n')
try:
import pkgutil
import importlib
import freecad
freecad.gui = FreeCADGui
for _, freecad_module_name, freecad_module_ispkg in pkgutil.iter_modules(freecad.__path__, "freecad."):
if freecad_module_ispkg:
Log('Init: Initializing ' + freecad_module_name + '\n')
freecad_module = importlib.import_module(freecad_module_name)
if any (module_name == 'init_gui' for _, module_name, ispkg in pkgutil.iter_modules(freecad_module.__path__)):
try:
importlib.import_module(freecad_module_name + '.init_gui')
Log('Init: Initializing ' + freecad_module_name + '... done\n')
except Exception as inst:
Err('During initialization the error ' + str(inst) + ' occurred in ' + freecad_module_name + '\n')
Err('-'*80+'\n')
Err(traceback.format_exc())
Err('-'*80+'\n')
Log('Init: Initializing ' + freecad_module_name + '... failed\n')
Log('-'*80+'\n')
Log(traceback.format_exc())
Log('-'*80+'\n')
else:
Log('Init: No init_gui module found in ' + freecad_module_name + ', skipping\n')
except ImportError as inst:
Err('During initialization the error ' + str(inst) + ' occurred\n')
Log ('Init: Running FreeCADGuiInit.py start script...\n')
# init the gui
# signal that the gui is up
App.GuiUp = 1
App.Gui = FreeCADGui
FreeCADGui.Workbench = Workbench
Gui.addWorkbench(NoneWorkbench())
# Monkey patching pivy.coin.SoGroup.removeAllChildren to work around a bug
# https://bitbucket.org/Coin3D/coin/pull-requests/119/fix-sochildlist-auditing/diff
def _SoGroup_init(self,*args):
import types
_SoGroup_init_orig(self,*args)
self.removeAllChildren = \
types.MethodType(FreeCADGui.coinRemoveAllChildren,self)
try:
from pivy import coin
_SoGroup_init_orig = coin.SoGroup.__init__
coin.SoGroup.__init__ = _SoGroup_init
except Exception:
pass
# init modules
InitApplications()
# set standard workbench (needed as fallback)
Gui.activateWorkbench("NoneWorkbench")
# Register .py, .FCScript and .FCMacro
FreeCAD.addImportType("Inventor V2.1 (*.iv)","FreeCADGui")
FreeCAD.addImportType("VRML V2.0 (*.wrl *.vrml *.wrz *.wrl.gz)","FreeCADGui")
FreeCAD.addImportType("Python (*.py *.FCMacro *.FCScript)","FreeCADGui")
FreeCAD.addExportType("Inventor V2.1 (*.iv)","FreeCADGui")
FreeCAD.addExportType("VRML V2.0 (*.wrl *.vrml *.wrz *.wrl.gz)","FreeCADGui")
#FreeCAD.addExportType("IDTF (for 3D PDF) (*.idtf)","FreeCADGui")
#FreeCAD.addExportType("3D View (*.svg)","FreeCADGui")
FreeCAD.addExportType("Portable Document Format (*.pdf)","FreeCADGui")
del(InitApplications)
del(NoneWorkbench)
del(StandardWorkbench)
Log ('Init: Running FreeCADGuiInit.py start script... done\n')