=head
 NAME: 
 DESCRIPTION: 检查钻孔Pad大小.
 PARAMETER:
	{items => [
		{
			name=>'job',
			label=>'原稿检查料号名',
			type=>'editable_enum',
			property=>{
				tl_field=>[name=>'text'],
				tl_data=>[
					{name=>'${JOB}',},
					{name=>'${JOB}_org',},
					{name=>'${JOB}_chk',},
					{name=>'${JOB}_pre',},
				]
			},
			remark=>'原稿检查料号名',
			must_field=>1,
			value=>'',
		},

	]}
	
 VERSION_HISTORY:
	V1.00 2011-05-27 Tony Guo
	   1.新版本
		
 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> ● 原稿检查料号名</p></font>
		  <p> 设置原稿料号名,如:${JOB}_chk;其中${JOB}是代表系统中的料号名.</p>
		<font color="#008000"><p> ● CheckList名称</p></font>
		  <p> 设置CheckList名称.</p>
		<font color="#008000"><p> ● Drill Summary CheckList编号</p></font>
		  <p> 设置Drill Summary CheckList编号.</p>
		<br>
		
  </body></html>
  
=cut

#use strict;
#my ($JOB,$GEN,$GUI,$DB,$JOB_ID,$PAR,$APP,$MW);
use utf8;
use Encode;
use Data::Dump 'dump';
my $Return = 'Done';
my $Job;
my $matrix;
my $max_via_size = 160;
try {
	
	#确定料号名;
	
	if(defined $PAR->{job}){
		$Job = $PAR->{job};
		$Job =~ s/\$\{JOB\}/$JOB/g;
	}
	if (!defined $Job or $Job =~ /^\s*$/){
		$GUI->msgbox(-icon=>'error',-text=>'未定义料号名');
		return 'Error';
	}
	
	my $layercount = $DB->get_jobinfo(-jobid=>$JOB_ID,
								   -jobcategory=>'org',
								   -jobinfo=>'TL_layer_count');
								   
	if ($layercount == 2){
		$GUI->msgbox(-text=>"2层板不存在Stack 结构的孔");
		$DB->save_jobinfo( -jobid=>$JOB_ID,-jobcategory=>'org',
							-jobinfohash=>{ATS_max_laser_stack_num=>0});	
		return $Return;

	}							   
	#判断料号是否存在;
	unless ($GEN->isJobExists(job=>$Job)){
		$GUI->msgbox(-icon=>'error',-text=>"料号[$Job]不存在!");
		return 'Error';
	}
	$GEN->openJob(job=>$Job);
	$GEN->PAUSE("Before Run Script,Drill matrix must be right！And make sure no outline on Drill layers!");
	
	
	my @steps = $GEN->getStepList(job=>$Job);
	my @selstep;
	if (@steps != 1){
		@selstep = $GUI->select_step(-title=>'请选择包含钻孔信息的Step',-steplist=>\@steps,-selectmode=>'multiple');
		return 'Cancel' unless @selstep;
	}
	else{
		@selstep = @steps;
	}
	
	
	#my %matrix = $GEN->getMatrix(job=>$Job);
	my %matrix=$GEN->getMatrix(job=>$Job,type=>'hash');
	############得到钻孔的层###########
	my %drill_info;
	foreach my $step(@selstep){
		foreach my $drill (sort {$matrix{$a}{row} <=>$matrix{$b}{row}} keys %matrix){
			if ($matrix{$drill}{context} eq 'board' and $matrix{$drill}{layer_type} eq 'drill' ){
				$drill_info{$step}{$drill} = $matrix{$drill};
				if ($drill_info{$step}{$drill}{drl_start_num} > $drill_info{$step}{$drill}{drl_end_num}) {
					($drill_info{$step}{$drill}{drl_start_num},$drill_info{$step}{$drill}{drl_end_num}) = ($drill_info{$step}{$drill}{drl_end_num},$drill_info{$step}{$drill}{drl_start_num});
					$drill_info{$step}{$drill}{tl_name} = 'drill'.$drill_info{$step}{$drill}{drl_start_num}.'-'.$drill_info{$step}{$drill}{drl_end_num};
				}
				
			}
		}
	}
	my %include_symbol;
	my %laser_layer;
	my %sel_layer;
	foreach my $step(@selstep){
		$GEN->openStep(job=>$Job,name=>$step);
		$GEN->units(type=>'mm');
		foreach my $layer(sort {$drill_info{$step}{$a}{drl_start_num} <=>$drill_info{$step}{$b}{drl_start_num} || $drill_info{$step}{$a}{drl_end_num} <=>$drill_info{$step}{$b}{drl_end_num}} keys %{$drill_info{$step}}){
			$GEN->workLayer(name=>$layer,clear_before=>'yes');
			my @features = $GEN->getFeatures(job=>$Job,step=>$step,layer=>$layer,units=>'mm');
			foreach my $size(@features){
				if ($size->{symbol}=~/r(\d+.?\d+)/i){
					if($1 < $max_via_size){
						 if (!grep /$1/,@{$include_symbol{$step}{$layer}}){
							push @{$include_symbol{$step}{$layer}},"r".$1;
						}
					}

				}	
			}
			if ( not @{$include_symbol{$step}{$layer}}){
				next;
			}
			$GEN->COM('filter_set',filter_name=>"popup",update_popup=>"no",include_syms=>join(";",@{$include_symbol{$step}{$layer}}));	
			$GEN->COM('filter_area_end',layer=>$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);
			my $nnn;	
			if ($GEN->getSelectCount()>0){
					foreach my $num($drill_info{$step}{$layer}{drl_start_num}..$drill_info{$step}{$layer}{drl_end_num}-1){
						$GEN->COM('filter_area_end',layer=>$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);
						$nnn=$num+1;
						my $tmp_layer = "scr_drl_".$num."_".$nnn;
						$GEN->selCopyOther(dest=>'layer_name',target_layer=>$tmp_layer);
						$laser_layer{$step}{$tmp_layer}{start_num} = $num;
						$laser_layer{$step}{$tmp_layer}{end_num} = $nnn;
						push @{$sel_layer{$step}},$tmp_layer if (!grep /$tmp_layer/,@{$sel_layer{$step}});
					}	
			}
		
		}
		
		my $sym = "r120";
		foreach my $change_layer(@{$sel_layer{$step}}){
			$GEN->workLayer(name=>$change_layer,clear_before=>'yes');
			$GEN->selChangeSym(symbol=>$sym);
		
		}
		
		
		$GEN->closeStep();
	
	}
# $GUI->debug(dump(\%laser_layer));
# $GUI->debug(dump(keys %sel_layer));
	# return;
	
	my @layer;
	my %laser_stack;
	my $tmp_layer = "script_tmp";
	# foreach my $step(keys %sel_layer){
	foreach my $step(keys %laser_layer){
		$GEN->openStep(job=>$Job,name=>$step);
		my $i=0;
		if (@{$sel_layer{$step}}<2){
			$GEN->deleteLayer(step=>$step,job=>$Job,layer=>"scr_drl_1_2") if ($GEN->isLayerExists(job=>$Job,layer=>"scr_drl_1_2"));
			$GEN->closeStep();
			next;
		}
		foreach my $layer(@{$sel_layer{$step}}){
			my ($sta,$end) = $layer =~ /scr_drl_(\d+)_(\d+)/i;
			my $j = $i;
			my $sel_count = 0;
			$j=$j+1;
			my $cul_layer = $layer;
			
			$GEN->copyLayer(source_job=>$Job,source_step=>$step,source_layer=>$cul_layer,dest_layer=>$tmp_layer,mode=>'replace');
			while($j<=@{$sel_layer{$step}}){
				$GEN->workLayer(name=>$tmp_layer,clear_before=>'yes');
				my $next_layer = $sel_layer{$step}[$j];
				my $tmp = $sel_layer{$step}[$j-1];
				if ($laser_layer{$step}{$tmp}{end_num} == $laser_layer{$step}{$next_layer}{start_num}){
					$GEN->selRefFeat(
								layers=>$next_layer,
								use=>'filter',
								# mode=>'touch',
								mode=>'cover',
								f_types=>'pad',
								polarity=>'positive',
								filter=>{feat_types=>'pad',
								polarity=>'positive',
								},
							);
					
					if ($GEN->getSelectCount()>0){
						$sel_count++;
						$GEN->selReverse();
						$GEN->selDelete() if($GEN->getSelectCount()>0); 	
					}else{
						last;	
					}
					
				}else{
					last;
					
				}
				$j++;
			}
			$GEN->deleteLayer(job=>$Job,step=>$step,layer=>$step.'_drl'.$sta.'_'.$end."_stack_laser") if ($GEN->isLayerExists(job=>$Job,layer=>$step.'_drl'.$sta.'_'.$end."_stack_laser"));
			if ($sel_count >= 11) {
				$GEN->selCopyOther(dest=>'layer_name',target_layer=>$step.'_drl'.$sta.'_'.$end."_stack_laser");
			}
			
			
			$GEN->deleteLayer(job=>$Job,step=>$step,layer=>$tmp_layer) if ($GEN->isLayerExists(job=>$Job,layer=>$tmp_layer));
			$laser_stack{$step}{$layer} = $sel_count;
			$i++;	
		}
		$GEN->closeStep();
	
	}
	# $GUI->debug(dump(\%laser_stack));
	# return;
	my $max_stack_via = 0;
	foreach my $step (keys %laser_stack){
		foreach my $layer(keys %{$laser_stack{$step}}){
			$GEN->deleteLayer(job=>$Job,step=>$step,layer=>$layer) if ($GEN->isLayerExists(job=>$Job,layer=>$layer));
			if ($max_stack_via < $laser_stack{$step}{$layer}){
				$max_stack_via = $laser_stack{$step}{$layer};
			}
			
		}
	}
	# $GUI->debug(dump(%laser_stack));
	if ($max_stack_via == 0){
		$GUI->msgbox(-text=>"此板不存在Stack 结构的孔");
	}
	
	$DB->save_jobinfo( -jobid=>$JOB_ID,-jobcategory=>'org',
						-jobinfohash=>{ATS_max_laser_stack_num=>$max_stack_via});
	

	
	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{
	
};
__END__
