#!/bin/csh
# File Name:    drill_size
# File Purpose: This file is a c-shell script that is called by the drill tool
#               manager function. It is called once for every drill size and
#               is used to calculate the actual drill size and to set the drill
#               bit designator. The script is passed a set of parameters that
#               can be used in this calculation. The script can be written in
#               other script languages (certainly perl) - see KIT 4578.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 14.08.00 - Initial design and development.
# Version 1B.:  Date: 26.10.00 - Addition of $TMP definition and useage.
# Version 1C.:  Date: 05.12.00 - Removal of "gui" alias.
# Version 1D.:  Date: 20.02.01 - Ensure that all "gawk" calls are modified to "awk" and that "awk95" is aliased.
############################# Start of notes ###############################
# This script is called by the drill tool manager every time a drill size needs
# to be changed. This can be globally after selecting the user parameters (see
# drill_size.prm) or the calc drills function within the D.T.M. window, or when
# a change is made to the finished hole size or the tolerance values of one drill
# size. Note that this script always works in mils.
# In order to make the calulation the script is called with a series of parameters.
# These parameters are:-
#  $1  - board thickness (as defined in the D.T.M. window)
#  $2  - finished hole size
#  $3  - hole type (VIA,PLATE,NPLATE)
#  $4  - min_tol
#  $5  - max_tol
#  $6  - user parameter string
#  $7  - output file pathname (which the system reads the results from)
#  $8  - tool number
#  $9  - drill bit designator
#  $10 - type2 (STD,LASER,PHOTO,PRESSFIT)
#  $11 - shape (HOLE,SLOT)
#  $12 - slot length
#
# By using a combination of these parameters, it is possible to calculate the drill
# size and return these (via the output file) to the D.T.M. In the default version of
# the script (see control variables below), the system takes the nominal size and passes
# it through an external drill table with another program. This returns the actual drill
# size that will be used, selected from the table of available drill bits. The system
# can have two values returned, the numeric drill size and the drill bit designator.
# The drill designator is a text string only, typically used for the metric bit size,
# but it can be used for any purpose. It should be noted however that this designator
# is applied to the drill features as an attribute (.bit) and it should be remembered
# that these attributes will get passed on to auto drill and auto rout manager hooks.
# Two variables have been included in this program that provide a little extra control.
# If the variable NO_DRILL_TABLE is set to yes (default is no) the script will return
# only the nominal size without looking up the external drill table.
# If the variable PROPER_METRIC is set to yes (default is no) the script performs a final
# calculation in order to ensure that when viewing the D.T.M. in metric, true metric sizes
# are shown in the drill size (instead of 2.9999).
############################# End of notes ###############################
# Define correct temp. dir. depending on env. vars.
if ($?GENESIS_TMP) then
	set TMP = $GENESIS_TMP
else if ($?GENESIS_DIR) then
	set TMP = $GENESIS_DIR/tmp
else
	set TMP = /genesis/tmp
endif

# Set up gui data and response files
set GUI_DATA = $TMP/gui_data.$$
set GUI_RESP = $TMP/gui_resp.$$

# Set varible to use to point to drill size selection program and table (typically $GENESIS_DIR/sys/hooks).
set DTM_PATH = `echo "$GENESIS_DIR/sys/hooks"`

# Set control variable to select use of external drill table.
set NO_DRILL_TABLE = `echo "no"`
# Set control variable to select use of correct metric return.
set PROPER_METRIC = `echo "no"`

# Detect current operating system.
set CURR_OS = `uname -s`

# If current operating system is NT, set alias to call awk95 as "awk".
if ($CURR_OS == "WINDOWS_NT") then
	alias awk '$GENESIS_DIR/e${GENESIS_VER}/nt/bin/awk95'
endif

# Set numeric variables to have text names.
set BD_THK = `echo "$1"`
set FIN_SZ = `echo "$2"`
set H_TYPE = `echo "$3"`
set MN_TOL = `echo "$4"`
set MX_TOL = `echo "$5"`
set USER_P = `echo "$6"`
set OUTPUT = `echo "$7"`
set TL_NUM = `echo "$8"`
set DRL_DS = `echo "$9"`
set TYPE_2 = `echo "$10"`
set SHAPE  = `echo "$11"`
set SL_LEN = `echo "$12"`

# Check to ensure that user parameters have been selected. If not, exit program.
if ($USER_P == "" || $USER_P == "empty_user_params") then
	echo WIN 200 200 >> $GUI_DATA
	echo FONT tbi18 >> $GUI_DATA
	echo BG 992222 >> $GUI_DATA
	echo FG 999999 >> $GUI_DATA
	echo LABEL Drill Tool Manager - WARNING >> $GUI_DATA
	echo LABEL You Must Select A User Param. >> $GUI_DATA
	echo LABEL This program will now abort. >> $GUI_DATA
	echo END >> $GUI_DATA

	gui $GUI_DATA
	\rm $GUI_DATA
	
	goto abort
endif

# Set the basic enlargement factor based on the user parameter selected.
if ($USER_P == "hasl") then
	# User selected hot air solder level finish - nominal enlargement set to 6 mils. (0.15mm).
	set ENLARGE = 6
else if ($USER_P == "imm_au") then
	# User selected immersion gold finish - nominal enlargement set to 4 mils. (0.10mm).
	set ENLARGE = 4
else if ($USER_P == "press_fit") then
	# User selected press fit finish - nominal enlargement set to 4 mils. (0.10mm).
	set ENLARGE = 4
endif

# Set default value to hole to indicate non-press fit (0).
set PRS_FIT = 0

# Nominal size for non-plated holes (finish size, plus mid-point of tolerance).
if ($H_TYPE == "NPLATE") then
	set SIZE = `echo "scale = 1 ; $FIN_SZ + (($MX_TOL - $MN_TOL)/2)" | bc -l`
else
	# Check to see if user is working with press fit hole board.
	if ($USER_P == "press_fit") then
		if ($H_TYPE == "PLATE") then
			# Working on press fit board - found plated (component) hole.
			if ($DRL_DS == "pf" || $DRL_DS == "PF") then
				# User has set "pf" or "PF" in designator column. Force hole to be press fit.
				set PRS_FIT = `echo "1"`
			else
				# Ask user to confirm if this component hole is pressfit.
				echo WIN 200 200 >> $GUI_DATA
				echo FONT tbr18 >> $GUI_DATA
				echo BG 225080 >> $GUI_DATA
				echo FG 999999 >> $GUI_DATA
				echo LABEL Press Fit Hole selection Pop-up. >> $GUI_DATA
				echo LABEL Working on hole size $FIN_SZ. >> $GUI_DATA
				echo LABEL Select press fit options. >> $GUI_DATA
				echo RADIO PRS_FIT Options: V 1 992222 >> $GUI_DATA
				echo Hole is press fit >> $GUI_DATA
				echo Hole is NOT press fit >> $GUI_DATA
				echo END >> $GUI_DATA
				echo FONT tbi14 >> $GUI_DATA
				echo BG 883333 >> $GUI_DATA
				echo LABEL Note: By adding pf in the designator column this script>> $GUI_DATA
				echo LABEL can automatically detect press fit holes >> $GUI_DATA
				echo BG 225080 >> $GUI_DATA
				echo END >> $GUI_DATA

				gui $GUI_DATA > $GUI_RESP
				source $GUI_RESP
				\rm $GUI_DATA $GUI_RESP
			endif

			if ($PRS_FIT == 1) then
				# Nominal size for press fit plated holes (finish size, plus enlarge factor, minus minimum tolerance (bottom limit)).
				set SIZE = `echo "scale = 1 ; $FIN_SZ + $ENLARGE - $MN_TOL" | bc -l`
			else
				# Nominal size for non-press fit plated holes (finish size, plus enlarge factor, plus mid-point of tolerance (mid. limit)).
				set SIZE = `echo "scale = 1 ; $FIN_SZ + $ENLARGE + (($MX_TOL - $MN_TOL)/2)" | bc -l`
			endif
		else
			# Nominal size for via holes in press fit board (finish size, plus enlarge factor, plus mid-point of tolerance (mid. limit)).
			set SIZE = `echo "scale = 1 ; $FIN_SZ + $ENLARGE + (($MX_TOL - $MN_TOL)/2)" | bc -l`
		endif
	else
		# Nominal size for plated holes (incl. vias) in non-press fit board (finish size, plus enlarge factor, plus mid-point of tolerance (mid. limit)).
		set SIZE = `echo "scale = 1 ; $FIN_SZ + $ENLARGE + (($MX_TOL - $MN_TOL)/2)" | bc -l`
	endif
endif

# If drill table is not to be used (see control variable at start).
if ($NO_DRILL_TABLE == "yes") then
	if ($PRS_FIT == 1 && $USER_P == "press_fit") then
		echo "$SIZE pf" > $OUTPUT
	else if ($PRS_FIT != 1 && $USER_P == "press_fit") then
		echo "$SIZE no_pf" > $OUTPUT
	else
		echo "$SIZE" > $OUTPUT
	endif
	goto abort
endif

# Pass nominal size through drill_size.tab file to select actual drill bit.
# Note that the drill table selection program should two values. The first is the drill size in mils. the second is the bit size in mm.
# The lines below set $RESP with both values ("size bit"). The grep -v "#" is used to filter comment lines from the drill table file.
if ($CURR_OS == "SunOS") then
	# Running on a Sun machine. Use nawk to run selection program (drill_size.awk) on drill table (drill_size.tab).
	set RESP = `grep -v "#" $DTM_PATH/drill_size.tab | nawk -v DRILL=$SIZE -v PFT=$PRS_FIT -f $DTM_PATH/drill_size.awk`
else if ($CURR_OS == "HP-UX" || $CURR_OS == "Linux" || $CURR_OS == "AIX") then
	# Running on a HP machine. Use awk to run selection program (drill_size.awk) on drill table (drill_size.tab).
	set RESP = `grep -v "#" $DTM_PATH/drill_size.tab | awk -v DRILL=$SIZE -v PFT=$PRS_FIT -f $DTM_PATH/drill_size.awk`
else if ($CURR_OS == "WINDOWS_NT") then
	# Running on a NT machine. Use awk (aliased to awk95) to run selection program (drill_size.awk) on drill table (drill_size.tab).
	set RESP = `grep -v "#" $DTM_PATH/drill_size.tab | awk -v DRILL=$SIZE -v PFT=$PRS_FIT -f $DTM_PATH/drill_size.awk`
endif

# Check that response from drill table is valid.
set TST = `echo "$RESP" | awk -F' ' '{if ($2 == "small") print 1; else if ($2 == "big") print 2; else print 3}'`

# If hole sizes falls outside normal ranges, warn user.
if ($TST == 1 || $TST == 2) then
	echo WIN 200 200 >> $GUI_DATA
	echo FONT tbi18 >> $GUI_DATA
	echo BG 992222 >> $GUI_DATA
	echo FG 999999 >> $GUI_DATA
	echo LABEL Hole Size Problem Pop-up >> $GUI_DATA
	echo LABEL WARNING - Hole finished size is $FHS. >> $GUI_DATA
	echo LABEL Hole drill size is $RESP[1]. >> $GUI_DATA
	if ($TST == 1) then
		echo LABEL This hole is too small to drill. >> $GUI_DATA
		set WARN = `echo "2_small"`
	else
		echo LABEL This hole is too big to drill. >> $GUI_DATA
		set WARN = `echo "2_big"`
	endif
	echo LABEL Note that the hole will exist in the DTM as nominal size only. >> $GUI_DATA
	echo END >> $GUI_DATA

	gui $GUI_DATA
	\rm $GUI_DATA

	# Send nominal size and warning designator to DTM.
	echo "$SIZE $WARN"
else
	# If true metric needs to be displayed (see control variable at start).
	if ($PROPER_METRIC == "yes") then
		set DRL_SIZE = `echo "$RESP" | awk -F' ' '{print ($2 / .0254)}'
		set DRL_DESG = `echo "$RESP" | awk -F' ' '{print $2}'
		echo "$DRL_SIZE $DRL_DESG" > $OUTPUT
	else
		echo "$RESP" > $OUTPUT
	endif
endif

abort:

exit
