!*********************************************************************************************************************************************************!
!* 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. 																															 *!
!*********************************************************************************************************************************************************!

! Paper-cutting method
/PREP7

!==============================================================================================
!-1-Replot the model lines
!==============================================================================================
AllSEL,ALL
KPC = DIVK_BSL_%BSL_NUM%
!KPC=200
!-----------------------------------------
*DO,i,1,ORIL_NUM
	*IF,i,EQ,1,THEN
			LSEL,S,LINE,,ORIL(i)
		*ELSE
			LSEL,A,LINE,,ORIL(i)
	*ENDIF
*ENDDO
!-----------------------------------------
/PNUM,KP,0  
/PNUM,LINE,1
/PNUM,AREA,0
/PNUM,VOLU,0
/PNUM,NODE,0
/PNUM,TABN,0
/PNUM,SVAL,0
/PNUM,ELEM,0
/NUMBER,0    !0: color+number
LPLOT

!=====================================================================================================
!-2-Pick up laminate baseline and the BaseSideKeyPoint
!=====================================================================================================
KSEL,S,KP,,BSLKP1_BSL_%BSL_NUM%
KSEL,A,KP,,BSLKP2_BSL_%BSL_NUM%
LSLK,S,0
/PNUM,LINE,1
/NUMBER,0
LPLOT

!======================================================================================================
!-3- Determine the flag controling the method of generating the keypoints on the side lines
!======================================================================================================

LAMDIREC = LAMDIREC_BSL_%BSL_NUM%
Nlam = Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
SID = SID_BSL_%BSL_NUM%_DIREC_%LAMDIREC%

*IF, noseflag_%BSL_NUM%, NE, 1, AND, tailflag_%BSL_NUM%, NE, 1, THEN

NCL1 = NCL1FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
NCL2 = NCL2FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC%

		*IF, NCL1, EQ, 0, THEN  !SSAS
		
				BC_L_FLAG = 1
		
		!	*ELSEIF,NCL1,EQ,1,OR,NCL1,EQ,2
		
			*ELSEIF,NCL1, EQ, 1
		
		!		*IF,THETA_1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, GE, NORM_ANGLE_1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,THEN
		!				BC_L_FLAG = 1                                                                                  !Use side line Information
		!			*ELSEIF,THETA_1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LT, NORM_ANGLE_1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
		
				BC_L_FLAG = 2                                                                                  !Cutting Inside BC Line
		
		!			*ELSE
		!		*ENDIF
		
			*ELSE
		*ENDIF
		
		*IF,NCL2,EQ,0,THEN  !SSAS
		
				BC_R_FLAG = 1
		
		!	*ELSEIF,NCL2,EQ,1,OR,NCL2,EQ,2
			
			*ELSEIF, NCL2, EQ, 1
		
		!		*IF,THETA_2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, GE ,NORM_ANGLE_2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,THEN
		!				BC_R_FLAG = 1                                                                                   !Outside BC Line
		!			*ELSEIF,THETA_2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LT ,NORM_ANGLE_2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
		
				BC_R_FLAG = 2                                                                                    !Inside BC Line
		
		!			*ELSE
		!		*ENDIF
		
			*ELSE
		*ENDIF

	*ELSE
*ENDIF

*IF, tailflag_%BSL_NUM%, NE, 1, THEN
!==============================================================================================
!Use the following to remember the corner keypoints of the remained area to be cut
!Should Always have this function
!--------------------------------------------------------------------------------
! Left side
LLSIDE = LB1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*GET, LLSIDE_KP1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LLSIDE, KP, 1         
*GET, LLSIDE_KP2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LLSIDE, KP, 2
!KLSIDE=(LY(LLSIDE,0.0)-LY(LLSIDE,1.0))/(LX(LLSIDE,0.0)-LX(LLSIDE,1.0))
*GET,LL_LENG,LINE,LLSIDE,LENG
!--------------------------------------------------------------------------------
! Right side
RLSIDE = LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*GET, RLSIDE_KP1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, RLSIDE, KP, 1         
*GET, RLSIDE_KP2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, RLSIDE, KP, 2
!KRSIDE=(LY(RLSIDE,0.0)-LY(RLSIDE,1.0))/(LX(RLSIDE,0.0)-LX(RLSIDE,1.0))
*GET,RL_LENG,LINE,RLSIDE,LENG

	*ELSE
*ENDIF


!==============================================================================================
!**********************************************************************************************
!20140730 update
! Get ABIG_ID_LINE

ASEL, S, AREA, , ABIG_UPDATE

*IF,tailflag_%BSL_NUM%,NE,1,THEN
	KSEL, S, KP, , abig_bottom_kp_BSL_%BSL_NUM%(1)
	*DO,i, 2, DIVK_BSL_%BSL_NUM%
		KSEL, A, KP, , abig_bottom_kp_BSL_%BSL_NUM%(i)
	*ENDDO
	LSLK,S,1
	
	!reselect the lines from the max and min, according to the selected area
!---------------------------------------------------------------------------
	LSLA,R      ! Now the bottom line should be in the selected lines
	
	! get the longest (most original) line in the selected bottom line
!---------------------------------------------------------------------	
	*GET, recount, LINE, 0, count
	
	*DEL, abig_bottom_line_BSL_%BSL_NUM%
	*DIM, abig_bottom_line_BSL_%BSL_NUM%, ARRAY, recount, 1
	
	*GET,abig_bottom_line_BSL_%BSL_NUM%(1), LINE, 0, NUM, MIN
	temp = abig_bottom_line_BSL_%BSL_NUM%(1)
	*DO,i,2, recount
		abig_bottom_line_BSL_%BSL_NUM%(i) = LSNEXT(temp)
		temp = abig_bottom_line_BSL_%BSL_NUM%(i)
	*ENDDO
	
	*DEL, abig_bottom_leng_BSL_%BSL_NUM%
	*DIM, abig_bottom_leng_BSL_%BSL_NUM%, ARRAY, recount, 1
	*DO,i,1, recount
		*GET, abig_bottom_leng_BSL_%BSL_NUM%(i), LINE, abig_bottom_line_BSL_%BSL_NUM%(i), LENG
	*ENDDO
	
	! bubble sort length
		*DO, i, 1, recount, 1 
			*DO, j, recount, 2, -1
				*IF, abig_bottom_leng_BSL_%BSL_NUM%(j), LT, abig_bottom_leng_BSL_%BSL_NUM%(j-1), THEN
						!---------------------------------------------
						buff = abig_bottom_leng_BSL_%BSL_NUM%(j)
						abig_bottom_leng_BSL_%BSL_NUM%(j) = abig_bottom_leng_BSL_%BSL_NUM%(j-1)
						abig_bottom_leng_BSL_%BSL_NUM%(j-1) = buff								
						!---------------------------------------------
						!---------------------------------------------
						buff2 = abig_bottom_line_BSL_%BSL_NUM%(j)
						abig_bottom_line_BSL_%BSL_NUM%(j) = abig_bottom_line_BSL_%BSL_NUM%(j-1)
						abig_bottom_line_BSL_%BSL_NUM%(j-1) = buff2		
						!---------------------------------------------
					*ELSE
				*ENDIF
			*ENDDO
		*ENDDO
	
	ABIG_ID_LINE = abig_bottom_line_BSL_%BSL_NUM%(recount)

*ENDIF

*IF,tailflag_%BSL_NUM%,EQ,1,THEN

	ABIG_ID_LINE = ABIG_ID_LINE_BSL_%BSL_NUM%

*ENDIF

!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

*IF, noseflag_%BSL_NUM%, NE, 1, AND, tailflag_%BSL_NUM%, NE, 1, THEN

	*IF, LAMDIREC_BSL_%BSL_NUM%, EQ, 1, THEN

			*IF, BC_L_FLAG, EQ, 1, AND, BC_R_FLAG, EQ, 1, THEN                            ! Both inside boundary
				
					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC1_L1_R1, mac

				*ELSEIF, BC_L_FLAG, EQ, 2, AND, BC_R_FLAG, EQ, 2
				
					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC1_L2_R2, mac				
				
!				*ELSEIF,BC_L_FLAG,EQ,1,AND,BC_R_FLAG,EQ,1				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC1_L1_R1,mac				
!				*ELSEIF,BC_L_FLAG,EQ,2,AND,BC_R_FLAG,EQ,1				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC1_L2_R1,mac					
!				*ELSEIF,BC_L_FLAG,EQ,1,AND,BC_R_FLAG,EQ,2				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC1_L2_R1,mac				

				*ELSE
			*ENDIF

		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,2

			*IF, BC_L_FLAG, EQ, 1, AND, BC_R_FLAG, EQ, 1, THEN                            ! Both inside boundary
				
					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC2_L1_R1, mac
				
				*ELSEIF, BC_L_FLAG, EQ, 2, AND, BC_R_FLAG, EQ, 2
				
					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC2_L2_R2,mac	

!				*ELSEIF,BC_L_FLAG,EQ,1,AND,BC_R_FLAG,EQ,1				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC2_L1_R1, mac				
!				*ELSEIF,BC_L_FLAG,EQ,2,AND,BC_R_FLAG,EQ,1				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC2_L2_R1, mac					
!				*ELSEIF,BC_L_FLAG,EQ,1,AND,BC_R_FLAG,EQ,2				
!					/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_DIREC2_L2_R1, mac				

				*ELSE
			*ENDIF

		*ELSE
	*ENDIF

	*IF,LAMDIREC_BSL_%BSL_NUM%,EQ,4,THEN
					
			/INPUT, VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_WEB_DIRECT4, mac
		
		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,3

			/INPUT, VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_WEB_DIRECT3, mac

		*ELSE
	*ENDIF


*ELSEIF, tailflag_%BSL_NUM%, EQ, 1

	*IF,LAMDIREC_BSL_%BSL_NUM%,EQ,1,THEN

			/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_TAIL_LPS,mac	

		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,2

			/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_TAIL_HPS,mac	

		*ELSE
	*ENDIF


*ELSEIF, noseflag_%BSL_NUM%, EQ, 1

	*IF,LAMDIREC_BSL_%BSL_NUM%,EQ,1,THEN

			/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_NOSE_LPS,mac	

		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,2

			/INPUT,VABS_APDL_Lamination/VABS_APDL_PAPERCUT_Modules/LAMTAILOR_NOSE_HPS,mac	

		*ELSE
	*ENDIF

*ELSE

*ENDIF


!copy the input layer ID into 1-D array to convinient output
	*VFUN,  alyr_BSL%BSL_NUM%,  COPY, SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1,2,1)

/NUMBER,1
/REPLOT 
	
ALLSEL, ALL