***Name: "Verdrahtet"		! Must be defined railing type name
***Code: 10006				!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.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 [1,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
cw:"Stützenbreite" D [0.01,0.1]=0.05          ! column width
cd:"Stützentiefe" D [0.01,0.1]=0.06          ! column depth
r1:"Geländerradius" D [0.01,0.1]=0.025          ! radius of rail
r2:"Röhrenradius" D [0.01,0.1]=0.002          ! radius of longitudinal tubes
vdft:"Vert. Abstand" D [0.1,1]=0.1    ! vert. dist. of field tubes ("closest to" principle)
mat_r:"Geländermat." M []=10     ! railing material
mat_c:"Stützenmaterial" M []=47      ! column material



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







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





10006:                            ! railing code

if res =0 then resol 8 else resol 36
mat_f=mat_c			! field tube material

offs=ColumnOffset                 ! column axis offset
offs=-offs                        ! as used in GDL-script



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


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


    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


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


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


    res=16          ! resol of rail tube
    aa=360/res


    for i=1 to res
        aa1=(i-1)*aa
        put r1*sin(aa1), r1*cos(aa1), 1
    next i


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



    tube res, nr+2, 1+2+16+32,
        get (res*3),

        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

        if c1>1 then

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

        else

            dx1=rail_l[c1+1][1]-rail_l[c1][1]
            dy1=rail_l[c1+1][2]-rail_l[c1][2]

        endif

        if c1=segm_l[current_segment][2] then

            dx2=rail_l[c1][1]-rail_l[c1-1][1]
            dy2=rail_l[c1][2]-rail_l[c1-1][2]

        else

            dx2=(rail_l[c1+1][1]-rail_l[c1][1])
            dy2=(rail_l[c1+1][2]-rail_l[c1][2])

        endif


        if dx1=0 then      !  counting a1 & a2
            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       ! counting of aa
            aa=a2-a1
        else
            aa=360+a2-a1
        endif



        if abs(a1-a2)>15  then  goto 1000666

        aaa=a1               ! rotating angle of column

        if dx1<0 then aaa=180+a1
        offs1=offs

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


        rotz aaa
        addy offs1

        prism 4, hc1,           ! normal columns
            -cw/2, -cd/2,
            -cw/2,  cd/2,
             cw/2,  cd/2,
             cw/2, -cd/2

        del 1+3+1

        goto 1000677


        1000666:

        dd=sqr(dx2^2+dy2^2)   ! column classic

        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 column_l[i][1]
        addy column_l[i][2]
        addz column_l[i][3]-vocb

        rotz a1
        rotz gg


        ww=max(cw,cd)

        m1=cd/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)

        wx3=cd*sin(bb/2)
        wy3=cd*cos(bb/2)


        off1=offs/sin(bb/2)              ! column offset from rail axis
        if aa>180 then off1=-off1

        addx off1

        if ww2>0 then

            prism_ 7, hc1,
                m1, 0, 15,
                m1+wx2, wy2, 15,
               -m1+wx1, wy1, 15,
               -m1, 0, 15,
               -m1+wx1, -wy1, 15,
                m1+wx2, -wy2, 15,
                m1, 0, -1

        else

            prism_ 5, hc1,
                m1, 0, 15,
                m1-wx3, wy3, 15,
               -m1, 0, 15,
                m1-wx3, -wy3, 15,
                m1, 0, 1

        endif

        del 6

        1000677:


    next i




    !*********************** field tubes

    material mat_f

    aa=0

    d=int((h3-h2)/vdft)
    if d+1-int((h3-h2)/vdft)<0.5 then d=d+1   ! "closest to vdft"

    dd=(h3-h2)/d

    res=8          ! resol of field tube cylinder
    aa=360/res

    if n<2 then

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

	    for kk=1 to d+1

	        for i=1 to res
	            aa1=(i-1)*aa
	            put r2*sin(aa1), r2*cos(aa1), 1
	        next i

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

	        tube res, c2-c1+1+2, 1+2+16+32,
	            get (res*3),

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

	         addz dd

	    next kk

	    del d+1

    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 kk=1 to d+1

        for i=1 to res
            aa1=(i-1)*aa
            put r2*sin(aa1), r2*cos(aa1), 1
        next i

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

        tube res, c2-c1+1+2, 1+2+16+32,
            get (res*3),

            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 dd

    next kk

    del d+1

    endif





ELSE

    offs=-offs

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

    !*********************************** right side 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


    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


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


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


    res=16          ! resol of rail tube
    aa=360/res


    for i=1 to res
        aa1=(i-1)*aa
        put r1*sin(aa1), r1*cos(aa1), 1
    next i


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



    tube res, nr+2, 1+2+16+32,
        get (res*3),

        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


        if c1>1 then

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

        else

            dx1=rail_r[c1+1][1]-rail_r[c1][1]
            dy1=rail_r[c1+1][2]-rail_r[c1][2]

        endif

        if c1=segm_r[current_segment][2] then

            dx2=rail_r[c1][1]-rail_r[c1-1][1]
            dy2=rail_r[c1][2]-rail_r[c1-1][2]

        else

            dx2=(rail_r[c1+1][1]-rail_r[c1][1])
            dy2=(rail_r[c1+1][2]-rail_r[c1][2])

        endif


        if dx1=0 then      !  counting a1 & a2
            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       ! counting of aa
            aa=a2-a1
        else
            aa=360+a2-a1
        endif



        if abs(a1-a2)>15  then  goto 1000668

        aaa=a1               ! rotating angle of column

        if dx1<0 then aaa=180+a1
        offs1=offs

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


        rotz aaa
        addy offs1

        prism 4, hc1,           ! normal columns
            -cw/2, -cd/2,
            -cw/2,  cd/2,
             cw/2,  cd/2,
             cw/2, -cd/2

        del 1+3+1

        goto 1000678


        1000668:

        dd=sqr(dx2^2+dy2^2)   ! column classic

        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 column_r[i][1]
        addy column_r[i][2]
        addz column_r[i][3]-vocb

        rotz a1
        rotz gg


        ww=max(cw,cd)

        m1=cd/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)

        wx3=cd*sin(bb/2)
        wy3=cd*cos(bb/2)


        off1=offs/sin(bb/2)              ! column offset from rail axis
        if aa>180 then off1=-off1

        addx off1

        if ww2>0 then

            prism_ 7, hc1,
                m1, 0, 15,
                m1+wx2, wy2, 15,
               -m1+wx1, wy1, 15,
               -m1, 0, 15,
               -m1+wx1, -wy1, 15,
                m1+wx2, -wy2, 15,
                m1, 0, -1

        else

            prism_ 5, hc1,
                m1, 0, 15,
                m1-wx3, wy3, 15,
               -m1, 0, 15,
                m1-wx3, -wy3, 15,
                m1, 0, 1

        endif

        del 6

        1000678:


    next i




    !*********************** field tubes

    material mat_f

    aa=0

    d=int((h3-h2)/vdft)
    if d+1-int((h3-h2)/vdft)<0.5 then d=d+1   ! "closest to vdft"

    dd=(h3-h2)/d

    res=8          ! resol of field tube cylinder
    aa=360/res

    if n<2 then

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

	    for kk=1 to d+1

	        for i=1 to res
	            aa1=(i-1)*aa
	            put r2*sin(aa1), r2*cos(aa1), 1
	        next i

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

	        tube res, c2-c1+1+2, 1+2+16+32,
	            get (res*3),

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

	         addz dd

	    next kk

	    del d+1

    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 kk=1 to d+1

        for i=1 to res
            aa1=(i-1)*aa
            put r2*sin(aa1), r2*cos(aa1), 1
        next i

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

        tube res, c2-c1+1+2, 1+2+16+32,
            get (res*3),

            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 dd

    next kk

    del d+1

    endif



ENDIF

1000644:

del top

RETURN
