!   TITLE:  ln_fillet_arc.mac (SM_TOOL)
!
!   OBJECTIVE:
!
!   This macro creates a fillet between a line and an arc.
!
!
!   COMMAND SYNTAX:
!
!                       (1)     (2)     (3)     (4)     (5)
!        ln_fillet_arc, line#, arc#, radius, kpline#, kparc#
!
!   ARGUMENTS:
!
!       (1) line#   =  Number of line
!
!       (2) arc#    =  Number of arc
!
!       (3) rfillet  =  The radius of the fillet.
!
!       (4) kpline# =  (Optional) KP # at line end to be trimmed
!                      If -1 is given, use line end #1
!                      If -2 is given, use line end #2
!
!       (5) kparc#  =  (Optional) KP # at arc end to be trimmed
!                      If -1 is given, use arc end #1
!                      If -2 is given, use arc end #2
!
!
!   DESCRIPTION:
!
!   Construct a tangent fillet between a line and an arc. The input     
!   line and arc must not cross. If they do, "profile_lines" can be     
!   called first. The lines may intersect (sharing a common keypoint or 
!   two seperate keypoints) or be non-intersecting. This restriction is 
!   needed for determining where the fillet should be placed.           
!
!   If arguments (4) and (5) are unspecified, the two end points which are
!   closest together will be used to determine fillet placement and which
!   ends to trim to the tangency points of the fillet.
!
!   If one of argument (4) and argument (5) is specified and the other left
!   blank, the nearest KP of the other entity will be used.
!
!   If both arguments (4) and (5) are specified, they will be used to
!   determine fillet placement and will be the ends that are trimmed.
!
!---------------------------------------------------------------------------
!
!*get,prkey_,active,0,prkey
!/nopr



line_=arg1
arc_=arg2
rfillet_=arg3
kpline_=arg4
kparc_=arg5

cm,kset_,kp
!cm,lset_,line

!lsel,s,line,,line_
!lsel,a,line,,arc_
!ksll

!
! Get info from line and arc
!
arc_info,'info_',1,line_,arc_

dmin=.99e30

*if,kpline_,eq,0,and,kparc_,eq,0,then

    ! Determine which two KP's are nearest to each other

    *do,i_,2,3
        *do,j_,2,3

            d_=distkp(info_(1,i_),info_(2,j_))

            *if,d_,lt,dmin,then
                dmin=d_
                kpl_=info_(1,i_)
                kpa_=info_(2,j_)
            *endif
        *enddo
    *enddo

*elseif,kpline_,ne,0,and,kparc_,eq,0,then

    ! Find arc KP closest to specified line KP
    *do,i_,2,3

        d_=distkp(kpline_,info_(2,i_))

        *if,d_,lt,dmin_,then
            dmin_=d_
            kpa_=info_(2,i_)
        *endif
    *enddo

*elseif,kparc_,ne,0,and,kpline_,eq,0,then

    *do,i_,2,3

        d_=distkp(info_(1,i_),kparc_)

        *if,d_,lt,dmin_,then
            dmin_=d_
            kpl_=info_(1,i_)
        *endif
    *enddo
*endif

*if,kpl_,ne,0,then
    kpline_=kpl_
*endif

*if,kpa_,ne,0,then
    kparc_=kpa_
*endif

!
! Make sure line and arc are coplanar
!
create_cs,1,'cs_1_',1,info_(2,4),info_(2,2),info_(2,3)

*if,kz(info_(1,2)),ne,0,or,kz(info_(1,3)),ne,0,then

*msg,error,line_,arc_
Line %i and arc %i must be coplanar.
return,,1

*endif

!
! Check that line and arc are close enough together for a fillet to be fit
!
ln_dist_kp,'dcl_',line_,info_(2,4)

*if,dcl_-info_(2,5),gt,2*rfillet_,then

*msg,error,line_,arc_
Fillet is too small to fit between line %i and arc %i
return,,1

*endif







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