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

!FINISH
/PREP7

! UPDL: 1) Get BSL_NUM

!========================================================
SID  = SID_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
Nlam = Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
DIVK = DIVK_BSL_%BSL_NUM%
*IF,tailflag_%BSL_NUM%,EQ,0,AND,noseflag_%BSL_NUM%,NE,1,THEN
		NCL1 = NCL1FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
		NCL2 = NCL2FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
	*ELSE
*ENDIF

!========================================================
! Side line at BSLKP1_BSL_%BSL_NUM%
!--------------------------------------------------------
! Select the keypoints and the lines
	KSEL,S,KP,,BSLKP1_BSL_%BSL_NUM%
	KSEL,A,KP,,BSLKP2_BSL_%BSL_NUM%
	LSLK,S,0
	!------------------------------------------------------------
	*GET, LLSIDE_KP1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LB1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, KP, 1         
	*GET, LLSIDE_KP2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LB1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, KP, 2
	*GET, LENGKP1, LINE, LB1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LENG
LKP1 = LB1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%

!--------------------------------------------------------
! Pick up Side line at BSLKP2_BSL_%BSL_NUM%
!--------------------------------------------------------
	KSEL,S,KP,,BSLKP1_BSL_%BSL_NUM%
	KSEL,A,KP,,BSLKP2_BSL_%BSL_NUM%
	LSLK,S,0
	!----------------------------------------------------  !LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC% is defined in "SIDE"
	*GET, RLSIDE_KP1_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, KP, 1         
	*GET, RLSIDE_KP2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LINE, LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, KP, 2
	*GET, LENGKP2, LINE, LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, LENG
LKP2 = LB2_BSL_%BSL_NUM%_DIREC_%LAMDIREC%


!FINISH


/PREP7
!========================================================

!Definitions:

!BC_L/R_FLAG=1: sampling keypoint on the selected side

!BC_L/R_FLAG=2: auto normal to the baseine tangent direction

*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



!========================================================
! Baseline sampling and splines
LSEL,U,LINE,,BSL_NUM
ALLSEL,ALL
*GET,BCKPN,KP,0,NUM,MAXD
LSEL,S,LINE,,BSL_NUM
*DO,i,1,DIVK
	*IF,BSL_%BSL_NUM%_INVERSE,EQ,0,THEN
			KL,BSL_NUM,1/(DIVK-1)*(i-1),BCKPN+i
		*ELSEIF,,BSL_%BSL_NUM%_INVERSE,EQ,1
			KL,BSL_NUM,1-1/(DIVK-1)*(i-1),BCKPN+i
		*ELSE
	*ENDIF
*ENDDO

KSEL,S,KP,,BCKPN+1,BCKPN+DIVK

*GET,KPC,KP,0,COUNT

SPLINE,ALL


	*IF,tailflag_%BSL_NUM%,EQ,0,THEN
	!==================================================
			*IF,LAMDIREC,EQ,1,THEN
			!==========================================		
					/INPUT,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA1,mac   ! - y
			
				*ELSEIF,LAMDIREC,EQ,2            ! + y
			
					/INPUT,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA2,mac
			
				*ELSEIF,LAMDIREC,EQ,4            ! + x
		
					/INPUT,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA4,mac
			
				*ELSEIF,LAMDIREC,EQ,3            ! - x
			
					/INPUT,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA3,mac
			
				*ELSE
			*ENDIF
		
		*ELSE
	*ENDIF

	
	*IF,tailflag_%BSL_NUM%,EQ,1,THEN
	!==================================================
			*IF,LAMDIREC,EQ,1,THEN
			!==========================================		

!					/input,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA_TAIL_LPS,mac
			
				*ELSEIF,LAMDIREC,EQ,2
			
!					/input,VABS_APDL_Lamination/VABS_APDL_ABIG_Modules/VABS_APDL_LAMA_TAIL_HPS,mac
			
				*ELSE
			*ENDIF

		*ELSE
	*ENDIF