compile function SPC_EVAL_ADE_RPT1()

begin condition
pre:
post:
end condition

* THIS FUNCTION WILL PLOT INTEGER VALUES FOR THICKNESS

declare function msg, set_property, send_event, dialog_box, utility, get_property
declare function set_printer, get_printer,Printer_Select
declare subroutine rlist, or_view, activate_save_select, yield

$insert rlist_equates
$insert msg_equates
$insert oiprint_equates
$insert spc_limits_equ
$insert logical

$insert spc_eval_ADE_equ

TableToUse = 'SPC_EVAL_ADE'
open TableToUse to TableVar else
    MsgInfo = ''
    MsgInfo<mcol$> = -2
    MsgInfo<mrow$> = -2
    MsgInfo<mtext$> = 'Unable to open ':TableToUse:'...' 
    Void = msg( '', MsgInfo )
    return 0
end

Params = dialog_box( 'SPC_EVAL_ADE_RPT1', @window, '*CENTER' )
if Params = 'CANCEL' or Params = '' then
   return 0
end

convert '*' to @fm in Params
TestDateFrom    = Params<1>
TestDateThru    = Params<2>
ToolNumber      = Params<3>
EvalRange       = Params<4>
EvalADEWaferID = Params<5>
DirToPrinter    = Params<6>

Stat = utility( 'CURSOR', 'H' )
Stmt = 'SELECT ':TableToUse:' BY DATE_TESTED BY TIME_TESTED'

* XPOSS ARE STATIC CAUSE THERE WILL ONLY BE SEVEN PLOTS PRINTED PER PAPER
XPosS1     = 1.11:@fm:2.49:@fm:3.87:@fm:5.25:@fm:6.63:@fm:8.01:@fm:9.38
XPosS2     = 1.17:@fm:2.55:@fm:3.93:@fm:5.31:@fm:6.69:@fm:8.07:@fm:9.44


YStartPos1 = ''
YStartPos2 = ''

XPosLast2  = ''
YPosLast2  = ''

EndStmt = ''
* TEST DATE INFORMATION
if TestDateFrom and TestDateThru then
   EndStmt :=  " WITH DATE_TESTED FROM ":QUOTE(TestDateFrom):' TO ':QUOTE(TestDateThru)
end else
	if TestDateFrom then
	    EndStmt :=  " WITH TEST_DATE GE ":QUOTE(TestDateFrom)
	end
	if TestDateThru then
	    EndStmt := " WITH TEST_DATE LE ":QUOTE(TestDateThru)
	end
end
Stmt:=EndStmt

Stmt := " AND WITH TOOL_NUMBER ":QUOTE(ToolNumber)
Stmt := " AND WITH RANGE ":QUOTE(EvalRange)
Stmt := " AND WITH WAFER_ID ":QUOTE(EvalADEWaferID)

Void = set_status(0)
rlist( Stmt, target_activelist$, '', '', '' )

if @reccount then

	TestDateS      = ''
	TestTimeS      = ''
	ShiftS         = ''
	EntryIDS       = ''
	ThicknessS      = ''
	PointsPlotted  = 0
	YDeltaNums     = ''
	PrintPageBreak = 0
	
   if DirToPrinter = 'Yes' then
      PrnFlag = 0
   end else
      PrnFlag = 3 ;* THREE IN OIPI IS SCREEN MAXIMIZED   
   end
   * PULL OFF VARIABLES
   SPCLimitInfo        = xlate( 'CONFIG', 'SPC_LIMITS', '', 'X' )
   begin case
      case EvalRange = 'Low'
         EvalADEMinAll       = SPCLimitInfo<EvalADELMin$>
         EvalADEMaxAll       = SPCLimitInfo<EvalADELMax$>
         EvalADEDateChgAll   = oconv( SPCLimitInfo<EvalADELDate$>, 'D2/' )
         EvalADEWaferIDAll   = SPCLimitInfo<EvalADELWaferID$>   
      case EvalRange = 'Mid'
         EvalADEMinAll       = SPCLimitInfo<EvalADEMMin$>
         EvalADEMaxAll       = SPCLimitInfo<EvalADEMMax$>
         EvalADEDateChgAll   = oconv( SPCLimitInfo<EvalADEMDate$>, 'D2/' )
         EvalADEWaferIDAll   = SPCLimitInfo<EvalADEMWaferID$>               
      case EvalRange = 'High'
         EvalADEMinAll       = SPCLimitInfo<EvalADEHMin$>
         EvalADEMaxAll       = SPCLimitInfo<EvalADEHMax$>
         EvalADEDateChgAll   = oconv( SPCLimitInfo<EvalADEHDate$>, 'D2/' )
         EvalADEWaferIDAll   = SPCLimitInfo<EvalADEHWaferID$>               
   end case
   locate EvalADEWaferID in EvalADEWaferIDAll using @vm setting WIDPos then
      EvalADEMin = EvalADEMinAll<1,WIDPos>
      EvalADEMax = EvalADEMaxAll<1,WIDPos>
      EvalADEDateChg = EvalADEDateChgAll<1,WIDPos>
   end else
      * ERROR ENGINEERING ERROR CONTACT PAUL
      MsgInfo = ''
      MText = 'Wafer ID# ':quote(EvalADEWaferID):'is not found in the limit set up.':CrLf$
      MText:= 'Please contact the engineering manager.'
      MsgInfo<mtext$> = MText
      MsgInfo<micon$> = 'H'
      Void = msg( '', MsgInfo )
      return 0 
   end
   *EvalADEStdevMin     = SPCLimitInfo<EvalADEStdevMin$>
   *EvalADEStdevMax     = SPCLimitInfo<EvalADEStdevMax$>
   *EvalADEStdevDateChg = SPCLimitInfo<EvalADEStdevDate$>   
   
   PrintPath = Printer_Select('',1)		;* Get default printer path
   Void = set_printer( 'INIT', '', '', '':@fm:'':@fm:'':@fm:'', 1, PrnFlag:@fm:75,PrintPath )
   Void = set_printer( 'LINESTYLE', PS_SOLID:@fm:1 )
   Void = set_printer( 'FILLSTYLE', BS_SOLID )

   * NOW SET UP YPOS ARRAYS   
   * REASON FOR THIS IS CAUSE MAX AND MIN CAN CHANGE
   *TStartPos1 = 2.44
   *TStartPos2 = 2.5
   * MAX CAN ONLY BE 7 DUE TO OUR DEFINED SCALE
   * UCL
   * POINT ONE
   * POINT TWO
   * TARGET
   * POINT THREE
   * POINT FOUR
   * LCL
	Target = ( EvalADEMin + ( (EvalADEMax-EvalADEMin)/2 ) )
	UToSpec = ( EvalADEMax - Target )
   ScaleInc = ( UToSpec/3 )
   YDeltaNums = EvalADEMax
   PointOne    = (EvalADEMax-ScaleInc)
   YDeltaNums<-1> = PointOne
   PointTwo    = (EvalADEMax-(ScaleInc*2))
   YDeltaNums<-1> = PointTwo
   YDeltaNums<-1> = Target
   PointThree  = (EvalADEMax-(ScaleInc*4))
   YDeltaNums<-1> = PointThree
   PointFour   = (EvalADEMax-(ScaleInc*5))
   YDeltaNums<-1> = PointFour
   YDeltaNums<-1> = ( EvalADEMin )
	
   gosub PrintTOF
   
	Eof = false$
	NumKeys = @reccount
	Cnt = 0
	loop
	    readnext EvalADEKey else Eof = true$
	  until Eof
	     read EvalADEEntry from TableVar, EvalADEKey else
			   MsgInfo = ''
			   MsgInfo<mcol$>  = -2
			   MsgInfo<mrow$>  = -2
			   MsgInfo<mtext$> = 'Error reading ':EvalADEKey:' from ':TableToUse:'!'
			   MsgInfo<micon$> = '!'
			   Void = MSG( '', MsgInfo )
			   return 0
	     end
	     * PULL OFF VARIABLES
	     TestDate  = oconv( EvalADEEntry<spc_eval_ADE_date_tested$>, 'D2/' )
	     TestTime  = oconv( EvalADEEntry<spc_eval_ADE_time_tested$>, 'MTH' )
	     Shift     = EvalADEEntry<spc_eval_ADE_shift$>
        EntryID   = EvalADEEntry<spc_eval_ADE_entry_id$>
        Thickness = EvalADEEntry<spc_eval_ADE_thick$>
        HoldThickness = Thickness
	     *
	     locate Thickness in YDeltaNums using @fm setting DPos then
           YPosToUse1 = YStartPos1<DPos>
           YPosToUse2 = YStartPos2<DPos> 
	     end else
	        if Thickness > EvalADEMax then
		        * POINT OUT OF CONTROL HIGH
		        * SET OTHER VARS UP TOP FOR OUT OF CONTROL
		        YPosToUse1 = OutofControlHighY
		        YPosToUse2 = OutofControlHighY+.06
	        end else
	           if Thickness < EvalADEMin then
	              * POINT OUT OF CONTROL LOW
	              YPosToUse1 = OutofControlLowY
	              YPosToUse2 = OutofControlLowY+.06
	           end else
		        	  * NOT OUT OF CONTROL BUT IS NOT EVENLY DIVISIBLE BY ONE OF THE SEVEN LINES
		        	  * FIND OUT WHICH IT IS CLOSER TO
		        	  locate Thickness in YDeltaNums by 'DR' using @fm setting Adjpos then
		        	     * THIS WILL NOT HAPPEN BASED ON ABOVE LOGIC
		        	  end
		        	  BTop = YDeltaNums<AdjPos-1>
		        	  BBot = YDeltaNums<AdjPos>
		        	  TopDif = BTop-Thickness
		        	  BotDif = Thickness-BBot
		        	  if TopDif = BotDif then
		        	     * BASICALLY IF IT IS EXACLY IN THE MIDDLE OF TWO POINTS
		        	     * THEN PUT IT CLOSER TO TARGET
		        	     if Thickness > Target then
		        	        YPosToUse1 = YStartPos1<AdjPos>
		        	        YPosToUse2 = YStartPos2<AdjPos>
		        	     end else
		        	        YPosToUse1 = YStartPos1<AdjPos-1>
		        	        YPosToUse2 = YStartPos2<AdjPos-1>
		        	     end
		        	  end
		        	  if TopDif > BotDif then
		        	     * CLOSER TO BOTTOM POINT
	        	        YPosToUse1 = YStartPos1<AdjPos>
	        	        YPosToUse2 = YStartPos2<AdjPos>
		        	  end else
		        	     * CLOSER TO TOP POINT
	        	        YPosToUse1 = YStartPos1<AdjPos-1>
	        	        YPosToUse2 = YStartPos2<AdjPos-1>	        	     
		        	  end
		        end
	        end
	     end
	     if PointsPlotted = 7 then ;* CAN ONLY PLOT SEVEN SO DO A PAGEBREAK AND PRINT TOF AND PRINT COLUMNS
	        PrintPageBreak = 1
	        gosub PrintBOF	
	        gosub PrintTOF
	     end
        XPosToUse1    = XPosS1<PointsPlotted+1>
        XPosToUse2    = XPosS2<PointsPlotted+1>
        *YPOSTOUSE1 = 2.25
        *YPOSTOUSE2 = 2.31        
        if YPosToUse1 = OutofControlHighY then
           Void = set_printer( "TEXTXY", oconv( HoldThickness, 'MD3' ), XPosToUse1-.16:@fm:OutofControlHighY-.19, '', 0 )           
        end
        if YPosToUse1 = OutofControlLowY then
           Void = set_printer( "TEXTXY", oconv( HoldThickness, 'MD3' ), XPosToUse1-.13:@fm:OutofControlLowY+.06, '', 0 )           
        end
        *
	     TestDateS<1,-1> = TestDate
	     TestTimeS<1,-1> = TestTime
	     ShiftS<1,-1>    = Shift
        EntryIDS<1,-1>  = EntryID
        ThicknessS<1,-1> = HoldThickness
	     
        * SET YOUR POINT AND THEN SET XY FOR THE LINE TO	      
        Void = set_printer( 'ELLIPSE', XPosToUse1:@FM:YPosToUse1:@FM:XPosToUse2:@FM:YPosToUse2, 0 )
	     PointsPlotted += 1
        if PointsPlotted > 1 then
           Void = set_printer( 'LINE', XPosLast2-.03:@fm:YPosLast2:@fm:XPosToUse2-.03:@fm:YPosToUse2, 0 )
        end
	     YPosLast2 = YPosToUse2
	     XPosLast2 = XPosToUse2
	repeat
	PrintPageBreak = 0
   gosub PrintBOF
   Void = set_printer( 'TERM', 1 )      
end else
   MsgInfo = ''
   MsgInfo<mcol$>  = -2
   MsgInfo<mrow$>  = -2
   MsgInfo<mtext$> = 'NO Records meeting your date criteria!!'
   MsgInfo<micon$> = '!'
   Void = MSG( '', MsgInfo )
end
return 0
*===============================================================================================*
PrintTOF:
Font = "Courier New,18,C,1"
convert ',' to @fm in Font
Tfont = Font
Stat = set_printer( 'FONT', Font )
Void = set_printer( 'TEXT', 'ADE# ':ToolNumber:' ':EvalRange:' Control Chart' )
Font<2> = 12
Font<3> = 'L'
Stat = set_printer( 'FONT', Font )
Void = set_printer( 'TEXT', '' )
	
TableColInfo = '^+4680':@vm:'^+4680':@vm:'^+4680'
PrintTbl = 'Characteristic Measured: Thickness':@vm:'Recipe: T-':EvalRange:' Cal':@vm:'Date ctl Limits Calculated: ':EvalADEDateChg
Void = set_printer( 'ADDTABLE', TableColInfo, '', PrintTbl, '', '', 0, TB_ALL:@fm:'C' )
  
PrintTbl = 'Unit of Measure: Thick':@vm:'Frequency: Shiftly':@vm:'Limits Calculated By: Peter Z.'
Void = set_printer( 'ADDTABLE', TableColInfo, '', PrintTbl, '', '', 0, TB_ALL:@fm:'C' )
 
PrintTbl = 'Sample ID: T-':EvalRange:@vm:'UCL: ':oconv( EvalADEMax, 'MD3' ):' LCL: ':oconv( EvalADEMin, 'MD3' ):@vm:'Target: ':oconv( Target, 'MD3' )
Void = set_printer( 'ADDTABLE', TableColInfo, '', PrintTbl, '', '', 0, TB_ALL:@fm:'C' )
  
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', 'Average X-Bar' )
  
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', 'UCL-':oconv( EvalADEMax, 'MD3' ) )

TableColInfo = '^+1080':@vm:'^+1987':@vm:'^+1987':@vm:'^+1987':@vm:'^+1987':@vm:'^+1987':@vm:'^+1987':@vm:'^+1987'
CurPos = get_printer( 'POS' )<2>
OutofControlHighY = CurPos-.13	     	     	        
for i = 1 to 7
	PrintTbl = oconv( YDeltaNums<i>, 'MD3' ):@vm:'':@vm:'':@vm:'':@vm:'':@vm:'':@vm:'':@vm:''
	Void = set_printer( 'ADDTABLE', TableColInfo, '', PrintTbl, '', '', 1, TB_ALL:@fm:'L' )
	CurPos = get_printer( 'POS' )<2>
	CurPos-=.13
	TStartPos1 = CurPos
	TStartPos2 = CurPos+.06
	YStartPos1<-1> = TStartPos1
	YStartPos2<-1> = TStartPos2
	if i = 4 then
	   * LINE FOR THE TARGET
      Void = set_printer( 'LINESTYLE', PS_SOLID:@fm:.5 )
      Void = set_printer( 'LINE', .75:@fm:CurPos+.035:@fm:10.3:@fm:CurPos+.035, 0 )
	end
next i
CurPos = get_printer( 'POS' )<2>
OutofControlLowY = CurPos+.06
Void = set_printer( 'TEXT', 'LCL-':oconv( EvalADEMin, 'MD3' ) )
Void = set_printer( 'LINESTYLE', PS_SOLID:@fm:1 )
return
*===============================================================================================*
PrintBOF:
* ALLOW ROOM ON THE BOTTOM FOR A POINT OUT OF CONTROL ON THE LOW SIDE
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', '' )
Font<2> = 8
Font<3> = 'L'
Stat = set_printer( 'FONT', Font )

*ColSize  = '^+662':@vm:'^+662':@vm:'^+662'
*NewColSize = ''
*ColData    = 'Pre':@vm:'Post':@vm:'Delta'
*for i = 1 to PointsPlotted
*   NewColSize<1,-1> = ColSize
*   ColData<1,-1>    = 'Pre':@vm:'Post':@vm:'Delta'
*next i
*NewColSize = '^+720':@vm:NewColSize
*ColData = '':@vm:ColData
*Void = set_printer( 'ADDTABLE', NewColSize, '', ColData, '', '', 0, TB_ALL:@fm:'L' )
*ColData = ''
*for i = 1 to PointsPlotted
*   ColData<1,-1> = PreS<i>
*   ColData<1,-1> = PostS<i>
*   ColData<1,-1> = DeltaS<i>
*next i
*ColData = '':@vm:ColData
*Void = set_printer( 'ADDTABLE', NewColSize, '', ColData, '', '', 1, TB_ALL:@fm:'L' )
*
Void = set_printer( 'TEXT', '' )
ColSize  = '^+1987'
NewColSize = ''
for i = 1 to PointsPlotted
   NewColSize<1,-1> = ColSize
next i
NewColSize = '^+1080':@vm:NewColSize

NewColData = ''
NewColData<-1> = oconv( ThicknessS, 'MD3' )
NewColData<-1> = TestDateS
NewColData<-1> = TestTimeS
NewColData<-1> = ShiftS
NewColData<-1> = EntryIDS
NewColData = insert( NewColData, 1, 1, 0, 'Thk' )
NewColData = insert( NewColData, 2, 1, 0, 'Date' )
NewColData = insert( NewColData, 3, 1, 0, 'Time' )
NewColData = insert( NewColData, 4, 1, 0, 'Shift' )
NewColData = insert( NewColData, 5, 1, 0, 'User' )
 
Void = set_printer( 'ADDTABLE', NewColSize, '', NewColData, '', '', 1, TB_ALL:@fm:'L' )

TestDateS = ''
TestTimeS = ''
ShiftS    = ''
EntryIDS  = ''
ThicknessS = ''
PointsPlotted = 0
Font<2> = 12
Font<3> = 'L'
Stat = set_printer( 'FONT', Font )

if PrintPageBreak then
   Void = set_printer( 'PAGEBREAK' )
end
return
*===============================================================================================*

