!   TITLE:  ara_fillet.mac (SM_TOOL)
!
!
!   OBJECTIVE:
!
!   This sub-macro creates an area fillet surfacebetween to planar area
!   surfaces.
!
!
!   COMMAND SYNTAX:
!
!    OPTION 1:
!
!                   (1)   (2)     (3)     (4)   (5)     (6)     (7)
!       ARA_FILLET, opt, area1, area1cs, dir1, area2, area2cs, dir2,
!
!                   offsetd
!                     (8)
!
!    OPTION 2:
!
!                   (1)   (2)    (3)    (4)    (5)    (6)    (7)    (8)
!       ARA_FILLET, opt, area1, ckpa1, xkpa1, ykpa1, area2, ckpa2, xkpa2,
!
!                    ykpa2, offsetd
!                     (9)    (10)
!
!    OPTION 3:
!
!                   (1)   (2)     (3)   (4)  (5)  (6)   (7)     (8)     (9)
!       ARA_FILLET, opt, actvcs, area1, xc1, yc1, zc1, rotxy1, rotyz1, rotzx1,
!
!                   area2, xc2,  yc2,  zc2, rotxy2, rotyz2, rotzx2, offsetd
!                   (10)   (11)  (12)  (13)  (14)    (15)    (16)     (17)
!
!
!   ARGUMENTS:
!
!       The first parameter argument is OPT, option. Their are 3 different
!       input argument structures for this command:
!
!           Option 1 (opt = 1):
!
!               This 1st option allows the user to create areas from pre-
!               existing coordinate systems.  These coordinate systems are
!               assumed to have their centers coplanar, and with their z
!               axes oriented normal, to the plane of their respective areas.
!               If they are not, then options 2 or 3 must be used.
!
!               In the case that these pre-existing coordinate systems are not
!               consistently oriented, this option allows the user to choose
!               the side of the coordinate system (+z or -z) to create the areas,
!               by specifying the parameters 'dir1' and 'dir2'.
!
!           Option 2 (opt = 2):
!
!               This option allows the user to create coordinate systems,
!               with z axes normal to their respective areas, by using the
!               keypoints of their corresponding areas.  This employs the
!               use of the CSKP command.  The proper choice of the center,
!               x axis, and y axis keypoints must be made to insure that
!               the z axis is oriented normal to and outward from the areas.
!               (The direction of the +z axis is outward from the areas,
!               where outward is defined as the side of the areas away from
!               the fillet.)
!
!           Option 3 (opt = 3):
!
!               This option allows the user to create coordinate systems,
!               with z axes normal to their respective areas by using a
!               coordinate position center and orientation.  This employs
!               the use of the CLOCAL command.  The proper choice of the
!               center x, y and z coordinates and rotation angles must be
!               made orient the to insure that the z axis is oriented normal
!               to and outward from the areas.
!
!       The following input needed for each option is as follows:
!
!       - Option 1:
!
!               area1 = number of the 1st area to be filleted.
!
!               area1cs = coordinate system # associated with area 1.
!
!               dir1 = z axis direction, positive or negative, that is
!                      outward from area 1. Options for this argument are:
!
!                       0 (or blank) = +z direction
!                       1 = -z direction
!
!               area2 = number of the 2nd area to be filleted.
!
!               area2cs = coordinate system # associated with area 2.
!
!               dir2 = z axis direction, positive or negative, that is
!                      outward from area 2.
!
!                       0 (or blank) = +z direction
!                       1 = -z direction
!
!               offsetd = offset distance at which the new areas will be
!                         placed, which is the fillet radius between areas
!                         1 and 2.
!
!       - Option 2:
!
!               area1 = number of the 1st area to be filleted.
!
!               ckpa1 = center keypoint of area 1 c.s.
!
!               xkpa1 = keypoint used to define +x axis of area 1 c.s.
!
!               ykpa1 = keypoint used to define +y axis of area 1 c.s.
!
!               area2 = number of the 2nd area to be filleted.
!
!               ckpa2 = center keypoint of area 2 c.s.
!
!               xkpa2 = keypoint used to define +x axis of area 2 c.s.
!
!               ykpa2 = keypoint used to define +y axis of area 2 c.s.
!
!               offsetd = offset distance at which the new areas will be
!                         placed, which is the fillet radius between areas
!                         1 and 2.
!
!       - Option 3:
!
!               area1 = number of the 1st area to be filleted.
!
!               xc1 = x coordinate of area 1 c.s.
!
!               yc1 = y coordinate of area 1 c.s.
!
!               zc1 = z coordinate of area 1 c.s.
!
!               rotxy1 = 1st rotation angle (+x to +y) of area 1 c.s.
!
!               rotyz1 = 2nd rotation angle (+y to +z) of area 1 c.s.
!
!               rotzx1 = 3rd rotation angle (+z to +x) of area 1 c.s.
!
!               area2 = number of the 2nd area to be filleted.
!
!               xc2 = x coordinate of area 2 c.s.
!
!               yc2 = y coordinate of area 2 c.s.
!
!               zc2 = z coordinate of area 2 c.s.
!
!               rotxy2 = 1st rotation angle (+x to +y) of area 2 c.s.
!
!               rotyz2 = 2nd rotation angle (+y to +z) of area 2 c.s.
!
!               rotzx2 = 3rd rotation angle (+z to +x) of area 2 c.s.
!
!               offsetd = offset distance at which the new areas will be
!                         placed, which is the fillet radius between areas
!                         1 and 2.
!
!
!   DESCRIPTION:
!
!   This sub-macro calculates the position of the axis of a filleted area
!   pair.  This is achieved by finding the intersection of two planes con-
!   structed parrallel to the planes to be filleted, each at a specified
!   distance which is equal to the fillet radius of the areas. This axis
!   will then be used to create a cylindrical coordinate system whose z axis
!   is aligned along the fillet axis.  This may then be used to create arc
!   lines and areas of the fillet.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
opt=arg1
!
!
*if,opt,eq,1,then
    area1=arg2
    area1cs=arg3
    dir1=arg4
    area2=arg5
    area2cs=arg6
    dir2=arg7
    offsetd=arg8
*endif
!
!
*if,opt,eq,2,then
    area1=arg2
    ckpa1=arg3
    xkpa1=arg4
    ykpa1=arg5
    area2=arg6
    ckpa2=arg7
    xkpa2=arg8
    ykpa2=arg9
    offsetd=ar10
*endif
!
!
*if,opt,eq,3,then
    actvcs=arg2
    area1=arg3
    xc1=arg4
    yc1=arg5
    zc1=arg6
    rotxy1=arg7
    rotyz1=arg8
    rotzx1=arg9
    area2=ar10
    xc2=ar11
    yc2=ar12
    zc2=ar13
    rotxy2=ar14
    rotyz2=ar15
    rotzx2=ar16
    offsetd=ar17
*endif
!
!
*if,opt,eq,1,then
    !
    csys,area1cs                ! create 1st area
    !
    *if,dir1,eq,0,then
        rfil=-offsetd
    *else
        rfil=offsetd
    *endif
    !
    create_cs,0,'offarea1',0,0,0,rfil,0,0,0
    !
    csys,area1cs
    !
    atran,offarea1,area1,,,,0,0
    !
    init_entity_num
    !
    lplot
    !
    !
    !
    csys,area2cs                ! create 2nd area
    !
    *if,dir2,eq,0,then
        rfil=-offsetd
    *else
        rfil=offsetd
    *endif
    !
    create_cs,0,'offarea2',0,0,0,rfil,0,0,0
    !
    csys,area2cs
    !
    atran,offarea2,area2,,,,0,0
    !
    init_entity_num
    !
    lplot
*endif
!
!
*if,opt,eq,2,then
    !
    create_cs,1,'area1cs',0,ckpa1,xkpa1,ykpa1       ! create area 1 c.s.
    !
    create_cs,1,'area2cs',0,ckpa2,xkpa2,ykpa2       ! create area 2 c.s.
    !
    !
    csys,area1cs                ! create 1st area
    !
    create_cs,0,'offarea1',0,0,0,-offsetd,0,0,0
    !
    csys,area1cs
    !
    atran,offarea1,area1,,,,0,0
    !
    init_entity_num
    !
    lplot
    !
    !
    !
    csys,area2cs                ! create 2nd area
    !
    create_cs,0,'offarea2',0,0,0,-offsetd,0,0,0
    !
    csys,area2cs
    !
    atran,offarea2,area2,,,,0,0
    !
    init_entity_num
    !
    lplot
*endif
!
!
*if,opt,eq,3,then
    !
    csys,actvcs                 ! create area 1 c.s.
    !
    create_cs,0,'area1cs',0,xc1,yc1,zc1,rotxy1,rotyz1,rotzx1
    !
    csys,actvcs                 ! create area 2 c.s.
    !
    create_cs,0,'area2cs',0,xc2,yc2,zc2,rotxy2,rotyz2,rotzx2
    !
    csys,area1cs                ! create 1st area
    !
    create_cs,0,'offarea1',0,0,0,-offsetd,0,0,0
    !
    csys,area1cs
    !
    atran,offarea1,area1,,,,0,0
    !
    init_entity_num
    !
    lplot
    !
    !
    !
    csys,area2cs                ! create 2nd area
    !
    create_cs,0,'offarea2',0,0,0,-offsetd,0,0,0
    !
    csys,area2cs
    !
    atran,offarea2,area2,,,,0,0
    !
    init_entity_num
    !
    lplot
*endif
!
!
aina,am-1,am                    ! perform boolean, area intersects area
!
init_entity_num
!
*get,kp1,line,lm,kp,1           ! retrieve KP's of intersection line
*get,kp2,line,lm,kp,2
!
asel,s,area,,area1              ! retrieve KP's and count of area 1
lsla,s
ksll,s
entity_array,'k','kpa1','array'
kpcnta1=ecount
!
!
asel,s,area,,area2              ! retrieve KP's and count of area 2
lsla,s
ksll,s
entity_array,'k','kpa2','array'
kpcnta2=ecount
!
init_entity_num
!
*set,exchk,
!
*do,iii,1,kpcnta1,1             ! retrieve a KP shared by areas 1 & 2
    *do,jjj,1,kpcnta2,1
        *if,kpa1(iii,1),eq,kpa2(jjj,1),then
            kp3=kpa1(iii,1)
            exchk=1
            *exit
        *endif
    *enddo
    !
    *if,exchk,eq,1,then
        *set,exchk,
        *exit
    *endif
*enddo
!
create_cs,1,,0,kp1,kp2,kp3
!
create_cs,0,'filltcs1',1,0,0,0,0,0,-90
!                           create 1st fillet c.s. at kp1
!
create_cs,0,'filltcs2',1,kx(kp2),ky(kp2),kz(kp2),0,0,0
!                           create 2nd fillet c.s. at kp2
!
ldele,lm,,,1
!
init_entity_num
!
lplot
!
!
*set,area1,
*set,area1cs,
*set,area2,
*set,area2cs,
*set,offsetd,
*set,kp1,
*set,kp2,
*set,kp3,
*set,offarea1,
*set,offarea2,
*set,iii,
*set,jjj,
*set,kpcnta1,
*set,kpcnta2,
*set,kpa1(1),
*set,kpa2(1),
*set,ecount,
!
*if,opt,eq,1,then
    *set,dir1,
    *set,dir2,
    *set,rfil,
*endif
!
*if,opt,eq,2,then
    *set,ckpa1,
    *set,xkpa1,
    *set,ykpa1,
    *set,ckpa2,
    *set,xkpa2,
    *set,ykpa2,
*endif
!
*if,opt,eq,3,then
    *set,actvcs,
    *set,xc1,
    *set,yc1,
    *set,zc1,
    *set,rotxy1,
    *set,rotyz1,
    *set,rotzx1,
    *set,xc2,
    *set,yc2,
    *set,zc2,
    *set,rotxy2,
    *set,rotyz2,
    *set,rotzx2,
*endif
!
*set,opt,

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