!   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,1
!
lugcs__=arg1
rout__=arg2
thk__=arg3
dhole__=arg4
leng__=arg5
lugang__=arg6
dcyl__=arg7
lshld__=arg8
radfil__=arg9
zoffh__=ar10
yoffh__=ar11
!
csys,lugcs__
!
clocal,csn+20,0,0,0,0,0,90,0
!
!
!                    (1)    (2)    (3)     (4)     (5)      (6)     (7)
!        LUG_FLAT, lug_cs, r_out, thick, d_hole, lug_leng, angle, end_leng
!
!
lug_flat,csn+20,rout__,0,0,leng__,lugang__,dcyl__
!
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
!
isolate_areas
configuration,-1,1
revolve_aset,lugcs__,360,2
!
vadd,vm-1,vm
init_entity_num
!
csys,lugcs__
create_cs,0,,0,0,0,0,0,0,0
!
lugang__=lugang__/2
rcyl__=(rout__/(sin((90-lugang__)*d2r_conv)))+(leng__/(tan((90-lugang__)*d2r_conv)))
!
*if,thk__,gt,0,then
    *if,lshld__,gt,0,then
        c___=2
        d___=2
        !
        *if,radfil__,gt,0,then
            *if,(c___*radfil__),lt,rcyl__,then
                c___=2*(nint(rcyl__/radfil__))
            *endif
            !
            *if,(lshld__+(d___*rout__)),lt,(d___*radfil__),then
                d___=2*(nint((lshld__+(d___*rout__))/radfil__))
            *endif
            !
            k,,(c___*radfil__)+(thk__/2),-2*rcyl__,-d___*lshld__
            k,,thk__/2,-2*rcyl__,-d___*lshld__
            k,,thk__/2,-2*rcyl__,lshld__
            k,,(c___*radfil__)+(thk__/2),-2*rcyl__,lshld__
        *else
            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__
        *endif
        !
        create_lines
        !
        *if,radfil__,gt,0,then
            ln_fillet,km-1,radfil__
        *endif
        !
        isolate_lines
        configuration,-1,1
        !
        al,all
        init_entity_num
        !
        *if,radfil__,gt,0,then
            k,,-((c___*radfil__)+(thk__/2)),-2*rcyl__,lshld__
            k,,-thk__/2,-2*rcyl__,lshld__
            k,,-thk__/2,-2*rcyl__,-d___*lshld__
            k,,-((c___*radfil__)+(thk__/2)),-2*rcyl__,-d___*lshld__
        *else
            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__
        *endif
        !
        create_lines
        !
        *if,radfil__,gt,0,then
            ln_fillet,km-2,radfil__
        *endif
        !
        isolate_lines
        configuration,-1,1
        !
        al,all
        init_entity_num
        !
        isolate_areas
        configuration,-1,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,1
    !
    extrude_aset,4*rcyl__
    vsbv,vm-1,vm,,delete,delete
    init_entity_num
*endif
!
csdele,csn+20
!
configuration,1,1
init_entity_num
configuration,-2,1
!
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__,
*set,c___,
*set,d___,
!
*if,prkey_,eq,1,then
    /go
*endif