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

!TailLPS_bsl  = 39
!TailLPS_Nlam = 36
!TailLPS_SID  = 5

!TailHPS_bsl  = 34
!TailHPS_Nlam = 36
!TailHPS_SID  = 5

!Tail_DIVK    = 50


!===============================================================

LSEL,ALL

*GET, CLN_preOVERLAP, LINE, 0, NUM, MIND
*GET, LALL_preOVERLAP, LINE, 0, COUNT

*DEL, L_preOVERLAP
*DIM, L_preOVERLAP, ARRAY, LALL_preOVERLAP, 1, 1

L_preOVERLAP(1) = CLN_preOVERLAP
tempo = L_preOVERLAP(1)

*DO,i,2,LALL_preOVERLAP
	L_preOVERLAP(i) = LSNEXT(tempo)
	tempo = L_preOVERLAP(i)
*ENDDO
!==============================================================


tailflag_%TailLPS_bsl% = 1
tailflag_%TailHPS_bsl% = 1

!========================================================================
! 1st do LPS
!------------------------------------------------------------------------
BSL_NUM  = TailLPS_bsl
Nlam     = TailLPS_Nlam
SID      = TailLPS_SID
DIVK     = Tail_DIVK
LAMDIREC = 1
*GET, BSLKP1, LINE, BSL_NUM, KP, 1         ! Get keypoint number at position 1
*GET, BSLKP2, LINE, BSL_NUM, KP, 2         ! Get keypoint number at position 2

!------------------------------------------------------------------------
!------------------------------------------------------------------------
!Identify Keypoint coordiante order
!Identify Keypoint line fraction
		*IF,KX(BSLKP1),LT,KX(BSLKP2),THEN
				BSLKP1_BSL_%BSL_NUM%=BSLKP1
				BSLKP2_BSL_%BSL_NUM%=BSLKP2
				LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
				LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
				BSL_%BSL_NUM%_INVERSE=0        !Baseline direction inverse flage
			*ELSEIF,KX(BSLKP1),GT,KX(BSLKP2)
				BSLKP1_BSL_%BSL_NUM%=BSLKP2
				BSLKP2_BSL_%BSL_NUM%=BSLKP1
				LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
				LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
				BSL_%BSL_NUM%_INVERSE=1        !Baseline direction inverse flage
			*ELSEIF,KX(BSLKP1),EQ,KX(BSLKP2)
!				*IF,KY(BSLKP1),LT,KY(BSLKP2),THEN
						BSLKP1_BSL_%BSL_NUM%=BSLKP1
						BSLKP2_BSL_%BSL_NUM%=BSLKP2
						LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
						LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
						BSL_%BSL_NUM%_INVERSE=0        !Baseline direction inverse flage
!					*ELSEIF,KY(BSLKP1),GT,KY(BSLKP2)	
!						BSLKP1_BSL_%BSL_NUM%=BSLKP2
!						BSLKP2_BSL_%BSL_NUM%=BSLKP1
!						LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
!						LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
!						BSL_%BSL_NUM%_INVERSE=1                !Baseline direction inverse flage
!					*ELSE
!				*ENDIF
			*ELSE	
		*ENDIF

!=============================================================================================
	DIVK_BSL_%BSL_NUM% = DIVK 
	Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = Nlam
	LAMDIREC_BSL_%BSL_NUM% = LAMDIREC
! Link laminating information with baseline number
	bsl_seg%SID% = BSL_NUM   
! record baseline number acoording to section ID 
	LAMDIREC_BSL_%BSL_NUM% = LAMDIREC
	SID_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = SID

!=============================================================================================
! Input the lamina layout (recomended input method)
! Thickness(dimensional) || Lamina(Layer)_id
!---------------------------------------------------------------------------------------------
*DEL, SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DIM, SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, TABLE, Nlam, 2, 1,
*TREAD, SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, UserLayoutLayerTypes/VABS_Layout_%SID%, txt,
!---------------------------------------------------------------------------------------------
! the sum of thickness increments referring to the model line corresponding to the layer numbers
*DEL,THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DIM,THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,Nlam,1       
	ADD=0
*DO,p,1,Nlam
	THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p) = ADD+SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)
	ADD = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p)
*ENDDO
THADD=0
*DO,p,1,Nlam
	THADD = THADD+SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)
*ENDDO
THADD = 5*THADD  !Just to make the boundary line clear

!=============================================================================================
! Baseline analysis
ALLSEL,ALL
*GET,BCKPN,KP,0,NUM,MAXD
LSEL,S,LINE,,BSL_NUM
*DO,i,1,DIVK_BSL_%BSL_NUM%
	KL,BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1),BCKPN+i  !!!!Generate sampling points on picked baseline
*ENDDO
KSEL,S,KP,,BCKPN + 1, BCKPN + DIVK_BSL_%BSL_NUM%
*GET,KPC,KP,0,COUNT
*GET,KPMAX,KP,0,NUM,MAX
*GET,KPMIN,KP,0,NUM,MIN
!--------------------------------------------------------20140729
*DEL, SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
!--------------------------------------------------------
*DIM, SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
---------------------------------------------------------
i=0
*DO,j,KPMIN,KPMAX
	*IF,KSEL(j),EQ,1,THEN
		i=i+1
		SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=KX(j)
		SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=KY(j)
	*ELSE
	*ENDIF
*ENDDO
!---------------------------------------------------------
! Sampling pointwise slope array
*DO,i, 2, DIVK_BSL_%BSL_NUM%
	SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)  = LSY(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1))
	SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) = LSY(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1))
	SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) = LSX(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1))
*ENDDO

!=====================================
*DO,i,1,ORIL_NUM
	*IF,i,EQ,1,THEN
			LSEL,S,LINE,,ORIL(i)
		*ELSE
			LSEL,A,LINE,,ORIL(i)
	*ENDIF
*ENDDO
!-------------------------------------
KSEL,S,KP,,BSLKP1
LSLK,R,0
LSEL,U,LINE,,BSL_NUM
*GET,BSL_left,LINE,0,NUM,MIN
SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSY(BSL_NUM,0) + LSY(BSL_left,1) )
SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSY(BSL_NUM,0) + LSY(BSL_left,1) )
SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSX(BSL_NUM,0) + LSX(BSL_left,1) ) 
!---------------------------------------------------------
!Delet used sampling points
KDELE, BCKPN + 1, BCKPN + DIVK_BSL_%BSL_NUM%

/INPUT,VABS_APDL_Lamination/VABS_APDL_TailAssemblyLine_Modules/TailLPS,mac



!========================================================================
!========================================================================
!========================================================================
! 2nd do HPS
!------------------------------------------------------------------------
BSL_NUM  = TailHPS_bsl  
Nlam     = TailHPS_Nlam 
SID      = TailHPS_SID  
DIVK     = Tail_DIVK    
LAMDIREC = 2
*GET, BSLKP1, LINE, BSL_NUM, KP, 1         ! Get keypoint number at position 1
*GET, BSLKP2, LINE, BSL_NUM, KP, 2         ! Get keypoint number at position 2

!------------------------------------------------------------------------
!------------------------------------------------------------------------
!Identify Keypoint coordiante order
!Identify Keypoint line fraction
		*IF,KX(BSLKP1),LT,KX(BSLKP2),THEN
				BSLKP1_BSL_%BSL_NUM%=BSLKP1
				BSLKP2_BSL_%BSL_NUM%=BSLKP2
				LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
				LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
				BSL_%BSL_NUM%_INVERSE=0        !Baseline direction inverse flage
			*ELSEIF,KX(BSLKP1),GT,KX(BSLKP2)
				BSLKP1_BSL_%BSL_NUM%=BSLKP2
				BSLKP2_BSL_%BSL_NUM%=BSLKP1
				LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
				LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
				BSL_%BSL_NUM%_INVERSE=1        !Baseline direction inverse flage
			*ELSEIF,KX(BSLKP1),EQ,KX(BSLKP2)
!				*IF,KY(BSLKP1),LT,KY(BSLKP2),THEN
						BSLKP1_BSL_%BSL_NUM%=BSLKP1
						BSLKP2_BSL_%BSL_NUM%=BSLKP2
						LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
						LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
						BSL_%BSL_NUM%_INVERSE=0        !Baseline direction inverse flage
!					*ELSEIF,KY(BSLKP1),GT,KY(BSLKP2)	
!						BSLKP1_BSL_%BSL_NUM%=BSLKP2
!						BSLKP2_BSL_%BSL_NUM%=BSLKP1
!						LFRAC_BSLKP1_BSL_%BSL_NUM%=0.0
!						LFRAC_BSLKP2_BSL_%BSL_NUM%=1.0
!						BSL_%BSL_NUM%_INVERSE=1                !Baseline direction inverse flage
!					*ELSE
!				*ENDIF
			*ELSE	
		*ENDIF

!=============================================================================================
	DIVK_BSL_%BSL_NUM% = DIVK 
	Nlam_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = Nlam
	LAMDIREC_BSL_%BSL_NUM% = LAMDIREC
! Link laminating information with baseline number
	bsl_seg%SID% = BSL_NUM   
! record baseline number acoording to section ID 
	LAMDIREC_BSL_%BSL_NUM% = LAMDIREC
	SID_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = SID

!=============================================================================================
! Input the lamina layout (recomended input method)
! Thickness(dimensional) || Lamina(Layer)_id
!---------------------------------------------------------------------------------------------
*DEL,   SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DIM,   SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, TABLE, Nlam, 2, 1,
*TREAD, SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%, UserLayoutLayerTypes/VABS_Layout_%SID%, txt,
!---------------------------------------------------------------------------------------------
! the sum of thickness increments referring to the model line corresponding to the layer numbers
*DEL,THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DIM,THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,Nlam,1       
	ADD=0
*DO,p,1,Nlam
	THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p) = ADD+SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)
	ADD = THsum_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p)
*ENDDO
THADD=0
*DO,p,1,Nlam
	THADD = THADD+SEGLAM_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(p,1)
*ENDDO
THADD = 5*THADD  !Just to make the boundary line clear

!=============================================================================================
! Baseline analysis
ALLSEL,ALL
*GET,BCKPN,KP,0,NUM,MAXD
LSEL,S,LINE,,BSL_NUM
*DO,i,1,DIVK_BSL_%BSL_NUM%
	KL,BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1),BCKPN+i  !!!!Generate sampling points on picked baseline
*ENDDO
KSEL,S,KP,,BCKPN + 1, BCKPN + DIVK_BSL_%BSL_NUM%
*GET,KPC,KP,0,COUNT
*GET,KPMAX,KP,0,NUM,MAX
*GET,KPMIN,KP,0,NUM,MIN
!--------------------------------------------------------20140729
*DEL, SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
*DEL, SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%
!--------------------------------------------------------
*DIM, SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
*DIM, SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%,ARRAY,KPC,1
---------------------------------------------------------
i=0
*DO,j,KPMIN,KPMAX
	*IF,KSEL(j),EQ,1,THEN
		i=i+1
		SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=KX(j)
		SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=KY(j)
	*ELSE
	*ENDIF
*ENDDO
!---------------------------------------------------------
! Sampling pointwise slope array
*DO,i, 2, DIVK_BSL_%BSL_NUM%
	SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)  = LSY(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1))
	SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) = LSY(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1))
	SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) = LSX(BSL_NUM,1/(DIVK_BSL_%BSL_NUM%-1)*(i-1)) 
*ENDDO
!=====================================
*DO,i,1,ORIL_NUM
	*IF,i,EQ,1,THEN
			LSEL,S,LINE,,ORIL(i)
		*ELSE
			LSEL,A,LINE,,ORIL(i)
	*ENDIF
*ENDDO
!-------------------------------------
KSEL,S,KP,,BSLKP1
LSLK,R,0
LSEL,U,LINE,,BSL_NUM
*GET,BSL_left,LINE,0,NUM,MIN
SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSY(BSL_NUM,0) + LSY(BSL_left,1) )
SEGPKY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSY(BSL_NUM,0) + LSY(BSL_left,1) )
SEGPKX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(1) = 0.5*( LSX(BSL_NUM,0) + LSX(BSL_left,1) ) 
!---------------------------------------------------------
!Delet used sampling points
KDELE, BCKPN + 1, BCKPN + DIVK_BSL_%BSL_NUM%

/INPUT,VABS_APDL_Lamination/VABS_APDL_TailAssemblyLine_Modules/TailHPS,mac

!---------------------------------------------------------
/INPUT,VABS_APDL_Lamination/VABS_APDL_TailAssemblyLine_Modules/Tail_KP,mac
/INPUT,VABS_APDL_Lamination/VABS_APDL_TailAssemblyLine_Modules/TailOverlap,mac
/INPUT,VABS_APDL_Lamination/VABS_APDL_TailAssemblyLine_Modules/VABS_APDL_TailArea,mac