


***Name: "Spindel mit schweren Balken"		! Must be defined railing type name
***Code: 10003			! 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.0, 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.8,0.8]=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 [0,3]=1         ! no Column, closest to, on every Tread, pieces: 0,1,2,3
ClosestToDistance: D [0.2,3.2]=0.80  ! distance of columns to each other
SnapToCorners:     B [0,1]=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:"Stützenradius" D [0,0.1]=0.025        ! radius of column
cyl:"Zylindr. Stangen" B [0,1]=1        ! cylindric pieces
r2:"Vert.Stangenradi" D [0,0.1]=0.0075   ! radius of vertical tubes
wh:"Abst.v.Stangen" D [0,1]=0.1             ! width of holes of vertical pieces
dl:"Höhe v.ob.Balken" D [0,1]=0.1          ! smaller distance of longitudinal tubes
mat_r:"Geländermat." M []=10     ! railing material
mat_c:"Trep.körpermat." M []=47      ! column material


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








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


10003:                            ! 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
wl=1.2*r1            		! width of longitudinal tube
hl=wl*0.5		        ! height of rail section
wp=2*r2		  		! vertical piece width (if rectangular)
dp=wp				! vertical piece depth
mat_f=mat_c			! field tube material


dist=wh+2*r2           ! distance between vertical pieces axises


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



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

    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



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

    material mat_r


    !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])


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

    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]+voct+vocb   ! 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



    !*********************** longitudinal tubes

    material mat_f


    if n<2 then

        c1=fqw1                   ! first rail point from railing coordinates
        c2=fqwn                  ! last rail point from railing coordinates

        for jj=1 to 2

        for kk=1 to 2

            for i=c1 to c2
                put rail_l [i][1], rail_l [i][2], rail_l [i][3]-h1+h2, 0
            next i

            tube 5, c2-c1+1+2, 0,
                -wl/2, -hl/2, 1,
                -wl/2,  hl/2, 1,
                 wl/2,  hl/2, 1,
                 wl/2, -hl/2, 1,
                -wl/2, -hl/2, 1,

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

             addz dl

        next kk

        del 2

        addz h3-h2-dl

    next jj

    del 2

    else

        xvxv=segm_l[current_segment][7]
        xvxvx=segm_l[current_segment][7]+n-1

        c1=column_l[xvxv][4]   ! first column point from railing coordinates
        c2=column_l[xvxvx][4]  ! last column point from railing coordinates

        for jj=1 to 2

        for kk=1 to 2

            for i=c1 to c2
                put rail_l [i][1], rail_l [i][2], rail_l [i][3]-h1+h2, 0
            next i

            tube 5, c2-c1+1+2, 0,
                -wl/2, -hl/2, 1,
                -wl/2,  hl/2, 1,
                 wl/2,  hl/2, 1,
                 wl/2, -hl/2, 1,
                -wl/2, -hl/2, 1,

                rail_l[c1][1]-(rail_l[c1+1][1]-rail_l[c1][1]), rail_l[c1][2]-(rail_l[c1+1][2]-rail_l[c1][2]), 	  	  	  rail_l[c1][3]-(rail_l[c1+1][3]-rail_l[c1][3]), 0,
                get (nsp),
                rail_l[c2][1]+(rail_l[c2][1]-rail_l[c2-1][1]), rail_l[c2][2]+(rail_l[c2][2]-rail_l[c2-1][2]), 	  	  	  rail_l[c2][3]+(rail_l[c2][3]-rail_l[c2-1][3]), 0

             addz dl

        next kk

        del 2

        addz h3-h2-dl

    next jj

    del 2

    endif




    !*********************** vertical pieces



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

    if n=1 then goto 1000310    !only one column in the segment

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

        qq=0
        xx00=0
        ds=0
        fwqx=0

        for ii=c11 to c21-1

            x1=(rail_l[ii+1][1]-rail_l[ii][1])
            y1=(rail_l[ii+1][2]-rail_l[ii][2])

            d1=sqr(x1^2+y1^2)
            ds=ds+d1              ! distance between two column axises

        next ii

        ds=ds-r1*2+2*r2                      ! effective distance
        if ds=0 then goto 1000310
        nx=int(ds/dist)                      ! number of holes
        if nx+1-(ds/dist)<0.5 then nx=nx+1   ! "closest to"
        if nx=0 then goto 1000310
        dve=ds/nx                            ! real distance of vertical elements
        dve2=dve/2                           ! real dist. of secondary vert. elem.
        nve=nx-1                             ! number of vertical elements

        dve=dve/2

        if nve<1 then goto 1000310

        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])

        ddd=sqr(dx1^2+dy1^2)   ! distance until point next to column
        aaz1=atn(dz1/(ddd+0.00001))      ! angle of vertical displacement

        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


        addx rail_l[c11][1]
        addy rail_l[c11][2]
        addz rail_l[c11][3]-h1+h2


        if ddd<r1 then

            rotz a1
            addx ddd
            xx00=dve-ddd
            goto 1000399

        endif


        rotz a1
        addx r1-r2                       ! place of the 0th (virtual) piece
        addz (r1-r2)*tan(aaz1)

        1000311:
        addx dve
        addz dve*tan(aaz1)
        qq=qq+1

        if  dve*qq<=ddd-(r1-r2) then
            gosub 1000320
            goto 1000311
        else
            xx00=dve*qq+r1-r2-ddd
            addx -xx00
            addz -xx00*tan(aaz1)
        endif

        1000399:

        if c11+1=c21 then goto 1000310


        for ii=c11+1 to c21-2              ! between two columns

            qqq=0

            dx1=(rail_l[ii][1]-rail_l[ii-1][1])
            dy1=(rail_l[ii][2]-rail_l[ii-1][2])

            dx2=(rail_l[ii+1][1]-rail_l[ii][1])
            dy2=(rail_l[ii+1][2]-rail_l[ii][2])
            dz2=(rail_l[ii+1][3]-rail_l[ii][3])

            ddd=sqr(dx2^2+dy2^2)          ! distance between two subsegments
            aaz2=atn(dz2/(ddd+0.00001))


            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


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


            rotz aa

            if xx00<=ddd then
                addx xx00
                addz xx00*tan(aaz2)
                gosub 1000320

                1000312:
                addx dve
                addz dve*tan(aaz2)
                qqq=qqq+1

                if xx00+qqq*dve<=ddd then
                    gosub 1000320
                    goto 1000312
                else
                    xx00=dve*qqq+xx00-ddd
                    addx -xx00
                    addz -xx00*tan(aaz2)
                endif
            else
                 xx00=xx00-ddd
                 addx ddd
                 addz ddd*tan(aaz2)
            endif

        next ii



        qqqq=0

        dx1=(rail_l[c21-1][1]-rail_l[c21-2][1])
        dy1=(rail_l[c21-1][2]-rail_l[c21-2][2])

        dx2=(rail_l[c21][1]-rail_l[c21-1][1])
        dy2=(rail_l[c21][2]-rail_l[c21-1][2])
        dz2=(rail_l[c21][3]-rail_l[c21-1][3])

        ddd=sqr(dx2^2+dy2^2)          ! distance between two subsegments
        aaz2=atn(dz2/(ddd+0.00001))


        if ddd<r1 then goto 1000310

        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


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


        rotz aa


        if xx00<ddd-r1 then
            addx xx00
            addz xx00*tan(aaz2)
            gosub 1000320

            1000322:
            addx dve
            addz dve*tan(aaz2)
            qqqq=qqqq+1

            if xx00+qqqq*dve<ddd-r1 then
                gosub 1000320
                goto 1000322
            endif
        endif


        1000310:

        del top


    next j



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


    !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])


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

    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]+voct+vocb   ! 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



    !*********************** longitudinal tubes

    material mat_f

    if n<2 then

        c1=fqw1                   ! first rail point from railing coordinates
        c2=fqwn                  ! last rail point from railing coordinates

        for jj=1 to 2

            for kk=1 to 2

                for i=c1 to c2
                    put rail_r [i][1], rail_r [i][2], rail_r [i][3]-h1+h2, 0
                next i

                tube 5, c2-c1+1+2, 0,
                    -wl/2, -hl/2, 1,
                    -wl/2,  hl/2, 1,
                     wl/2,  hl/2, 1,
                     wl/2, -hl/2, 1,
                    -wl/2, -hl/2, 1,

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

             addz dl

        next kk

        del 2

        addz h3-h2-dl

    next jj

    del 2


    else

        xvxv=segm_r[current_segment][7]
        xvxvx=segm_r[current_segment][7]+n-1

        c1=column_r[xvxv][4]   ! first column point from railing coordinates
        c2=column_r[xvxvx][4]  ! last column point from railing coordinates

        for jj=1 to 2

        for kk=1 to 2

            for i=c1 to c2
                put rail_r [i][1], rail_r [i][2], rail_r [i][3]-h1+h2, 0
            next i

            tube 5, c2-c1+1+2, 0,
                -wl/2, -hl/2, 1,
                -wl/2,  hl/2, 1,
                 wl/2,  hl/2, 1,
                 wl/2, -hl/2, 1,
                -wl/2, -hl/2, 1,

                rail_r[c1][1]-(rail_r[c1+1][1]-rail_r[c1][1]), rail_r[c1][2]-(rail_r[c1+1][2]-rail_r[c1][2]), 	  	  	  	  rail_r[c1][3]-(rail_r[c1+1][3]-rail_r[c1][3]), 0,
                get (nsp),
                rail_r[c2][1]+(rail_r[c2][1]-rail_r[c2-1][1]), rail_r[c2][2]+(rail_r[c2][2]-rail_r[c2-1][2]), 	  	  	  	  rail_r[c2][3]+(rail_r[c2][3]-rail_r[c2-1][3]), 0

             addz dl

        next kk

        del 2

        addz h3-h2-dl

    next jj

    del 2


    endif





    !*********************** vertical pieces


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

    if n=1 then goto 1000330    !only one column in the segment

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

        qq=0
        xx00=0
        ds=0
        fwqx=0

        for ii=c11 to c21-1

            x1=(rail_r[ii+1][1]-rail_r[ii][1])
            y1=(rail_r[ii+1][2]-rail_r[ii][2])

            d1=sqr(x1^2+y1^2)
            ds=ds+d1              ! distance between two column axises

        next ii

        ds=ds-r1*2+2*r2                      ! effective distance
        if ds=0 then goto 1000330
        nx=int(ds/dist)                      ! number of holes
        if nx+1-(ds/dist)<0.5 then nx=nx+1   ! "closest to"
        if nx=0 then goto 1000330
        dve=ds/nx                            ! real distance of vertical elements
        dve2=dve/2                           ! real dist. of secondary vert. elem.
        nve=nx-1                             ! number of vertical elements

        dve=dve/2

        if nve<1 then goto 1000330

        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])

        ddd=sqr(dx1^2+dy1^2)   ! distance until point next to column
        aaz1=atn(dz1/(ddd+0.00001))      ! angle of vertical displacement

        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


        addx rail_r[c11][1]
        addy rail_r[c11][2]
        addz rail_r[c11][3]-h1+h2


        if ddd<r1 then

            rotz a1
            addx ddd
            xx00=dve-ddd
            goto 1000398

        endif


        rotz a1
        addx r1-r2                       ! place of the 0th (virtual) piece
        addz (r1-r2)*tan(aaz1)

        1000331:
        addx dve
        addz dve*tan(aaz1)
        qq=qq+1

        if  dve*qq<=ddd-(r1-r2) then
            gosub 1000320
            goto 1000331
        else
            xx00=dve*qq+r1-r2-ddd
            addx -xx00
            addz -xx00*tan(aaz1)
        endif

        1000398:

        if c11+1=c21 then goto 1000330


        for ii=c11+1 to c21-2              ! between two columns

            qqq=0

            dx1=(rail_r[ii][1]-rail_r[ii-1][1])
            dy1=(rail_r[ii][2]-rail_r[ii-1][2])

            dx2=(rail_r[ii+1][1]-rail_r[ii][1])
            dy2=(rail_r[ii+1][2]-rail_r[ii][2])
            dz2=(rail_r[ii+1][3]-rail_r[ii][3])

            ddd=sqr(dx2^2+dy2^2)          ! distance between two subsegments
            aaz2=atn(dz2/(ddd+0.00001))


            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


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


            rotz aa

            if xx00<=ddd then
                addx xx00
                addz xx00*tan(aaz2)
                gosub 1000320

                1000332:
                addx dve
                addz dve*tan(aaz2)
                qqq=qqq+1

                if xx00+qqq*dve<=ddd then
                    gosub 1000320
                    goto 1000332
                else
                    xx00=dve*qqq+xx00-ddd
                    addx -xx00
                    addz -xx00*tan(aaz2)
                endif
            else
                 xx00=xx00-ddd
                 addx ddd
                 addz ddd*tan(aaz2)
            endif

        next ii



        qqqq=0

        dx1=(rail_r[c21-1][1]-rail_r[c21-2][1])
        dy1=(rail_r[c21-1][2]-rail_r[c21-2][2])

        dx2=(rail_r[c21][1]-rail_r[c21-1][1])
        dy2=(rail_r[c21][2]-rail_r[c21-1][2])
        dz2=(rail_r[c21][3]-rail_r[c21-1][3])

        ddd=sqr(dx2^2+dy2^2)          ! distance between two subsegments
        aaz2=atn(dz2/(ddd+0.00001))


        if ddd<r1 then goto 1000330

        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


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


        rotz aa


        if xx00<ddd-r1 then
            addx xx00
            addz xx00*tan(aaz2)
            gosub 1000320

            1000342:
            addx dve
            addz dve*tan(aaz2)
            qqqq=qqqq+1

            if xx00+qqqq*dve<ddd-r1 then
                gosub 1000320
                goto 1000342
            endif
        endif


        1000330:

        del top


    next j




ENDIF




goto 1000388



! ************ vertical pieces


1000320:

fwqx=fwqx+1

if fwqx/2=int(fwqx/2) then

    if cyl=1 then

        cylind h3-h2, r2

    else

        prism 4, h3-h2,
            -wp/2, -dp/2,
            -wp/2,  dp/2,
             wp/2,  dp/2,
             wp/2, -dp/2

    endif

else

    if cyl=1 then

        cylind dl, r2

        addz h3-h2-dl

        cylind dl, r2

        del 1

    else

        prism 4, dl,
            -wp/2, -dp/2,
            -wp/2,  dp/2,
             wp/2,  dp/2,
             wp/2, -dp/2

        addz h3-h2-dl

        prism 4, dl,
            -wp/2, -dp/2,
            -wp/2,  dp/2,
             wp/2,  dp/2,
             wp/2, -dp/2

        del 1

    endif


endif

return


1000388:


del top


RETURN
