-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeploy_Engine.py
More file actions
913 lines (822 loc) · 32.3 KB
/
Deploy_Engine.py
File metadata and controls
913 lines (822 loc) · 32.3 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# -*- coding: utf-8 -*-
import Rhino
import cPickle as pkl
import rhinoscriptsyntax as rs
import rsTools
reload(rsTools)
from rsTools import *
import Deploy_BaseTypes
reload(Deploy_BaseTypes)
from Deploy_BaseTypes import *
VIEWMODE=None
import os
#check which OS system, if nt, means windows
if os.name=='nt':
PATH_PATTERN='C:\\Users\\seah\\Scripting\\rhinoscript\\FacadePatterns\\'
else:
PATH_PATTERN='./FacadePatterns/'
PHASES=['BLOCK','MASSING','MASSINGSPLIT','TYPESRF','TYPEMESH','COMPONENTS']
def get_layer_name(phase):
return '$AGL_'+phase
COLOR_SET_01=[(60,160,208),
(255,89,25),
(242,192,120),
(250,237,202),
(193,219,179),
(126,188,137),
(8,111,161)
]
COLOR_SET_02=[(0,102,204),
(51,153,255),
(153,204,255),
(255,204,204),
(255,102,102),
(153,0,0)
]
DEFAULT_COLOR=(60,160,208)
PHASE_OBJECT_COLORS={
'BLOCK':[DEFAULT_COLOR],
'MASSING':[DEFAULT_COLOR],
'MASSINGSPLIT':[DEFAULT_COLOR],
'TYPESRF':[DEFAULT_COLOR],
'TYPEMESH':[DEFAULT_COLOR]
}
PHASE_OBJECT_COLORS['BLOCK']=COLOR_SET_01
PHASE_OBJECT_COLORS['TYPESRF']=COLOR_SET_01
PHASE_OBJECT_COLORS['TYPEMESH']=COLOR_SET_02
class Engine():
def __init__(self):
#propagate required layers
for phase in PHASES:
lay=get_layer_name(phase)
if not rs.IsLayer(lay):rs.AddLayer(lay)
#load data
#self.data=self.loadData()
self.data=PhaseObject(phase='Root')
#print('loaded data:',self.data)
#if self.data is None:
#self.importSrfTypesFromScene()
#print('imported data:',self.data)
#print('imported data:')
#for o in self.data:
# print(o.guid)
#load facade types
self.loadFacadeTypes()
#print('loaded facade types:',self.facadeTypes)
#loggin panel passed from UI
self.log_panel=None
#selection
self.selectedObjects=[]
self.selectedObject=None
self.selectedRhiObjects=[]
self.selectedRhiObject=None
#layers
self.initLayers()
#interaction modes
self.interaction_mode=AttrDict()
self.interaction_mode.auto_block=True
def suspendInteraction(self):
self.interaction_mode.auto_block=False
def resumeInteraction(self):
self.interaction_mode.auto_block=True
def logDataTree(self):
#if len(self.data)==0:return
#txt='DATA SIZE:'+str(len(self.data))+'\n'
txt=''
txt+=self.data.tree()
# for po in self.data:
# if po.is_root():
# txt+=po.tree()
self.logToTreePanel(txt)
def logToTreePanel(self,txt):
if self.log_tree_panel is None:
print('engine.log_panel not set')
return
else: self.log_tree_panel.Text=txt
def logToObjPanel(self,txt):
if self.log_obj_panel is None:
print('engine.log_obj_panel not set')
return
else: self.log_obj_panel.Text=txt
def logToRhiPanel(self,txt):
if self.log_rhi_panel is None:
print('engine.log_panel not set')
return
else: self.log_rhi_panel.Text=txt
#save and load data
def load(self):
self.data=self.loadData()
def loadData(self):
#serialize the dic from a txt file
try:
with open('Data','rb') as fp:
d1=pkl.load(fp)
return d1
except: return None
def save(self):
self.saveData(self.data)
def saveData(self,data):
try:
filename='Data'
with open(filename,'wb') as fp:
pkl.dump(data,fp)
print('engine data saved sucess')
except:
print('save faile failed')
#object life cycle managements
def addObject(self,guid,phase,typeIndex,parent=None,description='',needUpdate=False):
#print('add object ',shortGuid(guid))
if self.data.find('guid',guid):
print('obj exists in data')
return None
o=PhaseObject()
o.guid=guid
o.phase=phase
o.typeIndex=typeIndex
o.set_parent(parent)
o.description=description
o.needUpdate=needUpdate
#parent.add_child(o)
#rhino layer and attributes
rs.ObjectColor(guid,self.get_color(phase,typeIndex))
try:
layer=get_layer_name(phase)
print(layer)
rs.ObjectLayer(guid,layer)
except Exception as e:print(e)
#layer=self.get_layer_name(phase)
#rs.ObjectLayer(guid,layer)
self.data.add_child(o)
self.logDataTree()
return o
def deleteObjectsByGuid(self,guids):
# print('deleteObjectsByGuid')
# otd=[]
if guids is None: return
for guid in guids:
o=self.data.find('guid',guid)
if o is not None:
o.delete()
self.logDataTree()
def deleteObjectByGuid(self,guid):
print('deleteObjectsByGuid')
o=self.data.find('guid',guid)
print('DELETED OBJ',o)
if o is not None:
o.delete()
self.logDataTree()
def deleteObject(self,obj):
if obj is None: return None
#if rs.IsObject(obj.guid): rs.DeleteObject(obj.guid)
obj.delete()
def deleteObjects(self,phase,typeIndex=None):
#deletes objects off the same phase and type index
#<to be confirmed>
cons=[('phase',phase),('typeIndex',typeIndex)]
fos=self.data.find_all(cons)
for o in fos:
o.delete()
#selection management
def clearSelections(self):
self.selectedObjects=[]
def addToSelection(self,items):
for i in items:
self.selectedObjects.append(i)
def setSelection(self,items):
self.selectedObjects=items
def highlightSelection(self):
rs.UnselectAllObjects()
guids=self.getGuidFromADs(self.selectedObjects)
rs.SelectObjects(guids)
#data query and inspections
def inspectObject(self,guid):
try:
txt=''
for o in self.data.flattern():
if o.guid==guid:
txt+='guid :'+shortGuid(o.guid)+'\n'
txt+='phase:'+str(o.phase)+'\n'
txt+='typeI:'+str(o.typeIndex)+'\n'
txt+='typeIS:['
for i in o.typeIndices:
txt+=str(i)+','
txt+=']\n'
if o.parent is not None:
txt+='upStm:'+shortGuid(o.parent.guid)+'\n'
else:
txt+='upStm:'+'None\n'
txt+='dnStm:'
for c in o.children:
txt+=shortGuid(c.guid)+' '
txt+='\n'
txt+='dsrpt:'+o.description+'\n'
#txt+='down stream objects:\n'
#for o in o.children:
# txt+=shortGuid(o.guid)+'\n'
break
#print(txt)
return txt
except:PrintException()
def getGuidFromADs(self,oAD):
guids=[]
for o in oAD:
guids.append(o.guid)
return guids
def getObjectPhaseObject(self,obj,phase):
#finds the object's up or down stream objects
phase1=obj.phase
phase2=phase
i1=PHASES.index(phase1)
i2=PHASES.index(phase2)
direction=self.streamDirection(phase1,phase2)
selection=[]
if direction==0: return obj
# stop=i2+direction
while not i1==i2:
i1+=direction
selection+=obj.children
return selection
def streamDirection(self,phase1,phase2):
#determin the direction of phase 2 compares to phase 1
# -1=upStream, 1=downStream, 0=same phase
if phase1==phase2:return 0
i1=PHASES.index(phase1)
i2=PHASES.index(phase2)
if i2<i1: return -1
else: return 1
def getObjectByGuid(self,guid):
for o in self.data:
if o.guid==guid: return o
return None#,o.parent
def getObject(self,phaseIndex,typeIndex=None):
selection=[]
for o in self.data:
#print('@getObject',o.phase,phaseIndex)
if o.phase==phaseIndex:
if typeIndex is None: flag=True
elif typeIndex==o.typeIndex: flag=True
else: flag=False
if flag: selection.append(o)
return selection
def setObjectType(self,obj,typeIndex=0,index=None):
colorI=int(typeIndex)%len(COLOR_SET_01)
color=COLOR_SET_01[colorI]
rs.ObjectColor(obj.guid,color)
if index is None:
obj.typeIndex=int(typeIndex)
else:
obj.typeIndices[int(index)]=typeIndex
#layer managements
def initLayers(self):
#propagate required layers
parent=get_layer_name('LOCKEDLAYERS')
rs.AddLayer(get_layer_name('LOCKEDLAYERS'),locked=False)
for phase in PHASES:
lay=get_layer_name(phase)
if not rs.IsLayer(lay):rs.AddLayer(lay,locked=False)
else: rs.LayerLocked(lay,False)
#rs.ParentLayer(lay,parent)
rs.ExpandLayer(parent,False)
def isolateLayer(self,layerName):
names=rs.LayerNames()
rs.CurrentLayer('Default')
for n in names:
if n=='Default':continue
if n==get_layer_name('LOCKEDLAYERS'):
rs.LayerVisible(layerName,True)
continue
if n==layerName:
layers=rs.LayerVisible(layerName,True)
else:rs.LayerVisible(n,False)
#object generators
def genTypeMeshObject(self,obj):
facadeType=self.facadeTypes[obj.typeIndex]
downStream=obj.children
layerName=get_layer_name('TYPEMESH')
phaseIndex='TYPEMESH'
#print('!!! downStrem size:',len(downStream))
#print(downStream)
for ds in downStream:
self.deleteObject(ds)
#pass
try:
m=divideSrfToPattern(obj.guid,facadeType)
if rs.IsObject(m):
rs.ObjectLayer(m,layerName)
self.addObject(m,phaseIndex,obj.typeIndex,obj)
except Exception as e:
#print('exception in divideSrfToPattern:',Exception,':',e)
PrintException()
rs.EnableRedraw(True)
pass
def genTypeMesh(self,typeIndex=None):
#if not typeIndex given, will regenerate all types
rs.EnableRedraw(False)
meshes=[]
srfs=[]
updatedList=[]
phaseIndex='TYPEMESH'
upStreamIndex='TYPESRF'
layerName=get_layer_name('TYPEMESH')
#remove existing mesh
try:
self.deleteObjects(phaseIndex,typeIndex)
except Exception as e:print('except at delete :',e)
#find all the TYPESRF
try:
srfs=self.getObject(upStreamIndex,typeIndex)
except Exception as e: print('except genTypeMesh->self.getObject:',e)
for o in srfs:
if typeIndex is not None:
if not o.typeIndex==typeIndex: continue
#typeI=typeIndex%len(self.facadeTypes)
typeI=o.typeIndex
#print('typeI:',typeI,self.facadeTypes)
try:
facadeType=self.facadeTypes[typeI]
#print('facade type: ',facadeType)
except Exception as e:
print('exception in getting self.facadeTypes',e)
#rs.EnableRedraw(True)
return
try:
m=divideSrfToPattern(o.guid,facadeType)
if rs.IsObject(m):
rs.ObjectLayer(m,layerName)
self.addObject(m,phaseIndex,typeI,o)
except Exception as e:
#rs.EnableRedraw(True)
print('exception in divideSrfToPattern:',Exception,':',e)
#rs.EnableRedraw(True)
rs.EnableRedraw(True)
#generate new mesh
def genAllTypeMesh(self,sender,e):
self.genTypeMesh()
#checked "self."
#library and static variables managements
def loadFacadeTypes(self):
facadeTypes=[]
import os
#directory='./FacadePatterns/'
#directory='C:\\rhinoscript\\gitProject\\FacadePatterns\\'
directory=self.getPathPattern()
print(directory)
files=os.listdir(directory)
for f in files:
if f.find('.facade')>0:
filename=directory+f
print(filename)
#for mac use 'rb'
#with open(filename,'rb') as fp:
with open(filename,'r') as fp:
#for line in fp:
# print(line)
facadeTypes.append(pkl.load(fp))
self.facadeTypes=facadeTypes
def updateFacadeType(self,typeIndex,filename):
from System.IO import Directory, Path
#directory = Directory.GetCurrentDirectory()
#filename = Path.Combine(directory, filename)
directory=PATH_PATTERN
filename=directory+filename
#filename='.\\'+filename
print('file name @ updateFacadeType: ',filename)
try:
with open(filename,'r' ) as fp:
facade=pkl.load(fp)
except:
PrintException()
self.facadeTypes[typeIndex]=facade
for t in self.facadeTypes:
print(t)
def get_color_default(self):
return DEFAULT_COLOR
def get_color_set1(self,index):
return COLOR_SET_01[index%len(COLOR_SET_01)]
def get_color(self,phase='BLOCK',index=0):
colors=PHASE_OBJECT_COLORS[phase]
index=index%len(colors)
color=colors[index]
return color
def get_colors(self,phase,index):
colors=PHASE_OBJECT_COLORS['TYPESRF']
index=index%len(colors)
return SRFTYPECOLORS[index]
def getPathPattern(self):
return PATH_PATTERN
#assign UI actions
def assignAction(self,form):
#engine.importSrfTypesFromScene()
self.form=form
form.Closing +=self.onFormCloseEvents
#assign GENBLOCK button actions
form.UI_GENBLOCK.bt_view_block.Click+=self.handle_GENBLOCK_bt_view_block
form.UI_GENBLOCK.bt_view_srf.Click+=self.handle_GENBLOCK_bt_view_srf
form.UI_GENBLOCK.bt_interact.Click+=self.handle_GENBLOCK_bt_interact
form.UI_GENBLOCK.combo_typeIndex1.SelectedIndexChanged+=self.handle_GENBLOCK_combo_updates
form.UI_GENBLOCK.combo_typeIndex2.SelectedIndexChanged+=self.handle_GENBLOCK_combo_updates
form.UI_GENBLOCK.combo_typeTopIndex.SelectedIndexChanged+=self.handle_GENBLOCK_combo_updates
#assign GENTYPESRF button actions
form.UI_GENTYPESRF.bt_regen.Click+=self.genAllTypeMesh
form.UI_GENTYPESRF.bt_view_srf.Click+=self.handle_GENTYPESRF_bt_viewSrf
form.UI_GENTYPESRF.bt_view_mesh.Click+=self.handle_GENTYPESRF_bt_viewMesh
form.UI_GENTYPESRF.bt_inspect.Click+=self.handle_GENTYPESRF_bt_inspect
self.loadRhinoEvents()
self.handle_GENTYPESRF_bts1()
self.handle_GENTYPESRF_bts2()
self.handle_GENTYPESRF_comboBox()
self.log_tree_panel=form.treeTextBox
self.log_obj_panel=form.objTextBox
self.log_rhi_panel=form.rhiTextBox
#TOOBAR action assignment
form.UI_TOOLBAR.bt_delete.Click+=self.handle_TOOLBAR_bt_delete
self.logDataTree()
def loadRhinoEvents(self):
print('loading rhino events')
Rhino.RhinoDoc.EndSaveDocument+=self.saveEngineData
Rhino.RhinoDoc.SelectObjects+=self.onSelectObjects
Rhino.RhinoDoc.AddRhinoObject+=self.onAddRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject+=self.onDeleteRhinoObject
def unloadRhinoEvents(self):
Rhino.RhinoDoc.EndSaveDocument-=self.saveEngineData
Rhino.RhinoDoc.SelectObjects-=self.onSelectObjects
Rhino.RhinoDoc.AddRhinoObject-=self.onAddRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject-=self.onDeleteRhinoObject
def saveEngineData(self,sender,e):
self.save()
def onDeleteRhinoObject(self,sender,e):
#do not impliment
#it will be called even when moving an object
#if you want to delete an object, please use the UI delete button
pass
def onAddRhinoObject(self,sender,e):
obj=rs.FirstObject()
isBrep=rs.IsBrep(obj)
if self.interaction_mode.auto_block and isBrep:
self.addObject(obj,'BLOCK',0,None)
def onFormCloseEvents(self,sender,e):
self.save()
self.unloadRhinoEvents()
print('save data and unload Rhino events')
def onSelectObjects(self,sender,e):
try:
sel=rs.SelectedObjects()
self.selectedRhiObjects=sel
txt='sel '+str(len(sel))+':'
for o in sel:
txt+=shortGuid(o)+','
#print(txt)
self.logToRhiPanel(txt)
if len(sel)==0:
self.selectedRhiObject=None
self.selectedObject=None
self.form.UI_GENBLOCK.lb_selected_block.Text='Select block to edit'
if len(sel)==1:
guid=sel[0]
self.selectedRhiObject=guid
txt=self.inspectObject(guid)
self.logToObjPanel(txt)
#obj=self.getObjectByGuid(guid)
obj=None
for po in self.data.flattern():
if po.guid==guid:
obj=po
po.is_selected=True
else: po.is_selected=False
if obj is not None:
self.selectedObject=obj
#try to update the block panel
if obj.phase=='BLOCK':
self.update_GENBLOCK_PROPS()
self.logDataTree()
except:
PrintException()
#find selection in current data
def setComboIndexfromItem(self,combo,item):
index=0
#print(combo,item)
index=0
try:
index=combo.Items.index(item)
except:pass
#combo.SelectedIndex=index
combo.Text=str(item)
def update_GENBLOCK_PROPS(self):
try:
clear=False
if self.selectedObject is None: clear=True
elif self.selectedObject.phase!='BLOCK': clear=True
if clear:
self.form.UI_GENBLOCK.lb_selected_block.Text='Select Block to edit'
self.form.UI_GENBLOCK.combo_typeIndex1.SelectedIndex=0
self.form.UI_GENBLOCK.combo_typeIndex2.SelectedIndex=0
self.form.UI_GENBLOCK.combo_typeTopIndex.SelectedIndex=0
else:
obj=self.selectedObject
self.form.UI_GENBLOCK.lb_selected_block.Text='Editing '+shortGuid(obj.guid)
self.setComboIndexfromItem(self.form.UI_GENBLOCK.combo_typeIndex1,obj.typeIndices[0])
self.setComboIndexfromItem(self.form.UI_GENBLOCK.combo_typeIndex2,obj.typeIndices[1])
self.setComboIndexfromItem(self.form.UI_GENBLOCK.combo_typeTopIndex,obj.typeIndices[2])
except:PrintException()
def handle_TOOLBAR_bt_delete(self,sender,e):
print('del pressed')
if self.selectedObject:
print('deleting:',self.selectedObject.to_string())
self.selectedObject.delete()
self.logDataTree()
def handle_GENTYPESRF_bt_viewSrf(self,sender,e):
print('view srf clicked')
global VIEWMODE
VIEWMODE='TYPESRF'
self.isolateLayer(get_layer_name('TYPESRF'))
self.highlightSelection()
#self.viewMode_srfType='SRF'
def handle_GENBLOCK_bt_interact(self,sender,e):
try:
if self.interaction_mode.auto_block:
self.form.UI_GENBLOCK.bt_interact.Text='-Intr'
self.interaction_mode.auto_block=False
else:
self.form.UI_GENBLOCK.bt_interact.Text='+Intr'
self.interaction_mode.auto_block=True
except: PrintException()
def handle_GENTYPESRF_bt_viewMesh(self,sender,e):
print('view mesh clicked')
global VIEWMODE
VIEWMODE='TYPESRF'
self.isolateLayer(get_layer_name('TYPEMESH'))
self.highlightSelection()
def handle_GENTYPESRF_bt_inspect(self,sender,e):
obj=rs.GetObject('sel obj to inspect')
if obj is None:
print('nothing selected')
return
self.inspectObject(obj)
def handle_GENTYPESRF_bts1(self):
form=self.form
bts=form.UI_GENTYPESRF.bts1
# print(bts)
class handleBt1():
def __init__(self,i,engine):
self.index=i
self.engine=engine
self.toggle=False
def handle(self,sender,e):
if not self.toggle:
self.toggle=True
rs.UnselectAllObjects()
self.engine.clearSelections()
selection=[]
#print('handle bt 1:',self.index)
for o in self.engine.data:
# print('bt1 Click:',o.typeIndex,self.index,o.guid)
if o.typeIndex==self.index:
# print('selected')
try:
if rs.IsObject(o.guid):
selection.append(o)
except Exception as e: print('!except handleBt1 sselect ',e)
# print('bt1 Click selection size :',len(selection))
self.engine.setSelection(selection)
self.engine.highlightSelection()
else:
self.toggle=False
self.engine.setSelection([])
rs.UnselectAllObjects()
from Eto.Drawing import Color
for i in range(0,len(bts)):
bt=bts[i]
handler=handleBt1(i,self)
bt.Click+=handler.handle
def handle_GENTYPESRF_bts2(self):
form=self.form
#handle the second button which sets srf to a type
bts=form.UI_GENTYPESRF.bts2
# print(bts)
class handleBt2():
def __init__(self,i,engine):
self.index=i
self.engine=engine
def handle(self,sender,e):
try:
rs.UnselectAllObjects()
self.engine.clearSelections()
objs=rs.SelectedObjects()
if len(objs)==0:
objs=rs.GetObjects('select obj to change type, and press enter')
#case of mesh view
for ro in objs:
po,upStream=self.engine.getObjectByGuid(ro)
upStream.typeIndex=self.index
rs.ObjectColor(upStream.guid,SRFTYPECOLORS[self.index])
#TODO:the following line deleted upstream from engine
#TODO:color is not updating
# self.engine.genTypeMeshObject(upStream)
except Exception as e: print(e)
for i in range(0,len(bts)):
bt=bts[i]
handler=handleBt2(i,self)
bt.Click+=handler.handle
def handle_GENTYPESRF_comboBox(self):
form=self.form
combos=form.UI_GENTYPESRF.combos
class handleComboBox():
def __init__(self,i,engine,combo):
self.index=i
self.combo=combo
self.engine=engine
def handle(self,sender,arg):
try:
selIndex=self.combo.SelectedIndex
filename=self.combo.Items[selIndex]
print('combo box index changed to ',selIndex,filename.Text)
self.engine.updateFacadeType(self.index,filename.Text)
except Exception :PrintException()
try:
self.engine.genTypeMesh(self.index)
except Exception as e:
print('@ handle combo change',Exception,':',e)
for i in range(0,len(combos)):
combo=combos[i]
handler=handleComboBox(i,self,combo)
combo.SelectedIndexChanged+=handler.handle
#SelectedIndexChanged
def handle_GENBLOCK_bt_view_block(self,sender,e):
self.isolateLayer(get_layer_name('BLOCK'))
def handle_GENBLOCK_bt_view_srf(self,sender,e):
self.suspendInteraction()
rs.EnableRedraw(False)
try:
layername=get_layer_name('MASSING')
tolerance=0.0001
self.isolateLayer(layername)
sel=rs.ObjectsByLayer(layername)
print('sel from MASSING layer:',sel)
self.deleteObjectsByGuid(sel)
rs.CurrentLayer(layername)
#blocks=rs.ObjectsByLayer(get_layer_name('BLOCK'))
cons=[('phase','BLOCK')]
obj_blocks=self.data.find_all(cons)
print('flag_1')
rhi_blocks=self.data.find_all_guids(cons)
print('flag_2',rhi_blocks)
cblocks=rs.CopyObjects(rhi_blocks)
ublocks=rs.BooleanUnion(cblocks,True)
splitedSrfs=[]
horzSrfs=[]
vertSrfs=[]
#找出union block里的横竖面
print('Sparate horz and vert srfs')
for b in ublocks:
os=rs.ExplodePolysurfaces(b,True)
print('os',os)
#先把水平面分走
horzSrfs=[]
vertSrfs=[]
for s in os:
print('line 740')
if s is None:
continue
if not rs.IsObject(s):
continue
isHor,direct=isHorizonalSrf(s,True)
if isHorizonalSrf(s):
if direct<0:
rs.ObjectColor(s,(255,0,0))
else:
rs.ObjectColor(s,COLOR_SET_01[5])
horzSrfs.append(s)
else : vertSrfs.append(s)
blockedSrf=[]
parentDic={}
wheel=0
print('assign parent objects')
#Union block的横竖面找parent
for po in obj_blocks:
srfs=rs.ExplodePolysurfaces(po.guid,False)
for vsrf in vertSrfs:
pts2=rs.SurfaceEditPoints(vsrf)
for s in srfs:
pts1=rs.SurfaceEditPoints(s)
if listsEqual(pts1,pts2):
parentDic[vsrf]=po
rs.DeleteObjects(srfs)
print(parentDic)
print('split irregular polygons')
for s in vertSrfs:
parent=parentDic[s]
if parent is None:
print('parent is None')
rs.SelectObject(s)
continue
#rs.EnableRedraw(True)
phaseIndex='MASSING'
typeIndex=parent.typeIndices[0]
boundary=rs.DuplicateSurfaceBorder(s)
pts=rs.CurveEditPoints(boundary)
if len(pts)>5:
#print('splitting polygon')
#rs.EnableRedraw(False)
srfs=splitIrregularPolygon(s)
#print('splitIregPoly srfs=',srfs)
if srfs is None:
continue
splitedSrfs+=srfs
for ss in srfs:
#print(shortGuid(parent.guid))
o=self.addObject(ss,phaseIndex,typeIndex,parent)
if o is None: continue
self.setObjectType(o,typeIndex)
#rs.EnableRedraw(True)
else:
splitedSrfs.append(s)
o=None
try:
o=self.addObject(s,phaseIndex,typeIndex,parent)
except Exception as e:
print(e,s,phaseIndex,typeIndex,parent)
if o is None: continue
#print('o=',o)
self.setObjectType(o,typeIndex)
#self.logDataTree()
rs.DeleteObject(boundary)
except Exception as e:
print('exception:',e)
#PrintException()
rs.EnableRedraw(True)
self.resumeInteraction()
rs.EnableRedraw(True)
#TODO:give properties to splited srfs base on their belonging blocks
#splitedSrfs contain the splited srfs
#TODO:join the srfs that share the same manifold
# for i in range(0,len(splitedSrfs)):
# for j in range(0,len(splitedSrfs)):
# if i==j: continue
def handle_GENBLOCK_combo_updates(self,sender,e):
# txts=self.form.UI_GENBLOCK.lb_selected_block.Text
# combo=self.form.UI_GENBLOCK.combo_typeIndex1
# txts=txts.aplit(' ')
try:
if self.selectedObject is None: return
obj=self.selectedObject
ui=self.form.UI_GENBLOCK
obj.typeIndices[0]=ui.combo_typeIndex1.Items[ui.combo_typeIndex1.SelectedIndex].Text
obj.typeIndices[1]=ui.combo_typeIndex2.Items[ui.combo_typeIndex2.SelectedIndex].Text
obj.typeIndices[2]=ui.combo_typeTopIndex.Items[ui.combo_typeTopIndex.SelectedIndex].Text
colorI=int(obj.typeIndices[0])
colorI=colorI%len(COLOR_SET_01)
color=COLOR_SET_01[colorI]
rs.ObjectColor(obj.guid,color)
except:PrintException()
#update phase object rhino properties
def updatePhaseObjectColor(self,obj):
phase=obj.phase
index=obj.typeIndex
colors=PHASE_OBJECT_COLORS[phase]
colorIndex=index%len(colors)
color=colors[colorIndex]
#print('@updateColor:',color,obj.guid)
rs.ObjectColor(obj.guid,color)
#TODO:return the color PHASE_OBJECT_COLORS[phase][typeIndex]
#existing model treatments
def importSrfTypesFromScene(self):
data=[]
# srfs=rs.ObjectsByLayer(get_layer_name('TYPESRF'))
# for f in srfs:
# #rs.SelectObject(f)
# #rs.ObjectColor(f,(0,0,1))
# po=PhaseObject()
# po.guid=f
# po.phase='TYPESRF'
# po.typeIndex=int(rs.ObjectName(f))
# po.strTypeDescription=rs.ObjectName(f)
# self.updatePhaseObjectColor(po)
# data.append(po)
# self.data=data
# return
objs=rs.ObjectsByLayer(get_layer_name('BLOCK'))
for o in objs:
po=PhaseObject()
po.guid=o
po.phase='BLOCK'
name=rs.ObjectName(o)
typeIndex=0
try: typeIndex=int(name)
except:pass
print(name,typeIndex)
self.setObjectType(po,typeIndex,0)
data.append(po)
self.data=data
# def fixCurrentModel(self):
# #fix srf in current scene
# layer=get_layer_name('TYPESRF')
# srfs=rs.ObjectsByLayer(layer)
# counter=0
# global SRFTYPECOLORS
# for f in srfs:
# rs.ObjectLayer(f,layer)
# typeindex=int(rs.ObjectName(f))
# index=typeindex%len(SRFTYPECOLORS)
# color=SRFTYPECOLORS[index]
# rs.ObjectColor(f,color)
# counter+=1