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

*IF,LAMDIRECY,NE,0,AND,LAMDIRECX,EQ,0,THEN
		LAMDIREC = LAMDIRECY
	*ELSEIF,LAMDIRECY,EQ,0,AND,LAMDIRECX,NE,0
		LAMDIREC = LAMDIRECX
	*ELSEIF,LAMDIRECY,EQ,0,AND,LAMDIRECX,EQ,0	
		LAMDIREC = 0
	*ELSEIF,LAMDIRECY,NE,0,AND,LAMDIRECX,NE,0	
		LAMDIREC = 0
	*ELSE
		LAMDIREC = 0
*ENDIF

!========================================================================
!Lamination/SegmentLayout/Fnc_LayoutInput

*GET,BSL_NUM,LINE,0,NUM,MAX 
*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


!========================================================================
LSEL,S,LINE,,BSL_NUM
LPLOT
/REPLO  
/AUTO,1 
/REP,FAST 
!------------------------------------------------------------------------

	tailflag_%BSL_NUM% = tailflag 

	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
!DIVK=200
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


!!---------------------------------------------------------
!! Sort SEGCX%SID% in accending x order
!count =0 
!*DO,i,1,KPC
!	*DO,j,1,KPC 
!		*IF,SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i),EQ,SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),THEN 
!			count=count+1 
!		*ENDIF
!		!Bubble Sort 
!		*IF,SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i),LT,SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j),THEN 
!			TEMP1=SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) 
!			SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) 
!			SEGCX_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)=TEMP1
!			!&
!			TEMP2=SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i) 
!			SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(i)=SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) 
!			SEGCY_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)=TEMP2 
!		*ENDIF 
!	*ENDDO
!*ENDDO


!---------------------------------------------------------
! Sampling pointwise slope array
*DO,i,1, 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



!---------------------------------------------------------
!Delet used sampling points
KDELE, BCKPN + 1, BCKPN + DIVK_BSL_%BSL_NUM%


!!--------------------------------------------------------------------------------------------- 20140729 update
!! Round numerical error when the line is so sloped
!*IF,LAMDIREC,EQ,1,OR,LAMDIREC,EQ,2,THEN
!		*DO, j, 1 , DIVK_BSL_%BSL_NUM%
!			*IF,ABS(SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)),GE,0.999,THEN
!					SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j) = NINT(SEGPK_BSL_%BSL_NUM%_DIREC_%LAMDIREC%(j)/0.999)*0.999
!				*ELSE
!			*ENDIF
!		*ENDDO
!	*ELSE
!*ENDIF


!=============================================================================================
!=============================================================================================
!=============================================================================================
!------------------------------------------------------------------------
! Link Line_Boundary_Flags with baseline number
NCL1FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = NCL1FLAG
NCL2FLAG_BSL_%BSL_NUM%_DIREC_%LAMDIREC% = NCL2FLAG


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

			*IF,NCL1FLAG,EQ,0,THEN !SSAS
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/LEFT_SIDE_DIREC1_SSAS,mac  
	
				*ELSEIF,NCL1FLAG,EQ,1                   !ASAS
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/LEFT_SIDE_DIREC1_ASAS,mac    
	
				*ELSEIF,NCL1FLAG,EQ,2
	
	!				/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/LEFT_SIDE_DIREC1_UDS,mac 
	
				*ELSE
			*ENDIF

			*IF,NCL2FLAG,EQ,0,THEN
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/RIGHT_SIDE_DIREC1_SSAS,mac
	
				*ELSEIF,NCL2FLAG,EQ,1   				!ASAS   
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/RIGHT_SIDE_DIREC1_ASAS,mac
	
				*ELSEIF,NCL2FLAG,EQ,2
	
	!				/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/RIGHT_SIDE_DIREC1_UDS,mac 
	
				*ELSE
			*ENDIF
	
	!----------------------------------------------------------
		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,2
	
			*IF,NCL1FLAG,EQ,0,THEN !SSAS
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/LEFT_SIDE_DIREC2_SSAS, mac  

	
				*ELSEIF,NCL1FLAG,EQ,1
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/LEFT_SIDE_DIREC2_ASAS, mac 
	
				*ELSEIF,NCL1FLAG,EQ,2
	
				*ELSE
			*ENDIF

			*IF,NCL2FLAG,EQ,0,THEN
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/RIGHT_SIDE_DIREC2_SSAS, mac
	
				*ELSEIF,NCL2FLAG,EQ,1
	
					/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/RIGHT_SIDE_DIREC2_ASAS, mac 
	
				*ELSEIF,NCL2FLAG,EQ,2
	
				*ELSE
			*ENDIF
	
		*ELSE
	*ENDIF
	
	*IF,LAMDIREC_BSL_%BSL_NUM%,EQ,4,THEN
	
			/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/WEB_DIREC4, mac
	
		*ELSEIF,LAMDIREC_BSL_%BSL_NUM%,EQ,3
	
			/INPUT,VABS_APDL_Lamination/VABS_APDL_inputLAYOUT_Modules/WEB_DIREC3, mac
	
		*ELSE
	*ENDIF
	
	!===============
	ALLSEL,ALL
	/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,1 !	0:color and number; 
			  !	1:only color; 
			  !	2:only number; 
			  !-1:nothing
	!---------------
	LPLOT
	!===============