
// 获取涨缩值
function get_scale_info(par){
	var rows={},n=0;
	par.layers.forEach(function(layer){
		n ++;
		rows[layer].sequence   = n;
		rows[layer].layer      = layer;
		if(layer == "md"){
			rows[layer].x = 1.0004;
			rows[layer].y = 1.0004;
		}
		else{
			rows[layer].x = 1.0000;
			rows[layer].y = 1.0000;			
		}
	})
	
	var scale_factor = GUI.show_tableform({
		defaultsize:[380,400],title : '请确认涨缩值',rows : rows,
		showcheck : 1,gen : $GEN,
		columns : [
			{
				column_name:'layer',
				label:'层名称',
				width:70,
				type:'label',
			},
			{
				column_name:'x',
				label:'X涨缩值',
				width:130,
				type:'number',
			},
			{
				column_name:'y',
				label:'Y涨缩值',
				width:130,
				type:'number',
			},
		],
	});
	return scale_factor || "Cancel";
}

// drillmarge 输出
function drill_out(par){
	var nc_set = 'sec-jt';
	var ncsets = GEN.getNcsetsList({job:job,step:step,layer:par.layer});
	if( ncsets.length > 0 ){
		if (ncsets.filter(function(v){return /^($nc_set)$/.test(v)}).length > 0) {
			GEN.COM('ncset_delete',{name:nc_set});
		}
	}
	
	// 钻带涨缩中心点
	var x_anchor = (par.scale_x != 1 || par.scale_y != 1) ? PAR.profile_limits.xc : 0;
	var y_anchor = (par.scale_x != 1 || par.scale_y != 1) ? PAR.profile_limits.yc : 0;
	
	if(fstat.exist(PAR.out_dir+'/'+par.output_layer)){
		//? unlink
	};   

	var org_x,org_y,angle;
	if(PAR.profile_limits.ysize == 603 && PAR.profile_limits.xsize == 706){
		org_x = PAR.profile_limits.yc;	// - 15
		org_y = 5;
		angle = 270;
		datum.x = PAR.profile_limits.xmax - 5;
		datum.y = PAR.profile_limits.yc;
		datum.angle = 90;
		datum.type = 1;  // 拼版类型
	}else if(PAR.profile_limits.ysize == 603 && PAR.profile_limits.xsize == 606){
		datum.x = org_x = PAR.profile_limits.xc;//- 5
		datum.y = org_y = 5;
		datum.angle = angle = 0;
		datum.type = 0; // 拼版类型
	}else{
		datum.x = org_x = PAR.profile_limits.xc;//- 24
		datum.y = org_y = 5;
		datum.angle = angle = 0;
		datum.type = 0; // 拼版类型
	}
	
	// 铝片塞防焊和铝片塞树脂程序，bot面时需要mirror
	var xmirror = 'no';
	var ymirror = 'no';
	if (/al[pr](\d\d)(\d\d)/.test(par.layer)) {
		var start = RegExp.$1 + 0;
		var end = RegExp.$2 + 0;
		if (start > end) {
			xmirror = 'yes';
		}
	}

	// 获取@drl_section_data数据
	if( /(cdr)/.test(par.layer) && GEN.getLayerCount({job:job}) >= 4){
		var feat = GEN.getFeatures({job:job,step:step,layer:'pnl-map',units:'mm'});
		var data_tmp = feat.filter(function(v){return /(DRL_SECTION)/.test(v.attributes[0])});//获取监视孔坐标
		section_tool = map_info.P.min_drl >= 1100 ? 1100 : map_info.P.min_drl;
		drl_section_data = genMath.p_trans(datum,datum.angle,'no',-datum.x,-datum.y,data_tmp);
		// if($datum->{angle} == 90){
		// 	my @data_tmp = TL::GenMath->p_trans($datum,$datum->{angle},'no',0,0,@data_tmp);#坐标选择
		// 	foreach my $data (@data_tmp){
		// 		my $coorder_txt;
		// 		$coorder_txt = change_coode(data=>$data,datum=>$datum);
		// 		$drl_section_data->{$coorder_txt} = 1;
		// 	}
		// }else{
		// 	foreach my $data (@data_tmp){
		// 		my $coorder_txt;
		// 		$coorder_txt = change_coode(data=>{x=>$data->{x},y=>},datum=>$datum);
		// 		$drl_section_data->{$coorder_txt} = 1;
		// 	}
		// }
	}

	var id = nc_set;
	GEN.COM('ncset_units',{units:'mm'});
	if(/(cdr)/.test(par.layer)){
		GEN.affectedLayer({affected:'yes',layer:par.layer,clear_before:'yes'});
		GEN.COM("adv_filter_reset");
		GEN.COM("reset_filter_criteria,filter_name=,criteria=all");
		GEN.selectByFilter({attribute:[{attribute:'tl_string',text:"*LDI_HOLE_Two*"}]});
		GEN.selectByFilter({attribute:[{attribute:'tl_string',text:"*XRAY_HOLE*"}]});
		GEN.COM("adv_filter_reset");
		GEN.COM("reset_filter_criteria,filter_name=,criteria=all");
		if( GEN.getSelectCount()>0){
			GEN.selChangeSym({symbol:'r1'}) 
		};
		GEN.affectedLayer( {mode:'all',affected:'no'} );
	}
	
	// GEN.COM('open_sets_manager',test_current=>'no');
	GEN.COM('nc_create',{ncset:id,device:'basic_excellon',lyrs:par.layer,thickness:0});
	GEN.COM('nc_set_advanced_params',{layer:par.layer,ncset:id});
	GEN.COM('nc_set_current',{job:job,step:step,layer:par.layer,ncset:id});
	GEN.COM('nc_set_file_params',{single_sr:'no',output_path:PAR.out_dir,output_name:par.output_layer,zeroes:'none',units:'mm',tool_units:'mm',nf1:3,nf2:3,decimal:'no',modal_coords:'no'});
	GEN.COM('nc_register',{angle:angle,xoff:0,yoff:0,version:1,xorigin:org_x,yorigin:org_y,xscale:par.scale_x,yscale:par.scale_y,xscale_o:x_anchor,yscale_o:y_anchor,xmirror:xmirror,ymirror:ymirror});
	GEN.COM('nc_set_optim',{optimize:'yes',iterations:5,reduction_percent:1,cool_spread:PAR.cool_spread,break_sr:'yes',xspeed:2540,yspeed:2540,diag_mode:'45ort'});
	GEN.COM('nc_cre_output',{layer:par.layer,ncset:id});
	var profile = GEN.getProfileLimits({job:job,step:step,units:'mm'});
	GEN.origin({x:profile.xmin,y:profile.ymin});

	
	if(/(cdr)/.test(par.layer)){
		GEN.affectedLayer({affected:'yes',layer:par.layer,clear_before:'yes'});
		GEN.COM("adv_filter_reset");
		GEN.COM("reset_filter_criteria,filter_name=,criteria=all");
		GEN.selectByFilter({attribute:[{attribute:'tl_string',text:"*LDI_HOLE_Two*"}]});
		GEN.selectByFilter({attribute:[{attribute:'tl_string',text:"*XRAY_HOLE*"}]});
		GEN.COM("adv_filter_reset");
		GEN.COM("reset_filter_criteria,filter_name=,criteria=all");
		if( GEN.getSelectCount()>0){
			GEN.selChangeSym({symbol:'r3054'}) 
		};
		GEN.affectedLayer({ mode:'all',affected:'no'});
	}
	
	ncsets = GEN.getNcsetsList({job:job,step:step,layer:par.layer});
	if( ncsets.length > 0 ){
		if (ncsets.filter(function(v){ return /^($nc_set)$/.test(v)}).length > 0) {
			GEN.COM('ncset_delete',{name:nc_set});
		}
	}
}

// function edit_nc_file(par){
// 	open(TMP,par.output_file) || die "cannot open "+par.output_file;
// 	my @data = <TMP>;
// 	close (TMP);
// 	chomp @data;
// 	#
// 	my %data = split_program(data=>\@data);
// 	my $ans = convert_new_program(layer=>$par{layer},new_file=>$par{new_file},data=>\%data,x=>$par{x},y=>$par{y});
// 	return $ans if $ans;	

// 	return undef;
// }

function split_program(par){
	var ui_info = par.info;
	var info={};
	var flag="start",router_flag = undefined;
	par.data.forEach(function(line){
		if( flag == 'start' ){
			if( line == 'M48' ){
				flag = 'overhead';
			}
		}
		else if( flag == 'overhead' ){
			if( /^T01C/i.test(line) ){
				flag = 'head';
			}
		}
		else if( flag == 'head' ){
			if( !/^T\d+/i.test(line) ){
				flag = 'neck';
			}
		}
		else if( flag == 'neck' ){
			if( /^T01/i.test(line) ){
				flag = 'body';
			}
		}
		else if( flag == 'body' ){
			if( /^M30$/i.test(line) ){
				flag = 'end';
			}
		}
		
		if( flag == 'overhead' ){
			info.overhead.push(line)
		}
		else if( flag == 'head' ){
			if(/^T(\d+)C(.*)/.test(line)){
				if(2 - section_tool/1000 < 0.0001){
					section_number = RegExp.$1;
				}
			}
			info.head.push(line)
		}
		else if( flag == 'neck' ){
			info.neck.push(line)
		}
		else if( flag == 'body' ){
			if( /^(T(\d+))$/i.test(line) ){
				router_flag = RegExp.$1;
			}
			if(router_flag){
				var num = router_flag.slice(1) - 0;
				info.body[router_flag].num = num;
				// 判断孔的范围 2020-04-21
				if(num - section_number < 0.001 && /^T/.test(line)){
					var point = change_coode_to_vale(line);
					var are_check = 0;
					drl_section_data.forEach(function(symbol){
						var dist = genMath.point2sym_dist(point,symbol);
						if(dist == 0){
							are_check = 1;
						}
					})
					if(are_check){
						drl_section.push(line)
					}else{
						info.body[router_flag].value.push(line)
					}
				}else{
					info.body[router_flag].value.push(line)
				}
			}
		}
		else if( flag == 'end' ){
			info.end.push(line)
		}
	})
	return info;
}

function convert_new_program(par){
	open(NEWMYFILE,">$par{new_file}");
	var data = par.data; 
	
	
	##0.插入表头
	if($par{x} != 1 or $par{y} != 1){
		print NEWMYFILE 'M47,'.job.' '.'X:'.$par{x}.'  '.'Y:'.$par{y},"\n";
	}

	##1.头顶(刀径清单之前的)/overhead
	foreach my $line(@{$data{overhead}}){
		print NEWMYFILE $line,"\n";
	}
	##
	##2.程序头(router清单)/head
	my $tool_change = get_tool_change(layer=>$par{layer},data=>$par{data});
	
	#$GUI->debug(dump($tool_change));
	###人工排序一次
	$tool_change = manual_sort_tool(layer=>$par{layer},data=>$par{data},info=>$tool_change);
	return $tool_change if $tool_change eq 'Cancel';

	##目前扩孔后没有其它类型的孔 
	my $nibble_suffix = 1;
	my $check_nibble_tool = 0; 
	foreach my $line(@{$data{head}}){
		my ($tool,$suffix) = $line =~ /^(T\d+)C.*$/;
		next unless $tool;
		##刀序顺序使用最新的
		next unless $tool_change->{$tool}{new_tool};
		foreach my $item(values %$tool_change){
			if( $tool eq $item->{new_tool} ){
				if($tool_change->{$item->{new_tool}}{size} > 6.35){
					if(! $check_nibble_tool){
						print NEWMYFILE $tool.'C'.(3.15 + $nibble_suffix/1000),"\n";
						#$nibble_suffix++;
						$check_nibble_tool = 1;
					}
				}else{
					print NEWMYFILE $item->{new_header},"\n";
				}
				$PAR->{tool_num_count} = $item->{new_tool_num} unless ($PAR->{tool_num_count} and $PAR->{tool_num_count} > $item->{new_tool_num});
				#$hole_exit = 1 if $item->{new_header} =~ /503$/;
			}
		}
		
	}
	
	if($par{layer} =~ /(cdr|vpg|alp|alr|psp)/){
		print NEWMYFILE 'T99C0.549',"\n";
	}
	
	##3.脖子（程序头和正式T序坐标之间）/neck
	foreach my $line(@{$data{neck}}){
		next if $line =~ /g93/i;
		print NEWMYFILE $line,"\n";
	}
	
	if(@drl_section_data){
		print NEWMYFILE 'T'.$section_number,"\n";
		foreach my $tmp (@drl_section){
			print NEWMYFILE $tmp,"\n";
		}
	}

	##4.实际的每把T序/body
	my $nibble_tool_check = 0;
	foreach my $tool (sort{$data{body}{$a}{num}<=>$data{body}{$b}{num}} keys %{$data{body}}){
		##获取对应的新刀径
		my $new_tool;
		my $check_drl_section = 0;
		foreach my $old_tool(keys %$tool_change){
			if( $tool eq $tool_change->{$old_tool}{new_tool} ){
				$new_tool = $old_tool;
			}
		}
		next unless $new_tool;
		foreach my $line(@{$data{body}{$new_tool}{value}}){
			#if( abs($tool_change->{$new_tool}{size} - 3.175) < 0.001 ){
			#	if( $line =~ /^(T\d+)/ ){
			#		print NEWMYFILE '/',$tool,"\n";
			#	}
			#	else{
			#		print NEWMYFILE '/',$line,"\n";
			#	}
			#}
			#else{
				#if( $line =~ /^(T\d+)/ ){
				#	print NEWMYFILE $tool,"\n";
				#}
				#else{
				#	print NEWMYFILE $line,"\n";
				#}
			#}
			if( $tool_change->{$new_tool}{size} > 6.35 ){
			 	if( $line =~ /^(T\d+)/ ){
					if( !$nibble_tool_check ){
						print NEWMYFILE $tool,"\n";
						$nibble_tool_check = 1;
					}
			 	}
			 	else{
			 		print NEWMYFILE $line,"\n";#'G84X'.$tool_change->{$new_tool}{size}
			 	}
			 }
			 else{
				if( $line =~ /^(T\d+)/ ){
					print NEWMYFILE $tool,"\n";
				}
				else{
					if( $line =~ /X.*Y.*G85X.*Y.*/ ){
						my ($tmp1,$tmp2) = $line =~ /(X\-?\d*\.?\d*Y\-?\d*\.?\d*)G85(X\-?\d*\.?\d*Y\-?\d*\.?\d*)/;
						my $slot_star = change_coode_to_vale($tmp1);
						my $slot_end  = change_coode_to_vale($tmp2);
						my $slot_size = $tool_change->{$new_tool}{size};
						my $slot_length = $slot_size + TL::GenMath->line_length({xs=>$slot_star->{x},xe=>$slot_end->{x},ys=>$slot_star->{y},ye=>$slot_end->{y}});
						my $slot_info = ger_short_slot_vale(slot_size=>$slot_size,slot_length=>$slot_length);
						#if($slot_info){
						#	my $angle = $slot_info->{angle};
						#	my $point = TL::GenMath->rotate_point($slot_star,$slot_end,$angle);
						#	my $tmp_data = change_coode(data=>{x=>$point->{x},y=>$point->{y}},datum=>{x=>0,y=>0});
						#	my $tmp_line = $tmp1.'G85'.$tmp_data;
						#	print NEWMYFILE $tmp_line,"\n";
						#}else{
							print NEWMYFILE $line,"\n";
						#}
					}else{
						print NEWMYFILE $line,"\n";
					}
				}
			 }
		}
	}

	#插入M97坐标 'X-214500Y000000'
	if($par{layer} =~ /(cdr|vpg|alp|alr|psp)/){
		my @feat = GEN.getFeatures(job=>job,step=>step,layer=>'pnl-map',units=>'mm');
		my @feat_M97 = grep($_->{attributes}->[0] =~ /(DRL_TXT_DRL)/,@feat);
		my $coordinate_txt;
		my $mode;#判断*的位置是横放还是竖放 1 代表横放 2代表竖放
		if($feat_M97[0]->{x} >= $PAR->{sr}{xmin} and $feat_M97[0]->{x} <= $PAR->{sr}{xmax}){
			$mode = 1;
		}else{
			$mode = 2;
		}
		
		
		if($datum->{angle} == 90){
			my @M97_tmp = TL::GenMath->p_trans($datum,$datum->{angle},'no',0,0,({x=>$feat_M97[0]->{x},y=>$feat_M97[0]->{y}}));#返回的是一个数组
			if($mode == 1){
				$coordinate_txt = change_coode(data=>{x=>$M97_tmp[0]->{x} + 2.5 - 0.275,y=>$M97_tmp[0]->{y} - 25 + 0.275},datum=>$datum);
			}else{
				$coordinate_txt = change_coode(data=>{x=>$M97_tmp[0]->{x} - 25 + 0.275,y=>$M97_tmp[0]->{y} - 2.5 + 0.275},datum=>$datum);
			}
		}else{
			if($mode == 1){
				$coordinate_txt = change_coode(data=>{x=>$feat_M97[0]->{x} - 25 + 0.275,y=>$feat_M97[0]->{y} - 2.5 + 0.275},datum=>$datum);
			}else{
				$coordinate_txt = change_coode(data=>{x=>$feat_M97[0]->{x} + 2.5 - 0.275,y=>$feat_M97[0]->{y} - 25 + 0.275},datum=>$datum);
			}
		}
		if($datum->{angle} == 90){
			print NEWMYFILE 'T99',"\n";
			if($mode == 1){
				#GEN.PAUSE('M98');
				print NEWMYFILE 'M98,'.substr(job,0,6).' $S$I',"\n";
			}else{
				#GEN.PAUSE('M97');
				print NEWMYFILE 'M97,'.substr(job,0,6).' $S$I',"\n";
			}
			print NEWMYFILE $coordinate_txt,"\n";
		}else{
			print NEWMYFILE 'T99',"\n";
			if($mode == 1){
				print NEWMYFILE 'M97,'.substr(job,0,6).' $S$I',"\n"
			}else{
				print NEWMYFILE 'M98,'.substr(job,0,6).' $S$I',"\n";
			}
			print NEWMYFILE $coordinate_txt,"\n";
		}
	}

	##5.结束/end
	foreach my $line(@{$data{end}}){
		print NEWMYFILE $line,"\n";
	}
	##
	close (NEWMYFILE);
	return undef;
}

sub get_tool_change{
	my %par = @_;
	my %info;
	my %data = %{$par{data}};
	my $is_laser = $par{is_laser};
	my $row_num = 0;
	
	##
	my (%head,%special,$exist_2500);
	##先判断有无3.054的刀径(排第1)
	foreach my $line(@{$data{head}}){
		my ($tool,$size,$data) = $line =~ /^(T\d+)C(\d*\.?\d{3})(S?.*)$/;
		next unless $tool;
		$head{$tool}{data} = 'C'.$2;
		####
		if( abs($size - 0.001) < 0.0001 ){
			$head{$tool}{data} = 'C3.054';
			$row_num++;
			$info{$tool}{new_tool_num} = $row_num;
			$info{$tool}{new_tool} = 'T'.sprintf("%02s",$row_num);
			$info{$tool}{size} = 3.054;
			$info{$tool}{new_header} = $info{$tool}{new_tool}.$head{$tool}{data};
		}
	}
	
	foreach my $line(@{$data{head}}){
		my ($tool,$size,$data) = $line =~ /^(T\d+)C(\d*\.?\d{3})(.*)$/;
		next unless $tool;
		next if(abs($size - 0.001) < 0.0001);
		$row_num++;
		$info{$tool}{size} = $size;
		$info{$tool}{new_tool_num} = $row_num;
		$info{$tool}{new_tool} = 'T'.sprintf("%02s",$row_num);
		$info{$tool}{new_header} = $info{$tool}{new_tool}.$head{$tool}{data};
	}
	return \%info;
}

sub manual_sort_tool{
	my %par = @_;
	my $data = $par{data};
	my $tool_info = $par{info};
	my @items;
	my ($slot_flag,@slot_size);
	foreach my $num(1..scalar(keys %{$data->{body}})){
		my $tool = 'T'.sprintf("%02s",$num);
		next unless $tool_info->{$tool}{new_tool};
		foreach my $old_tool(sort{$tool_info->{$a}{new_tool_num}<=>$tool_info->{$b}{new_tool_num}} keys %$tool_info){
			if( $tool eq $tool_info->{$old_tool}{new_tool} ){
				my $suffix;
				my $size = $tool_info->{$old_tool}{size};
				push @items,{tool=>$old_tool,size=>$tool_info->{$old_tool}{size} >=  1 ? $tool_info->{$old_tool}{size} : '0'.$tool_info->{$old_tool}{size},suffix=>$suffix};
				last;
			}
		}
	}
	
	update_loading("$par{layer} 层请确认钻刀排列顺序...",0,position=>'n');
	my %form = $GUI->show_form(
		-title => "请确认钻孔刀径顺序",
		-defaultsize => [400,500],
		-columns => 2,
		-items => [
			{
				name => 'table',
				type => 'treeview',
				label => '',
				label_position => 'top',
				expand=>1,
				scrolledwindow=>{},
				property => {
					height_request=>200,
					tl_model_type => 'list',
					enable_grid_lines => 'none',
					tl_mapping =>['tool' => 'Glib::String','size' => 'Glib::String','suffix'=>'Glib::String'],
					tl_columns =>[
						{title=>'原始刀序',visible=>1,resizable=>1,min_width=>80,expand=>1,renders=>[{class=>'Text',property=>{editable=>0,background=>'#FCFBB6',foreground=>'#000000'},text=>'tool'}]},
						{title=>'刀径',visible=>1,resizable=>1,min_width=>80,expand=>1,renders=>[{class=>'Text',property=>{editable=>0,background=>'#FCFBB6',foreground=>'#000000'},text=>'size'}]},
						{title=>'后缀(可编辑)',visible=>1,resizable=>1,min_width=>80,expand=>1,renders=>[{class=>'Text',property=>{editable=>1,background=>'#C5DBF2',foreground=>'#000000'},text=>'suffix'}]},
					],
				},
				value => \@items,
				get_value_func => sub{ my %par = @_;
					my $tvdata = $par{widget}->tl_get_model_data();
					return $tvdata;
				},
				set_value_func => sub{ my %par = @_;
					my $v = $par{value};
					$par{widget}->tl_set_data($v);
					return 0;
				},
				button_position=>'top',
				buttons => [
					{   name=>'top',label=>'置顶',stock=>'gtk-goto-top',
						command=>sub{ my %p = @_;
							my $widget = $p{formpanel}->get_widget('table');
							$widget->set_property(reorderable=>1);
							$widget->tl_sel_moveto_top();
							$widget->set_property(reorderable=>0);
						}
					},
					{   name=>'up',label=>'上移',stock=>'gtk-go-up',
						command=>sub{ my %p = @_;
							my $widget = $p{formpanel}->get_widget('table');
							$widget->set_property(reorderable=>1);
							$widget->tl_sel_move_up();
							$widget->set_property(reorderable=>0);
						}
					},
					{   name=>'down',label=>'下移',stock=>'gtk-go-down',
						command=>sub{ my %p = @_;
							my $widget = $p{formpanel}->get_widget('table');
							$widget->set_property(reorderable=>1);
							$widget->tl_sel_move_down();
							$widget->set_property(reorderable=>0);
						}
					},
					{   name=>'bottom',label=>'置底',stock=>'gtk-goto-bottom',
						command=>sub{ my %p = @_;
							my $widget = $p{formpanel}->get_widget('table');
							$widget->set_property(reorderable=>1);
							$widget->tl_sel_moveto_bottom();
							$widget->set_property(reorderable=>0);
						}
					},
				],
			},
		]
	);
	return 'Cancel' unless %form;
	##将界面确认的结果重新整理一遍
	my %info;
	my @data = @{$form{table}};
	my $row_num;
	foreach my $item(@data){
		$row_num++;
		if( $item->{suffix} =~ /^\(w(\d+)\)$/i || $item->{suffix} =~ /^\(w(\d+)s\d+\)$/i || $item->{suffix} =~ /^\(s(\d+)\)$/i){
			$info{$item->{tool}}{suffix} = $item->{suffix};
			$info{$item->{tool}}{size} = $item->{size};
		}
		elsif($item->{suffix} =~ /^(\d+)$/i ){
			$info{$item->{tool}}{suffix} = $item->{suffix}+0;
			$info{$item->{tool}}{size} = $item->{size};
		}
		else{
			$info{$item->{tool}}{size} = $item->{size};
		}
		$info{$item->{tool}}{new_tool_num} = $row_num;
		$info{$item->{tool}}{new_tool} = 'T'.sprintf("%02s",$row_num);
		if( $item->{suffix} =~ /^\((w|s)(\d+)\)$/i || $item->{suffix} =~ /^\(w(\d+)s(\d+)\)$/i){
			$info{$item->{tool}}{new_header} = $info{$item->{tool}}{new_tool}.'C'.sprintf("%.3f",$info{$item->{tool}}{size}).$item->{suffix};
		}
		else{
			$info{$item->{tool}}{new_header} = $info{$item->{tool}}{new_tool}.'C'.sprintf("%.3f",$info{$item->{tool}}{size});
		}
	}
	##
	return \%info;
}

sub input_drill_program{
	my %par = @_;
	GEN.COM( "input_manual_reset");
	GEN.COM( "input_manual_set,path=$par{file},job=job,step=step,format=Excellon2,data_type=ascii,
				units=mm,coordinates=absolute,zeroes=none,nf1=3,nf2=3,decimal=no,separator=nl,tool_units=mm,layer=$par{new_layer},wheel=,
				wheel_template=,nf_comp=0,multiplier=1,text_line_width=0.0024,signed_coords=no,break_sr=yes,drill_only=no,merge_by_rule=no,threshold=200,resolution=3");
	GEN.COM( "input_manual,script_path=");
	GEN.zoomHome();
	return undef;
}

=head
	web server数据传输
=cut
sub  sec_web_services{
	use LWP::UserAgent;     
    use LWP;      
    use HTTP::Headers;    
    use HTTP::Response;
	use Encode;
	use JSON;
	use JSON qw(encode_json); 
	 my $url = shift;
	 my $data = shift;
	 my $json = JSON->new->allow_nonref;
	 my $ua = LWP::UserAgent->new();
     $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
	 my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
	 my $body = encode('utf8', $json->encode($data));
	 my $request = HTTP::Request->new('POST', $url, $header, $body);
	 my $res = $ua->request($request);
	 #$GUI->debug(-text=>dump($res,'-----------------------------------------------------------------',$res->is_success));
	 if ($res->is_success) { 
		return 0;
	 }else{
		return 1;
	 }
}


=head
	3:3 不省零
	坐标转换 data=>{x=>,y=>} datum=>{x=>,y=>}
=cut
sub change_coode
{
	my %par = @_;
	my ($check_x,$check_y);
	$par{data}->{x} = $par{data}->{x} - $par{datum}->{x};
	$par{data}->{y} = $par{data}->{y} - $par{datum}->{y};

	$check_x = $par{data}->{x} > 0 ? '': '-';
	$check_y = $par{data}->{y} > 0 ? '': '-';

	$par{data}->{x} = sprintf("%0.3f",$par{data}->{x});
	$par{data}->{x} = sprintf("%06s",abs($par{data}->{x})*1000);

	$par{data}->{y} = sprintf("%0.3f",$par{data}->{y});
	$par{data}->{y} = sprintf("%06s",abs($par{data}->{y})*1000);

	return 'X'.$check_x.($par{data}->{x}).'Y'.$check_y.($par{data}->{y});
}


=head
	坐标转换成数值
=cut
sub change_coode_to_vale
{
	my $string = shift;
	$string =~ /X(-?\w+)Y(-?\w+)/;
	my $x_data = sprintf("%0.3f",$1/1000);
	my $y_data = sprintf("%0.3f",$2/1000);
	return {x=>$x_data,y=>$y_data};
}


sub _deleteLayer{
    my %par = @_;
    foreach my $layer(@{$par{layer}}){
        GEN.deleteLayer(layer=>$layer) if (GEN.isLayerExists(job=>job,layer=>$layer));
    }
}


=head
	输出层新命名
=cut
sub  get_new_layer_name
{
		my job = shift;
		my @tmp = split/\-/,job;
		splice(@tmp,2,1);
		$tmp[2] =~ s/2//;
		unless($tmp[2]){
			splice(@tmp,2,1);
		}
		my $new = join('-',@tmp);
		return $new;
}

=head
	短槽拉伸值 slot_size=>,slot_length=> 
=cut
sub ger_short_slot_vale
{
	my %par = @_;
	my $info = {
					1.5 =>[
						{size_min=>0.4,size_max=>0.55,length=>0,angle=>0},
						{size_min=>0.6,size_max=>0.65,length=>0.07,angle=>5},
						{size_min=>0.7,size_max=>0.75,length=>0.0625,angle=>4},
						{size_min=>0.8,size_max=>0.95,length=>0.05,angle=>2.5},
						{size_min=>1.0,size_max=>1.25,length=>0.045,angle=>2},
						{size_min=>1.3,size_max=>1.6,length=>0.0375,angle=>1},
						{size_min=>1.65,size_max=>2.5,length=>0.0375,angle=>1},
						{size_min=>2.55,size_max=>999,length=>0.03,angle=>0.5},
					],
					1.75 =>[
						{size_min=>0.4,size_max=>0.55,length=>0.055,angle=>4},
						{size_min=>0.6,size_max=>0.65,length=>0.055,angle=>3},
						{size_min=>0.7,size_max=>0.75,length=>0.05,angle=>2.5},
						{size_min=>0.8,size_max=>0.95,length=>0.045,angle=>2},
						{size_min=>1.0,size_max=>1.25,length=>0.0375,angle=>1},
						{size_min=>1.3,size_max=>1.6,length=>0.03,angle=>0.5},
						{size_min=>1.65,size_max=>2.5,length=>0.03,angle=>0.5},
						{size_min=>2.55,size_max=>999,length=>0.025,angle=>0.5},
					],
					2.0 =>[
						{size_min=>0.4,size_max=>0.55,length=>0.045,angle=>3},
						{size_min=>0.6,size_max=>0.65,length=>0.0375,angle=>2},
						{size_min=>0.7,size_max=>0.75,length=>0.0375,angle=>1.5},
						{size_min=>0.8,size_max=>0.95,length=>0.03,angle=>1},
						{size_min=>1.0,size_max=>1.25,length=>0.03,angle=>1},
						{size_min=>1.3,size_max=>1.6,length=>0.025,angle=>0.5},
						{size_min=>1.65,size_max=>2.5,length=>0.02,angle=>0.5},
						{size_min=>2.55,size_max=>999,length=>0.02,angle=>0},
					],
	};
	my $type;
	if($par{slot_length}/$par{slot_size} <= 1.5){
		$type = 1.5;
	}elsif($par{slot_length}/$par{slot_size} > 1.5 and $par{slot_length}/$par{slot_size} <= 1.75){
		$type = 1.75;
	}elsif($par{slot_length}/$par{slot_size} > 1.75 and $par{slot_length}/$par{slot_size} <= 2){
		$type = 2.0;
	}elsif($par{slot_length}/$par{slot_size} > 2){
		return 0;
	}
	my $result;
	foreach my $tmp (@{$info->{$type}}){
		if($par{slot_size} >= $tmp->{size_min} and $par{slot_size} <= $tmp->{size_max}){
			$result = 1;
			return $tmp;
		}
	}
	unless ($result){
		return 0;
	}
}