***Name: "Spindel mit Platten"		! Must be defined railing type name
***Code: 10004			! Must be defined identification code
                                ! It must be also the subroutine label in GDL section

!Notes:

!B Bool       [] both option available [0] only 0 available [1] only 1 available
!I Integer    [0,15] value range 0=<p=<15, [] means unlimited value range [0] means param. not available
!D Double     [0.32,99] value range [] means unlimited value range [0] means param. not available
!M Material   [0] means param. not available
!P Pen        [0] means param. not available
!F Fill       [0] means param. not available
!T Text       [0] means param. not available
!S Font type  [0] means param. not available

***STD***         ! Must be defined standard parameters section delimiter.
                  ! Section may be empty, but parameter names and types not able to change

RailingOffset:     D [-1.0,1.0]=0.03 ! railing offset to edge of stair value: positive value means inside
RailingUpPos:      D [0.3, 10.0]=0.9     ! railing top position above step level
RailingDnPos:      D [0.0, 10.0]=0.1     ! railing bottom position above step level
ColumnOffset:      D [0,0]=0    	! column offset to railing line value: positive value means inside
ColumnOffsetUp:    D [0,0.5]=0       ! column horizontal overhang up
ColumnOffsetDn:    D [0,0.5]=0       ! column horizontal overhang down
FirstColumnOffset: D [0,3]=0.1       ! first column offset along the railing
CnPlacementType:   I [1,3]=1         ! no Column, closest to, on every Tread, pieces: 0,1,2,3
ClosestToDistance: D [0.02,3]=0.80  ! distance of columns to each other
SnapToCorners:     B []=1            ! 1=columns  at segment ends

***ADD***         ! Must be defined additional parameters section delimiter (section may be empty)

res:"Hohe Aufl.gen" B [0,1]=0
r1:"Sttzenradius" D [0.01,0.1]=0.02         ! radius of column
pd:"Plattentiefe" D [0.01,0.1]=0.02           ! panel depth
dpc:"Plattenrandabst." D [0.1,0.1]=0.01  ! distance of panel edge from column edge
mat_r:"Gelndermat." M []=10     ! railing material
mat_c:"Spindelmaterial" M []=47      ! column material
mat_f:"Feldmaterial" M []=47       ! field tube material

***GDL***         ! Must be defined section delimiter for railing GDL subroutine



!================================================ GDL OF RAILING
!================================================================


10004:                            ! railing code

if res =0 then resol 8 else resol 36

wr=4*r1      		      	! width of rail section
hr=0.66*wr         		! height of rail section


stat=1

dq=0.0001     ! used in primitives
yq=0.0001



IF current_side=0 then


    !*********************************** left side railing
    !***************************************************
    voct=segm_l[current_segment][8]             ! vertical oversize of columns at the top
    vocb=segm_l[current_segment][9]             ! vertical oversize of columns at the bottom

    !********************* rail


    h1=segm_l[current_segment][12]             ! height of railing
    h2=segm_l[current_segment][11]             ! height of bottom tube axis
    h3=h1-0.12


    nr=segm_l[current_segment][2]   !number of railing coordinates
    n=segm_l[current_segment][6]    !number of columns

    fqw1=segm_l[current_segment][3]  !first point index of current segment
    fqwn=segm_l[current_segment][3]+segm_l[current_segment][2]-1  !last point index of current segment


    material mat_r

    ff=wr/0.08     ! railing section resizing factors
    fff=hr/0.035


    !plus one point for tube direction at the beginning & in the end

    x1=rail_l [fqw1][1]-(rail_l [fqw1+1][1]-rail_l [fqw1][1])
    y1=rail_l [fqw1][2]-(rail_l [fqw1+1][2]-rail_l [fqw1][2])
    z1=rail_l [fqw1][3]-(rail_l [fqw1+1][3]-rail_l [fqw1][3])

    xn=rail_l [fqwn][1]+(rail_l [fqwn][1]-rail_l [fqwn-1][1])
    yn=rail_l [fqwn][2]+(rail_l [fqwn][2]-rail_l [fqwn-1][2])
    zn=rail_l [fqwn][3]+(rail_l [fqwn][3]-rail_l [fqwn-1][3])


    for i=fqw1 to fqwn
        put rail_l [i][1], rail_l [i][2], rail_l [i][3], 0
    next i


    tube 22, nr+2, 1+2+16+32,
        0.0,            -0.015625*fff,     1,
       -0.031225*ff,    -0.015625*fff,     1,
       -0.035491*ff,    -0.011532*fff,     1,
       -0.038878*ff,    -0.00588*fff,      1,
       -0.04*ff,         0.0,              1,
       -0.038878*ff,     0.00588*fff,      1,
       -0.035491*ff,     0.011532*fff,     1,
       -0.031225*ff,     0.015625*fff,     1,
       -0.022446*ff,     0.017761*fff,     1,
       -0.013524*ff,     0.019192*fff,     1,
       -0.004518*ff,     0.01991*fff,      1,
        0.004518*ff,     0.01991*fff,      1,
        0.013524*ff,     0.019192*fff,     1,
        0.022446*ff,     0.017761*fff,     1,
        0.031225*ff,     0.015625*fff,     1,
        0.035491*ff,     0.011532*fff,     1,
        0.038878*ff,     0.00588*fff,      1,
        0.04*ff,         0.0,              1,
        0.038878*ff,    -0.00588*fff,      1,
        0.035491*ff,    -0.011532*fff,     1,
        0.031225*ff,    -0.015625*fff,     1,
        0.0,            -0.015625*fff,     1,

        x1, y1, z1, 0,
        get (nsp),
        xn, yn, zn, 0



    !*********************** columns

    material mat_c


    for i=segm_l[current_segment][7] to segm_l[current_segment][7]+n-1

        c1=column_l[i][4]                  ! column point index

        hc1=rail_l[c1][3]-column_l[i][3]+vocb+voct   ! height of column

        addx column_l[i][1]
        addy column_l[i][2]
        addz column_l[i][3]-vocb

        cylind hc1, r1


        del 3

    next i


    if n<2 then goto 1000469    !only one column in the segment

    !*********************** vertical panel

    material mat_f


    for i=segm_l[current_segment][7] to segm_l[current_segment][7]+n-2

        for gk=1 to 2

            for kk=1 to 2

                qq=0

                c11=column_l[i][4]                  ! first column point index
                c21=column_l[i+1][4]                ! next column point index


                !*************************** interval next to column

                ! ********* calculation of a1 & a2

                if column_l[i][4]=segm_l[current_segment][3] then

                    dx1=rail_l[c11+1][1]-rail_l[c11][1]
                    dy1=rail_l[c11+1][2]-rail_l[c11][2]
                    dz1=rail_l[c11+1][3]-rail_l[c11][3]

                else

                    dx1=rail_l[c11][1]-rail_l[c11-1][1]
                    dy1=rail_l[c11][2]-rail_l[c11-1][2]
                    dz1=rail_l[c11][3]-rail_l[c11-1][3]

                endif

                dx2=rail_l[c11+1][1]-rail_l[c11][1]
                dy2=rail_l[c11+1][2]-rail_l[c11][2]
                dz2=rail_l[c11+1][3]-rail_l[c11][3]

                ddd1=sqr(dx1^2+dy1^2)          ! distance of two railing points
                ddd2=sqr(dx2^2+dy2^2)

                aaz1=atn(dz1/ddd1)             ! angle of vertical displacement
                aaz2=atn(dz2/ddd2)


                dx11=rail_l[c21][1]-rail_l[c21-1][1]
                dy11=rail_l[c21][2]-rail_l[c21-1][2]
                dz11=rail_l[c21][3]-rail_l[c21-1][3]


                if column_l[i+1][4]=segm_l[current_segment][3]+segm_l[current_segment][2]-1  then

                    dx22=rail_l[c21][1]-rail_l[c21-1][1]
                    dy22=rail_l[c21][2]-rail_l[c21-1][2]
                    dz22=rail_l[c21][3]-rail_l[c21-1][3]

                else

                    dx22=rail_l[c21+1][1]-rail_l[c21][1]
                    dy22=rail_l[c21+1][2]-rail_l[c21][2]
                    dz22=rail_l[c21+1][3]-rail_l[c21][3]

                endif

                ddd11=sqr(dx11^2+dy11^2)          ! distance of two railing point
                ddd22=sqr(dx22^2+dy22^2)

                aaz11=atn(dz11/ddd11)             ! angle of vertical displacement
                aaz22=atn(dz22/ddd22)



                if dx1=0 then
                    if dy1>0 then
                        a1=90
                    else
                        a1=270
                    endif
                else
                    if dx1>0 then
                        if dy1>0 then
                            a1=atn(dy1/dx1)
                        else
                            a1=360+atn(dy1/dx1)
                        endif
                    else
                        a1=180+atn(dy1/dx1)
                    endif
                endif


                if dx2=0 then
                    if dy2>0 then
                        a2=90
                    else
                        a2=270
                    endif
                else
                    if dx2>0 then
                        if dy2>0 then
                            a2=atn(dy2/dx2)
                        else
                            a2=360+atn(dy2/dx2)
                        endif
                    else
                        a2=180+atn(dy2/dx2)
                    endif
                endif



                ! ******** calculation of aa

                if a2>a1 then
                    aa=a2-a1
                else
                    aa=360+a2-a1
                endif

                if abs(a2-a1)<0.0001 then aa=0

                if aa<180 then bb=180-aa
                if aa>180 then bb=aa-180

                if aa>180 then gg=180+bb/2
                if aa<180 then gg=180-bb/2

                addx rail_l[c11][1]
                addy rail_l[c11][2]

                rotz a1+gg

            if abs(180-bb)<0.1 then
                ww=(r1+dpc)
            else
                ww=(r1+dpc)+pd/tan(bb/2)/2
            endif

            m1=pd/2/sin(bb/2)
            wx1=ww*cos(bb/2)
            wy1=ww*sin(bb/2)

            wwyy=2*m1*cos(bb/2)
            ww2=ww-wwyy

            wx2=ww2*cos(bb/2)
            wy2=ww2*sin(bb/2)


            zz1=(r1+dpc)*tan(aaz2)


            !******* if first/last rail point is in between dpc interval

            if (ddd2-r1)<dpc then

                dx3=rail_l[fqw1+2][1]-rail_l[fqw1+1][1]
                dy3=rail_l[fqw1+2][2]-rail_l[fqw1+1][2]
                dz3=rail_l[fqw1+2][3]-rail_l[fqw1+1][3]

                dd3=sqr(dx3^2+dy3^2)

                aaz3=atn(dz3/(dd3+0.000001))

                zzz111=(dpc-ddd2-r1)*tan(aaz3)

            endif


                yq1=yq

                if aa>180 then yq1=-yq1

        if aa>180 then

            vert  m1+wx2-yq1, wy2, rail_l[c11][3]-h1+h2+zz1
            vert  m1+wx2, wy2, rail_l[c11][3]-h1+h2+zz1

            vert -m1+wx1, wy1, rail_l[c11][3]-h1+h2+zz1
            vert -m1+wx1+yq1, wy1, rail_l[c11][3]-h1+h2+zz1

        else

            vert -m1+wx1, -wy1, rail_l[c11][3]-h1+h2+zz1
            vert -m1+wx1+yq1, -wy1, rail_l[c11][3]-h1+h2+zz1

            vert  m1+wx2-yq1, -wy2, rail_l[c11][3]-h1+h2+zz1
            vert  m1+wx2, -wy2, rail_l[c11][3]-h1+h2+zz1

        endif


                del 2+1


                !******* if first/last rail point is in between dpc interval

                if (ddd2-r1)<dpc then
                    c11=c11+1
                endif

                if (ddd11-r1)<dpc then
                    felteetel=1
                    c21=c21-1
                endif



                !**************************** intermediate interval

                for j=c11+1 to c21-1

                    ! ******* calculation of a1 & a2

                    dx1=rail_l[j][1]-rail_l[j-1][1]
                    dy1=rail_l[j][2]-rail_l[j-1][2]

                    dx2=rail_l[j+1][1]-rail_l[j][1]
                    dy2=rail_l[j+1][2]-rail_l[j][2]

                    if dx1=0 then
                        if dy1>0 then
                            a1=90
                        else
                            a1=270
                        endif
                    else
                        if dx1>0 then
                            if dy1>0 then
                                a1=atn(dy1/dx1)
                            else
                                a1=360+atn(dy1/dx1)
                            endif
                        else
                            a1=180+atn(dy1/dx1)
                        endif
                    endif


                    if dx2=0 then
                        if dy2>0 then
                            a2=90
                        else
                            a2=270
                        endif
                    else
                        if dx2>0 then
                            if dy2>0 then
                                a2=atn(dy2/dx2)
                            else
                                a2=360+atn(dy2/dx2)
                            endif
                        else
                            a2=180+atn(dy2/dx2)
                        endif
                    endif

                    ! ******* calculation of aa

                    if a2>a1 then
                        aa=a2-a1
                    else
                        aa=360+a2-a1
                    endif


                    if aa<180 then bb=180-aa
                    if aa>180 then bb=aa-180

                    if aa>180 then gg=180+bb/2
                    if aa<180 then gg=180-bb/2

                    addx rail_l[j][1]
                    addy rail_l[j][2]

                    rotz a1+gg

                    m1=pd/2/sin(bb/2)
                    yq1=yq

                    if aa>180 then m1=-m1
                    if aa>180 then yq1=-yq1

                    vert -m1, 0, rail_l[j][3]-h1+h2
                    vert -m1+yq1, 0, rail_l[j][3]-h1+h2
                    vert  m1-yq1, 0, rail_l[j][3]-h1+h2
                    vert  m1, 0, rail_l[j][3]-h1+h2

                    qq=qq+1

                    del 2+1

                next j


                !*************************** interval before next column

                if felteetel=1 then
                    c21=c21+1   ! original c21
                    felteetel=0
                endif


                ! ******** calculation of a1 & a2


                if dx11=0 then
                    if dy11>0 then
                        a1=90
                    else
                        a1=270
                    endif
                else
                    if dx11>0 then
                        if dy11>0 then
                            a1=atn(dy11/dx11)
                        else
                            a1=360+atn(dy11/dx11)
                        endif
                    else
                        a1=180+atn(dy11/dx11)
                    endif
                endif


                if dx22=0 then
                    if dy22>0 then
                        a2=90
                    else
                        a2=270
                    endif
                else
                    if dx22>0 then
                        if dy22>0 then
                            a2=atn(dy22/dx22)
                        else
                            a2=360+atn(dy22/dx22)
                        endif
                    else
                        a2=180+atn(dy22/dx22)
                    endif
                endif



                ! ******* calculation of aa

                if a2>a1 then
                    aa=a2-a1
                else
                    aa=360+a2-a1
                endif

                if abs(a2-a1)<0.0001 then aa=0

                if aa<180 then bb=180-aa
                if aa>180 then bb=aa-180

                if aa>180 then gg=180+bb/2
                if aa<180 then gg=180-bb/2

                addx rail_l[c21][1]
                addy rail_l[c21][2]

                rotz a1+gg


        if abs(180-bb)<0.1 then
            ww=(r1+dpc)
        else
            ww=(r1+dpc)+pd/tan(bb/2)/2
        endif

        m1=pd/2/sin(bb/2)
        wx1=ww*cos(bb/2)
        wy1=ww*sin(bb/2)

        wwyy=2*m1*cos(bb/2)
        ww2=ww-wwyy

        wx2=ww2*cos(bb/2)
        wy2=ww2*sin(bb/2)

                zz2=(r1+dpc)*tan(aaz11)

                yq1=yq

                if aa>180 then yq1=-yq1

        if aa>180 then

                vert  m1+wx2-yq1, -wy2, rail_l[c21][3]-h1+h2-zz2
                vert  m1+wx2, -wy2, rail_l[c21][3]-h1+h2-zz2

                vert -m1+wx1, -wy1, rail_l[c21][3]-h1+h2-zz2
                vert -m1+wx1+yq1, -wy1, rail_l[c21][3]-h1+h2-zz2

        else
                vert -m1+wx1, wy1, rail_l[c21][3]-h1+h2-zz2
                vert -m1+wx1+yq1, wy1, rail_l[c21][3]-h1+h2-zz2

                vert  m1+wx2-yq1, wy2, rail_l[c21][3]-h1+h2-zz2
                vert  m1+wx2, wy2, rail_l[c21][3]-h1+h2-zz2
        endif


                del 2+1

                addz dq

            next kk

            del 2

            addz -dq-h2+h3-dq

        next gk

        del 2

        gosub 1000477


    next i

1000469:

ELSE


    !*********************************** right side railing
    !***************************************************

voct=segm_r[current_segment][8]             ! vertical oversize of columns at the top
vocb=segm_r[current_segment][9]             ! vertical oversize of columns at the bottom



    !***************************** railing

    h1=segm_r[current_segment][12]             ! height of railing
    h2=segm_r[current_segment][11]             ! height of bottom tube axis
    h3=h1-0.12


    nr=segm_r[current_segment][2]   !number of railing coordinates
    n=segm_r[current_segment][6]    !number of columns


    fqw1=segm_r[current_segment][3]  !first point index of current segment
    fqwn=segm_r[current_segment][3]+segm_r[current_segment][2]-1  !last point index of current segment




    !********************* rail

    material mat_r

    ff=wr/0.08     ! railing section resizing factors
    fff=hr/0.035


    !plus one point for tube direction at the beginning & in the end

    x1=rail_r [fqw1][1]-(rail_r [fqw1+1][1]-rail_r [fqw1][1])
    y1=rail_r [fqw1][2]-(rail_r [fqw1+1][2]-rail_r [fqw1][2])
    z1=rail_r [fqw1][3]-(rail_r [fqw1+1][3]-rail_r [fqw1][3])

    xn=rail_r [fqwn][1]+(rail_r [fqwn][1]-rail_r [fqwn-1][1])
    yn=rail_r [fqwn][2]+(rail_r [fqwn][2]-rail_r [fqwn-1][2])
    zn=rail_r [fqwn][3]+(rail_r [fqwn][3]-rail_r [fqwn-1][3])


    for i=fqw1 to fqwn
        put rail_r [i][1], rail_r [i][2], rail_r [i][3], 0
    next i


    tube 22, nr+2, 1+2+16+32,
        0.0,            -0.015625*fff,     1,
       -0.031225*ff,    -0.015625*fff,     1,
       -0.035491*ff,    -0.011532*fff,     1,
       -0.038878*ff,    -0.00588*fff,      1,
       -0.04*ff,         0.0,              1,
       -0.038878*ff,     0.00588*fff,      1,
       -0.035491*ff,     0.011532*fff,     1,
       -0.031225*ff,     0.015625*fff,     1,
       -0.022446*ff,     0.017761*fff,     1,
       -0.013524*ff,     0.019192*fff,     1,
       -0.004518*ff,     0.01991*fff,      1,
        0.004518*ff,     0.01991*fff,      1,
        0.013524*ff,     0.019192*fff,     1,
        0.022446*ff,     0.017761*fff,     1,
        0.031225*ff,     0.015625*fff,     1,
        0.035491*ff,     0.011532*fff,     1,
        0.038878*ff,     0.00588*fff,      1,
        0.04*ff,         0.0,              1,
        0.038878*ff,    -0.00588*fff,      1,
        0.035491*ff,    -0.011532*fff,     1,
        0.031225*ff,    -0.015625*fff,     1,
        0.0,            -0.015625*fff,     1,

        x1, y1, z1, 0,
        get (nsp),
        xn, yn, zn, 0



    !*********************** columns

    material mat_c


    for i=segm_r[current_segment][7] to segm_r[current_segment][7]+n-1

        c1=column_r[i][4]                  ! column point index

        hc1=rail_r[c1][3]-column_r[i][3]+vocb+voct   ! height of column

        addx column_r[i][1]
        addy column_r[i][2]
        addz column_r[i][3]-vocb

        cylind hc1, r1


        del 3

    next i


    if n<2 then goto 1000479    !only one column in the segment

    !*********************** vertical panel

    material mat_f


    for i=segm_r[current_segment][7] to segm_r[current_segment][7]+n-2

        for gk=1 to 2

            for kk=1 to 2

                qq=0

                c11=column_r[i][4]                  ! first column point index
                c21=column_r[i+1][4]                ! next column point index


                !*************************** interval next to column

                ! ********* calculation of a1 & a2


                if column_r[i][4]=segm_r[current_segment][3] then

                    dx1=rail_r[c11+1][1]-rail_r[c11][1]
                    dy1=rail_r[c11+1][2]-rail_r[c11][2]
                    dz1=rail_r[c11+1][3]-rail_r[c11][3]

                else

                    dx1=rail_r[c11][1]-rail_r[c11-1][1]
                    dy1=rail_r[c11][2]-rail_r[c11-1][2]
                    dz1=rail_r[c11][3]-rail_r[c11-1][3]

                endif

                dx2=rail_r[c11+1][1]-rail_r[c11][1]
                dy2=rail_r[c11+1][2]-rail_r[c11][2]
                dz2=rail_r[c11+1][3]-rail_r[c11][3]

                ddd1=sqr(dx1^2+dy1^2)          ! distance of two railing points
                ddd2=sqr(dx2^2+dy2^2)

                aaz1=atn(dz1/ddd1)             ! angle of vertical displacement
                aaz2=atn(dz2/ddd2)


                dx11=rail_r[c21][1]-rail_r[c21-1][1]
                dy11=rail_r[c21][2]-rail_r[c21-1][2]
                dz11=rail_r[c21][3]-rail_r[c21-1][3]


                if column_r[i+1][4]=segm_r[current_segment][3]+segm_r[current_segment][2]-1  then

                    dx22=rail_r[c21][1]-rail_r[c21-1][1]
                    dy22=rail_r[c21][2]-rail_r[c21-1][2]
                    dz22=rail_r[c21][3]-rail_r[c21-1][3]

                else

                    dx22=rail_r[c21+1][1]-rail_r[c21][1]
                    dy22=rail_r[c21+1][2]-rail_r[c21][2]
                    dz22=rail_r[c21+1][3]-rail_r[c21][3]

                endif


                ddd11=sqr(dx11^2+dy11^2)          ! distance of two railing point
                ddd22=sqr(dx22^2+dy22^2)

                aaz11=atn(dz11/ddd11)             ! angle of vertical displacement
                aaz22=atn(dz22/ddd22)



                if dx1=0 then
                    if dy1>0 then
                        a1=90
                    else
                        a1=270
                    endif
                else
                    if dx1>0 then
                        if dy1>0 then
                            a1=atn(dy1/dx1)
                        else
                            a1=360+atn(dy1/dx1)
                        endif
                    else
                        a1=180+atn(dy1/dx1)
                    endif
                endif


                if dx2=0 then
                    if dy2>0 then
                        a2=90
                    else
                        a2=270
                    endif
                else
                    if dx2>0 then
                        if dy2>0 then
                            a2=atn(dy2/dx2)
                        else
                            a2=360+atn(dy2/dx2)
                        endif
                    else
                        a2=180+atn(dy2/dx2)
                    endif
                endif



                ! ******** calculation of aa

                if a2>a1 then
                    aa=a2-a1
                else
                    aa=360+a2-a1
                endif

                if abs(a2-a1)<0.0001 then aa=0


                if aa<180 then bb=180-aa
                if aa>180 then bb=aa-180

                if aa>180 then gg=180+bb/2
                if aa<180 then gg=180-bb/2

                addx rail_r[c11][1]
                addy rail_r[c11][2]

                rotz a1+gg

        if abs(180-bb)<0.1 then
            ww=(r1+dpc)
        else
            ww=(r1+dpc)+pd/tan(bb/2)/2
        endif

        m1=pd/2/sin(bb/2)
        wx1=ww*cos(bb/2)
        wy1=ww*sin(bb/2)

        wwyy=2*m1*cos(bb/2)
        ww2=ww-wwyy

        wx2=ww2*cos(bb/2)
        wy2=ww2*sin(bb/2)

                zz1=(r1+dpc)*tan(aaz2)


            !******* if first/last rail point is in between dpc interval

            if (ddd2-r1)<dpc then

                dx3=rail_r[fqw1+2][1]-rail_r[fqw1+1][1]
                dy3=rail_r[fqw1+2][2]-rail_r[fqw1+1][2]
                dz3=rail_r[fqw1+2][3]-rail_r[fqw1+1][3]

                dd3=sqr(dx3^2+dy3^2)

                aaz3=atn(dz3/(dd3+0.000001))

                zzz111=(dpc-ddd2-r1)*tan(aaz3)

            endif


                yq1=yq

                if aa>180 then yq1=-yq1

        if aa>180 then

                vert  m1+wx2-yq1, wy2, rail_r[c11][3]-h1+h2+zz1
                vert  m1+wx2, wy2, rail_r[c11][3]-h1+h2+zz1

                vert -m1+wx1, wy1, rail_r[c11][3]-h1+h2+zz1
                vert -m1+wx1+yq1, wy1, rail_r[c11][3]-h1+h2+zz1

        else

                vert -m1+wx1, -wy1, rail_r[c11][3]-h1+h2+zz1
                vert -m1+wx1+yq1, -wy1, rail_r[c11][3]-h1+h2+zz1

                vert  m1+wx2-yq1, -wy2, rail_r[c11][3]-h1+h2+zz1
                vert  m1+wx2, -wy2, rail_r[c11][3]-h1+h2+zz1

        endif


                del 2+1


                !******* if first/last rail point is in between dpc interval

                if (ddd2-r1)<dpc then
                    c11=c11+1
                endif

                if (ddd11-r1)<dpc then
                    felteetel=1
                    c21=c21-1
                endif


                !**************************** intermediate interval

                for j=c11+1 to c21-1

                    ! ******* calculation of a1 & a2

                    dx1=rail_r[j][1]-rail_r[j-1][1]
                    dy1=rail_r[j][2]-rail_r[j-1][2]

                    dx2=rail_r[j+1][1]-rail_r[j][1]
                    dy2=rail_r[j+1][2]-rail_r[j][2]

                    if dx1=0 then
                        if dy1>0 then
                            a1=90
                        else
                            a1=270
                        endif
                    else
                        if dx1>0 then
                            if dy1>0 then
                                a1=atn(dy1/dx1)
                            else
                                a1=360+atn(dy1/dx1)
                            endif
                        else
                            a1=180+atn(dy1/dx1)
                        endif
                    endif


                    if dx2=0 then
                        if dy2>0 then
                            a2=90
                        else
                            a2=270
                        endif
                    else
                        if dx2>0 then
                            if dy2>0 then
                                a2=atn(dy2/dx2)
                            else
                                a2=360+atn(dy2/dx2)
                            endif
                        else
                            a2=180+atn(dy2/dx2)
                        endif
                    endif

                    ! ******* calculation of aa

                    if a2>a1 then
                        aa=a2-a1
                    else
                        aa=360+a2-a1
                    endif


                    if aa<180 then bb=180-aa
                    if aa>180 then bb=aa-180

                    if aa>180 then gg=180+bb/2
                    if aa<180 then gg=180-bb/2

                    addx rail_r[j][1]
                    addy rail_r[j][2]

                    rotz a1+gg

                    m1=pd/2/sin(bb/2)
                    yq1=yq

                    if aa>180 then m1=-m1
                    if aa>180 then yq1=-yq1

                    vert -m1, 0, rail_r[j][3]-h1+h2
                    vert -m1+yq1, 0, rail_r[j][3]-h1+h2
                    vert  m1-yq1, 0, rail_r[j][3]-h1+h2
                    vert  m1, 0, rail_r[j][3]-h1+h2

                    qq=qq+1

                    del 2+1

                next j


                !*************************** interval before next column

                if felteetel=1 then
                    c21=c21+1   ! original c21
                    felteetel=0
                endif



                ! ******** calculation of a1 & a2


                if dx11=0 then
                    if dy11>0 then
                        a1=90
                    else
                        a1=270
                    endif
                else
                    if dx11>0 then
                        if dy11>0 then
                            a1=atn(dy11/dx11)
                        else
                            a1=360+atn(dy11/dx11)
                        endif
                    else
                        a1=180+atn(dy11/dx11)
                    endif
                endif


                if dx22=0 then
                    if dy22>0 then
                        a2=90
                    else
                        a2=270
                    endif
                else
                    if dx22>0 then
                        if dy22>0 then
                            a2=atn(dy22/dx22)
                        else
                            a2=360+atn(dy22/dx22)
                        endif
                    else
                        a2=180+atn(dy22/dx22)
                    endif
                endif



                ! ******* calculation of aa

                if a2>a1 then
                    aa=a2-a1
                else
                    aa=360+a2-a1
                endif

                if abs(a2-a1)<0.0001 then aa=0

                if aa<180 then bb=180-aa
                if aa>180 then bb=aa-180

                if aa>180 then gg=180+bb/2
                if aa<180 then gg=180-bb/2

                addx rail_r[c21][1]
                addy rail_r[c21][2]


                rotz a1+gg


        if abs(180-bb)<0.1 then
            ww=(r1+dpc)
        else
            ww=(r1+dpc)+pd/tan(bb/2)/2
        endif

        m1=pd/2/sin(bb/2)
        wx1=ww*cos(bb/2)
        wy1=ww*sin(bb/2)

        wwyy=2*m1*cos(bb/2)
        ww2=ww-wwyy

        wx2=ww2*cos(bb/2)
        wy2=ww2*sin(bb/2)

                zz2=(r1+dpc)*tan(aaz11)
                yq1=yq

                if aa>180 then yq1=-yq1

        if aa>180 then

            vert  m1+wx2-yq1, -wy2, rail_r[c21][3]-h1+h2-zz2
            vert  m1+wx2, -wy2, rail_r[c21][3]-h1+h2-zz2

            vert -m1+wx1, -wy1, rail_r[c21][3]-h1+h2-zz2
            vert -m1+wx1+yq1, -wy1, rail_r[c21][3]-h1+h2-zz2

        else

            vert -m1+wx1, wy1, rail_r[c21][3]-h1+h2-zz2
            vert -m1+wx1+yq1, wy1, rail_r[c21][3]-h1+h2-zz2

            vert  m1+wx2-yq1, wy2, rail_r[c21][3]-h1+h2-zz2
            vert  m1+wx2, wy2, rail_r[c21][3]-h1+h2-zz2

        endif

                del 2+1

                addz dq

            next kk

            del 2

            addz -dq-h2+h3-dq

        next gk

        del 2

        gosub 1000477


    next i


1000479:

ENDIF



goto 1000488



! ***************************** making of panels

1000477:

m=qq+2
qxq=0
qyq=0
qxqp=0
qyqp=0


! ************************ edges

!************ longitudinal edges

!**** bottom

for k=1 to 4*m-7 step 4      ! edges 1, n-1
    edge k, k+4, -1, -1, 0
next k

for k=2 to 4*m-6 step 4      ! edges n, 2*m-2
    edge k, k+4, -1, -1, 2
next k

for k=3 to 4*m-5 step 4      ! edges 2*m-1, 3*m-3
    edge k, k+4, -1, -1, 2
next k

for k=4 to 4*m-4 step 4      ! edges 3*m-2, 4*m-4
    edge k, k+4, -1, -1, 0
next k


!**** second level

for k=4*m+1 to 8*m-7 step 4   ! edges 4*m-3, 5*m-5
edge k, k+4, -1, -1, 2
next k

for k=4*m+4 to 8*m-4 step 4   ! edges 5*m-4, 6*m-6
edge k, k+4, -1, -1, 2
next k


!**** third level

for k=8*m+1 to 12*m-7 step 4   ! edges 6*m-5, 7*m-7
edge k, k+4, -1, -1, 2
next k

for k=8*m+4 to 12*m-4 step 4   ! edges 7*m-6, 8*m-8
edge k, k+4, -1, -1, 2
next k


!**** top

for k=12*m+1 to 16*m-7 step 4   ! edges 8*m-7, 9*m-9
edge k, k+4, -1, -1, 0
next k

for k=12*m+2 to 16*m-6 step 4   ! edges 9*m-8, 10*m-10
edge k, k+4, -1, -1, 2
next k

for k=12*m+3 to 16*m-5 step 4   ! edges 10*m-9, 11*m-11
edge k, k+4, -1, -1, 2
next k

for k=12*m+4 to 16*m-4 step 4   ! edges 11*m-10, 12*m-12
edge k, k+4, -1, -1, 0
next k


!****************** side edges

!**** side edges, bottom, front

for k=1 to 4*m-3 step 4       ! edges 12*m-11, 13*m-12
edge k, k+4*m, -1, -1, 1
next k


!**** side edges, bottom, back

for k=4 to 4*m step 4         ! edges 13*m-11, 14*m-12
edge k, k+4*m, -1, -1, 1
next k

!**** side edges, second level, front

edge 4*m+1, 4*m+1+4*m, -1, -1, 0

for k=4*m+5 to 8*m-7 step 4        ! edges 14*m-11, 15*m-12
qxq=qxq+1
if current_side=0 then
    stat=rail_l[c11+qxq][4]
else
    stat=rail_r[qxq+c11][4]
endif
edge k, k+4*m, -1, -1, stat
next k

edge 8*m-3, 8*m-3+4*m, -1, -1, 0

!**** side edges, second level, back

edge 4*m+4, 4*m+4+4*m, -1, -1, 0

for k=4*m+8 to 8*m-4 step 4        ! edges 15*m-11, 16*m-12
qyq=qyq+1
if current_side=0 then
    stat1=rail_l[qyq+c11][4]
else
    stat1=rail_r[qyq+c11][4]
endif
edge k, k+4*m, -1, -1, stat
next k

edge 8*m, 8*m+4*m, -1, -1, 0

!**** side edges, third level, front

for k=8*m+1 to 12*m-3 step 4        ! edges 16*m-11, 17*m-12
edge k, k+4*m, -1, -1, 1
next k

!**** side edges, third level, back

for k=8*m+4 to 12*m step 4        ! edges 17*m-11, 18*m-12
edge k, k+4*m, -1, -1, 1
next k


!****************** edges along depth

!**** bottom, first row

qwy1=0
for k=1 to 4*m-3 step 4        ! edges 18*m-11, 19*m-12
    if k=4*m-3 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k

!**** bottom, second row

qwy1=0
for k=2 to 4*m-2 step 4        ! edges 19*m-11, 20*m-12
    if k=4*m-2 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k

!**** bottom, third row

qwy1=0
for k=3 to 4*m-1 step 4        ! edges 20*m-11, 21*m-12
    if k=4*m-1 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k


!**** top, first row

qwy1=0
for k=12*m+1 to 16*m-3 step 4        ! edges 21*m-11, 22*m-12
    if k=16*m-3 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k

!**** top, second row

qwy1=0
for k=12*m+2 to 16*m-2 step 4        ! edges 22*m-11, 23*m-12
    if k=16*m-2 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k

!**** top, third row

qwy1=0
for k=12*m+3 to 16*m-1 step 4        ! edges 23*m-11, 24*m-12
    if k=16*m-1 then qwy1=0
    edge k, k+1,  -1, -1, qwy1
    qwy1=2
next k


!****************** second edges along depth

!**** bottom, first row        ! edges 24*m-11, 25*m-13

for k=1 to 4*m-7 step 4
edge k, k+5,  -1, -1, 2
next k

!**** bottom, second row        ! edges 25*m-12, 26*m-14

for k=2 to 4*m-6 step 4
edge k, k+5,  -1, -1, 2
next k

!**** bottom, third row        ! edges 26*m-13, 27*m-15

for k=3 to 4*m-5 step 4
edge k, k+5,  -1, -1, 2
next k


!**** top, first row        ! edges 27*m-14, 28*m-16

for k=12*m+1 to 16*m-7 step 4
edge k, k+5,  -1, -1, 2
next k

!**** top, second row        ! edges 28*m-15, 29*m-17

for k=12*m+2 to 16*m-6 step 4
edge k, k+5,  -1, -1, 2
next k

!**** top, third row        ! edges 29*m-16, 30*m-18

for k=12*m+3 to 16*m-5 step 4
edge k, k+5,  -1, -1, 2
next k



! ********************* polygons

!******* bottom polygons

!*** first row

for k=1 to m-1 step 1
pgon 3, 0, 2, -k, (k+24*m-12), -(k+18*m-11)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+18*m-12, k+m-1, -(k+24*m-12)
next k

!*** second row

for k=1 to m-1 step 1
pgon 3, 0, 2, -(k+m-1), (k+25*m-13), -(k+19*m-11)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+19*m-12, k+2*m-2, -(k+25*m-13)
next k

!*** third row

for k=1 to m-1 step 1
pgon 3, 0, 2, -(k+2*m-2), (k+26*m-14), -(k+20*m-11)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+20*m-12, k+3*m-3, -(k+26*m-14)
next k




!******* top polygons

!*** first row

for k=1 to m-1 step 1
pgon 3, 0, 2, k+8*m-8, k+21*m-11, -(k+27*m-15)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+21*m-12, k+9*m-9, -(k+27*m-15)
next k

!*** second row

for k=1 to m-1 step 1
pgon 3, 0, 2, k+9*m-9, k+22*m-11, -(k+28*m-16)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+22*m-12, k+10*m-10, -(k+28*m-16)
next k

!*** third row

for k=1 to m-1 step 1
pgon 3, 0, 2, k+10*m-10, k+23*m-11, -(k+29*m-17)
next k

for k=1 to m-1 step 1
pgon 3, 0, 2, k+23*m-12, k+11*m-11, -(k+29*m-17)
next k


!******* side polygons, front

!*** first row

for k=1 to m-1 step 1
pgon 4, 0, 0, k, k+12*m-11, -(k+4*m-4), -(k+12*m-12)
next k

!*** second row

for k=1 to m-1 step 1
qxqp=qxqp+1
if current_side=0 then
    stat=rail_l[c11+qxqp][4]
    if stat >1 then statp=2
else
    stat=rail_r[qxqp+c11][4]
    if stat >1 then statp=2
endif
pgon 4, 0, 2, k+4*m-4, k+14*m-11, -(k+6*m-6), -(k+14*m-12)
next k

!*** third row

for k=1 to m-1 step 1
pgon 4, 0, statp, k+6*m-6, k+16*m-11, -(k+8*m-8), -(k+16*m-12)
next k

!******* side polygons, back

!*** first row

for k=1 to m-1 step 1
pgon 4, 0, 0, -(k+3*m-3), (k+13*m-12), (k+5*m-5), -(k+13*m-11)
next k

!*** second row

for k=1 to m-1 step 1
qyqp=qyqp+1
if current_side=0 then
    stat=rail_l[c11+qyqp][4]
    if stat >1 then statp=2
else
    stat=rail_r[qyqp+c11][4]
    if stat >1 then statp=2
endif
pgon 4, 0, statp, -(k+5*m-5), (k+15*m-12), (k+7*m-7), -(k+15*m-11)
next k

!*** third row

for k=1 to m-1 step 1
pgon 4, 0, 0, -(k+7*m-7), (k+17*m-12), (k+11*m-11), -(k+17*m-11)
next k


!****** end polygons

! beginning
pgon 12, 0, 0, 12*m-11, 14*m-11, 16*m-11, 21*m-11, 22*m-11, 23*m-11, -(17*m-11), -(15*m-11), -(13*m-11), -(20*m-11), -(19*m-11), -(18*m-11)

! end
pgon 12, 0, 0, 13*m-12, 15*m-12, 17*m-12, 22*m-12, 23*m-12, 24*m-12, -(18*m-12), -(16*m-12), -(14*m-12), -(21*m-12), -(20*m-12), -(19*m-12)


body 2

base

return



1000488:

del top


RETURN
