!*********************************************************************************************************************************************************!
!* 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. 																															 *!
!*********************************************************************************************************************************************************!
!===============================================================
KPC = 10000
pi = 3.141592653589793238462643383279502884197169399375105820974
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!20141103 added for pitch tilt angle
*DEL,tttt
*Set,tttt,0
*IF, theta_tlt, NE, 0, THEN
	tttt=theta_tlt*(pi/180)
*ENDIF
!===============================================================!20140811
! Unit array
*DEL, one
*DEL, zero
*DIM, one,  ARRAY, KPC, 1, 1
*DIM, zero, ARRAY, KPC, 1, 1
*VFUN, one, COS, zero
!--------------------------------------------------------- 
! Array for information of Baseline
*DEL, thta1X
*DEL, thta1Y
*DEL, thta1SIN
*DEL, thta1COS
*DIM, thta1X,   ARRAY, KPC, 1
*DIM, thta1Y,   ARRAY, KPC, 1
*DIM, thta1SIN, ARRAY, KPC, 1
*DIM, thta1COS, ARRAY, KPC, 1
!---------------------------------------------------------
! Array for constant coordinate value of a single element
*DEL, Xe
*DEL, Ye
*DIM, Xe, ARRAY, KPC, 1
*DIM, Ye, ARRAY, KPC, 1
!----------------------------------------------------
! Array for distance of element to kp on baseline
*DEL, NMx
*DEL, NMxx
*DEL, NMy
*DEL, NMyy
*DIM, NMx,  ARRAY, KPC, 1
*DIM, NMxx, ARRAY, KPC, 1
*DIM, NMy,  ARRAY, KPC, 1
*DIM, NMyy, ARRAY, KPC, 1
*DEL, NML
*DIM, NML, ARRAY, KPC, 1
!------------------------------------------------------------------
! Array for calculating position vector dot-product tangent vector
*DEL, Ftmp1
*DEL, Ftmp2
*DIM, Ftmp1, ARRAY, KPC, 1, 1
*DIM, Ftmp2, ARRAY, KPC, 1, 1
*DEL, NdotT
*DIM, NdotT, ARRAY, KPC, 1, 1

!==============================================================================================
!==============================================================================================
!==============================================================================================
*ABSET,'Calculating Elemental Ply-Tilt', BAR

*DO,s,1,ORIL_NUM   ! loop baseline number

mm = (s/ORIL_NUM)*100
*ABCHECK, mm

	i = ORIL(s)             ! "i" is the baseline number
	LD = LAMDIREC_BSL_%i%   ! "LD" is the baseline laminate direction
!==============================================================================================
	KSEL,ALL
	*GET, Kfinal, KP, 0, NUM, MAXD
	*DO, u, 1, KPC
		KL, i, 1/(KPC-1)*(u-1), Kfinal + u  
	*ENDDO
	!---------------------------------------------------------
	KSEL, S, KP, , Kfinal + 1, Kfinal + KPC
	iii = 0
	*DO, kkk, Kfinal + 1, Kfinal + KPC
		*IF, KSEL(kkk), EQ, 1, THEN
			iii = iii + 1
			thta1X(iii) = KX(kkk)
			thta1Y(iii) = KY(kkk)
		*ENDIF
	*ENDDO
	KDELE, Kfinal + 1, Kfinal + KPC
	!---------------------------------------------------------
	*DO, v, 1, KPC
		thta1SIN(v) = LSY( i, 1/(KPC-1)*(v-1) )
		thta1COS(v) = LSX( i, 1/(KPC-1)*(v-1) )
	*ENDDO

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

	*DO, j, 1, nlam_bsl_%i%_direc_%LD%   ! loop layer numbers, "j" is the lamina number in the laminating order

		q = anum_BSL%i%(j)               ! get Area number from layer order

		*DO, p, 1, enumA%q%              ! "p" the looping element number in the layer area selected
!#############################################################################################################
		!------------------------------------------------------------
		! constant array of single element coordinates
		*VFACT, eCENTx_A%q%(p)
		*VFUN,  Xe, COPY, one
		
		*VFACT, eCENTy_A%q%(p)
		*VFUN,  Ye, COPY, one
		
		!------------------------------------------------------------
		! find length between element and kp on bsl for nornalization
		
		*VOPER, NMx, thta1X, SUB, Xe
		*VOPER, NMxx, NMx,  MULT, NMx
		*VOPER, NMy, thta1Y, SUB, Ye
		*VOPER, NMyy, NMy,  MULT, NMy
		
		*VOPER, NML, NMxx,  ADD, NMyy
		*VFUN, NML, SQRT, NML
		
		!==========================================================================
		! NdotT = ( ( thta1X - Xe )/NL )*thta1COS + ( ( thta1Y - Ye )/NL )*thta1SIN
		!--------------------------------------------------------------------------
		*VOPER, Ftmp1, thta1X, SUB, Xe
		*VOPER, Ftmp1, Ftmp1,  DIV, NML
		*VOPER, Ftmp1, Ftmp1,  MULT, thta1COS
		*VOPER, Ftmp2, thta1Y, SUB, Ye
		*VOPER, Ftmp2, Ftmp2,  DIV, NML
		*VOPER, Ftmp2, Ftmp2,  MULT, thta1SIN
		*VOPER, NdotT, Ftmp1, ADD, Ftmp2
		*VOPER, NdotT, NdotT, MULT, NdotT
		*VFUN, NdotT, SQRT, NdotT
		
		*VSCFUN, minlabl, LMIN, NdotT(1)    ! Find the Index of minimum  " N dot T "
		
		!*VSCFUN, minlabl, LMIN, NML(1)
		
		!/////////////////////////////////////////////////////////////////////////
		theta = ASIN( thta1SIN(minlabl) )*(180/pi) + tttt
		!/////////////////////////////////////////////////////////////////////////		

		*IF, LD, EQ, 2, THEN                           ! + y lamination
				*IF,theta,GE,0,THEN
						etheta1A%q%(p) = 180 + theta 
					*ELSEIF,theta,LT,0
						etheta1A%q%(p) = 180 + theta 
					*ELSE
				*ENDIF
			*ELSE
		*ENDIF
		
		*IF, LD, EQ, 1, THEN                           ! - y lamination
				*IF,theta,GE,0,THEN
						etheta1A%q%(p) = theta 
					*ELSEIF,theta,LT,0
						etheta1A%q%(p) = 360 + theta 
					*ELSE
				*ENDIF								
			*ELSE
		*ENDIF					
		
		*IF, LD, EQ, 3, THEN                           ! - x lamination
				*IF,theta,GE,0,THEN
						etheta1A%q%(p) = 180 + theta 
					*ELSEIF,theta,LT,0
						etheta1A%q%(p) = 360 + theta 
					*ELSE
				*ENDIF
			*ELSE
		*ENDIF					
		*IF, LD, EQ, 4, THEN                           ! + x lamination
				*IF,theta,GE,0,THEN
						etheta1A%q%(p) = theta 
					*ELSEIF,theta,LT,0
						etheta1A%q%(p) = 180 + theta  
					*ELSE
				*ENDIF
			*ELSE
		*ENDIF
!#################################################################################
		*ENDDO
	*ENDDO

*ENDDO
*ABFINISH

ALLSEL,ALL