=head
 NAME: generate_ref_netlist
 DESCRIPTION: 产生参考netlist文件
 PARAMETER:
	{items => [
		{
			name=>'step_filter',
			label=>'Step过滤',
			type=>'string',
			must_field=>0,
			value => '',
			remark=>'选择step时候的过滤器',
		},
	]}
	
 VERSION_HISTORY:
    V1.00 2013-03-02 Tomy
		by require
		
 HELP:
	<html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		  <p> </p>
		  <br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<font color="#008000"><p> ● step过滤</p></font>
		  <p> 选择step时候的过滤器</p>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		  <p> ● </p>
		  <br>
	</body></html>
  
=cut

#use strict;
#my ($JOB,$GEN,$GUI,$DB,$JOB_ID,$PAR);
use utf8;
use Encode;
use Data::Dump 'dump';
my ($Job,$Step);
my $Return = 'Done';

###处理step过滤
$Job = $JOB;
$PAR->{step_filter} = $PAR->{step_filter} ? $PAR->{step_filter} : '.*';


try{
	###检查Genesis料号是否存在并打开
	if (! $GEN->isJobExists(job=>$Job) ){
        $GUI->msgbox(-type=>'error',-text=>"料号 $Job 在Genesis中不存在, 请检查!");
        return 'Error';
    }
    $GEN->openJob(job=>$Job) unless ($GEN->isJobOpen(job=>$Job));
	
	###check and get work Step name 
    my @steps =  $GEN->getStepList(job=>$Job);
	if ( @steps == 0 ) {
		$GUI->msgbox(-icon=>'error',-text=>'在料号中没有Step存在,你将退出!');
		return 0;
	}
	elsif (@steps != 1){
        my @tmp_steps = grep(/$PAR->{step_filter}/,@steps);
        if ( @tmp_steps == 1 ) {
            $Step = $tmp_steps[0];
        }
        else {
            $Step = $GUI->select_step(-title=>'请选择工作Step',
                                      -steplist=>[@tmp_steps],
                                      -selectmode=>'single');
            return 'Cancel' unless ($Step);            
        }
	}
    else {
        $Step = $steps[0];
    }
    
    ###Open step and clear layer
    $GEN->openStep(job=>$Job,name=>$Step);
    $GEN->clearLayers();
    $GEN->affectedLayer(mode=>'all',affected=>'no');
    $GEN->units(type=>'inch');
    
    $GEN->COM('netlist_page_open',set=>'yes',job1=>$Job,step1=>$Step,type1=>'cur',job2=>$Job,step2=>$Step,type2=>'cur');
	$GEN->COM('netlist_recalc',job=>$Job,step=>$Step,type=>'cur',display=>'top');
	$GEN->COM('netlist_ref_update',job=>$Job,step=>$Step,source=>'cur',reduce=>'yes');
    $GEN->COM('netlist_page_close');
    
	$Return = 'Finish';
	
	$GUI->msgbox(-text=>"参考网络产生成功");
	
	###output and return status, if genesis error, it will output genesis error command
	unless ($GEN->{STATUS}){
		return $Return;
	}
	else{
		$GUI->msgbox(-icon=>'error',-text=>join("\n",@{$GEN->{STATUS}}));
		addFlowNotes(-notes=>"   Genesis Error:\n   ".join("\n   ",@{$GEN->{STATUS}}));
		return 'Error';
	}
}
catch Error::Simple with {
	my $error = encode("utf8",shift);
	$GUI->msgbox(-icon=>'error',-text=>$error);
	return 'Error';
}
finally{
	#_deleteLayer(layer=>[$TmpLayer]);
	$GEN->units(type=>'mm');
};

__END__
