/
TempoVis.py
174 lines (137 loc) · 8.3 KB
/
TempoVis.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
#/***************************************************************************
# * Copyright (c) Victor Titov (DeepSOIC) *
# * (vv.titov@gmail.com) 2016 *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This library is free software; you can redistribute it and/or *
# * modify it under the terms of the GNU Library General Public *
# * License as published by the Free Software Foundation; either *
# * version 2 of the License, or (at your option) any later version. *
# * *
# * This library 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 library; see the file COPYING.LIB. If not, *
# * write to the Free Software Foundation, Inc., 59 Temple Place, *
# * Suite 330, Boston, MA 02111-1307, USA *
# * *
# ***************************************************************************/
import FreeCAD as App
if App.GuiUp:
import FreeCADGui as Gui
from Show.FrozenClass import FrozenClass
from Show.DepGraphTools import getAllDependencies, getAllDependent, isContainer
class TempoVis(FrozenClass):
'''TempoVis - helper object to save visibilities of objects before doing
some GUI editing, hiding or showing relevant stuff during edit, and
then restoring all visibilities after editing.
Constructors:
TempoVis(document): creates a new TempoVis. Supplying document is mandatory. Objects not belonging to the document can't be modified via TempoVis.'''
def __define_attributes(self):
self.data = {} # dict. key = ("Object","Property"), value = original value of the property
self.cam_string = "" # inventor ASCII string representing the camera
self.viewer = None # viewer the camera is saved from
self.document = None
self.restore_on_delete = False # if true, restore() gets called upon object deletion. It becomes False after explicit call to Restore, and set to true by many methods.
self.links_are_lost = False # set to true after restore from JSON. Indictes to attempt to use ActiveDocument/ActiveViewer instead.
self._freeze()
def __init__(self, document):
self.__define_attributes()
self.document = document
def modifyVPProperty(self, doc_obj_or_list, prop_name, new_value):
'''modifyVPProperty(self, doc_obj_or_list, prop_name, new_value): modifies
prop_name property of ViewProvider of doc_obj_or_list, and remembers
original value of the property. Original values will be restored upon
TempoVis deletion, or call to restore().'''
if App.GuiUp:
if type(doc_obj_or_list) is not list:
doc_obj_or_list = [doc_obj_or_list]
for doc_obj in doc_obj_or_list:
if not hasattr(doc_obj.ViewObject, prop_name):
App.Console.PrintWarning("TempoVis: object {obj} has no attribute {attr}. Skipped.\n"
.format(obj= doc_obj.Name, attr= prop_name))
continue # silently ignore if object doesn't have the property...
if doc_obj.Document is not self.document: #ignore objects from other documents
raise ValueError("Document object to be modified does not belong to document TempoVis was made for.")
oldval = getattr(doc_obj.ViewObject, prop_name)
setattr(doc_obj.ViewObject, prop_name, new_value)
if not self.data.has_key((doc_obj.Name,prop_name)):
self.data[(doc_obj.Name,prop_name)] = oldval
self.restore_on_delete = True
def show(self, doc_obj_or_list):
'''show(doc_obj_or_list): shows objects (sets their Visibility to True). doc_obj_or_list can be a document object, or a list of document objects'''
self.modifyVPProperty(doc_obj_or_list, "Visibility", True)
def hide(self, doc_obj_or_list):
'''hide(doc_obj_or_list): hides objects (sets their Visibility to False). doc_obj_or_list can be a document object, or a list of document objects'''
self.modifyVPProperty(doc_obj_or_list, "Visibility", False)
def hide_all_dependent(self, doc_obj):
'''hide_all_dependent(doc_obj): hides all objects that depend on doc_obj. Groups, Parts and Bodies are not hidden by this.'''
self.hide( [o for o in getAllDependent(doc_obj) if not isContainer(o)])
def show_all_dependent(self, doc_obj):
'''show_all_dependent(doc_obj): shows all objects that depend on doc_obj. This method is probably useless.'''
self.show( getAllDependent(doc_obj) )
def hide_all_dependencies(self, doc_obj):
'''hide_all_dependencies(doc_obj): hides all objects that doc_obj depends on (directly and indirectly).'''
self.hide( getAllDependencies(doc_obj) )
def show_all_dependencies(self, doc_obj):
'''show_all_dependencies(doc_obj): shows all objects that doc_obj depends on (directly and indirectly). This method is probably useless.'''
self.show( getAllDependencies(doc_obj) )
def saveCamera(self):
vw = Gui.ActiveDocument.ActiveView
self.cam_string = vw.getCamera()
self.viewer = vw
self.restore_on_delete = True
def restoreCamera(self):
if not self.cam_string:
return
vw = self.viewer
if self.links_are_lost: # can happen after save-restore
import FreeCADGui as Gui
vw = Gui.ActiveDocument.ActiveView
vw.setCamera(self.cam_string)
def restore(self):
'''restore(): restore all ViewProvider properties modified via TempoVis to their
original values, and saved camera, if any. Called automatically when instance is
destroyed, unless it was called explicitly. Should not raise exceptions.'''
if self.links_are_lost:
self.document = App.ActiveDocument
self.viewer = Gui.ActiveDocument.ActiveView
self.links_are_lost = False
for obj_name, prop_name in self.data:
try:
setattr(self.document.getObject(obj_name).ViewObject, prop_name, self.data[(obj_name, prop_name)])
except Exception as err:
App.Console.PrintWarning("TempoVis: failed to restore {obj}.{prop}. {err}\n"
.format(err= err.message,
obj= obj_name,
prop= prop_name))
try:
self.restoreCamera()
except Exception as err:
App.Console.PrintWarning("TempoVis: failed to restore camera. {err}\n"
.format(err= err.message))
self.restore_on_delete = False
def forget(self):
'''forget(): resets TempoVis'''
self.data = {}
self.cam_string = ""
self.viewer = None
self.restore_on_delete = False
def __del__(self):
if self.restore_on_delete:
self.restore()
def __getstate__(self):
return (self.data.items(),
self.cam_string,
self.restore_on_delete)
def __setstate__(self, state):
self.__define_attributes()
items, self.cam_string, self.restore_on_delete = state
# need to convert keys to tuples (dict doesn't accept list as key; tuples are converted to lists by json)
items = [(tuple(item[0]), item[1]) for item in items]
self.data = dict(items)
self.links_are_lost = True