!*********************************************************************************************************************************************************!
!* 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. 																															 *!
!*********************************************************************************************************************************************************!
!===========================================================================================================================================
!===========================================================================================================================================
!===========================================================================================================================================
/PREP7
*DEL, anum_BSL%BSL_NUM%	
*DIM, anum_BSL%BSL_NUM%, ARRAY, Nlam
!SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%

			*GET,TIALKP0,KP,0,NUM,MAXD
			*DO, p, 1, Nlam-1
				*GET,CKPN,KP,0,NUM,MAXD
				*DO,j,1,KPC
					*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
					K,CKPN+j,X_ITR,Y_ITR,0
				*ENDDO
				!--------------------------------------------------------------------------------------
				!Remained area selection
				LSEL,S,LINE,,ABIG_ID_LINE
				ASLL,S,0			        ! Select area to cut
				*GET,ABIG_UPDATE,AREA,0,NUM,MAX
				!--------------------------------------------------------------------------------------
				!Cutting line selection
				KSEL,S,KP,,CKPN+1,CKPN+KPC

*IF,tailflag_%BSL_NUM%,EQ,0,THEN
		SPLINE,ALL
		KSEL,S,KP,,CKPN+1,CKPN+KPC
	*ELSEIF,tailflag_%BSL_NUM%,EQ,1
		BSPLIN,ALL
		KSEL,S,KP,,CKPN+1
		KSEL,A,KP,,CKPN+KPC
	*ELSE
*ENDIF
				LSLK,S,1
				ASBL,ABIG_UPDATE,ALL

*GET,A1,AREA,0,NUM,MAX
*GET,A2,AREA,0,NUM,MIN
LSEL,S,LINE,,ABIG_ID_LINE
ASLL,S,0
*GET,A3,AREA,0,NUM,MAX
*IF,A1,EQ,A3,THEN
		anum_BSL%BSL_NUM%(p)=A2
	*ELSEIF,A2,EQ,A3
		anum_BSL%BSL_NUM%(p)=A1
	*ELSE
*ENDIF

				!--------------------------------------------------------------------------------------	
				APLOT
				/REPLO  
				/AUTO,1 
				/REP,FAST 
				LSEL,ALL
			*ENDDO	
			ASEL,ALL
			APLOT

*IF,A1,EQ,A3,THEN
		anum_BSL%BSL_NUM%(Nlam) = A1
	*ELSEIF,A2,EQ,A3
		anum_BSL%BSL_NUM%(Nlam) = A2
	*ELSE
*ENDIF


!=======================================================================================================
AllSEL,ALL
/PNUM,KP,0  
/PNUM,LINE,0
/PNUM,AREA,1
/PNUM,VOLU,0
/PNUM,NODE,0
/PNUM,TABN,0
/PNUM,SVAL,0
/PNUM,ELEM,0
APLOT
!-------------------------------------------------------------------------------------------------------
/NUMBER,1 !	0:color and number;  !	1:only color; !	2:only number; !-1:nothing