!   TITLE:  ln_fillet_ln.mac (MANAGEMENT)
!
!
!   OBJECTIVE:
!
!   This macro creates a fillet between two nonintersecting lines.
!
!
!   COMMAND SYNTAX:
!
!                       (1)     (2)     (3)
!        LN_FILLET_LN, line#1, line#2, radius
!
!
!   ARGUMENTS:
!
!       (1) line#1 =  Line number of 1st line

!       (2) line#2 =  Line number of 2nd line
!
!       (3) radius =  The radius of the fillet.
!
!
!   DESCRIPTION:
!
!   Construct a tangent fillet between two lines. The input lines 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.
!
!   When two lines cross, four possible fillets can be constructed. The
!   correct fillet placement is determined by the location of the input
!   lines. The fillet is constructed in between the lines where the angle
!   between them is less than 180 degrees.
!
!   The lines are extended/trimmed as needed to share common keypoints
!   at the tangency points of the arc.
!
!   TO DO: If the input lines are parallel, the end points are directly
!   across from each other and the fillet radius is equal to 1/2 the
!   distance between lines, then a full radius is constructed.
!   The full radius is constructed using two 90 degree arc segments.
!
!
*get,prkey_,active,0,prkey
/nopr

line1_=arg1
line2_=arg2
rfillet_=arg3

! Return err_=1 if lines intersect (KP at intersection = kpi_)
ln_dist_ln,line1_,line2_

*if,err_,eq,1,then

    ! kplinei = KP# @ intersect & attached to line
    kplinei_=kpi_

    ! The lines intersect.  Assume they do not cross
    ! Extend the correct end of line #1
    *get,kp1_,line,line1_,kp,1
    *get,kp2_,line,line1_,kp,2

    d1_=distkp(kp1_,kpi_)
    d2_=distkp(kp2_,kpi_)

    *if,d1_,lt,d2_,then
        *if,d1_,eq,0.0,then
            kplinei_=kp1_
        *else
            extend,line1_,kp1_,d1_
        *endif
    *else
        *if,d2_,eq,0.0,then
            kplinei_=kp2_
        *else
            extend,line1_,kp2_,d2_
        *endif
    *endif

    ! Extend the correct end of line #2
    *get,kp1_,line,line2_,kp,1
    *get,kp2_,line,line2_,kp,2

    d1_=distkp(kp1_,kpi_)
    d2_=distkp(kp2_,kpi_)

    *if,d1_,lt,d2_,then
        *if,d1_,eq,0.0,then
            kplinei_=kp1_
        *else
            extend,line2_,kp1_,d1_
        *endif
    *else
        *if,d2_,eq,0.0,then
            kplinei_=kp2_
        *else
            extend,line2_,kp2_,d2_
        *endif
    *endif

    nummrg,kp                       ! Extend adds a KP in addition to kpi_
    ln_fillet,kplinei_,rfillet_

*elseif,err_,eq,2,then

*msg,warn,dist_
The input lines are skew with a distance of %.4g between them

*else

    ! ADD CODE HERE FOR CONTRUCTING A FULL RADIUS BETWEEN PARALLEL LINES

    !*get,kp1_,line,line1_,kp,1
    !*get,kp2_,line,line1_,kp,2

*msg,warn,dist_
The lines are parallel at distance D = %.4g

*endif

lplot

!
!
*set,line1_
*set,line2_
*set,rfillet_
*set,kplinei_
*set,d1_
*set,d2_
*set,kp1_
*set,kp2_

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