ATS_AIM_layer_rename_and_sort.js 13 KB
Newer Older
Scott Sun's avatar
Scott Sun committed
1
/*
Scott Sun's avatar
s  
Scott Sun committed
2 3 4 5
NAME: 
DESCRIPTION: ;
PARAMETER:
    [
Scott Sun's avatar
Scott Sun committed
6
		{
Scott Sun's avatar
s  
Scott Sun committed
7
			name : 'customer',
Scott Sun's avatar
s  
Scott Sun committed
8
			title : '客户配置',
Scott Sun's avatar
Scott Sun committed
9
			type : 'LineEdit',
Scott Sun's avatar
s  
Scott Sun committed
10
			property : {tool_tip : '客户配置'},
Scott Sun's avatar
Scott Sun committed
11 12
        },
        {
Scott Sun's avatar
s  
Scott Sun committed
13 14
			name : 'config_path',
			title : '配置地址',
Scott Sun's avatar
s  
Scott Sun committed
15
			type : 'LineEdit',
Scott Sun's avatar
s  
Scott Sun committed
16 17 18
			property : {tool_tip : '配置的路径'},
		}
	]
Scott Sun's avatar
Scott Sun committed
19
	
Scott Sun's avatar
s  
Scott Sun committed
20 21 22 23 24 25
 VERSION_HISTORY:
	V1.00 2020-04-17 Scott Sun
	    1.新版本
		
 HELP:
 	<html><body bgcolor="#DDECFE">
Scott Sun's avatar
Scott Sun committed
26
		<font size="3" color="#003DB2"><p>功能简介</p></font>
Scott Sun's avatar
s  
Scott Sun committed
27 28
		<p> 数据标准化 </p>
		<br>
Scott Sun's avatar
Scott Sun committed
29
		<font size="3" color="#003DB2"><p>参数配置</p></font>
Scott Sun's avatar
s  
Scott Sun committed
30 31
		<p> 客户配置 </p>
		<br>
Scott Sun's avatar
Scott Sun committed
32
		<font size="3" color="#003DB2"><p>注意事项</p></font>
Scott Sun's avatar
s  
Scott Sun committed
33 34
		<p> 无 </p>
		<br>
Scott Sun's avatar
Scott Sun committed
35 36
	</body></html>	
*/
Scott Sun's avatar
s  
Scott Sun committed
37 38
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
Scott Sun's avatar
Scott Sun committed
39
// 引入模块 包
Scott Sun's avatar
s  
Scott Sun committed
40
console.log("==============================================>data_format")
Scott Sun's avatar
Scott Sun committed
41 42 43
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
Scott Sun's avatar
s  
Scott Sun committed
44 45 46 47 48 49 50 51 52 53 54 55
var database = require("topsin.database");
database.addConnection($.conf.database_conf, "DFM");
var QDfm = database.query("DFM");
if ($.conf.product_type == "aimdfm") {
	QDfm.updateRow({
		table: "pdm_aimdfm_task",
		data: {
			current_process_title: $.process_title
		},
		where: { id: $.task_id }
	});
}
Scott Sun's avatar
Scott Sun committed
56
var GEN = $.gen;
Scott Sun's avatar
s  
Scott Sun committed
57 58 59 60 61 62 63 64 65 66 67
var Job = $.job_name;
var db = $.db
var Status = 'ok';
var resultData = [];
var PAR = {};
if ($.hasOwnProperty('script_parameter')){
	PAR = JSON.parse($.script_parameter);
}
if (!$.hasOwnProperty('config_path')){
	PAR.config_path = "cam/input_data"
}
Scott Sun's avatar
Scott Sun committed
68
try {
Scott Sun's avatar
s  
Scott Sun committed
69
    var mode = "use" // develop
Scott Sun's avatar
s  
Scott Sun committed
70 71 72 73 74 75 76 77 78 79
    var par = PAR;
	if(!par.hasOwnProperty("customer")){throw "未填写配置"}
    par.customer = par.customer[0].toUpperCase()+par.customer.slice(1).toLowerCase()
	var cfg = db.query("",function(q){
		return q.selectValue({
			table:'pub_conf',
			field:'text_data',
			where:{path : par.config_path}
		})
	});
Scott Sun's avatar
s  
Scott Sun committed
80

Scott Sun's avatar
s  
Scott Sun committed
81 82 83 84 85 86 87 88 89
	var config = eval(cfg)
	var job = Job;
    if(_.isEmpty(job)) throw "没有传入料号名!";
    job = job.toLowerCase()
    
	var custCfg = config.customer[par.customer]	// 获取客户配置
	if(!custCfg){throw "config error"}
	var step = custCfg.step.toLowerCase()	// 获取pcs_step
	// 料号验证
Scott Sun's avatar
Scott Sun committed
90 91
    if(!GEN.isJobExists({job:job})){ throw "job "+ job+ " is not exist" }
    if(!GEN.isJobOpen({job:job})){GEN.openJob({job:job})}
Scott Sun's avatar
s  
Scott Sun committed
92
    if(GEN.checkInout({job:job,mode:"test"}) != 0 && mode != "develop"){ throw "the job check" }
Scott Sun's avatar
s  
Scott Sun committed
93
	GEN.checkInout({job:job,mode:"out"});
Scott Sun's avatar
Scott Sun committed
94

Scott Sun's avatar
s  
Scott Sun committed
95 96 97 98 99 100
	// if(config.hasOwnProperty("merge_drill")){ // todo
	// 	mergeDrill({job:job})
	// }
	// start    martix_tl_name
	var matrix = GEN.getMatrix({job:job})
	var tl_name_matrix = tlNameMatrix({matrix:matrix,tl_name:custCfg.tl_name})   // 给matrix信息添加tl_name
Scott Sun's avatar
Scott Sun committed
101

Scott Sun's avatar
s  
Scott Sun committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	var format_cfg = config.data_format   // 拿到数据标准化配置

	// 排序和设属性
	var ret = sortLayer({job:job, rule:format_cfg, matrix:tl_name_matrix})
	if(ret.err){throw err}
	var after_sort_matrix = ret.sortNames
	// 设置钻孔
	err = setDrill({job:job, matrix:after_sort_matrix})
	if(err){throw err}
	
	// 改名
	err = reName({job:job, matrix:after_sort_matrix, cfg:format_cfg})
	if(err){throw err}
    // end
    GEN.checkInout({job:job,mode:"out"})  // 结束保存料号 关闭料号
	GEN.saveJob({ job: job });
	GEN.checkInout({job:job,mode:"in"})
	GEN.closeJob({job:job})

    QDfm.updateRow({
		table: "pdm_aimdfm_task",
		data: {
			progress: 33.33
		},
		where: { id: $.task_id }
	});
	if (GEN.hasError()) {
		Status = 'error';
		resultData.push({ type: "error", title: "GEN错误!", detail: [{ desc: _.join(GEN.STATUS, "\n") }] });
		return {
			status: Status,
			result_data: resultData
		};
	} else {
		resultData.push({ type: "info", title: "操作完成, 请注意检查!" });
		return {
			status: Status,
			result_data: resultData
		};
	}
}
catch (e) {
	Status = 'error';
    resultData.push({type: "error", title: "脚本执行出错!", detail: [{desc: _.toString(e)}]});
    return {status: Status, result_data: resultData};
Scott Sun's avatar
Scott Sun committed
147 148
}

Scott Sun's avatar
s  
Scott Sun committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
function tlNameMatrix(props){  // 设置标准名
	var matrix = props.matrix
	var tlname_rules = []
	props.tl_name.forEach(function(item){
		if(typeof item.orig_name == "string"){
			tlname_rules.push(item)
		} else {
			item.orig_name.forEach(function(name2){
				tlname_rules.push({
					orig_name: name2,
					tl_name: item.tl_name
				})
			})
		}
	})
	var res = Object.keys(matrix).map(function(key){  // 将matrix信息添加tl_name 如果没有tl_name 就返回原信息
		var val = matrix[key]
		if(val.hasOwnProperty("tl_name")){delete val.tl_name}
		var tlRule = tlname_rules.filter(function(rule){
			if(rule.orig_name === key){return true}
			var reg = new RegExp(rule.orig_name,"ig")
			return reg.test(key)
		})
		if(tlRule.length == 0){return val}
		for(var i=0;i<tlRule.length;i++){
			if(tlRule[i].orig_name === key){return _.assign(val,{tl_name:tlRule[0].tl_name})}
		}
		var reg = new RegExp(tlRule[0].orig_name,"ig")
		var tmp = reg.exec(key)
		if(tmp && tmp.length){
			var replaceArr = tmp.slice(1)
			var tl_name = tlRule[0].tl_name
			var params = {}
			for(var i = 0; i<replaceArr.length;i++){
				params["$"+(i+1)] = replaceArr[i]
			}
			replaceArr.forEach(function(v,i){
				tl_name = tl_name.replace("($"+(i+1)+")",v)
			})
			tl_name = replaceTlName(tl_name)
			function replaceTlName(name){
				var tmp = /(\([^)]*\))/ig.exec(name)
				if(!tmp){ return name }
				var param_arr = tmp[1].match(/\$\d+/ig)
				var newname = tmp[1]
				param_arr.forEach(function(v){
					newname = newname.replace(new RegExp("\\"+v,"ig"), params[v])
				})
				newname = name.replace(tmp[1],eval(newname))
				return replaceTlName(newname)
			}
			return _.assign(val,{tl_name:tl_name})
		}else {
			return _.assign(val,{tl_name:tlRule[0].tl_name})
		}
	})
	return res
Scott Sun's avatar
Scott Sun committed
206 207 208
}

function sortLayer(props){  // 排序方法  {job:要排序的料号, rule: {要改名的规则}
Scott Sun's avatar
s  
Scott Sun committed
209
    var job = props.job
Scott Sun's avatar
s  
Scott Sun committed
210 211 212 213 214 215 216 217 218
	var matrix =  GEN.getMatrix({job:job})
	var tl_matrix = props.matrix
	var matrixName = Object.keys(matrix).sort(function(a,b){return matrix[a].row - matrix[b].row})
    var matrixFirst = matrixName[0];  // 记录matrix中第一位
	var sortNames = [];
    var miscName = tl_matrix.filter(function(value){  // 找出需要排序的名称列表
		var flag = true;
		if (!value.tl_name || value.tl_name == ""){return true}
		var tl_name = value.tl_name
Scott Sun's avatar
Scott Sun committed
219
        props.rule.forEach(function(item,i){
Scott Sun's avatar
s  
Scott Sun committed
220 221
            var evalReg = "/"+item.tl_name+"/ig.test(tl_name)";
            if(tl_name == item.tl_name || eval(evalReg)){
Scott Sun's avatar
Scott Sun committed
222
                flag = false;
Scott Sun's avatar
s  
Scott Sun committed
223
                if(tl_name === item.tl_name){
Scott Sun's avatar
Scott Sun committed
224
                    var _item = JSON.parse(JSON.stringify(item));
Scott Sun's avatar
s  
Scott Sun committed
225 226 227
					_item.value = i;
					_item.name = value.name
					_item.rule = item.tl_name
Scott Sun's avatar
Scott Sun committed
228 229 230 231
                    sortNames.push(_item);
                } else {
                    var _item = JSON.parse(JSON.stringify(item));
                    _item.value = i;
Scott Sun's avatar
s  
Scott Sun committed
232 233 234 235 236
					var str = "/"+ item.tl_name +"/ig.exec(tl_name)"
					_item.value2 = eval(str).pop() - 0;
					_item.name = value.name
					_item.rule = item.tl_name
                    _item.tl_name = tl_name;
Scott Sun's avatar
Scott Sun committed
237 238 239 240 241
                    sortNames.push(_item);
                }
            }
        });
        return flag;
Scott Sun's avatar
s  
Scott Sun committed
242
	}).map(function(v){return v.name})
Scott Sun's avatar
Scott Sun committed
243 244 245 246 247 248 249
    miscName.forEach(function(n){ 
        if(matrix[n].context == 'board'){
            GEN.matrixLayerAttr({job:job,layer:n,context:'misc'}) 
        }
    })
    sortNames = sortNames.sort(function(a,b){
        return a.value - b.value || a.value2 - b.value2
Scott Sun's avatar
s  
Scott Sun committed
250 251
	})
	// 排序
Scott Sun's avatar
Scott Sun committed
252 253 254 255 256 257 258 259
    if(sortNames.length){
        if(sortNames[0].name !== matrixFirst) {  // 如果第一位和matrix中第一位不同,先把第一位插入到最前面
            GEN.matrixMoveRow({ job:job,layer:sortNames[0].name,before:matrixFirst })
        }
        sortNames.forEach(function(v,i,arr){
            if(i !== 0){
                GEN.matrixMoveRow({ job:job,layer:v.name,after:arr[i-1].name })             
            }
Scott Sun's avatar
s  
Scott Sun committed
260
            GEN.matrixLayerAttr(_.assign({job:job,layer:v.name},v.attr));
Scott Sun's avatar
Scott Sun committed
261
        })
Scott Sun's avatar
s  
Scott Sun committed
262 263 264 265
	}
	return {
		sortNames:sortNames
	}
Scott Sun's avatar
Scott Sun committed
266 267
}

Scott Sun's avatar
s  
Scott Sun committed
268 269
function setDrill(props){   // 设置钻孔
    var job = props.job
Scott Sun's avatar
s  
Scott Sun committed
270 271
	var matrix = props.matrix
	
Scott Sun's avatar
Scott Sun committed
272
    // 获取所有的钻孔层
Scott Sun's avatar
s  
Scott Sun committed
273 274
    var drillLayer =matrix.filter(function(v){
        return (v.attr.type == "drill" || v.attr.type == "rout") && v.attr.context == "board"
Scott Sun's avatar
Scott Sun committed
275 276
    })
    // 获取所有single层
Scott Sun's avatar
s  
Scott Sun committed
277 278 279 280
    var signalLayer =matrix.filter(function(v){
        return v.attr.type == "signal" && v.attr.context == "board"
	})
	var layerCount = GEN.getLayerCount({job:job})
Scott Sun's avatar
Scott Sun committed
281 282
    // findSignal 根据传入的数字找到对应的signal层 如 1 => top ; 2 => layer_2 
    function findSignal(num){
Scott Sun's avatar
s  
Scott Sun committed
283 284
        return signalLayer[Number(num-1)].name
	}
Scott Sun's avatar
Scott Sun committed
285 286
    function doDrill(drills){  // 分析钻孔层
        return drills.map(function(v){
Scott Sun's avatar
s  
Scott Sun committed
287 288
            if(v.name == "drill" || v.name=="rout"){
                return {start:findSignal(1), end:findSignal(layerCount), layer:v.name}
Scott Sun's avatar
Scott Sun committed
289
            } else {
Scott Sun's avatar
s  
Scott Sun committed
290
                var tmp = /(\d+)-(\d+)/ig.exec(v.tl_name);
Scott Sun's avatar
Scott Sun committed
291
                if(tmp){
Scott Sun's avatar
s  
Scott Sun committed
292 293 294
                    var start = findSignal(tmp[1]); 
                    var end = findSignal(tmp[2]); 
                    return {start:start, end:end, layer:v.name}
Scott Sun's avatar
Scott Sun committed
295 296 297 298 299 300
                } else {
                    return 0;
                }
            }
        })
    }
Scott Sun's avatar
s  
Scott Sun committed
301 302
	var drillSetList = doDrill(drillLayer);   // 分析得到钻孔设置结果
	drillSetList = drillSetList.filter(function(v){return v!==0})
Scott Sun's avatar
Scott Sun committed
303 304 305
    drillSetList.forEach(function(v){
        GEN.matrixLayerDrill({job:job,layer:v.layer,start:v.start ,end:v.end})
    })
Scott Sun's avatar
s  
Scott Sun committed
306 307
}

Scott Sun's avatar
s  
Scott Sun committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
function reName(props) { // 改名 par{job:要改名的料号, rule: {要改名的规则}
    var job = props.job
	var matrix = props.matrix
	var cfg = props.cfg
	matrix = matrix.filter(function(v){return v.name != v.new_name})
	var rename_list = matrix.map(function(item){
		var new_name;
		var tl_name = item.tl_name
		var rule = item.rule
		if(rule === tl_name){
			new_name = item.new_name
		} else {
			var reg = new RegExp(rule,"ig")
			var tmp = reg.exec(tl_name)
			if(!tmp){return undefined}
			var replaceArr = tmp.slice(1)
			new_name = item.new_name
			var params = {}
			for(var i = 0; i<replaceArr.length;i++){
				params["$"+(i+1)] = replaceArr[i]
			}
			replaceArr.forEach(function(v,i){
				tl_name = tl_name.replace("($"+(i+1)+")",v)
			})
			new_name = replaceTlName(new_name)
			function replaceTlName(name){
				var tmp = /(\([^)]*\))/ig.exec(name)
				if(!tmp){ return name }
				var param_arr = tmp[1].match(/\$\d+/ig)
				var newname = tmp[1]
				param_arr.forEach(function(v){
					newname = newname.replace(new RegExp("\\"+v,"ig"), params[v])
				})
				newname = name.replace(tmp[1],eval(newname))
				return replaceTlName(newname)
			}
		}
		return {
			orig_name: item.name,
			new_name: new_name
		}
	})
	rename_list = rename_list.filter(function(v){
		var flag = true
		if(!v){flag = false}
		if(!v.orig_name || !v.new_name ){flag = false}
		return flag
	})
	rename_list.forEach(function(v){
        if(v.orig_name !== v.new_name){
            GEN.renameLayer({job:job,layer:v.orig_name,new_name:v.new_name})
        }
    });
}

Scott Sun's avatar
s  
Scott Sun committed
363 364 365 366 367 368
function mergeDrill(props){ // 合并钻孔
    var newdrills = []
    var job = props.job;
    var step = GEN.getStepList({job:job})[0];
    var maxDrill = {name:"",value:0};
    function getDrl(str){
Scott Sun's avatar
s  
Scott Sun committed
369
        var num = /(\d+)-(\d+)\.drl/ig.exec(str);  // 匹配出 num-num
Scott Sun's avatar
s  
Scott Sun committed
370 371 372 373 374 375 376 377 378 379 380 381 382
        var tmp = [];
        for(var i = Number(num[1]);i<num[2];i++){
            if(i == 1){
                maxDrill = Number(num[2]) >  maxDrill.value? {name:str,value:Number(num[2])} : maxDrill;
            }
            tmp.push("L"+i+"-"+(i+1));
        }
        return tmp;
    } 
    var matrix = GEN.getMatrix({job:job});
    GEN.openStep({job : job,name:step});
    for(var key in matrix){
        var val = matrix[key];
Scott Sun's avatar
s  
Scott Sun committed
383
        if(val.layer_type == "drill" && /(\d+)-(\d+)\.drl/ig.test(val.name)){
Scott Sun's avatar
s  
Scott Sun committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
            var mergeTo = getDrl(val.name) // 要合并到的地方
            mergeTo.forEach(function(layerName){
                if(GEN.isLayerExists({job : job ,layer:layerName.toLowerCase()}) && newdrills.indexOf(layerName.toLowerCase()) < 0){
                    GEN.deleteLayer({job:job, layer:layerName.toLowerCase()})
                }
                if(!GEN.isLayerExists({job : job ,layer:layerName.toLowerCase()})){
                    GEN.createLayer({job:job,layer:layerName.toLowerCase(),conext:'board',type:val.layer_type});
                    newdrills.push(layerName.toLowerCase())
                }
                GEN.workLayer({name:layerName.toLowerCase(),display_number:1,clear_before:'yes'});
                GEN.copyLayer({source_job:job ,mode:'append',source_step:step,source_layer:val.name,dest_layer:layerName.toLowerCase()});
            })
        }
    }
    GEN.createLayer({job:job,layer:"b1-"+maxDrill.value,conext:'board',type:'drill'});
    GEN.workLayer({name:"b1-"+maxDrill.value,display_number:1,clear_before:'yes'});
    GEN.copyLayer({source_job:job ,mode:'append',source_step:step,source_layer:maxDrill.name,dest_layer:"b1-"+maxDrill.value});
Scott Sun's avatar
s  
Scott Sun committed
401 402
	GEN.affectedLayer({affected:"no",mode:"all"});
	GEN.clearLayers();
Scott Sun's avatar
s  
Scott Sun committed
403 404
    GEN.closeStep();
}
Scott Sun's avatar
s  
Scott Sun committed
405