# File Name:    set_table
# File Purpose: This file is called during the ARM process. It is a c-shell script
#               which is used to manipulate and set the rout size, drill/rout mode,
#               spindle speed and feed rate and the order dependant on the tool
#               parameters.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 30.10.00 - Initial design and development.
# Version 1B.:  Date: 05.12.00 - Removal of "gui" alias.
# Version 1C.:  Date: 16.01.01 - Add check for NT shell version. Also modify gawk calls dependant on version.
# Version 1D.:  Date: 20.02.01 - Ensure that all "gawk" calls are modified to "awk" and that "awk95" is aliased.
############################# Start of notes ###############################
# This file is located in the hooks/ncr directory and forms part of the Auto Rout
# Manager file set. It is a c-shell script which can be configured to perform changes
# to the ARM tools table (and thus the final output files) dependant on the different
# parameters allocated to each tool (size, hole/chain, step, etc.). 
# This script is started with two parameters passed to it. These parameters are a file
# which can be sourced ($1), and the name of an output file which the system will read
# when this program exits ($2). The source file contains details of the tools table and
# the ARM parameters and this is the information that can be used to define any actions
# required. The file contains the following formatted information:-
#
#set ncLAYER = (01-drl )
#set ncSET = (1 )
#set ncMACHINE = (m1 )
#set ncTHICKNESS = (0 )
#set ncTYPE         = (chain    chain    hole    hole    hole   )
#set ncSTEP         = (pcb      pcb      panel   panel   pcb    )
#set ncCHAIN        = (1        2        -1      -1      -1     )
#set ncSIZE         = (0.094488 0.094488 0.120   0.145   0.155  )
#set ncCOMP         = (right    left     none    none    none   )
#set ncPATH         = (2000     3000     0       0       0      )
#set ncCOUNT        = (0        0        20      5       15     )
#set ncFLAG         = (1        2        0       0       0      )
#set ncCW           = (1        0        0       0       0      )
#set ncFEED         = (30       60       0       0       0      )
#set ncSPEED        = (300      300      0       0       0      )
#
# The typical method of using this file is to work through the array information and
# make decisions such as tool order dependant on the tool attributes. The way that changes
# are made is to define an output file (the file name provided as $2) which should contain
# a line of parameters for each tool entry line. 
# Each output line must contain the following values:-
#
#  1. original index in array (starting with 1)
#  2. tool size (mils)
#  3. spiral mode (none,sw,machine) *** Note that this option is not currently available.
#  4. hole mode (rout,drill)
#  5. optional (y/n)
#  6. duplicate (y/n)
#  7. cutter compensation factor
#  8. spindle speed
#  9. feed rate
# 10. chain grouping (new/same)
# 11. process order
#
# This example program has been written to provide control over most of the functionality
# through the use of variables at the start of the program. Most site specific configurations
# should be achievable by setting these variables. This includes the following functions:-
#
# 1. Mark holes as optional (on/off).
# 2. Duplicate rout chains dependant on cutter size and step (turn on/off).
# 3. Ensure tool sizes are true metric (on/off).
# 4. Small (fixed) size adjustment for drill sizes.
# 5. Small (fixed) size adjustment for cutter compensation.
# 6. Drill in rout stage or move to extra drill layer.
# 7. Program abort if drill or rout details fall outside defined limits.
# 8. Default settings for spindle speed and feed rate.
############################# End of notes ################################ 

# Section to set up general parameters and variables.
# ---------------------------------------------------
# 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 the INFO alias and the tmp INFO file.
set INFO = $TMP/info
alias DO_INFO 'COM info,out_file=$INFO,write_mode=replace,args=\!:*;source $INFO;rm $INFO'

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

# 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

# Detect current tcsh version (old = 0, new = 1).
if ($CURR_OS == "WINDOWS_NT") then
	set NT_VER = `echo "$version" | awk -F'-' '{if ($7 < 5.0) print 0; else print 1}'`
endif

# Set variable names for parameter file to source and out_file to write to.
set source_script = $1
set out_file      = $2

# Force units to inch.
COM ncrset_units,units=inch

# Source standard parameter file.
source $source_script

# Check the machine file to ensure that imperial units are being used
# *** Note that these example hooks currently only support mils.
if ($CURR_OS == "WINDOWS_NT") then
	set HK_UNITS = `grep "^hook_units" $GENESIS_DIR/sys/hooks/ncr/config/machines/$ncMACHINE | awk -F' ' '{print $3}'`
else
	set HK_UNITS = `grep "^hook_units" $GENESIS_DIR/sys/hooks/ncr/config/machines/$ncMACHINE | awk -F' ' '{print $3}'`
endif	

if ($HK_UNITS != "inch") then
	# Hook units are not in inch. Warn user to change machine file (or rewrite hook) and exit.
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr18" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL ARM Hook Units Warning Pop-up." >> $GUI_DATA
	echo "LABEL WARNING - The hook_units in machine file $ncMACHINE is set to metric." >> $GUI_DATA
	echo "LABEL These example hooks currently only support mils.">> $GUI_DATA
	echo "LABEL The following actions are possible:-" >> $GUI_DATA
	echo "LABEL 1. Change the line in the machine file to read hook_units = inch" >> $GUI_DATA
	echo "LABEL 2. Modify the set_table hook and change all parameter values to be metric" >> $GUI_DATA
	echo "END" >> $GUI_DATA
	
	gui $GUI_DATA
	\rm $GUI_DATA
	goto abort
endif

# Section to set up operational variables.
# ----------------------------------------
# (NOTE - Where values below are used as tool sizes - the values are always in mils.)
# -----------------------------------------------------------------------------------
# Create output files with tool sizes based on metric tools (0=yes, 1=no).
set OUTL_MM = 0

# Define minimum drill size to include in rout layer.
set MIN_DRL = 0
# Define maximum drill size to include in rout layer.
set MAX_DRL = 999

# Action to take if drill size is too small (1=move to extra drill layer, 2=warn user and exit, 3=ignore).
set MIN_DRL_ACT = 1
# Action to take if drill size is too big (1=move to extra drill layer, 2=warn user and exit, 3=ignore).
set MAX_DRL_ACT = 1

# Define minimum drill size to mark for optional drilling.
set MIN_OPT_DRL = 999
# Define maximum drill size to mark for optional drilling.
set MAX_OPT_DRL = 999

# Size modification to be applied to all drill holes (eg. 1, -1 etc. - size in mils).
set DRL_SIZE_MOD = 0

# Define minimum rout cutter size to include in rout layer.
set MIN_RT = 19
# Define maximum rout cutter size to include in rout layer.
set MAX_RT = 130

# Action to take if rout cutter is too small (1=warn user and exit, 2=ignore).
set MIN_RT_ACT = 1
# Action to take if rout cutter is too big (1=warn user and exit, 2=ignore).
set MAX_RT_ACT = 1

# Define minimum cutter size to mark for duplicate routing (cutting path twice).
set MIN_DUP_RT = 0
# Define maximum cutter size to mark for duplicate routing (cutting path twice).
set MAX_DUP_RT = 45

# Size modification to be applied to all cutter compensations (eg. 1, -1 etc. - size in mils).
set COMP_SIZE_MOD = 0
# Match tool size with cutter compensation (1=yes, 2=no).
set LOCK_TL2CMP = 1

# Define if chains in same step and with same size should be grouped together (0=yes, 1=no).
set GROUP_CHN = 0

# Define if system should show user a warning if drill holes are moved to extra drill layer (0=no warning, 1=show warning).
set MV2DL_WARN = 1
# Define if system should show user a warning if program aborts due to being outside set limits (0=no warning, 1=show warning).
set SHOW_EXIT_WARN = 1

# Set default spindle speed, feed rate for drill holes and chains.
set DRL_SPIN = 10000
set DRL_FEED = 300
set SPIN = 6000
set FEED = 100

# Section to define actions dependant on tool information.
# --------------------------------------------------------
# Set drill warning flag to no ("0") as default.
set DRL_WARN = 0
# Set exit warning flag to no ("0") as default.
set EXIT_WARN = 0

# Work through all the tools, process all holes first.
set COUNT = 1
set ORD = 1
foreach TOOL ($ncTYPE)
	# Set hole size to be nearest integer (to avoid if loop crashing).
	if ($CURR_OS == "WINDOWS_NT") then
		if ($NT_VER == 0) then
			set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf(\"%4.0f\",($1*1000))}'`
		else
			set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf("%4.0f",($1*1000))}'`
		endif
	else
		set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf("%4.0f",($1*1000))}'`
	endif

	# Switch null tool output line off (default).
	set NULL_LINE = 0

	# Check to see if hole is too big or too small for rout layer.
	if ($TOOL == "hole") then
		if ($SIZE < $MIN_DRL) then
			if ($MIN_DRL_ACT == 1) then
				set H_MODE = `echo "drill"`
				echo "LABEL Tool designator number $COUNT - Drill size $ncSIZE[$COUNT] is moved to new drill layer" >> $TMP/drill_warn.$$
				set DRL_WARN = 1
			else if ($MIN_DRL_ACT == 2) then
				echo "LABEL Tool designator number $COUNT - Drill size $ncSIZE[$COUNT] too small for rout layer" >> $TMP/exit_warn.$$
				set EXIT_WARN = 1
				set NULL_LINE = 1
			else if ($MIN_DRL_ACT == 3) then
				set H_MODE = `echo "rout"`
			endif
		else if ($SIZE > $MAX_DRL) then
			if ($MAX_DRL_ACT == 1) then
				set H_MODE = `echo "drill"`
				echo "LABEL Tool designator number $COUNT - Drill size $ncSIZE[$COUNT] is moved to new drill layer" >> $TMP/drill_warn.$$
				set DRL_WARN = 1
			else if ($MAX_DRL_ACT == 2) then
				echo "LABEL Tool designator number $COUNT - Drill size $ncSIZE[$COUNT] too big for rout layer" >> $TMP/exit_warn.$$
				set EXIT_WARN = 1
				set NULL_LINE = 1
			else if ($MAX_DRL_ACT == 3) then
				set H_MODE = `echo "rout"`
			endif
		else
			set H_MODE = `echo "rout"`
		endif

		# Check to see if hole should be marked for optional drilling.
		if ($SIZE < $MIN_OPT_DRL) then
			set OPT = `echo "n"`
		else if ($SIZE > $MAX_OPT_DRL) then
			set OPT = `echo "n"`
		else
			set OPT = `echo "y"`
		endif

		# Add any additional size modification to the drill size.
		if ($CURR_OS == "WINDOWS_NT") then
			set TOOL_SIZE = `echo "$ncSIZE[$COUNT] $DRL_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
		else
			set TOOL_SIZE = `echo "$ncSIZE[$COUNT] $DRL_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
		endif

		# Calculate tool size based on metric conversion.
		if ($OUTL_MM == 0) then
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf(\"%4.2f\",$1)}'`
				else
					set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				endif
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 / 25.4)}'`
			else
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 / 25.4)}'`
			endif
		endif

		# Add line to output file (to redefine tool table settings).
		if ($NULL_LINE == 1) then
			# Program is configured to abort due to tool sizes / types. Add null line to drill table.
			echo "$COUNT 1 none rout n n 1 1 1 new $ORD" >> $out_file
		else
			echo "$COUNT $TOOL_SIZE none $H_MODE $OPT n 0 $DRL_SPIN $DRL_FEED new $ORD" >> $out_file
		endif

		@ ORD ++
	endif
	@ COUNT ++
end
	
# Work through all the tools, process all chains.
set COUNT = 1
set CHAIN_COUNT = 1
foreach TOOL ($ncTYPE)
	# Set hole size to be nearest integer (to avoid if loop crashing).
	if ($CURR_OS == "WINDOWS_NT") then
		if ($NT_VER == 0) then
			set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf(\"%4.0f\",($1*1000))}'`
		else
			set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf("%4.0f",($1*1000))}'`
		endif
	else
		set SIZE = `echo "$ncSIZE[$COUNT]" | awk -F' ' '{printf("%4.0f",($1*1000))}'`
	endif

	# Switch null tool output line off (default).
	set NULL_LINE = 0
	# Switch duplication of chains off (default).
	set DUP = 0

	# Check to see if cutter is too big or too small for rout layer.
	if ($TOOL == "chain") then
		if ($SIZE < $MIN_RT) then
			if ($MIN_RT_ACT == 1) then
				echo "LABEL Tool designator number $COUNT - Cutter size $ncSIZE[$COUNT] too small for rout layer" >> $TMP/exit_warn.$$
				set EXIT_WARN = 1
				set NULL_LINE = 1
			else if ($MIN_RT_ACT == 2) then
				# Configuration variable above is set to ignore rout cutter too small.
			endif
		else if ($SIZE > $MAX_DRL) then
			if ($MAX_RT_ACT == 1) then
				echo "LABEL Tool designator number $COUNT - Cutter size $ncSIZE[$COUNT] too big for rout layer" >> $TMP/exit_warn.$$
				set EXIT_WARN = 1
				set NULL_LINE = 1
			else if ($MAX_DRL_ACT == 3) then
				# Configuration variable above is set to ignore rout cutter too big.
			endif
		endif

		# Check to see if chain should be duplicated.
		if ($SIZE < $MIN_DUP_RT) then
			set DUP = 0
		else if ($SIZE > $MAX_DUP_RT) then
			set DUP = 0
		else
			set DUP = 1
		endif

		# Add any additional size modification to the cutter compensation.
		if ($CURR_OS == "WINDOWS_NT") then
			# If required, calculate cutter compensation based on metric conversion.
			if ($OUTL_MM == 0) then
				set COMP = `echo "$ncSIZE[$COUNT] $COMP_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
				if ($NT_VER == 0) then
					set COMP = `echo "$COMP" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf(\"%4.2f\",$1)}'`
				else
					set COMP = `echo "$COMP" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				endif
				set COMP = `echo "$COMP" | awk -F' ' '{print ($1 / 25.4)}'`
			else
				set COMP = `echo "$ncSIZE[$COUNT] $COMP_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
			endif
		else
			# If required, calculate cutter compensation based on metric conversion.
			if ($OUTL_MM == 0) then
				set COMP = `echo "$ncSIZE[$COUNT] $COMP_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
				set COMP = `echo "$COMP" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				set COMP = `echo "$COMP" | awk -F' ' '{print ($1 / 25.4)}'`
			else
				set COMP = `echo "$ncSIZE[$COUNT] $COMP_SIZE_MOD" | awk -F' ' '{print ($1 + ($2 / 1000))}'`
			endif
		endif

		# If required, set the tool size to be the same as the cutter compensation.
		if ($LOCK_TL2CMP == 1) then
			set TOOL_SIZE = `echo "$COMP"`
		else
			set TOOL_SIZE = `echo "$ncSIZE[$COUNT]"`
		endif

		# Calculate tool size based on metric conversion.
		if ($OUTL_MM == 0) then
			if ($CURR_OS == "WINDOWS_NT") then
				if ($NT_VER == 0) then
					set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf(\"%4.2f\",$1)}'`
				else
					set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				endif
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 / 25.4)}'`
			else
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 * 25.4)}' | awk -F' ' '{printf("%4.2f",$1)}'`
				set TOOL_SIZE = `echo "$TOOL_SIZE" | awk -F' ' '{print ($1 / 25.4)}'`
			endif
		else
			set TOOL_SIZE = `echo "$TOOL_SIZE"`
		endif

   		# Check to see if this chain should be grouped together with the previous chain (same size and step).
		# Set default grouping as no ("new").
		set GROUP = `echo "new"`
		if ($GROUP_CHN == 0) then
   			if ($CHAIN_COUNT > 1) then
				if ($SIZE == $PREVIOUS_SIZE && $ncSTEP[$COUNT] == $PREVIOUS_STEP) then
					set GROUP = `echo "same"`
				endif
			endif
			set PREVIOUS_SIZE = `echo "$SIZE"`
			set PREVIOUS_STEP = `echo "$ncSTEP[$COUNT]"`
		endif

		# Add line to output file (to redefine tool table settings).
		if ($NULL_LINE == 1) then
			# Program is configured to abort due to tool sizes / types. Add null line to drill table.
			echo "$COUNT 1 none rout n n 1 1 1 new $ORD" >> $out_file
		else
			echo "$COUNT $TOOL_SIZE none rout n n $COMP $SPIN $FEED $GROUP $ORD" >> $out_file
		endif

		@ ORD ++

		# If duplication is detected add additional change and increment the order count.
		if ($DUP == 1) then
			echo "$COUNT $TOOL_SIZE none rout n y $COMP $SPIN $FEED same $ORD" >> $out_file
			@ ORD ++
		endif

		@ CHAIN_COUNT ++
	endif
	@ COUNT ++
end
				
# If exit warning is set put window on screen to advise user.
if ($EXIT_WARN == 1 && $SHOW_EXIT_WARN == 1) then
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr18" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL ARM Tool Table Hook Pop-up." >> $GUI_DATA
	echo "LABEL WARNING - Hook settings have detected a situation where the script aborts." >> $GUI_DATA
	echo "LABEL The details are as follows:-" >> $GUI_DATA
	echo "FONT tbi18" >> $GUI_DATA
	echo "BG 336644" >> $GUI_DATA
	cat $TMP/exit_warn.$$ >> $GUI_DATA
	echo "FONT tbr18" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA	
	echo "END" >> $GUI_DATA
	
	gui $GUI_DATA > $GUI_RESP
	source $GUI_RESP; \rm $GUI_RESP; \rm $GUI_DATA
endif

# If drill warning is set put window on screen to advise user.
if ($DRL_WARN == 1 && $MV2DL_WARN == 1) then
	echo "WIN 200 200" >> $GUI_DATA
	echo "FONT tbr18" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA
	echo "FG 999999" >> $GUI_DATA
	echo "LABEL ARM Tool Table Hook Pop-up." >> $GUI_DATA
	echo "LABEL WARNING - Some features will be moved to an additional drill layer." >> $GUI_DATA
	echo "LABEL These items will be COPIED to the layer rt2drl...">> $GUI_DATA
	echo "LABEL The details are as follows:-" >> $GUI_DATA
	echo "FONT tbi18" >> $GUI_DATA
	echo "BG 336644" >> $GUI_DATA
	cat $TMP/drill_warn.$$ >> $GUI_DATA
	echo "FONT tbr18" >> $GUI_DATA
	echo "BG 992222" >> $GUI_DATA	
	echo "END" >> $GUI_DATA
	
	gui $GUI_DATA > $GUI_RESP
	source $GUI_RESP; \rm $GUI_RESP; \rm $GUI_DATA
endif

abort:
# Clean up temp. files if they exist.
if (-e $TMP/exit_warn.$$) then
	\rm $TMP/exit_warn.$$
endif
if (-e $TMP/drill_warn.$$) then
	\rm $TMP/drill_warn.$$
endif

exit






