#!/bin/csh
# File Name:    script
# File Purpose: This file is a c-shell script that is designed to perform a
#               feature higlight or selection routine according to a menu
#               selection made by the user.
# Written by:   Ian Ticehurst (Autom8 Ltd.)
# Version 1A.:  Date: 15.08.00 - Initial design and development.
# Version 1B.:  Date: 26.10.00 - Addition of $TMP definition and useage.
# Version 1C.:  Date: 05.12.00 - Remove "gui" alias.
############################# Start of notes ###############################
# This file is called from one place within the system. From the features filter
# pop-up, select the user filter button, when one of the user defined filters is
# selected, this script is called. It is passed one parameter, which is associated
# with the menu selection made. This association is controlled by another file that
# is in the hooks/ffilters, this file is called names. The names file is the file
# that creates the user defined filter menu. It is a simple format where the first
# word (or field) is the parameter name that is passed back to this script, and the
# rest of the line is the text string(s) that will form the menu. This when a menu
# selection is made, this script gets the first field of that line as parameter $1.
# This script is then able (using the case loop structure) to perform a series of
# operations dependant on the menu selection made.
# It can be seen that this script could perform any task it is programmed to do (in
# the default example script this is limited to highligt and selection functions)
# but it could also perfrom editing. However, it is suggested that any function
# programmed here should be kept within the context of the feature filter pop-up,
# reference selection, and other associated tools.
############################# 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 numeric variables to have text names.
set PARAM = `echo "$1"`

# Get first character of parameter (h = highlight, s = select).
set MODE = `echo "$PARAM" | cut -c 1`

# Set the no selection flag to run the selection function outside the loop (the default is 0 = run final selection).
set NO_SELECT = 0

# Reset features filter pop-up ready for the script selections.
COM filter_reset,filter_name=popup
# If mode is highlight clear any existing highlight, if mode is select clear any existing selections.
if ($MODE == "h") then
	COM clear_highlight
else
	COM sel_clear_feat
endif

# Set the filters dependant on the parameter chosen by the user.
switch ($PARAM) 
	case 'h_pads' :
		# Highlight all pads.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		breaksw

	case 'h_lines' :
		# Highlight all lines.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=line
		breaksw

   	case 'h_surf' :
		# Highlight all surfaces.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=surface
		breaksw
    
   	case 'h_neg' :
		# Highlight negative features.
		COM filter_set,filter_name=popup,update_popup=yes,polarity=negative
		breaksw

	case 'h_rect' :
		# Highlight all rectangles.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_set,filter_name=popup,update_popup=yes,include_syms=rect*
      	breaksw

	case 's_rect' :
		# Select all rectangles.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_set,filter_name=popup,update_popup=yes,include_syms=rect*
      	breaksw

	case 'h_smd' :
		# Highlight smd pads.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.smd
      	breaksw

	case 's_smd' :
		# Select smd pads.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.smd
      	breaksw

	case 'h_gold' :
		# Highlight gold connectors.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.gold_plating
      	breaksw

	case 's_gold' :
		# Select gold connectors.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.gold_plating
      	breaksw

	case 'h_tear_drop' :
		# Highlight tear drops.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.tear_drop
      	breaksw

	case 'h_nele' :
		# Highlight non-electric features.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.n_electric
      	breaksw

	case 'h_text' :
		# Highlight nomenclature.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.nomenclature
      	breaksw

	case 's_text' :
		# Select nomenclature.
		COM filter_set,filter_name=popup,update_popup=yes
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.nomenclature
      	breaksw

	case 'h_npth' :
		# Highlight non-pth holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=non_plated
      	breaksw

	case 's_npth' :
		# Select non-pth holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=non_plated
      	breaksw

	case 's_npth_pads' :
		# Select pads touching non-pth holes on all signal layers.

		# Ensure no layers are visible or selected.
		COM clear_layers
		COM affected_layer,name=,mode=all,affected=no

		# Affect all board drill layers.
		COM affected_filter,filter=(type=drill&context=board&side=top|bottom|inner|none&pol=positive)
		
		# Select all pads with the attribute .drill=non_plated.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=non_plated
		COM filter_area_strt
		COM filter_area_end,layer=,filter_name=popup,operation=select,area_type=none,inside_area=no,\
		intersect_area=no,lines_only=no,ovals_only=no,min_len=0,max_len=0,min_angle=0,max_angle=0
		
		# Check to ensure that some features have been selected.
		COM get_select_count
		if ($COMANS != 0) then
			# Add all board signal layers to the affect layers.
			COM affected_filter,filter=(type=signal|drill&context=board&side=top|bottom|inner|none&pol=positive)
		
			# Reset the features filter pop-up, and set the filter to be pads only.
			COM filter_reset,filter_name=popup
			COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad

			# Select all pads on affected layers which touch current selection (thus touching plated holes).
			COM sel_ref_feat,layers=drill,use=select,mode=touch,f_types=line\;pad\;surface\;arc\;text,\
			polarity=positive\;negative,include_syms=,exclude_syms=

			# Unaffect the drill layers, thus leaving only signal layers with selection.
			COM affected_filter,filter=(type=signal&context=board&side=top|bottom|inner&pol=positive)
			COM filter_reset,filter_name=popup
		else
			# No features available to use as reference selection. Warn user.
			echo "WIN 200 200" >> $GUI_DATA
			echo "FONT tbr14" >> $GUI_DATA
			echo "BG 992222" >> $GUI_DATA
			echo "FG 999999" >> $GUI_DATA
			echo "LABEL User Defined Selection Failure Pop-up" >> $GUI_DATA
			echo "LABEL WARNING - Cannot complete required selection." >> $GUI_DATA
			echo "LABEL There are no reference features selected." >> $GUI_DATA
			echo "END" >> $GUI_DATA

			gui $GUI_DATA
			\rm $GUI_DATA
		endif

		# Set the no selection flag to skip the selection function outside the loop.
		set NO_SELECT = 1
		breaksw

	case 'h_pth' :
		# Highlight pth holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=plated
      	breaksw

	case 's_pth' :
		# Select pth holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=plated
      	breaksw

	case 's_pth_pads' :
		# Select pads touching pth holes on all signal layers.

		# Ensure no layers are visible or selected.
		COM clear_layers
		COM affected_layer,name=,mode=all,affected=no

		# Affect all board drill layers.
		COM affected_filter,filter=(type=drill&context=board&side=top|bottom|inner|none&pol=positive)
		
		# Select all pads with the attribute .drill=plated.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=plated
		COM filter_area_strt
		COM filter_area_end,layer=,filter_name=popup,operation=select,area_type=none,inside_area=no,\
		intersect_area=no,lines_only=no,ovals_only=no,min_len=0,max_len=0,min_angle=0,max_angle=0
		
		# Check to ensure that some features have been selected.
		COM get_select_count
		if ($COMANS != 0) then
			# Add all board signal layers to the affect layers.
			COM affected_filter,filter=(type=signal|drill&context=board&side=top|bottom|inner|none&pol=positive)
		
			# Reset the features filter pop-up, and set the filter to be pads only.
			COM filter_reset,filter_name=popup
			COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad

			# Select all pads on affected layers which touch current selection (thus touching plated holes).
			COM sel_ref_feat,layers=drill,use=select,mode=touch,f_types=line\;pad\;surface\;arc\;text,\
			polarity=positive\;negative,include_syms=,exclude_syms=

			# Unaffect the drill layers, thus leaving only signal layers with selection.
			COM affected_filter,filter=(type=signal&context=board&side=top|bottom|inner&pol=positive)
			COM filter_reset,filter_name=popup
		else
			# No features available to use as reference selection. Warn user.
			echo "WIN 200 200" >> $GUI_DATA
			echo "FONT tbr14" >> $GUI_DATA
			echo "BG 992222" >> $GUI_DATA
			echo "FG 999999" >> $GUI_DATA
			echo "LABEL User Defined Selection Failure Pop-up" >> $GUI_DATA
			echo "LABEL WARNING - Cannot complete required selection." >> $GUI_DATA
			echo "LABEL There are no reference features selected." >> $GUI_DATA
			echo "END" >> $GUI_DATA

			gui $GUI_DATA
			\rm $GUI_DATA
		endif

		# Set the no selection flag to skip the selection function outside the loop.
		set NO_SELECT = 1
		breaksw

	case 'h_via' :
		# Highlight via holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=via
		breaksw

	case 's_via' :
		# Select via holes.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=via
      	breaksw

	case 's_via_pads' :
		# Select pads touching via holes on all signal layers.

		# Ensure no layers are visible or selected.
		COM clear_layers
		COM affected_layer,name=,mode=all,affected=no

		# Affect all board drill layers.
		COM affected_filter,filter=(type=drill&context=board&side=top|bottom|inner|none&pol=positive)
		
		# Select all pads with the attribute .drill=via.
		COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad
		COM filter_atr_set,filter_name=popup,condition=yes,attribute=.drill,option=via
		COM filter_area_strt
		COM filter_area_end,layer=,filter_name=popup,operation=select,area_type=none,inside_area=no,\
		intersect_area=no,lines_only=no,ovals_only=no,min_len=0,max_len=0,min_angle=0,max_angle=0
		
		# Check to ensure that some features have been selected.
		COM get_select_count
		if ($COMANS != 0) then
			# Add all board signal layers to the affect layers.
			COM affected_filter,filter=(type=signal|drill&context=board&side=top|bottom|inner|none&pol=positive)
		
			# Reset the features filter pop-up, and set the filter to be pads only.
			COM filter_reset,filter_name=popup
			COM filter_set,filter_name=popup,update_popup=yes,feat_types=pad

			# Select all pads on affected layers which touch current selection (thus touching plated holes).
			COM sel_ref_feat,layers=drill,use=select,mode=touch,f_types=line\;pad\;surface\;arc\;text,\
			polarity=positive\;negative,include_syms=,exclude_syms=

			# Unaffect the drill layers, thus leaving only signal layers with selection.
			COM affected_filter,filter=(type=signal&context=board&side=top|bottom|inner&pol=positive)
			COM filter_reset,filter_name=popup
		else
			# No features available to use as reference selection. Warn user.
			echo "WIN 200 200" >> $GUI_DATA
			echo "FONT tbr14" >> $GUI_DATA
			echo "BG 992222" >> $GUI_DATA
			echo "FG 999999" >> $GUI_DATA
			echo "LABEL User Defined Selection Failure Pop-up" >> $GUI_DATA
			echo "LABEL WARNING - Cannot complete required selection." >> $GUI_DATA
			echo "LABEL There are no reference features selected." >> $GUI_DATA
			echo "END" >> $GUI_DATA

			gui $GUI_DATA
			\rm $GUI_DATA
		endif

		# Set the no selection flag to skip the selection function outside the loop.
		set NO_SELECT = 1
		breaksw           
endsw

# If mode is highlight use highlight, if mode is select make selection.
if ($MODE == "h") then
	COM filter_highlight
else
	# Check to see if selection is already done is subroutine above ($NO_SELECT = 1).
	if ($NO_SELECT != 1) then
		# Selection is not already done. Run selection now.
		COM filter_area_strt
		COM filter_area_end,layer=,filter_name=popup,operation=select,area_type=none,inside_area=no,\
		intersect_area=no,lines_only=no,ovals_only=no,min_len=0,max_len=0,min_angle=0,max_angle=0
	endif
endif

exit
