-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlattice2RecomputeLocker.py
448 lines (375 loc) · 19.4 KB
/
lattice2RecomputeLocker.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#***************************************************************************
#* *
#* Copyright (c) 2015 - Victor Titov (DeepSOIC) *
#* <[email protected]> *
#* *
#* 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. *
#* *
#* This program 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 Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
__title__="Lattice Recompute Locker: hack to manual recompute of documents."
__author__ = "DeepSOIC"
__url__ = ""
import FreeCAD as App
from lattice2Common import *
import lattice2Executer as LE
def touch(obj):
'''some bastard object like Fusion ignore calls to touch() method. This shuold work around that problem.'''
obj.touch()
if hasattr(obj,"Proxy"):
#fixes mystery crash when touching recomputeLocker when it's locked
return
# the workaround is to reassign some properties...
for propname in obj.PropertiesList:
typ = obj.getTypeIdOfProperty(propname)
val = getattr(obj,propname)
if typ == 'App::PropertyLink':
setattr(obj,propname,val)
elif typ == 'App::PropertyLinkSub':
#val is (feature,["Edge1","Face2"])
setattr(obj,propname,val)
elif typ == 'App::PropertyLinkList':
setattr(obj,propname,val)
#elif typ == 'App::PropertyLinkSubList': #disabled due to FreeCAD bug #2602
# setattr(obj,propname,val)
def touchEverything(doc):
touch_count = 0
for obj in doc.Objects:
try:
touch(obj)
touch_count += 1
except:
App.Console.PrintError('Failed to touch object {objname}\n'
.format(objname= obj.Name) )
if touch_count == 0:
raise ValueError("forceRecompute: failed to touch any object!")
def recomputeFeature(featureToRecompute, bUndoable = True):
doc = featureToRecompute.Document
if bUndoable:
doc.openTransaction("Recompute "+featureToRecompute.Name)
if hasattr(featureToRecompute, 'Recomputing'):
if featureToRecompute.Recomputing == 'Disabled': #toposeries, paraseries...
featureToRecompute.Recomputing = 'Recompute Once'
if hasattr(featureToRecompute, "recompute"):
# new FreeCAD! yay!
featureToRecompute.recompute()
elif hasattr(featureToRecompute, "Proxy"):
#Python feature - easy!
featureToRecompute.Proxy.execute(featureToRecompute)
else:
infoMessage("RecomputeFeature","Selected feature is a C++ feature. Recomputing them with this command was temporarily disabled, because it is known to break dependencies. The command will be frozen, till a reliable way of recomputing c++ feature gets exposed.")
return
featureToRecompute.purgeTouched()
for docobj in featureToRecompute.InList:
touch(docobj)
if bUndoable:
doc.commitTransaction()
def makeRecomputeLocker(name):
'''makeRecomputeLocker(name): makes a RecomputeLocker document object.'''
obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython",name)
LatticeRecomputeLocker(obj)
if FreeCAD.GuiUp:
ViewProviderLatticeRecomputeLocker(obj.ViewObject)
return obj
class LatticeRecomputeLocker:
"The LatticeRecomputeLocker object. Mainly used as a mean to stop FreeCAD's automatic recomputes."
def __init__(self,obj):
self.Type = "LatticeRecomputeLocker"
obj.addProperty("App::PropertyLink","LinkToSelf","Lattice RecomputeLocker","Link to self, that breaks the DAG, which causes standard FreeCAD recomputes to fail.")
obj.addProperty("App::PropertyBool","LockRecomputes","Lattice RecomputeLocker", "Set to true to disable automatic recomputes in FreeCAD")
obj.Proxy = self
def onChanged(self, obj, prop): #prop is a string - name of the property
if prop == "LockRecomputes":
if obj.LockRecomputes:
obj.LinkToSelf = obj
else:
obj.LinkToSelf = None
def execute(self, obj):
pass
def RecomputeDocument(self, obj, bUndoable = True):
oldState = obj.LockRecomputes
obj.LockRecomputes = False
doc = obj.Document
if bUndoable:
doc.openTransaction("Recompute document")
doc.recompute()
if bUndoable:
doc.commitTransaction()
obj.LockRecomputes = oldState
def collectTouchedDict(self, selfobj):
doc = selfobj.Document
dict = {}
for docobj in doc.Objects:
dict[docobj.Name] = 'Touched' in docobj.State
return dict
def restoreTouched(self, selfobj, dict):
doc = selfobj.Document
for docobj in doc.Objects:
if dict[docobj.Name] != ('Touched' in docobj.State):
if dict[docobj.Name] == True:
touch(docobj)
else:
docobj.purgeTouched()
class ViewProviderLatticeRecomputeLocker:
"A View Provider for LatticeRecomputeLocker object"
def __init__(self,vobj):
vobj.Proxy = self
def getIcon(self):
if self.Object.LockRecomputes:
return getIconPath("Lattice2_RecomputeLocker_Locked.svg")
else:
return getIconPath("Lattice2_RecomputeLocker_Unlocked.svg")
def attach(self, vobj):
self.ViewObject = vobj
self.Object = vobj.Object
def setEdit(self,vobj,mode):
return False
def unsetEdit(self,vobj,mode):
return
def __getstate__(self):
return None
def __setstate__(self,state):
return None
def dumps(self):
return None
def loads(self,state):
return None
# --------------------------------/document object------------------------------
# --------------------------------Gui commands----------------------------------
def getLocker():
if hasattr(App.ActiveDocument,"LatticeRecomputeLocker"):
return App.ActiveDocument.LatticeRecomputeLocker
else:
return None
class _CommandMakeLockerObj:
"Command to create RecomputeLocker feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_MakeFeature.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Make recompute locker object"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Make recompute locker object. Doing this is necessary to enable recompute locking hacktionality."),
'CmdType':"ForEdit"}
def Activated(self):
if getLocker() is None:
FreeCADGui.addModule("lattice2RecomputeLocker")
FreeCADGui.doCommand("lattice2RecomputeLocker.makeRecomputeLocker('LatticeRecomputeLocker')")
FreeCADGui.doCommand("App.ActiveDocument.LatticeRecomputeLocker.purgeTouched()")
else:
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(translate("Lattice2_RecomputeLocker", "A recompute locker object already exists in this document. Only one such object can be made.", None))
mb.setWindowTitle(translate("Lattice2_RecomputeLocker","Nothing to do", None))
mb.exec_()
def IsActive(self):
if not App.ActiveDocument: return False
if hasattr(App.ActiveDocument,'RecomputesFrozen'):
return False # new FreeCAD, with proper recompute disablement. Disable the hack.
else:
return (bool(App.ActiveDocument) and getLocker() is None)
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_MakeFeature', _CommandMakeLockerObj())
class _CommandLockRecomputes:
"Command to lock automatic recomputes"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_LockRecomputes.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Lock recomputes"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Lock recomputes: prevent FreeCAD's automatic recomputes."),
'CmdType':"ForEdit"}
def Activated(self):
if hasattr(App.ActiveDocument,'RecomputesFrozen'):
FreeCADGui.doCommand("App.ActiveDocument.RecomputesFrozen = True")
elif getLocker() is not None:
FreeCADGui.addModule("lattice2RecomputeLocker")
FreeCADGui.doCommand("lattice2RecomputeLocker.getLocker().LockRecomputes = True")
FreeCADGui.doCommand("lattice2RecomputeLocker.getLocker().touch()") #gets rid of the tick, plus updates the icon.
else:
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(translate("Lattice2_RecomputeLocker", "There is no recompute locker object in the document. Please create one, first.", None))
mb.setWindowTitle(translate("Lattice2_RecomputeLocker","fail", None))
mb.exec_()
def IsActive(self):
if not App.ActiveDocument: return False
if hasattr(App.ActiveDocument,'RecomputesFrozen'):
return App.ActiveDocument.RecomputesFrozen == False
else:
return getLocker() is not None and not getLocker().LockRecomputes
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_LockRecomputes', _CommandLockRecomputes())
class _CommandUnlockRecomputes:
"Command to unlock automatic recomputes"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_UnlockRecomputes.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Unlock recomputes"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Unlock recomputes: switch on FreeCAD's automatic recomputes."),
'CmdType':"ForEdit"}
def Activated(self):
if hasattr(App.ActiveDocument,'RecomputesFrozen'):
FreeCADGui.doCommand("App.ActiveDocument.RecomputesFrozen = False")
elif getLocker() is not None:
FreeCADGui.addModule("lattice2RecomputeLocker")
FreeCADGui.doCommand("lattice2RecomputeLocker.getLocker().LockRecomputes = False")
FreeCADGui.doCommand("lattice2RecomputeLocker.getLocker().purgeTouched()") #gets rid of the tick, plus updates the icon.
else:
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(translate("Lattice2_RecomputeLocker", "There is no recompute locker object in the document. Please create one, first.", None))
mb.setWindowTitle(translate("Lattice2_RecomputeLocker","fail", None))
mb.exec_()
def IsActive(self):
if not App.ActiveDocument: return False
if hasattr(App.ActiveDocument,'RecomputesFrozen'):
return App.ActiveDocument.RecomputesFrozen == True
else:
return getLocker() is not None and getLocker().LockRecomputes
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_UnlockRecomputes', _CommandUnlockRecomputes())
class _CommandRecomputeFeature:
"Command to recompute single object"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_RecomputeFeature.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Recompute feature"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","RecomputeFeature: recompute selected objects."),
'CmdType':"ForEdit"}
def Activated(self):
sel = FreeCADGui.Selection.getSelectionEx()
FreeCADGui.addModule("lattice2RecomputeLocker")
for selobj in sel:
FreeCADGui.doCommand("lattice2RecomputeLocker.recomputeFeature(App.ActiveDocument."+selobj.ObjectName+")")
def IsActive(self):
return len(FreeCADGui.Selection.getSelectionEx()) > 0
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_RecomputeFeature', _CommandRecomputeFeature())
class _CommandRecomputeDocument:
"Command to recompute whole document"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_RecomputeDocument.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Recompute document"),
'Accel': "",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Recompute document: recompute the document, ignoring that recomputes are locked."),
'CmdType':"ForEdit"}
def Activated(self):
try:
if hasattr(App.ActiveDocument, 'RecomputesFrozen'):
FreeCADGui.doCommand(
'_lock = App.ActiveDocument.RecomputesFrozen\n'
'App.ActiveDocument.RecomputesFrozen = False\n'
'App.ActiveDocument.recompute()\n'
'App.ActiveDocument.RecomputesFrozen = _lock\n'
'del _lock\n'
)
else: #old FC, hacky recompute control
if getLocker() is not None:
FreeCADGui.addModule("lattice2RecomputeLocker")
FreeCADGui.doCommand("lattice2RecomputeLocker.getLocker().Proxy.RecomputeDocument(lattice2RecomputeLocker.getLocker())")
else:
FreeCADGui.doCommand("App.ActiveDocument.recompute()")
except Exception as err:
msgError(err)
def IsActive(self):
return App.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_RecomputeDocument', _CommandRecomputeDocument())
class _CommandForceRecompute:
"Command to force recompute of every feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_ForceRecompute.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Force recompute"),
'Accel': "Shift+F5",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Force recompute: recompute all features in the document."),
'CmdType':"ForEdit"}
def Activated(self):
try:
FreeCADGui.addModule("lattice2RecomputeLocker")
FreeCADGui.doCommand("lattice2RecomputeLocker.touchEverything(App.ActiveDocument)")
_CommandRecomputeDocument().Activated()
except Exception as err:
msgError(err)
def IsActive(self):
return App.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_ForceRecompute', _CommandForceRecompute())
class _CommandTouch:
"Command to touch a feature"
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_RecomputeLocker_Touch.svg"),
'MenuText': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Touch selected features"),
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Lattice2_RecomputeLocker","Touch selected features: mark selected features as needing recomputing."),
'CmdType':"ForEdit"}
def Activated(self):
FreeCADGui.addModule("lattice2RecomputeLocker")
try:
sel = FreeCADGui.Selection.getSelectionEx()
if len(sel) == 0:
infoMessage("Touch command",
"'Touch selected features' command. Touches selected objects. 'Touched' means the object was changed and should be recomputed; if nothing is touched, recomputing the document does nothing.\n\n"
"Please select objects to be marked as touched first, then invoke this command. If all selected objects are touched already, the 'Touched' state is undone (purged).")
return
n_touched = 0
for so in sel:
if 'Touched' in so.Object.State:
n_touched += 1
if n_touched < len(sel):
# not all selected objects are currently touched. Touch the remaining...
FreeCADGui.doCommand("for so in Gui.Selection.getSelectionEx(): lattice2RecomputeLocker.touch(so.Object)")
else:
#all selected objects are already touched.
FreeCADGui.doCommand("for so in Gui.Selection.getSelectionEx(): so.Object.purgeTouched()")
except Exception as err:
msgError(err)
def IsActive(self):
return App.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLocker_Touch', _CommandTouch())
exportedCommands = [
"Lattice2_RecomputeLocker_MakeFeature",
"Lattice2_RecomputeLocker_LockRecomputes",
"Lattice2_RecomputeLocker_UnlockRecomputes",
"Lattice2_RecomputeLocker_RecomputeFeature",
"Lattice2_RecomputeLocker_RecomputeDocument",
"Lattice2_RecomputeLocker_ForceRecompute",
"Lattice2_RecomputeLocker_Touch"
]
try:
if float(App.Version()[1]) >= 17.0:
exportedCommands.remove("Lattice2_RecomputeLocker_MakeFeature")
except Exception as err:
App.Console.PrintWarning("Failed to parse version string: {v}".format(v= App.Version()[1]))
#assume modern
exportedCommands.remove("Lattice2_RecomputeLocker_MakeFeature")
class CommandRecomputeGroup:
def GetCommands(self):
global exportedCommands
return tuple(exportedCommands)
def GetDefaultCommand(self): # return the index of the tuple of the default command.
return 0
def GetResources(self):
return { 'MenuText': 'Lattice recompute control:',
'ToolTip': 'Document recompute controlling tools from Lattice2 workbench',
'CmdType':"ForEdit"}
def IsActive(self): # optional
return App.ActiveDocument is not None
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Lattice2_RecomputeLockerGroup', CommandRecomputeGroup())
def msgbox(strmsg):
mb = QtGui.QMessageBox()
mb.setIcon(mb.Icon.Warning)
mb.setText(strmsg)
mb.setWindowTitle("debug")
mb.exec_()