!   TITLE:  tangent_bend.mac (SM_TOOL)
!
!
!   OBJECTIVE:
!
!   This macro calculates the parameters of a bend path tangent to a bend
!   radius.
!
!
!   COMMAND SYNTAX:
!
!                     (1)  (2) (3) (4) (5)   (6)    (7)   (8)
!       TANGENT_BEND, bcs, x1, y1, x2, y2, r_bend, zpos, path_?
!
!
!   ARGUMENTS:
!
!       (1)  bcs = The bend center coordinate system (CS).
!
!       (2)  x1 = X-coordinate (cartesian) of the 1st tangent path end point.
!
!       (3)  y1 = Y-coordinate (cartesian) of the 1st tangent path end point.
!
!       (4)  x2 = X-coordinate (cartesian) of the 2nd tangent path end point.
!
!       (5)  y2 = Y-coordinate (cartesian) of the 2nd tangent path end point.
!
!       (6)  r_bend = bend radius.
!
!       (7)  zpos = Z coordinate of the tangent path end points.
!
!       (8)  path_? = boolean to decide to create path lines:
!
!               0 = Do NOT create tangent bend path.
!
!               1 = Create tangent bend path.
!
!
!   DESCRIPTION:
!
!   The bend CS may be any type of CS (i.e., cartesian, cylindrical, etc.).
!   The origin of the bend CS is the center of the bend radius. The
!   coordinates of the tangent path points are relative to the bend CS.
!
!   The bend path may be created along 2 different paths. Therefore, the
!   convwntionfor creating the path is from tangent point 1 to tangent point
!   2, in a CCW direction about the bend CS ( the positive right-hand rule).
!   If the alternate path is desired (from tangent point 2 to tangent point
!   1, CCW), then simply reversing the input points will create the alternate
!   path.
!
!   If it is desired to create the line path, then specifying argument 8 as
!   1 will create the line path. Otherwise, only the pertinent data is output
!   only.
!
!   EXAMPLES: (execute 'mod_assemb_prep' prior to running each):
!
!           TANGENT_BEND,2,-1,1.5,1.75,-1.25,1.5,1,1
!
!           TANGENT_BEND,1,1.75,-1.25,-1,1.5,1.5,,1
!
!           TANGENT_BEND,0,1,1.5,-1.75,-1.25,1.5,1,1
!
!           TANGENT_BEND,0,-1.75,-1.25,1,1.5,1.5,1,1
!
!       (execute 'create_circle,1.5' after each to view relationship)
!
!
!       (1) l_tl1 = the length of the straight portion of tangent line 1.
!
!       (2) l_tl2 = the length of the straight portion of tangent line 2.
!
!       (3) ang_tl1 = the angle of tangent line 1 (relative to the bend
!                     center CS X-axis).
!
!       (4) ang_tl2 = the angle of tangent line 2 (relative to the bend
!                     center CS X-axis).
!
!       (5) ang_tp1 = the angle of the tangent KP for tangent line 1
!                     (relative to the bend center CS X-axis).
!
!       (6) ang_tp2 = the angle of the tangent KP for tangent line 2
!                     (relative to the bend center CS X-axis).
!
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
configuration,0
!
bcs__=arg1
x1__=arg2
y1__=arg3
x2__=arg4
y2__=arg5
brad__=arg6
zpos__=arg7
path__=arg8
!
csys,bcs__
clocal,csn+150,0,0,0,0
bcs__=csn+150
!
k,,x1__,y1__,zpos__
k,,x2__,y2__,zpos__
!
init_entity_num
kp1_=km-1
kp2_=km
!
clocal,csn+151,1,0,0,0
cbcs__=csn+151
!
ang1_=ky(kp1_)
ang2_=ky(kp2_)
!
l1_=sqrt((x1__**2)+(y1__**2))
l2_=sqrt((x2__**2)+(y2__**2))
!
tl1_=sqrt((l1_**2)-(brad__**2))
tl2_=sqrt((l2_**2)-(brad__**2))
!
theta1_=atan(tl1_/brad__)*r2d_conv
theta2_=atan(tl2_/brad__)*r2d_conv
!
csys,bcs__
clocal,csn+200,0,0,0,0,(ang1_+theta1_),0,0
r1cs_=csn+200
k,,brad__,0,zpos__
init_entity_num
tkp1_=km
!
csys,bcs__
clocal,csn+201,0,0,0,0,(ang2_-theta2_),0,0
r2cs_=csn+201
k,,brad__,0,zpos__
init_entity_num
tkp2_=km
!
l_tl1=distkp(kp1_,tkp1_)
l_tl2=distkp(kp2_,tkp2_)
!
csys,bcs__
create_cs,0,,1,kx(kp1_),ky(kp1_),kz(kp1_)
ang_convert,ky(tkp1_),0,'ang_tl1'
!
csys,bcs__
create_cs,0,,1,kx(kp2_),ky(kp2_),kz(kp2_)
ang_convert,ky(tkp2_),0,'ang_tl2'
!
csys,cbcs__
ang_convert,ky(tkp1_),0,'ang_tp1'
ang_convert,ky(tkp2_),0,'ang_tp2'
!
*if,path__,eq,1,then
    lstr,kp1_,tkp1_
    !
    csys,cbcs__
    l,tkp1_,tkp2_
    !
    lstr,tkp2_,kp2_
*elseif,path__,eq,0,then
    kdele,km-3,km,1
*endif
!
configuration,1
init_entity_num
lplot
!
csdele,r1cs_
csdele,r2cs_
csdele,bcs__
csdele,cbcs__
!
*set,bcs__,
*set,x1__,
*set,y1__,
*set,x2__,
*set,y2__,
*set,brad__,
*set,zpos__,
*set,tkp1_,
*set,tkp2_,
*set,kp1_,
*set,kp2_,
*set,tl1_,
*set,tl2_,
*set,l1_,
*set,l2_,
*set,theta1_,
*set,theta2_,
*set,cbcs__,
*set,r1cs_,
*set,r2cs_,
!
*if,prkey_,eq,1,then
    /go
*endif