!*********************************************************************************************************************************************************!
!* Copyright (c) <2015> <Purdue University> All rights reserved.																					     *!
!*                                                                      																			     *!
!* Developed by:  Fang Jiang, Wenbin Yu                                                                                                                  *!
!*        <Prof. Yu’s Research Group>,                                                                                                                   *!
!*        <School of Aeronautics and Astronautics, Purdue University>                                                                                    *!
!*        <https://cdmhub.org/projects/ansysvabsgui2015>                                                                                                 *!
!*                                                                      																			     *!
!* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),    *!
!* to deal with the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,  *!
!* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:            *!
!*                                                                      																			     *!
!* •Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.                        *!
!*                                                                      																			     *!
!* •Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimers in the documentation *!
!*  and/or other materials provided with the distribution.                                                                                               *!
!*                                                                      																			     *!
!* •Neither the names of < Prof. Yu’s Research Group, School of Aeronautics and Astronautics, Purdue University >,                                       *!
!*  nor the names of its contributors may be used to endorse or promote products derived from this Software without specific prior written permission.   *!
!*                                                                      																			     *!
!* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *!
!* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *!
!* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER     *!
!* DEALINGS WITH THE SOFTWARE. 																															 *!
!*********************************************************************************************************************************************************!
!==============================================================
!1st do LPS
!-------------------------------------------------------------
BSL_NUM = TailLPS_bsl 
Nlam    = TailLPS_Nlam
SID     = TailLPS_SID 
DIVK    = Tail_DIVK
LAMDIREC = 1
!-------------------------------------------------------------
/PREP7
!-------------------------------------------------------------
! Baseline sampling and splines
ALLSEL,ALL
*GET, BCKPN, KP, 0, NUM, MAXD
LSEL,S,LINE,,BSL_NUM
*DO,i,1,DIVK
	KL,BSL_NUM,1/(DIVK-1)*(i-1),BCKPN+i
*ENDDO

KSEL, S, KP, , BCKPN+1, BCKPN+DIVK
*GET, KPC, KP, 0, COUNT
BSPLIN,ALL
!SPLINE,ALL
!-------------------------------------------------------------
		p = Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC%		
		*GET,CKPN,KP,0,NUM,MAXD

			*DO,j,1,KPC-1

					*IF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),GT,0,THEN	

							DX = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))
							DY = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)+DX                                     !X_ITR: new x coordinate
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)-DY

						*ELSEIF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),EQ,0

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)-THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)

						*ELSEIF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),LT,0

							DX = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))
							DY = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) - DX
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) - DY

						*ELSE
					*ENDIF

				*IF, Y_ITR, GT, KY(KP_TAILMID(p)), THEN
						K,CKPN+j,X_ITR,Y_ITR,0
					*ELSE
				*ENDIF					
			*ENDDO
			!--------------------------------------------------------------------------------------------------------------------------------------------------------------

			KSEL,ALL
			KSEL,S,KP,,CKPN+1,CKPN+j
			KSEL,A,KP,,KP_TAILMID(p)
!			SPLINE,ALL
!			LSLK,S,1
				BSPLIN,ALL

			!-----------------------------------
			! line up tail point and the right kp of mid tail line
			KSEL,ALL
			L,BCKPN+1,CKPN+1
			L,BCKPN+DIVK,KP_TAILMID(1)          
			!-----------------------------------
			KSEL, S, KP, , BCKPN+1, BCKPN+DIVK  ! Baseline Position
			KSEL, A, KP, , CKPN+1, CKPN+j       ! Bottom line position
			*DO,i,1,KP_TAIL_CNT
				KSEL,A,KP,,KP_TAILMID(i)
			*ENDDO
			LSLK,S,1
			AL,ALL

			KSEL, S, KP, , CKPN+1
			KSEL, A, KP, , KP_TAILMID(KP_TAIL_CNT) 
			LSLK,S,1
			*GET,ABIG_ID_LINE_BSL_%BSL_NUM%,LINE,0,NUM,MAX

!==============================================================
! 2nd do HPS
!-------------------------------------------------------------
BSL_NUM = TailHPS_bsl 
Nlam    = TailHPS_Nlam
SID     = TailHPS_SID 
DIVK    = Tail_DIVK
LAMDIREC = 2
!-------------------------------------------------------------
/PREP7
!-------------------------------------------------------------
! Baseline sampling and splines
ALLSEL,ALL
*GET, BCKPN, KP, 0, NUM, MAXD
LSEL,S,LINE,,BSL_NUM
*DO,i,1,DIVK
	KL,BSL_NUM,1/(DIVK-1)*(i-1),BCKPN+i
*ENDDO

KSEL, S, KP, , BCKPN+1, BCKPN+DIVK
*GET, KPC, KP, 0, COUNT
BSPLIN,ALL
!SPLINE,ALL
!-------------------------------------------------------------
		p = Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
		*GET,CKPN,KP,0,NUM,MAXD		
			!--------------------------------------------------------------------------------------------------------------------------------------------------------------		
			*DO,j,1,KPC-1

					*IF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),GT,0,THEN	

							DX = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))
							DY = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) - DX                                     !X_ITR: new x coordinate
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) + DY                                     !Y_ITE: new y coordinate

						*ELSEIF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),EQ,0

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) + THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)

						*ELSEIF,SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),LT,0

							DX = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))
							DY = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)*ABS(SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j))

							X_ITR = SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) + DX
							Y_ITR = SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) + DY

						*ELSE
					*ENDIF

				*IF, Y_ITR, LT, KY(KP_TAILMID(p)), THEN
						K,CKPN+j,X_ITR,Y_ITR,0
					*ELSE
				*ENDIF					
			*ENDDO
			!--------------------------------------------------------------------------------------------------------------------------------------------------------------

			KSEL,ALL
			KSEL,S,KP,,CKPN+1,CKPN+j
			KSEL,A,KP,,KP_TAILMID(p)
!			SPLINE,ALL
!			LSLK,S,1
				BSPLIN,ALL

			!-----------------------------------
			! line up tail point and the right kp of mid tail line
			KSEL,ALL
			L,BCKPN+1,CKPN+1
			L,BCKPN+DIVK,KP_TAILMID(1)          
			!-----------------------------------
			KSEL, S, KP, , BCKPN+1, BCKPN+DIVK
			KSEL, A, KP, , CKPN+1, CKPN+j
			*DO,i,1,KP_TAIL_CNT
				KSEL,A,KP,,KP_TAILMID(i)
			*ENDDO
			LSLK,S,1
			AL,ALL

			KSEL, S, KP, , CKPN+1
			KSEL, A, KP, , KP_TAILMID(KP_TAIL_CNT) 
			LSLK,S,1
			*GET,ABIG_ID_LINE_BSL_%BSL_NUM%,LINE,0,NUM,MAX

ASEL,ALL
!=============
/PNUM,KP,0  
/PNUM,LINE,0
/PNUM,AREA,1
!/PNUM,VOLU,1
!/PNUM,NODE,1
!/PNUM,TABN,1
!/PNUM,SVAL,1
!/PNUM,ELEM,1
!--------------
/NUMBER,0 !Specify whether numbers, colors, or both are used for displays 
		  !	0:color and number; 
		  !	1:only color; 
		  !	2:only number; 
		  !-1:nothing
!===============
APLOT