#!/bin/csh
# This is a program for the example Genesis Tooling Forms.
# This file will exist in form 2.
# The purpose of this program is to allow the user to run a panelisation routine.
# It is called by the "Run Panelisation" button.
# 
# Written by Ian Ticehurst (Autom8 Ltd.)
# Version 1A. Date: 12.09.00 - Initial design and development.
# Version 1B. Date: 11.12.00 - Add source of info. file to support gui creation.
# Version 1C. Date: 12.12.00 - Re-write the gui windows to use the sourced font and colours.
# Version 1D. Date: 05.02.01 - Modify to only check job path if job moved from original location.
#                            - Add save time update to forms.
# Version 1E. Date: 09.02.01 - Modify the setting and useage of $TMP.
# Version 1F. Date: 14.03.01 - Check how "$GENESIS_EDIR" is set to ensure paths are set correctly.
# Version 1G. Date: 02.04.01 - Allow for panel schemes in users home directory (.genesis/hooks etc.)
######################################################################### 

source $_genesis_root/sys/scripts/include_me

set_in_progress genform2 pan

# Close Form
COM close_form,job=$JOB,form=genform2

# Check info.source file to ensure that job can be panelized.
source $JOB_PATH/user/info.source
if ($PAN == 0 || $NET == 0) then
   # Errors exist and this job cannot continue until errors are fixed.
   o_tooling
   o FONT $NORM_FNT
   o BG $WARN_COL
   o BW 0
   o LABEL Panel Error Popup
   o LABEL Panelization cannot be run.
   o LABEL The analysis and/or the netlist check have not been approved.
   o LABEL Re-run the final analysis review function.
   o BG $NORM_COL
   o END

   do_gui

   # Open Form
   COM show_form,job=$JOB,form=genform2

   # Change colour of action button to show action failed.
   COM edit_form,job=$JOB,form=genform2,elem=pan,color=$FAILED_COLOR

   # Mark process as failed.
   COM edit_form,job=$JOB,form=genform2,elem=pan_dte,value=Review not done

   # Add line to log to confirm action status.
   log FAILED genform2.pan Panelization Not Done

   goto abort
endif

# Get a list of available panel schemes.
if (-e $HOME/.genesis/hooks/panel/scheme) then
	set PAN_SCH = `ls $HOME/.genesis/hooks/panel/scheme`
else
	set PAN_SCH = `ls $_genesis_root/sys/hooks/panel/scheme`
endif

# If no available schemes abort program.
if ($#PAN_SCH == 0) then
   # Warn user that no panel schemes exist.
   o_tooling
   o FONT $NORM_FNT
   o BG $WARN_COL
   o BW 0
   o LABEL Panel Error Popup
   o LABEL No panelization schemes exist.
   o LABEL Exiting program.
   o BG $NORM_COL
   o END

   do_gui

   # Open Form
   COM show_form,job=$JOB,form=genform2

   # Change colour of action button to show action failed.
   COM edit_form,job=$JOB,form=genform2,elem=pan,value=,color=$FAILED_COLOR

   # Mark process as failed.
   COM edit_form,job=$JOB,form=genform2,elem=pan_dte,value=No panel schemes

   # Add line to log to confirm action status.
   log FAILED genform2.pan No Panelization Schemes available

   goto abort
endif

# Get user to select panel scheme to use.
o_tooling
o FONT $NORM_FNT
o BG $NORM_COL
o BW 0
o LABEL Panel Scheme Selection Popup
o LABEL Select panelization scheme to use.
o BG $LSTBG_COL
o LIST schm 5 S.
foreach SCH ($PAN_SCH)
   o $SCH
end
o END
o BG $NORM_COL
o END

do_gui

COM open_auto_panelize,job=$JOB,panel=panel,pcb=pcb,scheme=$schm
COM autopan_place_pcbs,job=$JOB,panel=panel,pcb=pcb,scheme=$schm,mode=best
# COM autopan_apply_trans,job=$JOB,panel=panel,pcb=pcb,scheme=$schm
COM autopan_place_objects,job=$JOB,panel=panel,pcb=pcb,scheme=$schm
COM autopan_fill,job=$JOB,panel=panel,pcb=pcb,scheme=$schm

# Open Form

COM show_form,job=$JOB,form=genform2
set_complete genform2 pan

save_job

exit 0

abort:
COM show_form,job=$JOB,form=genform2
exit 1
