-
Notifications
You must be signed in to change notification settings - Fork 50
/
archipack_stair.py
3110 lines (2774 loc) · 117 KB
/
archipack_stair.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
# -*- coding:utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- 1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
# noinspection PyUnresolvedReferences
import bpy
# noinspection PyUnresolvedReferences
from bpy.types import Operator, PropertyGroup, Mesh, Panel
from bpy.props import (
FloatProperty, BoolProperty, IntProperty, CollectionProperty,
StringProperty, EnumProperty, FloatVectorProperty
)
from .bmesh_utils import BmeshEdit as bmed
from .panel import Panel as Lofter
from mathutils import Vector, Matrix
from math import sin, cos, pi, floor, acos
from .archipack_manipulator import Manipulable, archipack_manipulator
from .archipack_2d import Line, Arc
from .archipack_preset import ArchipackPreset, PresetMenuOperator
from .archipack_object import ArchipackCreateTool, ArchipackObject
from .archipack_polylines import Io
from .archipack_dimension import DimensionProvider
class Stair():
def __init__(self, left_offset, right_offset, steps_type, nose_type, z_mode, nose_z, bottom_z):
self.steps_type = steps_type
self.nose_type = nose_type
self.l_shape = None
self.r_shape = None
self.next_type = 'NONE'
self.last_type = 'NONE'
self.z_mode = z_mode
# depth of open step
self.nose_z = nose_z
# size under the step on bottom
self.bottom_z = bottom_z
self.left_offset = left_offset
self.right_offset = right_offset
self.last_height = 0
def set_matids(self, matids):
self.idmat_top, self.idmat_step_front, self.idmat_raise, \
self.idmat_side, self.idmat_bottom, self.idmat_step_side = matids
def set_height(self, step_height, z0):
self.step_height = step_height
self.z0 = z0
@property
def height(self):
return self.n_step * self.step_height
@property
def top_offset(self):
return self.t_step / self.step_depth
@property
def top(self):
return self.z0 + self.height
@property
def left_length(self):
return self.get_length("LEFT")
@property
def right_length(self):
return self.get_length("RIGHT")
def step_size(self, step_depth):
t_step, n_step = self.steps(step_depth)
self.n_step = n_step
self.t_step = t_step
self.step_depth = step_depth
return n_step
def p3d_left(self, verts, p2d, i, t, landing=False):
x, y = p2d
if self.z_mode == '2D':
verts.append((x, y, 0))
else:
nose_z = min(self.step_height, self.nose_z)
zl = self.z0 + t * self.height
zs = self.z0 + i * self.step_height
if self.z_mode == 'LINEAR':
z0 = max(0, zl)
z1 = z0 - self.bottom_z
verts.extend([(x, y, z0), (x, y, z1)])
else:
if "FULL" in self.steps_type:
z0 = 0
else:
z0 = max(0, zl - nose_z - self.bottom_z)
z3 = zs + max(0, self.step_height - nose_z)
z4 = zs + self.step_height
if landing:
if "FULL" in self.steps_type:
z2 = 0
z1 = 0
else:
z2 = max(0, min(z3, z3 - self.bottom_z))
z1 = z2
else:
z1 = min(z3, max(z0, zl - nose_z))
z2 = min(z3, max(z1, zl))
verts.extend([(x, y, z0),
(x, y, z1),
(x, y, z2),
(x, y, z3),
(x, y, z4)])
def p3d_right(self, verts, p2d, i, t, landing=False):
x, y = p2d
if self.z_mode == '2D':
verts.append((x, y, 0))
else:
nose_z = min(self.step_height, self.nose_z)
zl = self.z0 + t * self.height
zs = self.z0 + i * self.step_height
if self.z_mode == 'LINEAR':
z0 = max(0, zl)
z1 = z0 - self.bottom_z
verts.extend([(x, y, z1), (x, y, z0)])
else:
if "FULL" in self.steps_type:
z0 = 0
else:
z0 = max(0, zl - nose_z - self.bottom_z)
z3 = zs + max(0, self.step_height - nose_z)
z4 = zs + self.step_height
if landing:
if "FULL" in self.steps_type:
z2 = 0
z1 = 0
else:
z2 = max(0, min(z3, z3 - self.bottom_z))
z1 = z2
else:
z1 = min(z3, max(z0, zl - nose_z))
z2 = min(z3, max(z1, zl))
verts.extend([(x, y, z4),
(x, y, z3),
(x, y, z2),
(x, y, z1),
(x, y, z0)])
def p3d_cstep_left(self, verts, p2d, i, t):
x, y = p2d
if self.z_mode == '2D':
verts.append((x, y, 0))
else:
nose_z = min(self.step_height, self.nose_z)
zs = self.z0 + i * self.step_height
z3 = zs + max(0, self.step_height - nose_z)
z1 = min(z3, zs - nose_z)
verts.append((x, y, z1))
verts.append((x, y, z3))
def p3d_cstep_right(self, verts, p2d, i, t):
x, y = p2d
if self.z_mode == '2D':
verts.append((x, y, 0))
else:
nose_z = min(self.step_height, self.nose_z)
zs = self.z0 + i * self.step_height
z3 = zs + max(0, self.step_height - nose_z)
z1 = min(z3, zs - nose_z)
verts.append((x, y, z3))
verts.append((x, y, z1))
def straight_stair(self, length):
self.next_type = 'STAIR'
s = self.straight(length)
return StraightStair(s.p, s.v, self.left_offset, self.right_offset, self.steps_type,
self.nose_type, self.z_mode, self.nose_z, self.bottom_z)
def straight_landing(self, length, last_type='STAIR'):
self.next_type = 'LANDING'
s = self.straight(length)
return StraightLanding(s.p, s.v, self.left_offset, self.right_offset, self.steps_type,
self.nose_type, self.z_mode, self.nose_z, self.bottom_z, last_type=last_type)
def curved_stair(self, da, radius, left_shape, right_shape, double_limit=pi):
self.next_type = 'STAIR'
n = self.normal(1)
n.v = radius * n.v.normalized()
if da < 0:
n.v = -n.v
a0 = n.angle
c = n.p - n.v
return CurvedStair(c, radius, a0, da, self.left_offset, self.right_offset,
self.steps_type, self.nose_type, self.z_mode, self.nose_z, self.bottom_z,
left_shape, right_shape, double_limit=double_limit)
def curved_landing(self, da, radius, left_shape, right_shape, double_limit=pi, last_type='STAIR'):
self.next_type = 'LANDING'
n = self.normal(1)
n.v = radius * n.v.normalized()
if da < 0:
n.v = -n.v
a0 = n.angle
c = n.p - n.v
return CurvedLanding(c, radius, a0, da, self.left_offset, self.right_offset,
self.steps_type, self.nose_type, self.z_mode, self.nose_z, self.bottom_z,
left_shape, right_shape, double_limit=double_limit, last_type=last_type)
def get_z(self, t, mode):
if mode == 'LINEAR':
return self.z0 + t * self.height
else:
step = 1 + floor(t / self.t_step)
return self.z0 + step * self.step_height
def make_profile(self, t, side, profile, verts, faces, matids, next=None, tnext=0):
z0 = self.get_z(t, 'LINEAR')
dz1 = 0
t, part, dz0, shape = self.get_part(t, side)
if next is not None:
tnext, next, dz1, shape1 = next.get_part(tnext, side)
xy, s = part.proj_xy(t, next)
v_xy = s * xy.to_3d()
z, s = part.proj_z(t, dz0, next, dz1)
v_z = s * Vector((-xy.y * z.x, xy.x * z.x, z.y))
x, y = part.lerp(t)
verts += [Vector((x, y, z0)) + v.x * v_xy + v.y * v_z for v in profile]
def project_uv(self, rM, uvs, verts, indexes, up_axis='Z'):
if up_axis == 'Z':
uvs.append([(rM * Vector(verts[i])).to_2d() for i in indexes])
elif up_axis == 'Y':
uvs.append([(x, z) for x, y, z in [(rM * Vector(verts[i])) for i in indexes]])
else:
uvs.append([(y, z) for x, y, z in [(rM * Vector(verts[i])) for i in indexes]])
def get_proj_matrix(self, part, t, nose_y):
# a matrix to project verts
# into uv space for horizontal parts of this step
# so uv = (rM * vertex).to_2d()
tl = t - nose_y / self.get_length("LEFT")
tr = t - nose_y / self.get_length("RIGHT")
t2, part, dz, shape = self.get_part(tl, "LEFT")
p0 = part.lerp(t2)
t2, part, dz, shape = self.get_part(tr, "RIGHT")
p1 = part.lerp(t2)
v = (p1 - p0).normalized()
return Matrix([
[-v.y, v.x, 0, p0.x],
[v.x, v.y, 0, p0.y],
[0, 0, 1, 0],
[0, 0, 0, 1]
]).inverted()
def _make_nose(self, i, s, verts, faces, matids, uvs, nose_y):
f = len(verts)
if self.z_mode == '2D':
faces.append(f)
return
t = self.t_step * i
# a matrix to project verts
# into uv space for horizontal parts of this step
# so uv = (rM * vertex).to_2d()
rM = self.get_proj_matrix(self, t, nose_y)
if self.z_mode == 'LINEAR':
return rM
tl = t - nose_y / self.get_length("LEFT")
tr = t - nose_y / self.get_length("RIGHT")
t2, part, dz, shape = self.get_part(tl, "LEFT")
p0 = part.lerp(t2)
self.p3d_left(verts, p0, s, t2)
t2, part, dz, shape = self.get_part(tr, "RIGHT")
p1 = part.lerp(t2)
self.p3d_right(verts, p1, s, t2)
start = 3
end = 6
offset = 10
# left, top, right
matids.extend([self.idmat_step_side,
self.idmat_top,
self.idmat_step_side])
faces += [(f + j, f + j + 1, f + j + offset + 1, f + j + offset) for j in range(start, end)]
u = nose_y
v = (p1 - p0).length
w = verts[f + 2][2] - verts[f + 3][2]
s = int((end - start) / 2)
uvs += [[(u, verts[f + j][2]), (u, verts[f + j + 1][2]),
(0, verts[f + j + 1][2]), (0, verts[f + j][2])] for j in range(start, start + s)]
uvs.append([(0, 0), (0, v), (u, v), (u, 0)])
uvs += [[(u, verts[f + j][2]), (u, verts[f + j + 1][2]),
(0, verts[f + j + 1][2]), (0, verts[f + j][2])] for j in range(start + s + 1, end)]
if 'STRAIGHT' in self.nose_type or 'OPEN' in self.steps_type:
# face bottom
matids.append(self.idmat_bottom)
faces.append((f + end, f + start, f + offset + start, f + offset + end))
uvs.append([(u, v), (u, 0), (0, 0), (0, v)])
if self.steps_type != 'OPEN':
if 'STRAIGHT' in self.nose_type:
# front face bottom straight
matids.append(self.idmat_raise)
faces.append((f + 12, f + 17, f + 16, f + 13))
uvs.append([(0, w), (v, w), (v, 0), (0, 0)])
elif 'OBLIQUE' in self.nose_type:
# front face bottom oblique
matids.append(self.idmat_raise)
faces.append((f + 12, f + 17, f + 6, f + 3))
uvs.append([(0, w), (v, w), (v, 0), (0, 0)])
matids.append(self.idmat_side)
faces.append((f + 3, f + 13, f + 12))
uvs.append([(0, 0), (u, 0), (u, w)])
matids.append(self.idmat_side)
faces.append((f + 6, f + 17, f + 16))
uvs.append([(0, 0), (u, w), (u, 0)])
# front face top
w = verts[f + 3][2] - verts[f + 4][2]
matids.append(self.idmat_step_front)
faces.append((f + 4, f + 3, f + 6, f + 5))
uvs.append([(0, 0), (0, w), (v, w), (v, 0)])
return rM
def make_faces(self, f, rM, verts, faces, matids, uvs):
if self.z_mode == '2D':
return
elif self.z_mode == 'LINEAR':
start = 0
end = 3
offset = 4
matids.extend([self.idmat_side,
self.idmat_top,
self.idmat_side,
self.idmat_bottom])
elif "OPEN" in self.steps_type:
# faces dessus-dessous-lateral marches fermees
start = 3
end = 6
offset = 10
matids.extend([self.idmat_step_side,
self.idmat_top,
self.idmat_step_side,
self.idmat_bottom])
else:
# faces dessus-dessous-lateral marches fermees
start = 0
end = 9
offset = 10
matids.extend([self.idmat_side,
self.idmat_side,
self.idmat_side,
self.idmat_step_side,
self.idmat_top,
self.idmat_step_side,
self.idmat_side,
self.idmat_side,
self.idmat_side,
self.idmat_bottom])
u_l0 = 0
u_l1 = self.t_step * self.left_length
u_r0 = 0
u_r1 = self.t_step * self.right_length
s = int((end - start) / 2)
uvs += [[(u_l0, verts[f + j][2]), (u_l0, verts[f + j + 1][2]),
(u_l1, verts[f + j + offset + 1][2]), (u_l1, verts[f + j + offset][2])] for j in range(start, start + s)]
self.project_uv(rM, uvs, verts, [f + start + s, f + start + s + 1,
f + start + s + offset + 1, f + start + s + offset])
uvs += [[(u_r0, verts[f + j][2]), (u_r0, verts[f + j + 1][2]),
(u_r1, verts[f + j + offset + 1][2]), (u_r1, verts[f + j + offset][2])] for j in range(start + s + 1, end)]
self.project_uv(rM, uvs, verts, [f + end, f + start, f + offset + start, f + offset + end])
faces += [(f + j, f + j + 1, f + j + offset + 1, f + j + offset) for j in range(start, end)]
faces.append((f + end, f + start, f + offset + start, f + offset + end))
class StraightStair(Stair, Line):
def __init__(self, p, v, left_offset, right_offset, steps_type, nose_type, z_mode, nose_z, bottom_z):
Stair.__init__(self, left_offset, right_offset, steps_type, nose_type, z_mode, nose_z, bottom_z)
Line.__init__(self, p, v)
self.l_line = self.offset(-left_offset)
self.r_line = self.offset(right_offset)
def make_step(self, i, verts, faces, matids, uvs, nose_y=0):
rM = self._make_nose(i, i, verts, faces, matids, uvs, nose_y)
t0 = self.t_step * i
f = len(verts)
p = self.l_line.lerp(t0)
self.p3d_left(verts, p, i, t0)
p = self.r_line.lerp(t0)
self.p3d_right(verts, p, i, t0)
if self.z_mode != '2D':
t1 = t0 + self.t_step
p = self.l_line.lerp(t1)
self.p3d_left(verts, p, i, t1)
p = self.r_line.lerp(t1)
self.p3d_right(verts, p, i, t1)
self.make_faces(f, rM, verts, faces, matids, uvs)
if "OPEN" in self.steps_type:
faces.append((f + 13, f + 14, f + 15, f + 16))
matids.append(self.idmat_step_front)
uvs.append([(0, 0), (0, 1), (1, 1), (1, 0)])
def get_length(self, side):
return self.length
def get_lerp_vect(self, posts, side, i, t_step, respect_edges, z_offset=0, t0_abs=None):
if t0_abs is not None:
t0 = t0_abs
else:
t0 = i * t_step
t, part, dz, shape = self.get_part(t0, side)
dz /= part.length
n = part.normal(t)
z0 = self.get_z(t0, 'STEP')
z1 = self.get_z(t0, 'LINEAR')
posts.append((n, dz, z0, z1 + t0 * z_offset))
return [t0]
def n_posts(self, post_spacing, side, respect_edges):
return self.steps(post_spacing)
def get_part(self, t, side):
if side == 'LEFT':
part = self.l_line
elif side == '2D':
part = self
else:
part = self.r_line
return t, part, self.height, 'LINE'
def measure_point(self, d, uid):
p0 = self.l_line.p0.to_3d()
p1 = self.r_line.p0.to_3d()
d.add_dimension_point(uid, p0)
d.add_dimension_point(uid + 1, p1)
class CurvedStair(Stair, Arc):
def __init__(self, c, radius, a0, da, left_offset, right_offset, steps_type, nose_type,
z_mode, nose_z, bottom_z, left_shape, right_shape, double_limit=pi):
Stair.__init__(self, left_offset, right_offset, steps_type, nose_type, z_mode, nose_z, bottom_z)
Arc.__init__(self, c, radius, a0, da)
self.l_shape = left_shape
self.r_shape = right_shape
self.edges_multiples = round(abs(da), 6) > double_limit
# left arc, tangeant at start and end
self.l_arc, self.l_t0, self.l_t1, self.l_tc = self.set_offset(-left_offset, left_shape)
self.r_arc, self.r_t0, self.r_t1, self.r_tc = self.set_offset(right_offset, right_shape)
def set_offset(self, offset, shape):
arc = self.offset(offset)
t0 = arc.tangeant(0, 1)
t1 = arc.tangeant(1, 1)
tc = arc.tangeant(0.5, 1)
if self.edges_multiples:
i, p, t = t0.intersect(tc)
tc.v *= 2 * t
tc.p = p
i, p, t2 = tc.intersect(t1)
else:
i, p, t = t0.intersect(t1)
t0.v *= t
t1.p = p
t1.v *= t
return arc, t0, t1, tc
def get_length(self, side):
if side == 'RIGHT':
arc = self.r_arc
shape = self.r_shape
t0 = self.r_t0
else:
arc = self.l_arc
shape = self.l_shape
t0 = self.l_t0
if shape == 'CIRCLE':
return arc.length
else:
if self.edges_multiples:
# two edges
return t0.length * 4
else:
return t0.length * 2
def _make_step(self, t_step, i, s, verts, landing=False):
tb = t_step * i
f = len(verts)
t, part, dz, shape = self.get_part(tb, "LEFT")
p = part.lerp(t)
self.p3d_left(verts, p, s, tb, landing)
t, part, dz, shape = self.get_part(tb, "RIGHT")
p = part.lerp(t)
self.p3d_right(verts, p, s, tb, landing)
return f
def _make_edge(self, t_step, i, j, f, rM, verts, faces, matids, uvs):
tb = t_step * i
# make edges verts after regular ones
if self.l_shape != 'CIRCLE' or self.r_shape != 'CIRCLE':
if self.edges_multiples:
# edge 1
if tb < 0.25 and tb + t_step > 0.25:
f0 = f
f = len(verts)
if self.l_shape == 'CIRCLE':
self.p3d_left(verts, self.l_arc.lerp(0.25), j, 0.25)
else:
self.p3d_left(verts, self.l_tc.p, j, 0.25)
if self.r_shape == 'CIRCLE':
self.p3d_right(verts, self.r_arc.lerp(0.25), j, 0.25)
else:
self.p3d_right(verts, self.r_tc.p, j, 0.25)
self.make_faces(f0, rM, verts, faces, matids, uvs)
# edge 2
if tb < 0.75 and tb + t_step > 0.75:
f0 = f
f = len(verts)
if self.l_shape == 'CIRCLE':
self.p3d_left(verts, self.l_arc.lerp(0.75), j, 0.75)
else:
self.p3d_left(verts, self.l_t1.p, j, 0.75)
if self.r_shape == 'CIRCLE':
self.p3d_right(verts, self.r_arc.lerp(0.75), j, 0.75)
else:
self.p3d_right(verts, self.r_t1.p, j, 0.75)
self.make_faces(f0, rM, verts, faces, matids, uvs)
else:
if tb < 0.5 and tb + t_step > 0.5:
f0 = f
f = len(verts)
# the step goes through the edge
if self.l_shape == 'CIRCLE':
self.p3d_left(verts, self.l_arc.lerp(0.5), j, 0.5)
else:
self.p3d_left(verts, self.l_t1.p, j, 0.5)
if self.r_shape == 'CIRCLE':
self.p3d_right(verts, self.r_arc.lerp(0.5), j, 0.5)
else:
self.p3d_right(verts, self.r_t1.p, j, 0.5)
self.make_faces(f0, rM, verts, faces, matids, uvs)
return f
def make_step(self, i, verts, faces, matids, uvs, nose_y=0):
# open stair with closed face
# step nose
rM = self._make_nose(i, i, verts, faces, matids, uvs, nose_y)
f = 0
if self.l_shape == 'CIRCLE' or self.r_shape == 'CIRCLE':
# every 6 degree
n_subs = max(1, int(abs(self.da) / pi * 30 / self.n_step))
if self.z_mode == '2D':
n_subs = max(1, int(abs(self.da) / pi * 60 / self.n_step))
t_step = self.t_step / n_subs
for j in range(n_subs):
f0 = f
f = self._make_step(t_step, n_subs * i + j, i, verts)
if j > 0:
self.make_faces(f0, rM, verts, faces, matids, uvs)
f = self._make_edge(t_step, n_subs * i + j, i, f, rM, verts, faces, matids, uvs)
else:
f = self._make_step(self.t_step, i, i, verts)
f = self._make_edge(self.t_step, i, i, f, rM, verts, faces, matids, uvs)
if self.z_mode != '2D':
self._make_step(self.t_step, i + 1, i, verts)
self.make_faces(f, rM, verts, faces, matids, uvs)
if self.z_mode != 'LINEAR' and "OPEN" in self.steps_type:
# back face top
faces.append((f + 13, f + 14, f + 15, f + 16))
matids.append(self.idmat_step_front)
uvs.append([(0, 0), (0, 1), (1, 1), (1, 0)])
def get_part(self, t, side):
if side == 'RIGHT':
arc = self.r_arc
shape = self.r_shape
t0, t1, tc = self.r_t0, self.r_t1, self.r_tc
elif side == '2D':
arc = self
shape = 'CIRCLE'
t0, t1, tc = self.l_t0, self.l_t1, self.l_tc
else:
arc = self.l_arc
shape = self.l_shape
t0, t1, tc = self.l_t0, self.l_t1, self.l_tc
if shape == 'CIRCLE':
return t, arc, self.height, shape
else:
if self.edges_multiples:
# two edges
if t <= 0.25:
return 4 * t, t0, 0.25 * self.height, shape
elif t <= 0.75:
return 2 * (t - 0.25), tc, 0.5 * self.height, shape
else:
return 4 * (t - 0.75), t1, 0.25 * self.height, shape
else:
if t <= 0.5:
return 2 * t, t0, 0.5 * self.height, shape
else:
return 2 * (t - 0.5), t1, 0.5 * self.height, shape
def get_lerp_vect(self, posts, side, i, t_step, respect_edges, z_offset=0, t0_abs=None):
if t0_abs is not None:
t0 = t0_abs
else:
t0 = i * t_step
res = [t0]
t1 = t0 + t_step
zs = self.get_z(t0, 'STEP')
zl = self.get_z(t0, 'LINEAR')
# vect normal
t, part, dz, shape = self.get_part(t0, side)
n = part.normal(t)
dz /= part.length
posts.append((n, dz, zs, zl + t0 * z_offset))
if shape != 'CIRCLE' and respect_edges:
if self.edges_multiples:
if t0 < 0.25 and t1 > 0.25:
zs = self.get_z(0.25, 'STEP')
zl = self.get_z(0.25, 'LINEAR')
t, part, dz, shape = self.get_part(0.25, side)
n = part.normal(1)
posts.append((n, dz, zs, zl + 0.25 * z_offset))
res.append(0.25)
if t0 < 0.75 and t1 > 0.75:
zs = self.get_z(0.75, 'STEP')
zl = self.get_z(0.75, 'LINEAR')
t, part, dz, shape = self.get_part(0.75, side)
n = part.normal(1)
posts.append((n, dz, zs, zl + 0.75 * z_offset))
res.append(0.75)
elif t0 < 0.5 and t1 > 0.5:
zs = self.get_z(0.5, 'STEP')
zl = self.get_z(0.5, 'LINEAR')
t, part, dz, shape = self.get_part(0.5, side)
n = part.normal(1)
posts.append((n, dz, zs, zl + 0.5 * z_offset))
res.append(0.5)
return res
def n_posts(self, post_spacing, side, respect_edges):
if side == 'LEFT':
arc, t0, shape = self.l_arc, self.l_t0, self.l_shape
else:
arc, t0, shape = self.r_arc, self.r_t0, self.r_shape
step_factor = 1
if shape == 'CIRCLE':
length = arc.length
else:
if self.edges_multiples:
if respect_edges:
step_factor = 2
length = 4 * t0.length
else:
length = 2 * t0.length
steps = step_factor * max(1, round(length / post_spacing, 0))
# print("respect_edges:%s t_step:%s n_step:%s" % (respect_edges, 1.0 / steps, int(steps)))
return 1.0 / steps, int(steps)
def measure_point(self, d, uid):
p0 = self.l_t0.p0.to_3d()
p1 = self.r_t0.p0.to_3d()
if self.edges_multiples:
p2 = self.l_tc.p0.to_3d()
p3 = self.r_tc.p0.to_3d()
d.add_dimension_point(uid + 2, p2)
d.add_dimension_point(uid + 3, p3)
p4 = self.l_t1.p0.to_3d()
p5 = self.r_t1.p0.to_3d()
d.add_dimension_point(uid, p0)
d.add_dimension_point(uid + 1, p1)
d.add_dimension_point(uid + 4, p4)
d.add_dimension_point(uid + 5, p5)
class StraightLanding(StraightStair):
def __init__(self, p, v, left_offset, right_offset, steps_type,
nose_type, z_mode, nose_z, bottom_z, last_type='STAIR'):
StraightStair.__init__(self, p, v, left_offset, right_offset, steps_type,
nose_type, z_mode, nose_z, bottom_z)
self.last_type = last_type
@property
def height(self):
return 0
@property
def top_offset(self):
return self.t_step / self.v.length
@property
def top(self):
if self.next_type == 'LANDING':
return self.z0
else:
return self.z0 + self.step_height
def step_size(self, step_depth):
self.n_step = 1
self.t_step = 1
self.step_depth = step_depth
if self.last_type == 'LANDING':
return 0
else:
return 1
def make_step(self, i, verts, faces, matids, uvs, nose_y=0):
if i == 0 and self.last_type != 'LANDING':
rM = self._make_nose(i, 0, verts, faces, matids, uvs, nose_y)
else:
rM = self.get_proj_matrix(self.l_line, self.t_step * i, nose_y)
f = len(verts)
j = 0
t0 = self.t_step * i
p = self.l_line.lerp(t0)
self.p3d_left(verts, p, j, t0)
p = self.r_line.lerp(t0)
self.p3d_right(verts, p, j, t0)
t1 = t0 + self.t_step
p = self.l_line.lerp(t1)
self.p3d_left(verts, p, j, t1, self.next_type != 'LANDING')
p = self.r_line.lerp(t1)
self.p3d_right(verts, p, j, t1, self.next_type != 'LANDING')
self.make_faces(f, rM, verts, faces, matids, uvs)
if "OPEN" in self.steps_type and self.next_type != 'LANDING':
faces.append((f + 13, f + 14, f + 15, f + 16))
matids.append(self.idmat_step_front)
uvs.append([(0, 0), (0, 1), (1, 1), (1, 0)])
def straight_landing(self, length):
return Stair.straight_landing(self, length, last_type='LANDING')
def curved_landing(self, da, radius, left_shape, right_shape, double_limit=pi):
return Stair.curved_landing(self, da, radius, left_shape,
right_shape, double_limit=double_limit, last_type='LANDING')
def get_z(self, t, mode):
if mode == 'STEP':
return self.z0 + self.step_height
else:
return self.z0
class CurvedLanding(CurvedStair):
def __init__(self, c, radius, a0, da, left_offset, right_offset, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape, double_limit=pi, last_type='STAIR'):
CurvedStair.__init__(self, c, radius, a0, da, left_offset, right_offset, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape, double_limit=double_limit)
self.last_type = last_type
@property
def top_offset(self):
if self.l_shape == 'CIRCLE' or self.r_shape == 'CIRCLE':
return self.t_step / self.step_depth
else:
if self.edges_multiples:
return 0.5 / self.length
else:
return 1 / self.length
@property
def height(self):
return 0
@property
def top(self):
if self.next_type == 'LANDING':
return self.z0
else:
return self.z0 + self.step_height
def step_size(self, step_depth):
if self.l_shape == 'CIRCLE' or self.r_shape == 'CIRCLE':
t_step, n_step = self.steps(step_depth)
else:
if self.edges_multiples:
t_step, n_step = 0.5, 2
else:
t_step, n_step = 1, 1
self.n_step = n_step
self.t_step = t_step
self.step_depth = step_depth
if self.last_type == 'LANDING':
return 0
else:
return 1
def make_step(self, i, verts, faces, matids, uvs, nose_y=0):
if i == 0 and 'LANDING' not in self.last_type:
rM = self._make_nose(i, 0, verts, faces, matids, uvs, nose_y)
else:
rM = self.get_proj_matrix(self.l_arc, self.t_step * i, nose_y)
f = len(verts)
if self.l_shape == 'CIRCLE' or self.r_shape == 'CIRCLE':
n_subs = max(1, int(abs(self.da / pi * 30 / self.n_step)))
t_step = self.t_step / n_subs
for j in range(n_subs):
f0 = f
f = self._make_step(t_step, n_subs * i + j, 0, verts)
if j > 0:
self.make_faces(f0, rM, verts, faces, matids, uvs)
f = self._make_edge(t_step, n_subs * i + j, 0, f, rM, verts, faces, matids, uvs)
else:
f = self._make_step(self.t_step, i, 0, verts)
f = self._make_edge(self.t_step, i, 0, f, rM, verts, faces, matids, uvs)
self._make_step(self.t_step, i + 1, 0, verts, i == self.n_step - 1 and 'LANDING' not in self.next_type)
self.make_faces(f, rM, verts, faces, matids, uvs)
if "OPEN" in self.steps_type and 'LANDING' not in self.next_type:
faces.append((f + 13, f + 14, f + 15, f + 16))
matids.append(self.idmat_step_front)
uvs.append([(0, 0), (0, 1), (1, 1), (1, 0)])
def straight_landing(self, length):
return Stair.straight_landing(self, length, last_type='LANDING')
def curved_landing(self, da, radius, left_shape, right_shape, double_limit=pi):
return Stair.curved_landing(self, da, radius, left_shape,
right_shape, double_limit=double_limit, last_type='LANDING')
def get_z(self, t, mode):
if mode == 'STEP':
return self.z0 + self.step_height
else:
return self.z0
class StairGenerator():
def __init__(self, d):
self.d = d
self.parts = d.parts
self.last_type = 'NONE'
self.stairs = []
self.steps_type = 'NONE'
self.sum_da = 0
self.user_defined_post = None
self.user_defined_uvs = None
self.user_defined_mat = None
def add_part(self, type, steps_type, nose_type, z_mode, nose_z, bottom_z, center,
radius, da, width_left, width_right, length, left_shape, right_shape):
self.steps_type = steps_type
if len(self.stairs) < 1:
s = None
else:
s = self.stairs[-1]
if "S_" not in type:
self.sum_da += da
# start a new stair
if s is None:
if type == 'S_STAIR':
p = Vector((0, 0))
v = Vector((0, length))
s = StraightStair(p, v, width_left, width_right, steps_type, nose_type, z_mode, nose_z, bottom_z)
elif type == 'C_STAIR':
if da < 0:
c = Vector((radius, 0))
else:
c = Vector((-radius, 0))
s = CurvedStair(c, radius, 0, da, width_left, width_right, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape)
elif type == 'D_STAIR':
if da < 0:
c = Vector((radius, 0))
else:
c = Vector((-radius, 0))
s = CurvedStair(c, radius, 0, da, width_left, width_right, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape, double_limit=0)
elif type == 'S_LANDING':
p = Vector((0, 0))
v = Vector((0, length))
s = StraightLanding(p, v, width_left, width_right, steps_type, nose_type, z_mode, nose_z, bottom_z)
elif type == 'C_LANDING':
if da < 0:
c = Vector((radius, 0))
else:
c = Vector((-radius, 0))
s = CurvedLanding(c, radius, 0, da, width_left, width_right, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape)
elif type == 'D_LANDING':
if da < 0:
c = Vector((radius, 0))
else:
c = Vector((-radius, 0))
s = CurvedLanding(c, radius, 0, da, width_left, width_right, steps_type,
nose_type, z_mode, nose_z, bottom_z, left_shape, right_shape, double_limit=0)
else:
if type == 'S_STAIR':
s = s.straight_stair(length)
elif type == 'C_STAIR':
s = s.curved_stair(da, radius, left_shape, right_shape)
elif type == 'D_STAIR':
s = s.curved_stair(da, radius, left_shape, right_shape, double_limit=0)
elif type == 'S_LANDING':
s = s.straight_landing(length)
elif type == 'C_LANDING':
s = s.curved_landing(da, radius, left_shape, right_shape)
elif type == 'D_LANDING':
s = s.curved_landing(da, radius, left_shape, right_shape, double_limit=0)
self.stairs.append(s)
self.last_type = type
def n_steps(self, step_depth):
n_steps = 0
for stair in self.stairs:
n_steps += stair.step_size(step_depth)
return n_steps
def set_height(self, step_height):
z = 0
for stair in self.stairs:
stair.set_height(step_height, z)
z = stair.top
def make_stair(self, height, step_depth, verts, faces, matids, uvs, nose_y=0):
n_steps = self.n_steps(step_depth)