!   TITLE:  screw_thread_2d.mac (SM_TOOL)
!
!
!   This sub-macro creates a 2d planar axisymmetric screw thread profile.
!   This is used for a preliminary analysis of screw thread stresses in
!   an axisymmetric profile cut through the screw and mating member.
!
!   The threads are layed out in relation to a cartesian c.s. The first
!   keypoint is created at the origin of this c.s. and is the outermost
!   point at the base of the first thread.  The rest of the threads are
!   then created in the first quadrant (+x and +y) of this c.s.
!
!   The thread parameters are chosen for the worst case (conservative)
!   loading conditions; i.e., the smallest root radii, the largest tooth
!   height, etc. Therefore, the user should enter in values for D_MJR and
!   D_MIN that will yield the worst loading dimensions based on the allow-
!   able tolerances as defined by ASME B1.1 - 1989 ( revision of ANSI
!   B1.1 - 1982). This would be the maximum major diameter and the min-
!   imum minor diameter.  If the desired analysis is for the maximum mat-
!   erial condition (MMC), then D_MJR and D_MIN should not be specified,
!   and the program will calculate the thread parameters based on formulae
!   derived at MMC.
!
!   Input for this macro:
!
!           prof = thread profile.  Options are:
!
!               'i' = internal thread profile
!               'x' = external thread profile
!
!           n_thrd = number of threads to create.  If length of
!                thread engagement is known, then specify
!                THRD_LN and leave this parameter blank.
!
!           thrd_ln = thread length.  If N_THRD is not specified,
!                 then the program will calculate the number
!                 of threads in a user specified length of
!                 thread engagement.
!
!           n_inch = number of threads per inch
!
!           stndrd = thread standard.
!
!               'j' = UNJ thread standard, which specifies
!                     a root radius, R_THRD.
!
!               'un' (or blank) = UN thread profile, flat
!                         surface at thread root.
!
!           d_mjr = thread MAXIMUM major diameter
!
!           d_min = thread MINIMUM minor diameter
!
!
!
! SCREW_THREAD_2D, prof, n_thrd, thrd_ln, n_inch, stndrd, d_mjr, d_min
!
!
*get,prkey_,active,0,prkey
/nopr

prof=arg1
n_thrd=arg2
thrd_ln=arg3
n_inch=arg4
stndrd=arg5
d_mjr=arg6
d_min=arg7
!
!
lstart=lm_prev
!
!
*get,zzzz,parm,stndrd,type
!
*if,zzzz,eq,0,then
    stndrd='un'
*endif
!
!
*if,n_thrd,eq,0,then
    n_thrd=nint(thrd_ln*n_inch)+1
*endif
!
!
p_thrd=1/n_inch         ! calculation of thread pitch
!
*if,d_mjr,eq,0,then
    h_thrd=(sqrt(3)/2)*p_thrd   ! calculate thread height (MMC)
*else
    h_thrd=(d_mjr-d_min)/2      ! calculate thread height (worst case)
*endif
!
thrd_ang=(2*atan((p_thrd/2)/h_thrd))*dconv  ! thread angle
!
r_thrd=0.15011*p_thrd           ! fillet radius at thread root
!
!
!


*go,:skip


*if,prof,eq,'x',then
!
!
*get,thrd_cs,active,,csys
!
csn=csn+1
ndselcs=csn
clocal,ndselcs,0,0,0,0,(90-thrd_ang/2),0,0
!
csys,thrd_cs
!
*if,stndrd,eq,'j',then
    *do,iii,0,2*n_thrd,1
        *if,mod(iii,2),eq,0,then
            k,,iii*(p_thrd/2),0,0
        *elseif,mod(iii,2),eq,1,then
            k,,iii*(p_thrd/2),h_thrd,0
            !
            init_entity_num
            !
            k_tip=km
        *endif
        !
        init_entity_num
        !
        *if,iii,gt,0,then
            *if,mod(iii,2),eq,0,then
            lstr,km,k_tip
            *elseif,mod(iii,2),eq,1,then
            lstr,km-1,k_tip
            *endif
        *endif
        !
        init_entity_num
        !
        *if,iii,ge,3,then
           *if,mod(iii,2),eq,1,then
            lfillt,lm-1,lm,r_thrd
           *endif
        *endif
        !
        init_entity_num
    *enddo
    !
    r_width=p_thrd/8
    root_h=(r_width/2)/tan((thrd_ang/2)*rconv)
    r2edge=0.649519*p_thrd
    h_thrd2=root_h+r2edge
    !
    k,,0,h_thrd2,0
    k,,kx(km),h_thrd2,0
    !
    create_lines
    !
    lsel,selv,loc,x,(0.5*p_thrd),(n_thrd-0.5)*p_thrd
    lsel,r,loc,y,(r_thrd/cos(thrd_ang*rconv))*(sin(thrd_ang*rconv))**2,h_thrd
    lsel,r,loc,z,0
    lsel,a,line,,lm-1
    lsel,a,line,,lm-(2*n_thrd+n_thrd-1)
    !
    ln_part_ln,lm
    !
    init_entity_num
    !
    *do,iij,1,2*n_thrd-1,2
        lstr,km-(2*n_thrd-iij),km-(2*n_thrd-iij-1)
    *enddo
    !
    init_entity_num
    !
    *if,d_mjr,eq,0,then
        lsel,selv,loc,y,1.015*((7/8)*h_thrd),h_thrd
    *else
        lsel,selv,loc,y,1.015*(h_thrd2),h_thrd
    *endif
    !
    lsel,r,loc,x,0,n_thrd*p_thrd
    lsel,r,loc,z,0
    !
    ldele,all,,,1
    !
    init_entity_num
    !
    ksel,selv,loc,x,0
    ksel,r,loc,y,0
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k1_thrd=km
    !
    init_entity_num
    !
    ksel,selv,loc,x,n_thrd*p_thrd
    ksel,r,loc,y,0
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k2_thrd=km
    !
    init_entity_num
    !
*elseif,stndrd,eq,'un',then
    !
    *do,ijk,0,2*n_thrd,1
        *if,mod(ijk,2),eq,0,then
            k,,ijk*(p_thrd/2),0,0
        *elseif,mod(ijk,2),eq,1,then
            k,,ijk*(p_thrd/2),h_thrd,0
        *endif
        !
        init_entity_num
        !
        *if,ijk,gt,0,then
            lstr,km-1,km
        *endif
        !
        init_entity_num
    *enddo
    !
    k,,0,h_thrd/4,0
    k,,kx(km),h_thrd/4,0
    !
    create_lines
    !
    *if,d_mjr,eq,0,then
        k,,0,(7/8)*h_thrd,0
        k,,kx(km),(7/8)*h_thrd,0
    *else
        r_width=p_thrd/4
        root_h=(r_width/2)/tan((thrd_ang/2)*rconv)
        r2edge=(d_mjr-d_min)/2
        h_thrd2=root_h+r2edge
        !
        k,,0,h_thrd2,0
        k,,kx(km),h_thrd2,0
    *endif
    !
    create_lines
    !
    lsel,s,line,,lm-1
    cm,lcut,line
    !
    init_entity_num
    !
    lsel,s,line,,lm-(2*n_thrd+1),lm
    !
    ln_part_ln,lcut
    !
    init_entity_num
    !
    ldele,lcut,,,1
    !
    init_entity_num
    !
    *do,iij,1,4*n_thrd-2,2
        *if,iij,eq,1,then
            counter=0
            incrm=3
        *endif
        !
        counter=counter+1
        !
        lstr,km-(4*n_thrd-1)+iij,km-(4*n_thrd-1)+(iij+1)
        !
        *get,lm,line,,num,max
        !
        *if,mod(counter,2),eq,0,then
            lfillt,lm,lstart+(2*n_thrd+5)+incrm,.0010
            lfillt,lm,lstart+(2*n_thrd+5)+incrm+3,.0010
            incrm=incrm+6
        *endif
    *enddo
    !
    init_entity_num
    !
    lsel,selv,loc,y,0,0.985*(h_thrd/4)
    !
    *if,d_mjr,eq,0,then
        lsel,a,loc,y,1.015*((7/8)*h_thrd),h_thrd
    *else
        lsel,selv,loc,y,1.015*(h_thrd2),h_thrd
    *endif
    !
    lsel,r,loc,x,0,n_thrd*p_thrd
    lsel,r,loc,z,0
    !
    ldele,all,,,1
    !
    init_entity_num
    !
    ksel,selv,loc,x,(h_thrd/4)/tan(thrd_ang*rconv)
    ksel,r,loc,y,h_thrd/4
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k1_thrd=km
    !
    init_entity_num
    !
    ksel,selv,loc,x,(n_thrd*p_thrd)-((h_thrd/4)/tan(thrd_ang*rconv))
    ksel,r,loc,y,h_thrd/4
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k2_thrd=km
    !
    init_entity_num
*endif
!
k,,kx(k1_thrd),-1.3*p_thrd,0
k,,kx(k2_thrd),-1.3*p_thrd,0
!
create_lines
!
lstr,km-1,k1_thrd
lstr,km,k2_thrd
!
init_entity_num
!
isolate_lines
!
al,all
!
init_entity_num
!
lplot
!
!
*endif      ! END external thread profile
!
!
!
!
*if,prof,eq,'i',then
!
!
*if,stndrd,eq,'j',then
    *do,iii,0,2*n_thrd,1
        *if,mod(iii,2),eq,0,then
            k,,iii*(p_thrd/2),0,0
        *elseif,mod(iii,2),eq,1,then
            k,,iii*(p_thrd/2),h_thrd,0
            !
            init_entity_num
            !
            k_tip=km
        *endif
        !
        init_entity_num
        !
        *if,iii,gt,0,then
            *if,mod(iii,2),eq,0,then
            lstr,km,k_tip
            *elseif,mod(iii,2),eq,1,then
            lstr,km-1,k_tip
            *endif
        *endif
        !
        init_entity_num
        !
        *if,iii,ge,3,then
           *if,mod(iii,2),eq,1,then
            lfillt,lm-1,lm,r_thrd
           *endif
        *endif
        !
        init_entity_num
    *enddo
    !
    r_width=p_thrd/8
    root_h=(r_width/2)/tan((thrd_ang/2)*rconv)
    r2edge=0.649519*p_thrd
    h_thrd2=root_h+r2edge
    !
    k,,0,h_thrd2,0
    k,,kx(km),h_thrd2,0
    !
    create_lines
    !
    lsel,selv,loc,x,(0.5*p_thrd),(n_thrd-0.5)*p_thrd
    lsel,r,loc,y,(r_thrd/cos(thrd_ang*rconv))*(sin(thrd_ang*rconv))**2,h_thrd
    lsel,r,loc,z,0
    lsel,a,line,,lm-1
    lsel,a,line,,lm-(2*n_thrd+n_thrd-1)
    !
    ln_part_ln,lm
    !
    init_entity_num
    !
    *do,iij,1,2*n_thrd-1,2
        lstr,km-(2*n_thrd-iij),km-(2*n_thrd-iij-1)
    *enddo
    !
    init_entity_num
    !
    *if,d_mjr,eq,0,then
        lsel,selv,loc,y,1.015*((7/8)*h_thrd),h_thrd
    *else
        lsel,selv,loc,y,1.015*(h_thrd2),h_thrd
    *endif
    !
    lsel,r,loc,x,0,n_thrd*p_thrd
    lsel,r,loc,z,0
    !
    ldele,all,,,1
    !
    init_entity_num
    !
    ksel,selv,loc,x,0
    ksel,r,loc,y,0
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k1_thrd=km
    !
    init_entity_num
    !
    ksel,selv,loc,x,n_thrd*p_thrd
    ksel,r,loc,y,0
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k2_thrd=km
    !
    init_entity_num
    !
*elseif,stndrd,eq,'un',then
    !
    *do,ijk,0,2*n_thrd,1
        *if,mod(ijk,2),eq,0,then
            k,,ijk*(p_thrd/2),0,0
        *elseif,mod(ijk,2),eq,1,then
            k,,ijk*(p_thrd/2),h_thrd,0
        *endif
        !
        init_entity_num
        !
        *if,ijk,gt,0,then
            lstr,km-1,km
        *endif
        !
        init_entity_num
    *enddo
    !
    k,,0,(0.250*p_thrd)/(2*tan((thrd_ang/2)*rconv)),0
    k,,kx(km),(0.250*p_thrd)/(2*tan((thrd_ang/2)*rconv)),0
    !
    create_lines
    !
    *if,d_mjr,eq,0,then
         k,,0,h_thrd-(0.041667*p_thrd)/(2*tan((thrd_ang/2)*rconv)),0
         k,,kx(km),h_thrd-(0.041667*p_thrd)/(2*tan((thrd_ang/2)*rconv)),0
    *else
        r_width=p_thrd/4
        root_h=(r_width/2)/tan((thrd_ang/2)*rconv)
        r2edge=(d_mjr-d_min)/2
        h_thrd2=root_h+r2edge
        !
        k,,0,h_thrd2,0
        k,,kx(km),h_thrd2,0
    *endif
    !
    create_lines
    !
    lsel,s,line,,lm-1,lm
    cm,lcut,line
    !
    init_entity_num
    !
    lsel,s,line,,lm-(2*n_thrd+1),lm
    !
    ln_part_ln,lcut
    !
    init_entity_num
    !
    ldele,lcut,,,1
    !
    init_entity_num
    !
    *do,iij,1,4*n_thrd-2,2
        *if,iij,eq,1,then
            counter=0
            incrm=3
        *endif
        !
        counter=counter+1
        !
        lstr,km-(4*n_thrd-1)+iij,km-(4*n_thrd-1)+(iij+1)
        !
        *get,lm,line,,num,max
        !
        *if,mod(counter,2),eq,0,then
            lfillt,lm,lstart+(2*n_thrd+5)+incrm,.0050
            lfillt,lm,lstart+(2*n_thrd+5)+incrm+3,.0050
            incrm=incrm+6
        *endif
    *enddo
    !
    init_entity_num
    !
    lsel,selv,loc,y,0,0.985*(0.250*p_thrd)/(2*tan((thrd_ang/2)*rconv))
    !
    *if,d_mjr,eq,0,then
        lsel,a,loc,y,1.015*(h_thrd-(0.041667*p_thrd)/(2*tan((thrd_ang/2)*rconv))),h_thrd
    *else
        lsel,selv,loc,y,1.015*(h_thrd2),h_thrd
    *endif
    !
    lsel,r,loc,x,0,n_thrd*p_thrd
    lsel,r,loc,z,0
    !
    ldele,all,,,1
    !
    init_entity_num
    !
    ksel,selv,loc,x,(h_thrd/4)/tan(thrd_ang*rconv)
    ksel,r,loc,y,h_thrd/4
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k1_thrd=km
    !
    init_entity_num
    !
    ksel,selv,loc,x,(n_thrd*p_thrd)-((h_thrd/4)/tan(thrd_ang*rconv))
    ksel,r,loc,y,h_thrd/4
    ksel,r,loc,z,0
    !
    get_max_entity
    !
    k2_thrd=km
    !
    init_entity_num
*endif
!
k,,kx(k1_thrd),-1.3*p_thrd,0
k,,kx(k2_thrd),-1.3*p_thrd,0
!
create_lines
!
lstr,km-1,k1_thrd
lstr,km,k2_thrd
!
init_entity_num
!
isolate_lines
!
al,all
!
init_entity_num
!
lplot
!
!
*endif      ! END external thread profile
!
!
!
!
*get,thrd_cs,active,,csys
!
csn=csn+1
!
*if,prof,eq,'x',then
    !
    min_d_cs=csn
    clocal,min_d_cs,0,-kx(k1_thrd),-ky(k1_thrd),0,0,0,0
    csys,thrd_cs
    atran,min_d_cs,am,,,,0,1
    !
    nscp=n_thrd     ! # of surface contact points
    y_nd_d=h_thrd       ! nodal distances in y direction
    !
*elseif,prof,eq,'i',then
    !
    mjr_d_cs=csn
    clocal,mjr_d_cs,0,-kx(k1_thrd),-ky(k1_thrd),0,0,0,0
    csys,thrd_cs
    atran,mjr_d_cs,am,,,,0,1
*endif
!
!
init_entity_num
!
lplot
!
*if,thrd_ass,eq,0,then
    *set,iii,
    *set,iij,
    *set,ijk,
    *set,k_tip,
    *set,n_thrd,
    *set,n_inch,
    *set,r_thrd,
    *set,p_thrd,
    *set,h_thrd,
    *set,h_thrd2,
    *set,k1_thrd,
    *set,k2_thrd,
    *set,thrd_ang,
    *set,root_h,
    *set,r_width,
    *set,r2edge,
    *set,prof,
    *set,stndrd,
    *set,d_mjr,
    *set,d_min,
    *set,zzzz,
    *set,counter,
    *set,incrm,
    *set,lstart,
    *set,thrd_ln,
    *set,y_nd_d,
*endif


*if,prkey_,eq,1,then
    /go
*endif

:skip