=head
 NAME:  
 DESCRIPTION: 放大文字框
 PARAMETER:
	[
		{
			name : 'step_filter',
			title : '工作Step',
			type : 'LineEdit',
			property:{tool_tip:''}
		},
		{
			name : 'ss_suffix',
			title : '分离文字字符层层后缀',
			type : 'LineEdit',
			property:{tool_tip:'如未设置，则默认为：-ref'},
		},
		{
			name : 'sm_ref',
			title : '文字辅助层后缀',
			type : 'LineEdit',
			property:{tool_tip:'如未设置，则默认为：-ref'},
		},
		{
			name : 'is_select_lyr',
			type : 'RadioBox',
			title : '是否选择层别',
			property : {tool_tip:'未设定，则默认no',
						size_policy:'Expanding,Fixed',item_list:[{name:'yes',text:'Yes'},{name:'no',text:'No'}]},
			pack : {row:9,column:1},
		},
		{
			name : 'del_backup',
			type : 'RadioBox',
			title : '删除TL备份层',
			property : {tool_tip:'未设定，则默认yes',size_policy:'Expanding,Fixed',item_list:[{name:'yes',text:'Yes'},{name:'no',text:'No'}]},
			pack : {row:9,column:1},
		},
		{
			name : 'save_job',
			title : '保存料号',
			type : 'RadioBox',
			property : {
				size_policy:'Expanding,Fixed',
				item_list:[
					{name:'Yes',text:'Yes'},
					{name:'No',text:'No'},
				],
				tool_tip:'脚本结束后自动保存料号，未设定，默认为No'
			},
			pack : {row:1,column:1},
		}
		
	]
	
 VERSION_HISTORY:
	V1.00 2017-3-22 Cody Yu
	    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>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		  <p> ● 无 </p>
		  <br>
	</body></html>
  
=cut

use strict;
use Encode;
use utf8;
use Data::Dump 'dump';
my ($Job,$Step) = ($JOB,undef);
$PAR->{is_select_lyr} = $PAR->{is_select_lyr} || 'no';
$PAR->{del_backup} = 'yes' unless $PAR->{del_backup};
$PAR->{sm_ref} = '-ref' unless $PAR->{sm_ref};
$PAR->{save_job} = 'No' unless $PAR->{save_job};

my $Return = 'done';
my $units = 'inch';
$PAR->{crate_tmp} = 'yes' unless $PAR->{crate_tmp};

$PAR->{tol} = 1;

try {
	##
	show_loading("判断是否选择料号..",0,position=>'n');
	unless( $Job){
		$GUI->msgbox(-icon=>'error',-text=>"请先选择料号后再执行脚本!");
        return 'Cancel';
	}
	update_loading("检查${Job}是否存在..",0,position=>'n');
	##检查料号是否存在
	unless ( $GEN->isJobExists(job=>$Job) ){
        $GUI->msgbox(-icon=>'error',-text=>"料号 $Job 不存在，请确认。");
        return 'Cancel';
    }
	
    update_loading("正在打开料号$Job...",0,position=>'n');
    $GEN->openJob(job=>$Job) unless ($GEN->isJobOpen(job=>$Job));
	
	###选择工作step
	update_loading("过滤工作step...",0,position=>'n');
	my @steps =  $GEN->getStepList(job=>$Job);
	if ( @steps == 0 ) {
		$GUI->msgbox(-icon=>'error',-text=>'在料号中没有Step存在,你将退出!');
		return 0;
	}
	elsif (@steps != 1){
        $PAR->{step_filter} = '.*' unless $PAR->{step_filter};
        my @tmp_steps = grep(/$PAR->{step_filter}/,@steps);
		if ( @tmp_steps == 0 ) {
            $GUI->msgbox(-icon=>'warning',-text=>'根据脚本参数过滤出来的step不存在，请检查资料或者脚本参数配置！');
			return 'Cancel';
        }
        elsif ( @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];
    }
	
	$GEN->openStep(job=>$Job,name=>$Step);
	$GEN->clearLayers();
	$GEN->affectedLayer( mode=>'all',affected=>'no' );
	$GEN->COM( "sel_options,clear_mode=clear_after,display_mode=all_layers,area_inout=inside,area_select=select,select_mode=standard,area_touching_mode=exclude");
	$GEN->units( type=>$units );
	$GEN->zoomHome();
	
	##过滤 board layers
	update_loading("过滤工作层...",0,position=>'n');
	my @work_layers = get_work_layer();
	return 'Cancel' if $work_layers[0] eq 'Cancel';
	
	##创建辅助层
	$PAR->{crate_tmp} = 'no';
	if( $PAR->{crate_tmp} =~ /yes/i ){
		update_loading("正在创建文字层对应的防焊参考层，请稍候...",0,position=>'n');
		foreach my $layer(@work_layers){
			my $sm_layer = $layer eq 'c1' ? 'm1' : 'm2';
			my $ref_layer = $layer.'-cover+';
			next unless ( $GEN->isLayerExists(job=>$Job,layer=>$sm_layer) );
			if( $layer =~ /^c2/ ){
				$GEN->createLayer(job=>$Job,layer=>$ref_layer,context=>'misc',type=>'document',after=>$layer) unless( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer) );
			}
			else{
				$GEN->createLayer(job=>$Job,layer=>$ref_layer,context=>'misc',type=>'document',before=>$layer) unless( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer) );
			}
			$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$sm_layer,dest_layer=>$ref_layer,mode=>'replace',invert=>'no');
			$GEN->affectedLayer(affected=>'yes',layer=>[$ref_layer],clear_before=>'yes');
			$GEN->selContourize();
			$GEN->COM('sel_resize',size=>10,corner_ctl=>'no');
		}
	}
	
	foreach my $layer(@work_layers){
		if ($GEN->isLayerExists(job=>$Job,layer=>$layer.$PAR->{ss_suffix})) {
			$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$layer.$PAR->{ss_suffix},dest_layer=>$layer.'_tl+++',mode=>'replace',invert=>'no');
		}else{
			$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$layer,dest_layer=>$layer.'_tl+++',mode=>'replace',invert=>'no');
		}
	}
	# 开始放大文字框
	my $default_layer = $work_layers[0];
	my $ref_layer = $work_layers[0].$PAR->{sm_ref};
	
	if ($GEN->isLayerExists(job=>$Job,layer=>$work_layers[0].$PAR->{ss_suffix})) {
		$default_layer = $work_layers[0].$PAR->{ss_suffix};
	}
	
	##
	my ($tmp1,$tmp2) = ('tl_script_tmp1','tl_script_tmp2');
	##
	$GEN->deleteLayer(job=>$Job,layer=>[$tmp1,$tmp2],step=>$Step);
	#$GEN->createLayer(job=>$Job,layer=>$tmp1,context=>'misc',type=>'document');
	#$GEN->createLayer(job=>$Job,layer=>$tmp2,context=>'misc',type=>'document');
	##
	$GEN->workLayer(name=>$default_layer,display_number=>1,clear_before=>'yes');
	$GEN->displayLayer(name=>$ref_layer,number=>2) if( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer));
	my %info;
	$info{type} = 'silk_text';
	$info{scale_type} = 'scale';
	$info{x} = $info{y} = 1.05;
	$info{text_x} = $info{text_y} = 1.05;
	$info{text_space} = 10;
	
	while(1){
		update_loading("请确认文字放大参数，不再继续制作请点取消！",0,position=>'n');
		%info = $GUI->show_form(
			-title => '请确认信息：',
			-showcheck => 1,-gen=>$GEN,
			-defaultsize=>[380,300],
			-columns =>'2',
			-excludehideitem=>1,
			-items=>[
				{
					name=>'type',
					label=>'类型',
					type=>'radio',
					col_span=>2,
					property=>{
						tl_columns=>2,
						tl_list=>['silk_text'=>'放大字符','silk_frame'=>'放大文字框',],
					},
					must_field=>1,
					value=>$info{type},
					validate_func=>sub{
						my %par=@_;
						return{} if $par{mode} eq 'save';
						if (defined $par{value} and $par{value} eq 'silk_text'){
							$par{formpanel}->set_item_visible(title2=>1);
							$par{formpanel}->set_item_visible(text_type=>1);
							$par{formpanel}->set_item_visible(title1=>0);
							$par{formpanel}->set_item_visible(scale_type=>0);
							$par{formpanel}->set_value(scale_type=>$info{scale_type}||'scale');
						}
						else{
							$par{formpanel}->set_item_visible(title2=>0);
							$par{formpanel}->set_item_visible(text_type=>0);
							$par{formpanel}->set_value(text_type=>$info{text_type}||'single');
							$par{formpanel}->set_item_visible(title1=>1);
							$par{formpanel}->set_item_visible(scale_type=>1);
						}
						return {};
					},
				},
				{
					name=>'title1',
					type=>'frame',
					property=>{label=>'放大字符框参数:'},
					expand=>1,
				},
				{
					name=>'scale_type',
					label=>'模式',
					type=>'radio',
					col_span=>2,
					property=>{
						tl_columns=>2,
						tl_list=>['scale'=>'按比例放大','value'=>'按间距放大'],
					},
					must_field=>1,
					value=>$info{scale_type},
					validate_func=>sub{
						my %par=@_;
						return{} if $par{mode} eq 'save';
						if (defined $par{value} and $par{value} eq 'value'){
							$par{formpanel}->set_item_visible(x=>0);
							$par{formpanel}->set_item_visible(y=>0);
							$par{formpanel}->set_item_visible(value_x=>1);
							$par{formpanel}->set_item_visible(value_y=>1);
						}
						else{
							$par{formpanel}->set_item_visible(x=>1);
							$par{formpanel}->set_item_visible(y=>1);
							$par{formpanel}->set_item_visible(value_x=>0);
							$par{formpanel}->set_item_visible(value_y=>0);
						}
						return {};
					},
				},
				{
					name => 'x',
					label => 'X',
					type => 'number',
					value=>$info{x}||1.05,
					visible_func => sub{
						my %par = @_;
						my $value = $par{formpanel}->get_value('scale_type');
						if ($value eq 'scale'){
							return 'show';
						}
						else{
							return 'hide';
						}
					},
				},
				{
					name => 'y',
					label => 'Y',
					type => 'number',
					value=>$info{y}||1.05,
					visible_func => sub{
						my %par = @_;
						my $value = $par{formpanel}->get_value('scale_type');
						if ($value eq 'scale'){
							return 'show';
						}
						else{
							return 'hide';
						}
					},
				},
				{
					name => 'value_x',
					label => 'X',
					type => 'number',
					units=>'mil',
					must_field=>1,
					value=>$info{value_x},
					visible_func => sub{
						my %par = @_;
						my $value = $par{formpanel}->get_value('scale_type');
						if ($value eq 'value'){
							return 'show';
						}
						else{
							return 'hide';
						}
					},
				},
				{
					name => 'value_y',
					label => 'Y',
					type => 'number',
					units=>'mil',
					must_field=>1,
					value=>$info{value_y},
					visible_func => sub{
						my %par = @_;
						my $value = $par{formpanel}->get_value('scale_type');
						if ($value eq 'value'){
							return 'show';
						}
						else{
							return 'hide';
						}
					},
				},
				{
					name=>'title2',
					type=>'frame',
					property=>{label=>'放大字符参数:'},
					expand=>1,
				},
				{
					name=>'text_type',
					label=>'模式',
					type=>'radio',
					col_span=>2,
					property=>{
						tl_columns=>2,
						tl_list=>['layer'=>'自动放大','single'=>'单个放大'],
					},
					must_field=>1,
					value=>$info{text_type}||'single',
				},
				{
					name => 'text_x',
					label => 'X',
					type => 'number',
					value=>$info{text_x}||1.05,
				},
				{
					name => 'text_y',
					label => 'Y',
					type => 'number',
					value=>$info{text_y}||1.05,
				},
				{
					name => 'text_space',
					label => '字符间距',
					type => 'number',
					units=>'mil',
					value=>$info{text_space}||10,
				},
			]
		);
		return $Return unless %info;
		if( $info{type} eq 'silk_frame' ){
			$default_layer = enlarge_slik_frame(default_layer=>$default_layer,ref_layer=>$ref_layer,info=>\%info,tmp1=>$tmp1,tmp2=>$tmp2);
			return $default_layer if $default_layer eq 'Cancel';
		}
		else{
			if( $info{text_type} eq 'layer' ){
				my $ans = enlarge_slik_layer(default_layer=>$default_layer,ref_layer=>$ref_layer,info=>\%info,tmp1=>$tmp1,tmp2=>$tmp2);
				return $ans if $ans;
			}
			else{
				$default_layer = enlarge_text_single(default_layer=>$default_layer,ref_layer=>$ref_layer,info=>\%info,tmp1=>$tmp1,tmp2=>$tmp2);
				return $default_layer if $default_layer eq 'Cancel';
			}
		}
		$GEN->deleteLayer(job=>$Job,layer=>[$tmp1,$tmp2],step=>$Step);
	}
	
	if ($PAR->{del_backup} eq 'yes'){
		foreach my $layer(@work_layers){
			$GEN->deleteLayer(job=>$Job,layer=>[$layer.'_tl+++']) ;
		}
	}
	
	$GEN->affectedLayer(mode=>'all',affected=>'no');
	update_loading("【 放大文字框 】完成..",1,position=>'n');
	hide_loading();
	
	##保存料号
	if( $PAR->{save_job} =~ /yes/i ){
		show_loading("$Job 正在保存料号，请稍候...",0,position=>'n');
		$GEN->checkInout(job=>$Job,mode=>'out');
		$GEN->saveJob(job=>$Job);
		hide_loading();
	}
	
	unless ($GEN->{STATUS}){
		return $Return;
	}
	else{
		$GEN->COM('disp_on');
		$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 = shift;
	$GEN->COM('disp_on');
	$GUI->msgbox(-icon=>'error',-text=>$error);
	return 'Error';
}
finally{
	$GEN->COM('disp_on');
};


sub get_work_layer{
	my %par = @_;
	update_loading("过滤层别...",0,position=>'n');
	my @silk;
	# 获取工作层
	my %matrix = $GEN->getMatrix(job=>$Job,type=>'hash');
	foreach my $layer (sort {$matrix{$a}{row} <=> $matrix{$b}{row}}  keys %matrix) {
		if(defined($matrix{$layer}{context}) and $matrix{$layer}{context} eq 'board' ){
			if(defined($matrix{$layer}{tl_type}) and $matrix{$layer}{tl_type} eq 'silk_screen' ){
				push @silk,$layer;
			}
		}
	}
	unless( @silk ){
		$GUI->msgbox(-icon=>'warning',-text=>'资料中无文字层，请确认');
		return 'Cancel';
	}
	if ($PAR->{is_select_lyr} eq 'yes' and scalar @silk > 1) {
		my (%tmp_matrix);
		foreach my $layer (@silk) {
			$tmp_matrix{$layer} = $matrix{$layer};
		}
		@silk = $GUI->select_layer(
			-title=>'请选择工作层别',
			-layermatrix=>\%tmp_matrix,
			-selectmode => 'multiple');#single
		return 'Cancel' unless(@silk);
	}
	return @silk;
}

sub enlarge_slik_frame{
	my %par = @_;
	my $default_layer = $par{default_layer};
	#$GUI->debug(dump('---111',$default_layer));
	my $ref_layer = $par{ref_layer};
	my %info = %{$par{info}};
	my ($tmp1,$tmp2) = ($par{tmp1},$par{tmp2});
	
	while(1){
		#$GEN->affectedLayer(affected=>'yes',layer=>[$tmp1,$tmp2],clear_before=>'yes');
		#$GEN->selDelete();
		my $work_layer;
		if( $default_layer ){
			$work_layer = $default_layer;
			$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
			$default_layer = undef;
		}
		else{
			while(1){
				$work_layer = $GEN->getWorkLayer();
				last if $work_layer;
				update_loading("请选择一个文字层设为工作层",0,position=>'n');
				my $ans = $GEN->PAUSE('Please Set Work Layer');
				return 'Cancel' unless $ans eq 'OK';
			}
		}
		$ref_layer = $work_layer.$PAR->{sm_ref};
		$GEN->displayLayer(name=>$ref_layer,number=>2)if ( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer) );
		update_loading("请选择需按当前比例放大的文字框，不需制作了请【左键框选空白处】",0,position=>'n');
		my %tmp_con;
		if($info{scale_type} eq 'scale'){
			%tmp_con = $GEN->MOUSE(type=>'r',text=>"Please Select! Scale x:$info{x}  y:$info{y}");
		}
		else{
			%tmp_con = $GEN->MOUSE(type=>'r',text=>"Please Select! Space x:$info{value_x} mil  y:$info{value_y} mil");
		}
		$work_layer = $GEN->getWorkLayer();
		unless( defined $work_layer ){
			$GUI->msgbox(-icon=>'warning',-text=>'没有设定工作层，程序无法继续下去！');
			return 'Cancel';
		}
		if ( $GEN->getSelectCount() > 0 ){
			$GEN->selCopyOther(target_layer=>$tmp1,invert=>'no');
			$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$tmp1,dest_layer=>$tmp2,mode=>'replace',invert=>'no');
		}
		else{
			$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
			$GEN->selectByFilter(area_rect=>\%tmp_con);
			if ( $GEN->getSelectCount() > 0 ){
				$GEN->selCopyOther(target_layer=>$tmp1,invert=>'no');
				$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$tmp1,dest_layer=>$tmp2,mode=>'replace',invert=>'no');
			}
			else{
				$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
				$GEN->displayLayer(name=>$ref_layer,number=>2)if ( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer) );
				$default_layer = $GEN->getWorkLayer();
				last;
			}
		}
		$GEN->COM('disp_off');
		$GEN->units( type=>$units );
		##获取涨缩信息
		my $text_tmp = 'tl_tmp_text';
		$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$tmp1,dest_layer=>$text_tmp,mode=>'replace',invert=>'no');
		$GEN->affectedLayer(affected=>'yes',layer=>[$text_tmp],clear_before=>'yes');
		$GEN->selectByFilter(feat_types=>'line\;arc',polarity=>'positive',profile=>'all');
		$GEN->COM('sel_change_sym', symbol => 'r0')if ( $GEN->getSelectCount() > 0 );
		my $layer_limits = $GEN->getLayerLimits(job=>$Job,step=>$Step,layer=>$text_tmp,units=>$units);
		$layer_limits->{xc} = $layer_limits->{xmin}+$layer_limits->{xsize}/2;
		$layer_limits->{yc} = $layer_limits->{ymin}+$layer_limits->{ysize}/2;
		my ($scalex,$scaley);
		if( $info{scale_type} eq 'scale' ){
			$scalex = $info{x};
			$scaley = $info{y};
		}
		else{
			#my $new_limits;
			#$new_limits->{xsize} = $layer_limits->{xsize}+($info{value_x}/1000)*2;
			#$new_limits->{ysize} = $layer_limits->{ysize}+($info{value_y}/1000)*2;
			#$scalex = $new_limits->{xsize}/$layer_limits->{xsize};
			#$scaley = $new_limits->{ysize}/$layer_limits->{ysize};
			$scalex = 1 + ($info{value_x}*2)/($layer_limits->{xsize}*1000);
			$scaley = 1 + ($info{value_y}*2)/($layer_limits->{ysize}*1000);
		}
		$GEN->deleteLayer(job=>$Job,layer=>[$text_tmp]);
		##
		$GEN->workLayer(name=>$tmp1,display_number=>1,clear_before=>'yes');
		$GEN->selTransform(mode=>'anchor',oper=>'scale',x_anchor=>$layer_limits->{xc},y_anchor=>$layer_limits->{yc},x_scale=>$scalex,y_scale=>$scaley);
		my $tmp_symbol = 'tl_script_tmp';
		#$GEN->COM( "delete_entity,job=$Job,type=symbol,name=$tmp_symbol" ) if( $GEN->isSymbolExists(job=>$Job,symbol=>$tmp_symbol) );
		$GEN->COM('sel_all_feat');
		$GEN->COM( "sel_create_sym,symbol=$tmp_symbol,x_datum=0,y_datum=0,delete=no,fill_dx=0.1,fill_dy=0.1,attach_atr=no,retain_atr=no" );
		$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
		$GEN->selRefFeat(layers=>$tmp2,use=>'filter',mode=>'cover');
		$GEN->COM( "sel_substitute,mode=substitute,symbol=$tmp_symbol,tol=$PAR->{tol},x_datum=0,y_datum=0,dcode=0" );
		$GEN->selectByFilter(include_syms=>$tmp_symbol);
		$GEN->selBreak()if ( $GEN->getSelectCount() > 0 );
		$GEN->COM('disp_on');
		$default_layer = $GEN->getWorkLayer();
		$GEN->COM('truncate_layer', layer =>$tmp1);
		$GEN->COM('truncate_layer', layer =>$tmp2);
		
		if( $GEN->{STATUS} ){
			#$GEN->COM('disp_on');
			$GUI->msgbox(-icon=>'error',-text=>join("\n",@{$GEN->{STATUS}}));
			return 'Error';
		}
		$GEN->units(type=>$units);
	}
	return $default_layer;
}

sub enlarge_slik_layer{
	my %par = @_;
	my $default_layer = $par{default_layer};
	my %info = %{$par{info}};
	my ($curlayer,$testlayer) = ($par{tmp1},$par{tmp2});
	while(1){
		update_loading("请将需要放大的文字move到一个辅助层上并设为工作层...",0,position=>'n');
		my $ans = $GEN->PAUSE('Please Set Layer');
		return 'Cancel' unless $ans eq 'OK';
		$curlayer = $GEN->getWorkLayer();
		last unless $curlayer;
		unless ( $GEN->isLayerEmpty( job=>$Job,step=>$Step,layer=>$curlayer) ){
			$GEN->affectedLayer(affected=>'yes',layer=>[$curlayer],clear_before=>'yes');
			$GEN->selCopyOther(target_layer=>$testlayer,invert=>'no',size=>$info{text_space});
		}
		else{
			last;
		}
		update_loading("正在放大$curlayer 层上字符，请稍候...",0,position=>'n');
		my $ref_layer = 'tl_silk_layer';
		$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$curlayer,dest_layer=>$ref_layer,mode=>'replace',invert=>'no');
		$GEN->COM('disp_off');
		$GEN->units( type=>$units );
		##获取涨缩信息
		$GEN->affectedLayer(affected=>'yes',layer=>[$testlayer],clear_before=>'yes');
		$GEN->selContourize(accuracy=>0.25,clean_hole_size=>3,clean_hole_mode=>'x_and_y');
		$GEN->workLayer(name=>$curlayer,display_number=>1,clear_before=>'yes');
		$GEN->affectedLayer(affected=>'yes',layer=>[$testlayer]);
		# $GEN->PAUSE('AAAAA');
		my $index = 1;
		while( 1 ) {
		    $GEN->COM("reset_filter_criteria,filter_name=popup,criteria=all");
			$GEN->COM('filter_reset',filter_name=>'popup');
			$GEN->COM("adv_filter_reset");
			$GEN->COM("reset_filter_criteria,filter_name=,criteria=all");
			$GEN->COM('adv_filter_set',filter_name=>'popup',update_popup=>'yes',indexes=>$index);
			$GEN->COM('filter_area_strt');
			$GEN->COM('filter_area_end',layer=>$testlayer,filter_name=>'popup',operation=>'select',area_type=>'none',inside_area=>'no',intersect_area=>'no');
			if ( $GEN->getSelectCount() > 0 ){
				my $layer_limits = getLayerLimits(job=>$Job,step=>$Step,layer=>$testlayer,units=>$units,options => 'select');
				$layer_limits->{xc} = $layer_limits->{xmin}+$layer_limits->{xsize}/2;
				$layer_limits->{yc} = $layer_limits->{ymin}+$layer_limits->{ysize}/2;
				$GEN->COM("adv_filter_reset");
				$GEN->COM("reset_filter_criteria,filter_name=ref_select,criteria=all");
				$GEN->COM("sel_ref_feat,layers=,use=select,mode=touch,pads_as=shape,f_types=line\;pad\;surface\;arc\;text,polarity=positive\;negative,include_syms=,exclude_syms=");
				if ( $GEN->getSelectCount() > 0 ){
					$GEN->selTransform(mode=>'anchor',oper=>'scale',x_anchor=>$layer_limits->{xc},y_anchor=>$layer_limits->{yc},x_scale=>$info{text_x},y_scale=>$info{text_y});
				}
			}
			else {
				last;
			}
			$index++;
		}
		#$GEN->COM('truncate_layer', layer =>$testlayer);
		$default_layer = $curlayer;
		$GEN->COM('disp_on');
		$GEN->workLayer(name=>$curlayer,display_number=>1,clear_before=>'yes');
		$GEN->displayLayer(name=>$ref_layer,number=>2) if( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer));
		update_loading("请确认$curlayer 文字放大结果并制作到正式文字层...",0,position=>'n');
		$ans = $GEN->PAUSE('Please Check');
		return 'Cancel' unless $ans eq 'OK';
		
		#$GEN->COM('truncate_layer', layer =>$curlayer);
		$GEN->deleteLayer(job=>$Job,layer=>[$ref_layer]);
		$GEN->clearLayers();
		$GEN->affectedLayer( mode=>'all',affected=>'no' );
		
		if( $GEN->{STATUS} ){
			$GUI->msgbox(-icon=>'error',-text=>join("\n",@{$GEN->{STATUS}}));
			return 'Error';
		}
		$GEN->units(type=>$units);
	}
	return undef;
}

sub enlarge_text_single{
	my %par = @_;
	my $default_layer = $par{default_layer};
	my $ref_layer = $par{ref_layer};
	my %info = %{$par{info}};
	my ($tmp1,$tmp2) = ($par{tmp1},$par{tmp2});
	while(1){
		my $work_layer;
		if( $default_layer ){
			$work_layer = $default_layer;
			$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
			$default_layer = undef;
		}
		else{
			while(1){
				$work_layer = $GEN->getWorkLayer();
				last if $work_layer;
				update_loading("请选择一个文字层设为工作层",0,position=>'n');
				my $ans = $GEN->PAUSE('Please Set Work Layer');
				return 'Cancel' unless $ans eq 'OK';
			}
		}
		#$ref_layer = $work_layer.'_tl+++';
		$GEN->displayLayer(name=>$ref_layer,number=>2)if( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer));
		update_loading("请选择需按当前比例放大的字符，不需制作了请【左键框选空白处】",0,position=>'n');
		my %tmp_con = $GEN->MOUSE(type=>'r',text=>"Please Select! Scale x:$info{text_x}  y:$info{text_y}");
		
		$work_layer = $GEN->getWorkLayer();
		unless( defined $work_layer ){
			$GUI->msgbox(-icon=>'warning',-text=>'没有设定工作层，程序无法继续下去！');
			return 'Cancel';
		}
		if ( $GEN->getSelectCount() > 0 ){
			$GEN->selMoveOther(target_layer=>$tmp1,invert=>'no');
			#$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$tmp1,dest_layer=>$tmp2,mode=>'replace',invert=>'no');
		}
		else{
			$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
			$GEN->selectByFilter(area_rect=>\%tmp_con);
			if ( $GEN->getSelectCount() > 0 ){
				$GEN->selMoveOther(target_layer=>$tmp1,invert=>'no');
				#$GEN->copyLayer(source_job=>$Job,source_step=>$Step,source_layer=>$tmp1,dest_layer=>$tmp2,mode=>'replace',invert=>'no');
			}
			else{
				$GEN->workLayer(name=>$work_layer,display_number=>1,clear_before=>'yes');
				$GEN->displayLayer(name=>$ref_layer,number=>2) if( $GEN->isLayerExists(job=>$Job,layer=>$ref_layer));
				$default_layer = $GEN->getWorkLayer();
				last;
			}
		}
		$GEN->COM('disp_off');
		$GEN->units( type=>$units );
		##获取涨缩信息
		$GEN->affectedLayer(affected=>'yes',layer=>[$tmp1],clear_before=>'yes');
		my $layer_limits = $GEN->getLayerLimits(job=>$Job,step=>$Step,layer=>$tmp1,units=>$units);
		$layer_limits->{xc} = $layer_limits->{xmin}+$layer_limits->{xsize}/2;
		$layer_limits->{yc} = $layer_limits->{ymin}+$layer_limits->{ysize}/2;
		my ($scalex,$scaley) = ($info{text_x},$info{text_y});
		##
		$GEN->workLayer(name=>$tmp1,display_number=>1,clear_before=>'yes');
		$GEN->selTransform(mode=>'anchor',oper=>'scale',x_anchor=>$layer_limits->{xc},y_anchor=>$layer_limits->{yc},x_scale=>$scalex,y_scale=>$scaley);
		$GEN->selMoveOther(target_layer=>$work_layer,invert=>'no');
		$default_layer = $work_layer;
		$GEN->COM('truncate_layer', layer =>$tmp1);
		$GEN->COM('disp_on');
		
		if( $GEN->{STATUS} ){
			$GEN->COM('disp_on');
			$GUI->msgbox(-icon=>'error',-text=>join("\n",@{$GEN->{STATUS}}));
			return 'Error';
		}
		$GEN->units(type=>$units);
	}
	return $default_layer;
}

####GEN lib no options 属性
sub getLayerLimits {
	my %par = @_;
	$par{units} = 'inch' if (! defined $par{units});
	my %return;
	$GEN->INFO(
		'units'=>$par{units},
		'entity_type'=>'layer',
		'entity_path'=>"$par{job}/$par{step}/$par{layer}",
		'data_type'=>'limits',
		'options' => $par{options}
	);	
	$return{xmin} = $GEN->{doinfo}{gLIMITSxmin};
	$return{ymin} = $GEN->{doinfo}{gLIMITSymin};
	$return{xmax} = $GEN->{doinfo}{gLIMITSxmax};
	$return{ymax} = $GEN->{doinfo}{gLIMITSymax};	
	$return{xsize} = $return{xmax} - $return{xmin};
	$return{ysize} = $return{ymax} - $return{ymin};
	if (wantarray){
		return %return;
	}else{
		return {%return};
	}	
}


__END__


