-
Notifications
You must be signed in to change notification settings - Fork 7
/
spb_Crv_fitRebuild.py
1708 lines (1360 loc) · 61.2 KB
/
spb_Crv_fitRebuild.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
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
An alternative to _Rebuild for curves, this script can test different degrees and control
point counts to find a rebuild result within a deviation tolerance of the input curve.
TODO:
Limit distance between consecutive control points?
"""
from __future__ import absolute_import, division, print_function, unicode_literals
"""
170624-27: Created.
...
200307: When degree is not specified, now iterates from degree 3 to 5 before incrementing the number of control points.
200401, 10: Improved handling of bad deviation result for closed curves.
200610: Import-related update. Purged some of this history.
200611: Bug fix.
200622: Modified some option inputs.
210312: Modified some option default values. Removed a print(statement used for debugging.
210412: Added filter for PolylineCurves.
220328: Added bPreserveEndG2 option.
220809, 0823, 0910: Import-related update.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import rhinoscriptsyntax as rs
import scriptcontext as sc
from System import Guid
import Eto.Drawing as drawing
import Eto.Forms as forms
import xCurve
import spb_Crv_deviation
import spb_NurbsCrv_fitByTranslatingControlPts
class Opts:
keys = []
values = {}
names = {}
sDialogTexts = {}
riOpts = {}
riAddOpts = {}
stickyKeys = {}
def addOptionDouble(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionDouble(
getObj, englishName=names[key], numberValue=riOpts[key])
def addOptionInteger(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionInteger(
getObj, englishName=names[key], intValue=riOpts[key])
def addOptionList(key, names, listValues, values):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionList(
getObj,
englishOptionName=names[key],
listValues=listValues,
listCurrentIndex=values[key])
def addOptionToggle(key, names, riOpts):
return lambda getObj: ri.Custom.GetBaseClass.AddOptionToggle(
getObj, englishName=names[key], toggleValue=riOpts[key])
key = 'fDevTol'; keys.append(key)
values[key] = 0.1 * sc.doc.ModelAbsoluteTolerance
names[key] = key[1:]
sDialogTexts[key] = ""
riOpts[key] = ri.Custom.OptionDouble(values[key])
riAddOpts[key] = addOptionDouble(key, names, riOpts)
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'bPreserveEndG1'; keys.append(key)
values[key] = True
names[key] = key[1:]
sDialogTexts[key] = "Preserve end tangent dirs."
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bPreserveEndG2'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Preserve end curvatures/radii"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bFurtherTranslateCps'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Further translate CPs to minimize CP count (May be slow.)"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bLimitDegree'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Limit degree"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bMatchDegree'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Same as input curve"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iDegree'; keys.append(key)
values[key] = 3
names[key] = 'CrvDegree'
sDialogTexts[key] = ""
riOpts[key] = ri.Custom.OptionInteger(values[key])
riAddOpts[key] = addOptionInteger(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bLimitMinCpCt'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Limit min. CP count"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iMinCpCt'; keys.append(key)
values[key] = 4
names[key] = key[1:]
sDialogTexts[key] = "Min. CP count"
riOpts[key] = ri.Custom.OptionInteger(values[key])
riAddOpts[key] = addOptionInteger(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bLimitMaxCpCt'; keys.append(key)
values[key] = True
names[key] = key[1:]
sDialogTexts[key] = "Limit max. CP count"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bMatchMaxCpCt'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Same as input curve"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iMaxCpCt'; keys.append(key)
values[key] = 32
names[key] = key[1:]
sDialogTexts[key] = "Max. CP count"
riOpts[key] = ri.Custom.OptionInteger(values[key])
riAddOpts[key] = addOptionInteger(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessPolyCrv'; keys.append(key)
values[key] = True
names[key] = 'PolyCrv'
sDialogTexts[key] = "Polycurves"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessPolyCrvSegs'; keys.append(key)
values[key] = True
names[key] = 'Process'
sDialogTexts[key] = (
None,
"In whole ",
"Each segment individually")
riOpts[key] = ri.Custom.OptionToggle(values[key], 'WholePolyCrv', 'EachSegment')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessLinear'; keys.append(key)
values[key] = False
names[key] = 'Linear'
sDialogTexts[key] = "Lines"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessPolyline'; keys.append(key)
values[key] = False
names[key] = 'PolylineInWhole'
sDialogTexts[key] = "Polylines in whole"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessArc'; keys.append(key)
values[key] = False
names[key] = 'Arc'
sDialogTexts[key] = "Arcs"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessEllipse'; keys.append(key)
values[key] = True
names[key] = 'Ellipse'
sDialogTexts[key] = "Ellipses"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessOtherRat'; keys.append(key)
values[key] = True
names[key] = 'OtherRationalNurbs'
sDialogTexts[key] = "Other rational NURBS"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessNonRatWithInternalPolyknot'; keys.append(key)
values[key] = True
names[key] = 'NurbsWithInternalPolyknots'
sDialogTexts[key] = "Having internal polyknots"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessNonRatWithSomeFullPolyknot'; keys.append(key)
values[key] = False
names[key] = 'Multiplicity'
sDialogTexts[key] = (
None,
"Any multiplicity ",
"Some full")
riOpts[key] = ri.Custom.OptionToggle(values[key], 'Any', 'SomeFull')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessUniformNonRat'; keys.append(key)
values[key] = False
names[key] = 'Uniform'
sDialogTexts[key] = "Uniform"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessBezierNonRat'; keys.append(key)
values[key] = False
names[key] = 'Bezier'
sDialogTexts[key] = "Bezier"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bProcessOtherNonRat'; keys.append(key)
values[key] = True
names[key] = 'NonrationalNurbs'
sDialogTexts[key] = "Other"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bReplace'; keys.append(key)
values[key] = True
names[key] = 'Action'
sDialogTexts[key] = "Output:", "Add new ", "Replace input"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'Add', 'Replace')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'; keys.append(key)
values[key] = True
names[key] = key[1:]
sDialogTexts[key] = "Echo"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
names[key] = key[1:]
sDialogTexts[key] = "Debug"
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
riAddOpts[key] = addOptionToggle(key, names, riOpts)
stickyKeys[key] = '{}({})'.format(key, __file__)
@classmethod
def loadSticky(cls):
for key in cls.stickyKeys:
if cls.stickyKeys[key] in sc.sticky:
if key in cls.riOpts:
cls.riOpts[key].CurrentValue = cls.values[key] = sc.sticky[cls.stickyKeys[key]]
else:
cls.values[key] = sc.sticky[cls.stickyKeys[key]]
@classmethod
def setValues(cls):
for key in cls.keys:
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
@classmethod
def saveSticky(cls):
for key in cls.stickyKeys:
if key in cls.riOpts:
sc.sticky[cls.stickyKeys[key]] = cls.riOpts[key].CurrentValue
else:
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
class FitRebuildCurveDialog(forms.Dialog[bool]):
def __init__(self):
Opts.loadSticky()
# Initialize dialog box
self.Title = 'Rebuild Curve'
self.Padding = drawing.Padding(10)
self.Resizable = False
self.divider = forms.Label()
#self.divider.Text = ''
self.divider.Height = 2
self.divider.BackgroundColor = drawing.Colors.Gray
self.radioButtonList = {}
self.checkBoxes = {}
self.labels = {}
self.textBoxes = {}
for key in Opts.keys:
if key[0] == 'b':
if isinstance(Opts.sDialogTexts[key], str):
self.checkBoxes[key] = forms.CheckBox(Text=Opts.sDialogTexts[key])
self.checkBoxes[key].Checked = Opts.values[key]
elif isinstance(Opts.sDialogTexts[key], tuple):
self.radioButtonList[key] = forms.RadioButtonList()
self.radioButtonList[key].DataStore = Opts.sDialogTexts[key][1:]
self.radioButtonList[key].SelectedIndex = int(Opts.values[key])
#self.radioButtonList[key].Orientation = forms.Orientation.Horizontal
elif key[0] == 'f':
self.labels[key] = forms.Label(Text=Opts.sDialogTexts[key])
self.textBoxes[key] = forms.TextBox(Text=str(Opts.values[key]))
elif key[0] == 'i':
self.labels[key] = forms.Label(Text=Opts.sDialogTexts[key])
self.textBoxes[key] = forms.TextBox(Text=str(int(Opts.values[key])))
elif key[0] == 's':
self.radioButtonList[key] = forms.RadioButtonList()
self.radioButtonList[key].DataStore = Opts.sDialogTexts[key]
self.radioButtonList[key].SelectedIndex = Opts.sDialogTexts[key].index(Opts.values[key])
# Set initial state.
self.enableTextBox_iDegree()
self.enableTextBox_iMinCpCt()
self.enableTextBox_iMaxCpCt()
self.enableCheckBox_bPreserveEndG2()
self.enableRadioButtonList_bProcessPolyCrvSegs()
self.enableRadioButtonList_bProcessNonRatWithOnlyFullyMultiK()
self.enableCheckBox_bProcessBezier()
# Events.
self.textBoxes['fDevTol'].TextChanged += self.onChange_fDevTol
self.checkBoxes['bLimitDegree'].CheckedChanged += self.onChange_bLimitDegree
self.checkBoxes['bMatchDegree'].CheckedChanged += self.onChange_bMatchDegree
self.textBoxes['iDegree'].TextChanged += self.onChange_iDegree
self.checkBoxes['bLimitMinCpCt'].CheckedChanged += self.onChange_bLimitMinCpCt
self.textBoxes['iMinCpCt'].TextChanged += self.onChange_iMinCpCt
self.checkBoxes['bLimitMaxCpCt'].CheckedChanged += self.onChange_bLimitMaxCpCt
self.checkBoxes['bMatchMaxCpCt'].CheckedChanged += self.onChange_bMatchMaxCpCt
self.textBoxes['iMaxCpCt'].TextChanged += self.onChange_iMaxCpCt
self.checkBoxes['bPreserveEndG1'].CheckedChanged += self.onChange_bPreserveEndG1
self.checkBoxes['bProcessPolyCrv'].CheckedChanged += self.OnChange_bProcessPolyCrv
self.checkBoxes['bProcessUniformNonRat'].CheckedChanged += self.OnChange_bProcessUniformNonRational
self.checkBoxes['bProcessNonRatWithInternalPolyknot'].CheckedChanged += self.OnChange_bProcessNonRatWithAnyMultiK
self.DefaultButton = forms.Button(Text = 'OK')
self.DefaultButton.Click += self.onOkButtonClick
self.AbortButton = forms.Button(Text = 'Cancel')
self.AbortButton.Click += self.onCancelButtonClick
# Layout.
layout = forms.DynamicLayout()
#layout.Spacing = drawing.Size(5, 5)
#layout.DefaultPadding = 5
layout.DefaultSpacing = drawing.Size(5, 5)
layout.AddSeparateRow(
None,
"Deviation tolerance",
self.textBoxes['fDevTol'],
)
layout.BeginVertical()
layout.AddSpace()
layout.AddRow(
self.checkBoxes['bLimitDegree'],
self.checkBoxes['bMatchDegree'],
self.textBoxes['iDegree'],
)
layout.AddSpace()
layout.AddRow(
self.checkBoxes['bLimitMinCpCt'],
None,
self.textBoxes['iMinCpCt'],
)
layout.AddSpace()
layout.AddRow(
self.checkBoxes['bLimitMaxCpCt'],
self.checkBoxes['bMatchMaxCpCt'],
self.textBoxes['iMaxCpCt'],
)
layout.EndVertical()
layout.AddSpace()
layout.AddSpace()
layout.BeginVertical()
layout.AddRow(
self.checkBoxes['bPreserveEndG1'],
self.checkBoxes['bPreserveEndG2'],
)
layout.EndVertical()
layout.AddSpace()
layout.AddSpace()
layout.BeginVertical()
layout.AddRow(self.checkBoxes['bFurtherTranslateCps'])
layout.EndVertical()
layout.AddSpace()
layout.AddSpace()
layout.AddRow("Process these curves:")
layout.EndVertical()
layout.BeginVertical()
layout.AddSeparateRow(
"",
self.checkBoxes['bProcessPolyCrv'],
self.radioButtonList['bProcessPolyCrvSegs'])
layout.AddSeparateRow(
"",
self.checkBoxes['bProcessLinear'],
self.checkBoxes['bProcessPolyline'],
)
layout.AddSeparateRow(
"",
self.checkBoxes['bProcessArc'],
self.checkBoxes['bProcessEllipse'],
self.checkBoxes['bProcessOtherRat'],
)
layout.AddSeparateRow("", "Non-rational NURBS:")
layout.AddSeparateRow(
" ",
self.checkBoxes['bProcessNonRatWithInternalPolyknot'],
self.radioButtonList['bProcessNonRatWithSomeFullPolyknot'],
)
layout.AddSeparateRow(
" ",
self.checkBoxes['bProcessUniformNonRat'],
self.checkBoxes['bProcessBezierNonRat'],
)
layout.AddSeparateRow(
" ",
self.checkBoxes['bProcessOtherNonRat'],
None,
)
layout.EndVertical()
layout.AddSeparateRow("")
key = 'bReplace'
layout.AddSeparateRow(Opts.sDialogTexts[key][0], self.radioButtonList[key])
layout.BeginVertical()
layout.AddRow("")
layout.AddRow(
self.checkBoxes['bEcho'],
None,
self.checkBoxes['bDebug'],
)
layout.AddRow("")
layout.EndVertical()
layout.AddSeparateRow(None, self.DefaultButton, None, self.AbortButton, None)
#layout.AddRow(self.okButton, self.cancelButton)
# Set the dialog content
self.Content = layout
# Functions that set properties of controls based on the values of other controls.
# This is used in both __init__ and by individual onChange methods.
def enableCheckBox_bMatchDegree(self):
self.checkBoxes['bMatchDegree'].Enabled = (
self.checkBoxes['bLimitDegree'].Checked)
def enableTextBox_iDegree(self):
self.textBoxes['iDegree'].Enabled = (
self.checkBoxes['bLimitDegree'].Checked and
not self.checkBoxes['bMatchDegree'].Checked)
def enableTextBox_iMinCpCt(self):
self.textBoxes['iMinCpCt'].Enabled = (
self.checkBoxes['bLimitMinCpCt'].Checked)
def enableCheckBox_bMatchMaxCpCt(self):
self.checkBoxes['bMatchMaxCpCt'].Enabled = (
self.checkBoxes['bLimitMaxCpCt'].Checked)
def enableTextBox_iMaxCpCt(self):
self.textBoxes['iMaxCpCt'].Enabled = (
self.checkBoxes['bLimitMaxCpCt'].Checked and
not self.checkBoxes['bMatchMaxCpCt'].Checked)
def enableCheckBox_bPreserveEndG2(self):
self.checkBoxes['bPreserveEndG2'].Enabled = (
self.checkBoxes['bPreserveEndG1'].Checked)
def enableRadioButtonList_bProcessPolyCrvSegs(self):
self.radioButtonList['bProcessPolyCrvSegs'].Enabled = (
self.checkBoxes['bProcessPolyCrv'].Checked)
def enableRadioButtonList_bProcessNonRatWithOnlyFullyMultiK(self):
self.radioButtonList['bProcessNonRatWithSomeFullPolyknot'].Enabled = (
self.checkBoxes['bProcessNonRatWithInternalPolyknot'].Checked)
def enableCheckBox_bProcessBezier(self):
self.checkBoxes['bProcessBezierNonRat'].Enabled = (
self.checkBoxes['bProcessUniformNonRat'].Checked)
def onChange_bLimitCrvDev(self, sender, e):
self.enableTextBox_fDevTol()
def onChange_fDevTol(self, sender, e):
try:
float(self.textBoxes['fDevTol'].Text)
self.textBoxes['fDevTol'].BackgroundColor = drawing.SystemColors.ControlBackground
except:
self.textBoxes['fDevTol'].BackgroundColor = drawing.Colors.Red
def onChange_bLimitDegree(self, sender, e):
self.enableCheckBox_bMatchDegree()
self.enableTextBox_iDegree()
def onChange_bMatchDegree(self, sender, e):
self.enableTextBox_iDegree()
def onChange_iDegree(self, sender, e):
try:
iDegree = int(self.textBoxes['iDegree'].Text)
if iDegree < 3:
iDegree = 3
elif iDegree > 5:
iDegree = 5
self.textBoxes['iDegree'].Text = str(iDegree)
self.textBoxes['iDegree'].BackgroundColor = drawing.SystemColors.ControlBackground
except:
self.textBoxes['iDegree'].BackgroundColor = drawing.Colors.Red
def onChange_bLimitMinCpCt(self, sender, e):
self.enableTextBox_iMinCpCt()
def onChange_iMinCpCt(self, sender, e):
try:
iMinCpCt = int(self.textBoxes['iMinCpCt'].Text)
self.textBoxes['iMinCpCt'].BackgroundColor = drawing.SystemColors.ControlBackground
except:
self.textBoxes['iMinCpCt'].BackgroundColor = drawing.Colors.Red
def onChange_bLimitMaxCpCt(self, sender, e):
self.enableCheckBox_bMatchMaxCpCt()
self.enableTextBox_iMaxCpCt()
def onChange_bMatchMaxCpCt(self, sender, e):
self.enableTextBox_iMaxCpCt()
def onChange_iMaxCpCt(self, sender, e):
try:
iMaxCpCt = int(self.textBoxes['iMaxCpCt'].Text)
self.textBoxes['iMaxCpCt'].BackgroundColor = drawing.SystemColors.ControlBackground
except:
self.textBoxes['iMaxCpCt'].BackgroundColor = drawing.Colors.Red
def onChange_bPreserveEndG1(self, sender, e):
self.enableCheckBox_bPreserveEndG2()
def OnChange_bProcessPolyCrv(self, sender, e):
self.enableRadioButtonList_bProcessPolyCrvSegs()
def OnChange_bProcessNonRatWithAnyMultiK(self, sender, e):
self.enableRadioButtonList_bProcessNonRatWithOnlyFullyMultiK()
def OnChange_bProcessUniformNonRational(self, sender, e):
self.enableCheckBox_bProcessBezier()
def onCancelButtonClick(self, sender, e):
self.Close(False)
def onOkButtonClick(self, sender, e):
# Save values.
for key in Opts.keys:
if key in self.checkBoxes:
Opts.values[key] = self.checkBoxes[key].Checked
elif key in self.radioButtonList:
if key[0] == 's':
Opts.values[key] = (
Opts.sDialogTexts[key][self.radioButtonList[key].SelectedIndex]
)
if key[0] == 'b':
Opts.values[key] = bool(self.radioButtonList[key].SelectedIndex)
elif key in self.textBoxes:
if key[0] == 'f':
try:
Opts.values[key] = float(self.textBoxes[key].Text)
except:
s = "Invalid input for {}.".format(key[1:])
s += " {} will be used instead.".format(Opts.values[key])
print(s)
if key[0] == 'i':
try:
Opts.values[key] = int(self.textBoxes[key].Text)
except:
s = "Invalid input for {}.".format(key[1:])
s += " {} will be used instead.".format(Opts.values[key])
print(s)
# Save sticky.
for key in Opts.keys:
if key in Opts.stickyKeys:
sc.sticky[Opts.stickyKeys[key]] = Opts.values[key]
self.Close(True)
def getPreselectedCurves():
gObjs_Preselected = [rdObj.Id for rdObj in sc.doc.Objects.GetSelectedObjects(includeLights=False, includeGrips=False)]
if gObjs_Preselected:
gCrvs_Preselected = []
iter = rd.ObjectEnumeratorSettings()
iter.NormalObjects = True
iter.LockedObjects = False
iter.IncludeLights = False
iter.IncludeGrips = False
for rdRhinoObject in sc.doc.Objects.GetObjectList(iter):
if rdRhinoObject.Id in gObjs_Preselected:
if rdRhinoObject.ObjectType == rd.ObjectType.Curve:
gCrvs_Preselected.append(rdRhinoObject.Id)
if gCrvs_Preselected:
if Opts.values['bEcho']:
s = "{} curves".format(len(gCrvs_Preselected))
s += " were preselected and will thus be the selection set."
print(s)
return tuple(gCrvs_Preselected)
def getInput():
"""Get curves with optional input."""
def setCurveFilter():
go_CrvTypes = ri.Custom.GetOption()
go_CrvTypes.SetCommandPrompt("Curve type(s) to process")
idxs_Opt_CrvTypes = {}
while True:
key = 'bProcessPolyCrv'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessPolyCrvSegs'; idxs_Opt_CrvTypes[key] = (Opts.riAddOpts[key](go_CrvTypes)
if Opts.values['bProcessPolyCrv']
else None)
key = 'bProcessLinear'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessPolyline'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessArc'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessEllipse'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessOtherRat'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessNonRatWithInternalPolyknot'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessNonRatWithSomeFullPolyknot'; idxs_Opt_CrvTypes[key] = (
Opts.riAddOpts[key](go_CrvTypes)
if Opts.values['bProcessNonRatWithInternalPolyknot']
else None)
key = 'bProcessUniformNonRat'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
key = 'bProcessBezierNonRat'; idxs_Opt_CrvTypes[key] = (Opts.riAddOpts[key](go_CrvTypes)
if Opts.values['bProcessUniformNonRat']
else None)
key = 'bProcessOtherNonRat'; idxs_Opt_CrvTypes[key] = Opts.riAddOpts[key](go_CrvTypes)
#idxs_Opt_CrvTypes['YesToAll'] = go_CrvTypes.AddOption('YesToAll')
#idxs_Opt_CrvTypes['NoToAll'] = go_CrvTypes.AddOption('NoToAll')
res = go_CrvTypes.Get()
if res != ri.GetResult.Option:
break
#elif go_CrvTypes.OptionIndex() == idxs_Opt_CrvTypes['YesToAll']:
# for key in sCrvFilterOpts:
# Opts.riOpts[key].CurrentValue = True
#elif go_CrvTypes.OptionIndex() == idxs_Opt_CrvTypes['NoToAll']:
# for key in sCrvFilterOpts:
# Opts.riOpts[key].CurrentValue = False
Opts.setValues()
Opts.saveSticky()
go_CrvTypes.ClearCommandOptions()
sCrvFilterOpts = [s[8:] for s in Opts.keys if s[:8] == 'bProcess']
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select curves")
go.GeometryFilter = rd.ObjectType.Curve
# # Custom geometry filter to only select NurbsCurve wire curves.
# def curvesEdgesNotInBlockInstGeomFilter(rdObj, geom, compIdx):
# if rdObj.ObjectType == rd.ObjectType.InstanceReference: return False
# print(geom
# print(compIdx.Index
# return isinstance(geom, rg.Curve)
# return geom.GetType() == rg.Curve
# go.SetCustomGeometryFilter(curvesEdgesNotInBlockInstGeomFilter)
go.AcceptNumber(True, True)
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
go.EnableClearObjectsOnEntry(False) # Do not clear objects in go on repeats of While loop.
go.EnableUnselectObjectsOnExit(False) # Do not unselect object when an option selected, a number is entered, etc.
print("\nMaxCpCt: \"0\" will allow any amount.")
print("Curve types to process: {}".format(
', '.join([s for s in sCrvFilterOpts if Opts.values['bProcess'+s]])))
#bPreselectedObjsChecked = False
Opts.loadSticky()
idxs_Opts = {}
while True:
idxs_Opts['CrvFilter'] = go.AddOption('CrvTypesToProcess')
key = 'fDevTol'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bPreserveEndG1'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bPreserveEndG2'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bFurtherTranslateCps'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bLimitDegree'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bMatchDegree'; idxs_Opts[key] = (Opts.riAddOpts[key](go)[0]
if Opts.values['bLimitDegree']
else None)
key = 'iDegree'; idxs_Opts[key] = (Opts.riAddOpts[key](go)[0]
if (
Opts.values['bLimitDegree'] and
not Opts.values['bMatchDegree'])
else None)
key = 'bLimitMinCpCt'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'iMinCpCt'; idxs_Opts[key] = (Opts.riAddOpts[key](go)[0]
if Opts.values['bLimitMinCpCt']
else None)
key = 'bLimitMaxCpCt'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bMatchMaxCpCt'; idxs_Opts[key] = (Opts.riAddOpts[key](go)[0]
if Opts.values['bLimitMaxCpCt']
else None)
key = 'iMaxCpCt'; idxs_Opts[key] = (Opts.riAddOpts[key](go)
if (
Opts.values['bLimitMaxCpCt'] and
not Opts.values['bMatchDegree'])
else None)
key = 'bReplace'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bEcho'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
key = 'bDebug'; idxs_Opts[key] = Opts.riAddOpts[key](go)[0]
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
## Use bPreselectedObjsChecked so that only objects before the
## first call to go.GetMultiple is considered.
#if not bPreselectedObjsChecked and go.ObjectsWerePreselected:
# bPreselectedObjsChecked = True
# go.EnablePreSelect(False, ignoreUnacceptablePreselectedObjects=True)
if res == ri.GetResult.Cancel:
go.Dispose()
return
elif res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return tuple(
[objrefs]
+
[Opts.values[key] for key in Opts.keys])
else:
# An option was selected or a number was entered.
key = 'fDevTol'
if go.Option().Index == idxs_Opts['CrvFilter']:
setCurveFilter()
if res == ri.GetResult.Number:
Opts.riOpts[key].CurrentValue = go.Number()
if Opts.riOpts[key].CurrentValue < 0.0:
Opts.riOpts[key].CurrentValue = Opts.riOpts[key].InitialValue
Opts.setValues()
Opts.saveSticky()
go.ClearCommandOptions()
def removeNesting(rgCrv0):
"""
Parameters:
rgCrv0: rg.PolylineCurve or rg.
Returns:
Success: Curves without nesting.
Fail or N/A: None
"""
def convertMonoSpanPolylineCrvToLineCrv(crv):
if isinstance(crv, rg.PolylineCurve):
if crv.PointCount == 2:
line = rg.Line(crv.PointAtStart, crv.PointAtEnd)
return rg.LineCurve(line)
sType_C0 = rgCrv0.GetType().Name
if not sType_C0 == 'PolyCurve' or sType_C0 == 'PolylineCurve':
return None
rgCrv_WIP = rgCrv0.DuplicateCurve()
if sType_C0 == 'PolylineCurve':
rc = convertMonoSpanPolylineCrvToLineCrv(rgCrv_WIP)
if rc:
rgCrv_WIP.Dispose()
return rc
elif sType_C0 == 'PolyCurve':
bNestingRemoved = rgCrv_WIP.RemoveNesting()
if rgCrv_WIP.SegmentCount == 1:
exploded = rgCrv0.Explode()[0]
rgCrv_WIP.Dispose()
rgCrv_WIP = exploded
rc = convertMonoSpanPolylineCrvToLineCrv(rgCrv_WIP)
if rc:
rgCrv_WIP.Dispose()
return rc
# Process each segment in polysegment PolyCurve.
bSuccess, pline = rgCrv_WIP.TryGetPolyline()
if bSuccess:
rgCrv_WIP.Dispose()
return pline.ToPolylineCurve()
segs_forOut = []
bNewSeg = False
for seg in rgCrv_WIP.DuplicateSegments():
rc = convertMonoSpanPolylineCrvToLineCrv(seg)
if rc:
seg.Dispose()
segs_forOut.append(rc)
bNewSeg = True
else:
segs_forOut.append(rgCrv_WIP)
if bNewSeg:
rgCrv_WIP.Dispose()
joined = rg.Curve.JoinCurves(segs_forOut)
if len(joined) != 1:
raise (ValueError ("{} curves resulted from Curve.JoinCurves.".format(
len(joined))))
return joined
else:
for seg in segs_forOut: seg.Dispose()
if bNestingRemoved: return rgCrv_WIP
rgCrv_WIP.Dispose()
def doesCurvePassTypeFilter(rgCurve0, **kwargs):
"""
Returns: bool
"""
def getOpt(key): return kwargs[key] if key in kwargs else Opts.values[key]
bProcessPolyCrv = getOpt('bProcessPolyCrv')
bProcessLinear = getOpt('bProcessLinear')
bProcessPolyline = getOpt('bProcessPolyline')
bProcessArc = getOpt('bProcessArc')
bProcessEllipse = getOpt('bProcessEllipse')
bProcessOtherRat = getOpt('bProcessOtherRat')
bProcessNonRatWithInternalPolyknot = getOpt('bProcessNonRatWithInternalPolyknot')
bProcessNonRatWithSomeFullPolyknot = getOpt('bProcessNonRatWithSomeFullPolyknot')
bProcessUniformNonRat = getOpt('bProcessUniformNonRat')
bProcessBezierNonRat = getOpt('bProcessBezierNonRat')
bProcessOtherNonRat = getOpt('bProcessOtherNonRat')
sType_c0 = rgCurve0.GetType().Name
if sType_c0 == 'PolyCurve':
if bProcessPolyCrv:
return True, None
else:
return False, "Skipped PolyCurve."
if sType_c0 == 'LineCurve':
if bProcessLinear:
return True, None
else:
return False, "Skipped LineCurve."
if sType_c0 == 'PolylineCurve':
if bProcessPolyline:
return True, None
else:
return False, "Skipped PolylineCurve."
if sType_c0 == 'ArcCurve':
if bProcessArc:
return True, None
else:
return False, "Skipped ArcCurve."
if rgCurve0.IsLinear(1e-6):
if bProcessLinear:
return True, None
else:
return False, "Skipped linear {}.".format(sType_c0)
rc = xCurve.getArcCurve(
rgCrv0=rgCurve0,
bTolByRatio=False,
fTolRatio=None,
fDevTol=1e-9,
fMinNewCrvLen=100.0*sc.doc.ModelAbsoluteTolerance,
fMaxRadius=(1e6)*sc.doc.ModelAbsoluteTolerance)