forked from holonking/rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploy_BaseTypes.py
More file actions
261 lines (222 loc) · 7.2 KB
/
Deploy_BaseTypes.py
File metadata and controls
261 lines (222 loc) · 7.2 KB
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
# -*- coding: utf-8 -*-
T='├─'
I='│'
L='└─'
D='─'
SELECT='█'
SPACE=' '
import os
import rhinoscriptsyntax as rs
def shortGuid(guid):
if guid is None:return 'None'
guid=str(guid)
txt=str(guid[:2])+str(guid[-2:])
return '['+txt+']'
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
class AttrDict(dict):
def __init__(self, *args, **kwargs):
# super(AttrDict, self).__init__(*args, **kwargs)
super(dict,self).__init__(*args, **kwargs)
self.__dict__ = self
class PhaseObject():
def __init__(self, parent=None,phase='None',typeIndex=0,guid=None,*args, **kwargs):
#super(AttrDict,self).__init__(*args, **kwargs)
self.name='UnnamedPO'
self.guid=guid
self.children=[]
self.parent=parent
if parent is not None:
self.set_parent(parent)
self.phase=phase
self.needUpdate=False
self.level=0
#usualy each surface has one type index
self.typeIndex=typeIndex
#during earlier stage,
#a massing can have multiple index
#by functionalities
self.typeIndices=[0]*10
self.is_selected=False
self.description=''
def __str__(self):
txt=self.phase+'( {} )_{}'.format(len(self.children),self.typeIndex)
if self.guid is not None:
txt+=shortGuid(self.guid)
return txt
def to_string(self):
txt=self.phase+'( {} )_{}'.format(len(self.children),self.typeIndex)
if self.guid is not None:
txt+=shortGuid(self.guid)
return txt
@property
def root(self):
if self.parent is not None:
return self.parent.root
return self
def children_count(self):
return len(self.children)
def is_root(self):
if self.parent is None:
return True
return False
def is_end_node(self):
if self.parent is None:return True
index=self.parent.children.index(self)
if index==len(self.parent.children)-1:return True
return False
def is_leaf(self):
if self.children_count()==0:return True
return False
def find_all(self,name_val_pairs,basket=[]):
#usage:
#conditions=[('phase','BLOCK'),('typeIndex',1)]
#tree.find_all(conditions)
match=True
for name,val in name_val_pairs:
if self.__dict__[name] != val:
match=False
break
if match:
basket.append(self)
for c in self.children:
basket=c.find_all(name_val_pairs,basket)
return basket
def find_all_guids(self,name_val_pairs,basket=[]):
match=True
for name,val in name_val_pairs:
if self.__dict__[name] != val:
match=False
break
if match:
basket.append(self.guid)
for c in self.children:
basket=c.find_all_guids(name_val_pairs,basket)
return basket
def find(self,name,val):
if self.__dict__[name]==val:
return self
for c in self.children:
o=c.find(name,val)
if o is not None: return o
return None
def flattern(self,_bin=[]):
_bin.append(self)
#print(_bin)
for c in self.children:
_bin=c.flattern(_bin)
return _bin
def tree(self,is_end_node=True,starting_node=None,out_str='',_print=False):
if starting_node is None:
starting_node=self
out_str=''
prefix=''
#else: prefix=' '+SPACE
parent=self.parent
if parent is not None and starting_node is not None:
gparent=parent.parent
end_condition=[]
go=True
while gparent is not None and parent!=starting_node:
i=gparent.children.index(parent)
if i==len(gparent.children)-1 :
end_condition.append(True)
else: end_condition.append(False)
parent=gparent
gparent=parent.parent
if parent==starting_node:
#end_condition.append(True)
break
end_condition.append(True)
end_condition.reverse()
for c in end_condition:
if not c: prefix+=I+SPACE
else :prefix+=' '+SPACE
# for i in range(self.level):
# prefix+=I+SPACE
if starting_node==self:
leader=''
else:
leader=L if is_end_node else T
name=str(self)
#name=self.phase+'({})'.format(len(self.children))
#name=name+'_'+shortGuid(self.guid)+'_'+str(self.typeIndex)
if self.is_selected:
#name+=SELECT
name='▌ '+name
txt=prefix+leader+name
if _print:print (txt)
out_str+=txt+'\n'
for i in range(len(self.children)):
child=self.children[i]
is_end=True if i>=len(self.children)-1 else False
out_str+=child.tree(is_end_node=is_end,starting_node=starting_node,_print=_print)
return out_str
def select(self):
self.is_selected=True
def unselect(self):
self.is_selected=False
def set_parent(self,parent):
if parent is None: return
self.parent=parent
parent.add_child(self)
#update children level
#for c in self.children:
# c.level=self.level+1
def set_children(self,children):
self.children=children
#for c in self.children:
# c.level=self.level+1
def add_child(self,child):
child.parent=self
self.children.append(child)
#child.level=self.level+1
def delete(self):
#print('@PhaseObject.delete self.parent=',self.parent)
if self.parent is not None:
#print('removing self from',self.parent)
self.parent.remove_child(self)
if self.children:
for c in self.children:
c.delete()
if self.guid is not None:
if rs.IsObject(self.guid):
rs.DeleteObject(self.guid)
def remove_child(self,child):
#end father and son relationship
#fist if statement is import if the child is
#assigned to another father before deleting
try:
index=self.children.index(child)
del self.children[index]
except:pass
pass
def demo():
A=PhaseObject()
B=PhaseObject(A,phase='B',typeIndex=322)
C=PhaseObject(A,phase='C')
D=PhaseObject(B,phase='D')
E=PhaseObject(A,phase='E',typeIndex=322)
F=PhaseObject(D,phase='F',typeIndex=322)
print(F.root)
print(A.tree())
fo=A.find('phase','F')
print('found form a',fo)
cons=[('typeIndex',322)]
fos=A.find_all(cons)
print('find_all:')
for o in fos:
print(o)
print('----------------')
B.delete()
print(A.tree())
return A
if __name__ == "__main__":
demo()
# demo()