-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlattice2ViewFromPlacement.py
203 lines (180 loc) · 8.55 KB
/
lattice2ViewFromPlacement.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
#***************************************************************************
#* *
#* 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
if App.GuiUp:
import FreeCADGui as Gui
from lattice2Common import *
import lattice2BaseFeature as LBF
import lattice2CompoundExplorer as LCE
try:
import Show
except ImportError:
Show = None
try:
from Show.SceneDetail import SceneDetail
except ImportError:
SceneDetail = None
from copy import copy
class SDParameter(SceneDetail if SceneDetail is not None else object):
"""SDParameter(param_path, param_name, param_type, param_val = None): Plugin for TempoVis for changing a parameter."""
class_id = 'SDParameter'
mild_restore = True
param_path = ''
param_name = ''
param_type = ''
def __init__(self, param_path, param_name, param_type, param_val = None):
self.key = '{param_path}/[{param_type}]{param_name}'.format(**vars())
self.param_path = param_path
self.param_name = param_name
self.param_type = param_type
if param_val is not None:
self.data = param_val
def scene_value(self):
import FreeCAD as App
hgrp = App.ParamGet(self.param_path)
getter, def1, def2 = {
'Bool' : (hgrp.GetBool , True, False),
'Float' : (hgrp.GetFloat , 1.0, 2.0 ),
'Int' : (hgrp.GetInt , 1, 2 ),
'String' : (hgrp.GetString , 'a', 'b' ),
'Unsigned': (hgrp.GetUnsigned, 1, 2 ),
}[self.param_type]
val1 = getter(self.param_name, def1)
val2 = getter(self.param_name, def2)
absent = val1 == def1 and val2 == def2
return val1 if not absent else None
def apply_data(self, val):
import FreeCAD as App
hgrp = App.ParamGet(self.param_path)
setter = {
'Bool' : (hgrp.SetBool ),
'Float' : (hgrp.SetFloat ),
'Int' : (hgrp.SetInt ),
'String' : (hgrp.SetString ),
'Unsigned': (hgrp.SetUnsigned),
}[self.param_type]
deleter = {
'Bool' : (hgrp.RemBool ),
'Float' : (hgrp.RemFloat ),
'Int' : (hgrp.RemInt ),
'String' : (hgrp.RemString ),
'Unsigned': (hgrp.RemUnsigned),
}[self.param_type]
if val is None:
deleter(self.param_name)
else:
setter(self.param_name, val)
def getSelectedPlacement(obj, sub):
leaves = LCE.AllLeaves(obj.Shape)
if sub == '':
return 0 if len(leaves) == 1 else None
subshape = obj.Shape.getElement(sub)
getter = {
'Vertex': (lambda sh: sh.Vertexes),
'Edge': (lambda sh: sh.Edges),
'Face': (lambda sh: sh.Faces)
} [subshape.ShapeType]
matches = []
for index, leaf in enumerate(leaves):
for it_sh in getter(leaf):
if it_sh.isSame(subshape):
matches.append(index)
assert(len(matches) < 2)
return matches[0]
# --------------------------------Gui commands----------------------------------
_library = {} #instances of TempoVis, per document
_old_par = None #original value of DragAtCursor parameter
_stack_len = 0 #how many times did we tried to modify the parameter
class CommandViewFromPlacement:
def GetResources(self):
return {'Pixmap' : getIconPath("Lattice2_ViewFromPlacement.svg"),
'MenuText': "View from placement",
'Accel': "",
'ToolTip': "View from placement. Places camera to where selected placement is. Click again to restore view.",
'CmdType':"ForEdit"}
def Activated(self):
import FreeCADGui as Gui
global _stack_len
global _old_par
oldTV = _library.pop(App.ActiveDocument.Name, None)
if oldTV is None:
V = App.Vector
sel = Gui.Selection.getSelectionEx()
if len(sel) == 1 and len(sel[0].SubElementNames) < 2:
#get placement
index = getSelectedPlacement(sel[0].Object, sel[0].SubElementNames[0] if len(sel[0].SubElementNames) == 1 else '')
plm = LBF.getPlacementsList(sel[0].Object)[index]
OZ = plm.Rotation.multVec(V(0,0,1))
OY = plm.Rotation.multVec(V(0,1,0))
OX = plm.Rotation.multVec(V(1,0,0))
print(OX)
print(OZ)
#prepare
tv = Show.TempoVis(App.ActiveDocument)
tv.saveCamera()
if SceneDetail is not None:
tv.modify(SDParameter('User parameter:BaseApp/Preferences/View','DragAtCursor','Bool', False))
else:
dt = SDParameter('User parameter:BaseApp/Preferences/View','DragAtCursor','Bool', False)
if _stack_len == 0:
_old_par = dt.scene_value()
_stack_len += 1
dt.apply_data(dt.data)
#set up camera
Gui.ActiveDocument.ActiveView.setCameraType('Perspective')
from pivy import coin
cam = Gui.ActiveDocument.ActiveView.getCameraNode()
cam.position = tuple(plm.Base)
rot = App.Rotation(V(), -OZ, -OX,'ZYX')
cam.orientation = tuple(rot.Q)
cam.heightAngle = -cam.heightAngle.getValue() #reversing height angle inverts the image. Using it in conjunction with upside-down camera to effectively reverse how view reacts to mouse.
cam.focalDistance = 1e-5 #small focal distance makes camera spin about itself
#all done, remember.
_library[App.ActiveDocument.Name] = tv
else:
infoMessage("Lattice2 View From Placement",
"Lattice2 View From Placement command."
"\n\nPlease select a placement, and invoke this tool. The camera will be placed there,"
" and you'll be able to rotate around."
"\n\nClick again to leave the mode.")
else:
oldTV.restore()
if Gui.ActiveDocument.ActiveView.getCameraType() == 'Perspective':
cam = Gui.ActiveDocument.ActiveView.getCameraNode()
cam.heightAngle = abs(cam.heightAngle.getValue()) #workaround
if SceneDetail is None:
_stack_len -= 1
if _stack_len == 0:
dt = SDParameter('User parameter:BaseApp/Preferences/View','DragAtCursor','Bool', False)
dt.apply_data(_old_par)
def IsActive(self):
if Show is None: return False
return hasattr(Gui.activeView(), 'getCameraNode')
if App.GuiUp:
FreeCADGui.addCommand('Lattice2_ViewFromPlacement', CommandViewFromPlacement())
exportedCommands = [
"Lattice2_ViewFromPlacement",
]