!   TITLE:  lug_spherical.mac (MISC_PART)
!
!
!   OBJECTIVE:
!
!   This sub-macro creates a spherical mounting lug as a miscellaneous part.
!
!
!   COMMAND SYNTAX:
!
!                         (1)    (2)    (3)    (4)      (5)      (6)
!        LUG_SPHERICAL, lug_cs, r_out, thick, d_hole, lug_leng, angle,
!
!                       d_cyl, shld_leng, rad_fil, Z_offst_H, Y_offst_H
!                        (7)      (8)       (9)       (10)      (11)
!
!
!   ARGUMENTS:
!
!       (1) lug_cs = the coordinate system to create lug relative to.
!
!       (2) r_out = outer radius of the lug spherical surface.
!
!       (3) thick = thickness (length) of the lug.
!
!       (4) d_hole = center hole (shaft) diameter. If left blank (0),
!                    the lug is created with no center hole.
!
!       (5) lug_leng = length of the lug, as measured from the hole/radius
!                      center to the straight/flat lug end (along the
!                      Z-axis).
!
!       (6) angle = angle between the opposite straight surfaces of the
!                   cylinder. Default is 0 (parallel surfaces).
!                       [This argument will be ignored if D_CYL
!                           (argument 7) is specified.]
!
!       (7) d_cyl = diameter of the circular flat end of the lug, located
!                   a distance specified in LUG_LENG (argument 5).
!                       [If this argument is specified, then the angle
!                           specified in argument 6 is ignored.]
!
!       (8) shld_leng = length of the lug, as measured from the hole/radius
!                       center to the filleted shoulder surface. (along the
!                       Z-axis).
!
!       (9) rad_fil = radius of the fillet adjoining the flat lug with the
!                     cylindrical end shoulder.
!
!       (10) Z_offst_H = Z offset distance of the hole center from the lug CS.
!
!       (11) Y_offst_H = Y offset distance of the hole center from the lug CS.
!
!
!   DESCRIPTION:
!
!   The lug is a flat plate with thickness formed from the hemispherical end of
!   a cylinder. The cylinder may be a straight cylinder of constant diameter,
!   or a conic section of linearly varying diameter.
!
!   The lug coordinate system (CS) is a CS specified in argument 1 where the
!   spherical surface at the end of the lug is centered.
!
!   The lug profile is created with the cylinder axis along the Z-axis, and the
!   spherical profile radius is measured from the X-axis of the lug CS specified
!   in argument 1. The length of the lug is measured from the lug CS XY plane to
!   the flat end face. The shoulder length of the lug is measured from the lug
!   CS XY plane to the filleted surface of the lug shoulder. The thickness of
!   the lug is the thickness of the 2 parallel faces of the luf in which the
!   hole is created.
!
!   If the hole diameter (argument 4) is not specified, the lug is created
!   with no center hole.  If the thickness (argument 3) is not specified, the
!   lug is created with no shoulder (cylindrical or conic section profile).
!
!   The hole may be created at the lug CS center, or at any offset distance as
!   specified in arguments 10 and 11 (Z_offst_H and Y_offst_H respectively.
!   This allows the lug hole to be created at any position on the surface of
!   the lug.
!
!   To view a lug created from this macro, run the following example line of
!   code (must execute 'mod_assemb_prep' prior to running):
!
!                       LUG_SPHERICAL,0,1,1.00,1.00,4,30,,3,1,1
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
configuration,0
!
lugcs_=arg1
rout_=arg2
thk_=arg3
dhole_=arg4
leng_=arg5
lugang_=arg6
dcyl__=arg7
lshld__=arg8
radfil__=arg9
zoffh__=ar10
yoffh__=ar11
!
csys,lugcs_
!
create_cs,0,,0,0,0,0,0,0,0
create_cs,0,,0,0,0,0,0,90,0
!
*if,dcyl__,gt,0,then
    *if,(dcyl__/2),gt,rout_,then
        h__=sqrt((dcyl__/2)**2+leng_**2)
        beta_=atan((dcyl__/2)/leng_)*dconv
        gamma_=acos(rout_/h__)*dconv
        lugang_=2*(beta_+gamma_-90)
    *else
        lugang_=0
    *endif
    !
    *set,h__,
    *set,beta_,
    *set,gamma_,
*endif
!
*if,lugang_,eq,0,then
    k,,rout_,0,0
    k,,rout_,leng_,0
    k,,-rout_,leng_,0
    k,,-rout_,0,0
*elseif,lugang_,gt,0,then
    lugang_=lugang_/2
    !
    a___=leng_/(tan((90-lugang_)*rconv))
    b___=rout_*cos(lugang_*rconv)
    c___=sqrt((rout_**2)-(b___**2))
    m___=rout_/(sin((90-lugang_)*rconv))
    !
    rcyl__=m___+a___
    !
    k,,b___,-c___,0
    k,,rcyl__,leng_,0
    k,,-rcyl__,leng_,0
    k,,-b___,-c___,0
    !
    *set,a___,
    *set,b___,
    *set,c___,
    *set,m___,
*endif
!
create_area
!
k,,0,-rout_,0
k,,0,leng_,0
!
create_lines,,1
!
asbl,am,lm,,delete,delete
init_entity_num
!
adele,am,,,1
init_entity_num
!
asel,s,area,,am
revolve_aset,lugcs_,360,2
!
vadd,vm-1,vm
init_entity_num
!
csys,lugcs_
create_cs,0,,0,0,0,0,-90,0,0
!
create_sphere,rout_,,2,90,180,2
!
vsel,s,volu,,vm-1,vm
vadd,all
!
init_entity_num
!
*if,lugang_,gt,0,then
    vsbv,vm,vm-3,,delete,keep
    init_entity_num
    !
    vadd,vm-4,vm
    init_entity_num
*elseif,lugang_,eq,0,then
    vadd,vm-3,vm
    init_entity_num
    !
    rcyl__=2.2*rout_
*endif
!
csys,lugcs_
create_cs,0,,0,0,0,0,0,0,0
!
*if,thk_,gt,0,then
    *if,lshld__,gt,0,then
        k,,2*rcyl__,-2*rcyl__,-2*rout_
        k,,thk_/2,-2*rcyl__,-2*rout_
        k,,thk_/2,-2*rcyl__,lshld__
        k,,2*rcyl__,-2*rcyl__,lshld__
        !
        create_lines
        !
        *if,radfil__,gt,0,then
            ln_fillet,km-1,radfil__
        *endif
        !
        isolate_lines
        configuration,-1
        !
        al,all
        init_entity_num
        !
        !
        k,,-2*rcyl__,-2*rcyl__,lshld__
        k,,-thk_/2,-2*rcyl__,lshld__
        k,,-thk_/2,-2*rcyl__,-2*rout_
        k,,-2*rcyl__,-2*rcyl__,-2*rout_
        !
        create_lines
        !
        *if,radfil__,gt,0,then
            ln_fillet,km-2,radfil__
        *endif
        !
        isolate_lines
        configuration,-1
        !
        al,all
        init_entity_num
        !
        isolate_areas
        configuration,-1
        extrude_aset,4*rcyl__
        !
        vsel,s,volu,,vm-1,vm
        cm,vset1_,volu
        !
        init_entity_num
        !
        vsbv,vm-2,vset1_,delete,delete
        init_entity_num
        !
        cmdele,vset1_
    *endif
*endif
!
*if,dhole_,gt,0,then
    csys,lugcs_
    create_cs,0,,0,2*rcyl__,yoffh__,zoffh__,0,0,-90
    !
    create_circ_area,dhole_/2,0
    !
    isolate_areas
    configuration,-1
    !
    extrude_aset,4*rcyl__
    vsbv,vm-1,vm,,delete,delete
    init_entity_num
*endif
!
configuration,1
init_entity_num
!
aplot
!
*set,lugcs_,
*set,rout_,
*set,thk_,
*set,dhole_,
*set,leng_,
*set,lugang_,
*set,dcyl__,
*set,lshld__,
*set,radfil__,
*set,zoffh__,
*set,yoffh__,
*set,rcyl__,
!
*if,prkey_,eq,1,then
    /go
*endif