mytest.js 15 KB
Newer Older
Scott Sun's avatar
s  
Scott Sun committed
1

Scott Sun's avatar
Scott Sun committed
2 3 4 5 6 7 8 9
// 引入模块 包
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
var IKM = $.ikm;
var GEN = $.gen;
var GUI = $.gui;
var Job = $.job;
Scott Sun's avatar
s  
Scott Sun committed
10 11 12
var JobId = $.job_id;
var Status = 'ok';
var resultData = [];
Scott Sun's avatar
Scott Sun committed
13
try {
Scott Sun's avatar
s  
Scott Sun committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	// var config = require("../config.js")   // 读取配置文件 config
	var cfg = IKM.select_value({table:'pub_conf',field:'text_data',where:{path : 'cam/input_data'}})  // 拿到框架中的系统配置
	if (!cfg || cfg == ""){throw "cfg can not find"}  
	var par = {    // ??? 通过某种方式拿到料号和客户名
		job:"i4",
		customer: "Customer_2"   // Customer_1 || Customer_2 || New_data
	}
	var config = eval(cfg)
	var job = par.job || Job;
    if(_.isEmpty(job)) throw "没有传入料号名!";
	job = job.toLowerCase()
	// 如果genesis已经存在这个料号
	err = delSameJob({job:job,delSame:config.delSameJob}); if(err){throw err};
	var custCfg = config.customer[par.customer]
	if(!custCfg){throw "config error"}
	var step = custCfg.step.toLowerCase()
	// 获取路径下文件信息
	var path = par.path || custCfg.path
	var pathInfo = fs.listDir(path, 1)
	// 判断存不存在料号文件
	var jobFile = pathInfo.filter(function(v){return v.baseName.toLowerCase() == job})
	if(jobFile.length == 0){throw "job file is not exist"}
	var jobFiles = []  // 记录要分析的文件
	if (!jobFile[0].isDir) {
		jobFiles.push(jobFile[0])
	} else {
		jobFiles = fs.listDir(jobFile[0].path)
	}
	// 分析料号文件 得到料号导入信息 导入类型
	var jobInfo = analyJobFiles({jobFiles:jobFiles.filter(function(v){return v.isFile}),custCfg:custCfg,config:config,job:job })
	// IKM.msg(jobInfo.data.map(function(v){return v.format}))
	if(jobInfo.data.length == 0){throw "file error"}
	var importOk = false;  // 是否成功导入
	if(/odb/ig.test(jobInfo.type)){  // odb导入
		if(jobInfo.data.length == 1){  // 只分析到一个tgz文件 直接导入
			var err = importJob({name:job,db:custCfg.db,path:jobInfo.data[0].file.path},config.delSameJob)
			if(err){throw err}
		} else {  // 多个tgz文件 说明需要合并
			var mergeFiles = jobInfo.data.filter(function(item){
				var err = importJob({name:item.file.baseName,db:custCfg.db,path:item.file.path},config.delSameJob)
				if(err){throw err}
				return /yes/ig.test(item.format.isMerge)
			})
			var arr_job = mergeFiles[0]
			var arr_step;
			var steplist = GEN.getStepList({job:arr_job.file.baseName})
			if(steplist.indexOf(arr_job.format.arrStep) < 0){
				arr_step = steplist[0]
			}else{arr_step = arr_job.format.arrStep}
			GEN.openJob({job:job});
			GEN.openJob({job:arr_job.file.baseName});
			GEN.copyStep({  // 合并
				source_job:arr_job.file.baseName,
				source_name:arr_step,
				dest_job:job,
				dest_name:arr_step,
			});
			GEN.closeJob({ job: arr_job.file.baseName });
			GEN.deleteJob({ job: arr_job.file.baseName });  // 合并后 删除array的料号
		}
		importOk = true
Scott Sun's avatar
Scott Sun committed
75 76
	}

Scott Sun's avatar
s  
Scott Sun committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if(/gerber/ig.test(jobInfo.type)){  // gerber导入
		GEN.createJob({name:job,db:custCfg.db})
		GEN.createStep({job:job,name:step})
		var gerberInfo = jobInfo.data.map(function(item){
			var gerberCfg = JSON.parse(JSON.stringify(item.format));
			var file = item.file
			gerberCfg.layer = file.name.toLowerCase()
			gerberCfg.path = file.path
			gerberCfg.format = item.type
			gerberCfg.job = job
			gerberCfg.step = step
			return gerberCfg
		})
		GEN.COM("input_manual_reset")
		gerberInfo.forEach(function(v){
			GEN.COM("input_manual_set",v)
		})
		GEN.COM("input_manual")
		importOk = true
	}
Scott Sun's avatar
s  
Scott Sun committed
97

Scott Sun's avatar
s  
Scott Sun committed
98
	if(!importOk){throw "import error"}
Scott Sun's avatar
s  
Scott Sun committed
99

Scott Sun's avatar
s  
Scott Sun committed
100 101 102 103 104 105 106 107 108 109 110
	// 导入结束
	var matrix = GEN.getMatrix({job:job}); 
	comp = Object.keys(matrix).filter(function(v){  // 如果有comp层 删除
		return /^comp_\+_/ig.test(v)
	})
	if (comp.length > 0){
		var tmp = GEN.getStepList({job:job})
		GEN.openStep({job:job,name:tmp[0]})
		GEN.COM("delete_comp")
		GEN.closeStep()
	}
Scott Sun's avatar
s  
Scott Sun committed
111

Scott Sun's avatar
s  
Scott Sun committed
112 113 114 115 116
	// !!!! tmp
	// GEN.checkInout({job:job,mode:"out"})  // 结束保存料号 关闭料号
	// GEN.saveJob({ job: job });
	// GEN.checkInout({job:job,mode:"in"})
	// GEN.closeJob({job:job})
Scott Sun's avatar
s  
Scott Sun committed
117

Scott Sun's avatar
Scott Sun committed
118 119 120
    return 'Done';
}
catch (error) {
Scott Sun's avatar
s  
Scott Sun committed
121
	GUI.msg(error)
Scott Sun's avatar
Scott Sun committed
122 123
    return 'Error';
}
Scott Sun's avatar
s  
Scott Sun committed
124
function delSameJob(props){
Scott Sun's avatar
s  
Scott Sun committed
125
    var job = props.job
Scott Sun's avatar
s  
Scott Sun committed
126 127 128 129 130 131 132
    var delSame = props.delSame
    if(GEN.isJobExists({job:job})){   
        if(/^yes$/ig.test(delSame)){
            if(GEN.isJobOpen({job:job})){ GEN.closeJob({job:job}) }
            GEN.deleteJob({job:job})
        } else {
            return "job "+ job+ " exist"
Scott Sun's avatar
Scott Sun committed
133 134 135
        }
    }
}
Scott Sun's avatar
s  
Scott Sun committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
function analyJobFiles(props){
	var files = props.jobFiles;
	var config = props.config;
	var job = props.job;
	var custCfg = props.custCfg;
	var rules = custCfg.rules;
	var paramsFile = files.filter(function(file){return /param/ig.test(file.baseName)})
	var tmp_hash = {}
	paramsFile.forEach(function(file){
		tmp_hash[file.baseName] = (function(){
			var tmparr = fs.readFile(file.path).split("\n")
			var tmp = {}
			tmparr.map(function(v){
				var arr = v.split(/\b\s+\b/)
				tmp[arr[0].replace(/-/ig,"_")] = arr[1]
Scott Sun's avatar
s  
Scott Sun committed
151
			})
Scott Sun's avatar
s  
Scott Sun committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
			return tmp
		})() 
	})
	
	var fileInfo = files.reduce(function(a,file){
		try {
			rules.forEach(function(item){
				if(item.valid({file:file, job:job})){
					var format = config.formats[item.format]
					var format1;
					if(item.format_params){
						format1 = item.format_params({params:tmp_hash})
					}
					if(format1){
						throw {file:file,type:item.type,format:format1}
					}else{
						throw {file:file,type:item.type,format:format}
					}
				}
			})
		} catch (info) { 
			a.push(info)
Scott Sun's avatar
Scott Sun committed
174
		}
Scott Sun's avatar
s  
Scott Sun committed
175 176 177 178
		return a
	},[])
	var odbfiles = fileInfo.filter(function(item){return /odb/ig.test(item.type)})
	if(odbfiles.length == 0){
Scott Sun's avatar
s  
Scott Sun committed
179
		return {
Scott Sun's avatar
s  
Scott Sun committed
180 181
			type: "Gerber",
			data: fileInfo
Scott Sun's avatar
Scott Sun committed
182
		}
Scott Sun's avatar
s  
Scott Sun committed
183 184 185 186 187 188
	} else {
		return {
			type: "ODB",
			data: odbfiles
		}
	}
Scott Sun's avatar
Scott Sun committed
189
}
Scott Sun's avatar
s  
Scott Sun committed
190 191 192 193 194 195
function importJob(props,delSameJob){
    var name = props.name
    if (GEN.isJobExists({ job: name })) {
        if (/^yes$/ig.test(delSameJob)) {
            if(GEN.isJobOpen({ job: name })){ GEN.closeJob({ job: name }); }
            GEN.deleteJob({ job: name });
Scott Sun's avatar
s  
Scott Sun committed
196
        }
Scott Sun's avatar
s  
Scott Sun committed
197 198
        else {
            return "job "+name+" exist";
Scott Sun's avatar
Scott Sun committed
199 200
        }
    }
Scott Sun's avatar
s  
Scott Sun committed
201
    GEN.importJob(props);
Scott Sun's avatar
Scott Sun committed
202
}
Scott Sun's avatar
s  
Scott Sun committed
203 204


Scott Sun's avatar
s  
Scott Sun committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514









/*
NAME: 
DESCRIPTION: ;
PARAMETER:
    [
		{
			name : 'path',
			title : '资料路径',
			type : 'LineEdit',
			property : {tool_tip : '存放所有料号的文件夹路径,不需要进入到料号文件夹内'},
		},
		{
			name : 'customer',
			title : '客户配置',
			type : 'LineEdit',
			property : {tool_tip : '客户配置'},
		},
		{
			name : 'config_path',
			title : '配置地址',
			type : 'LineEdit',
			property : {tool_tip : '配置的路径'},
		}
	]
	
 VERSION_HISTORY:
	V1.00 2020-04-15 Scott Sun
	    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>
		<p> 料号 和 客户配置 </p>
		<br>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		<p> 无 </p>
		<br>
	</body></html>	
*/
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// 引入模块 包
// console.log("=============================================>input_start");
// console.log("=============================================>input_start");
// console.log("=============================================>input_start");
// console.log("=============================================>input_start");
// var $ = require('topcam.scriptfunc').argv();
// var fs = require('fs');
// var _ = require('lodash');
// 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 }
// 	});
// }
// var GEN = $.gen;
// 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"
// }
// try {
// 	var par = PAR;
	
// 	console.log(par);
	
// 	if(!par.hasOwnProperty("customer")){throw "未填写配置"}
// 	var cfg = db.query("",function(q){
// 		return q.selectValue({
// 			table:'pub_conf',
// 			field:'text_data',
// 			where:{path : par.config_path}
// 		})
// 	})

// 	if (!cfg || cfg == ""){throw "cfg can not find"}  
	
// 	var config = eval(cfg)
// 	if(!config.hasOwnProperty("customer")){throw "config error"}
// 	var job = Job;
// 	if(_.isEmpty(job)) throw "没有传入料号名!";
// 	job = job.toLowerCase()
// 	console.log(typeof config)
// 	console.log("=================Job:"+job)
// 	throw "tmp"
// 	// 如果genesis已经存在这个料号
// 	err = delSameJob({job:job,delSame:config.delSameJob}); if(err){throw err};
// 	var custCfg = config.customer[par.customer]
// 	if(!custCfg){throw "config error"}
// 	var step = custCfg.step.toLowerCase()
// 	// 获取路径下文件信息
// 	var path = par.path || custCfg.path
// 	var pathInfo = fs.listDir(path, 1)
// 	// 判断存不存在料号文件
// 	var jobFile = pathInfo.filter(function(v){return v.baseName.toLowerCase() == job})
// 	if(jobFile.length == 0){throw "job file is not exist"}
// 	var jobFiles = []  // 记录要分析的文件
// 	if (!jobFile[0].isDir) {
// 		jobFiles.push(jobFile[0])
// 	} else {
// 		jobFiles = fs.listDir(jobFile[0].path)
// 	}
// 	// 分析料号文件 得到料号导入信息 导入类型
// 	var jobInfo = analyJobFiles({jobFiles:jobFiles.filter(function(v){return v.isFile}),custCfg:custCfg,config:config,job:job })
// 	if(jobInfo.data.length == 0){throw "file error"}
// 	var importOk = false;  // 是否成功导入
// 	if(/odb/ig.test(jobInfo.type)){  // odb导入
// 		if(jobInfo.data.length == 1){  // 只分析到一个tgz文件 直接导入
// 			var err = importJob({name:job,db:custCfg.db,path:jobInfo.data[0].file.path},config.delSameJob)
// 			if(err){throw err}
// 		} else {  // 多个tgz文件 说明需要合并
// 			var mergeFiles = jobInfo.data.filter(function(item){
// 				var err = importJob({name:item.file.baseName,db:custCfg.db,path:item.file.path},config.delSameJob)
// 				if(err){throw err}
// 				return /yes/ig.test(item.format.isMerge)
// 			})
// 			var arr_job = mergeFiles[0]
// 			var arr_step;
// 			var steplist = GEN.getStepList({job:arr_job.file.baseName})
// 			if(steplist.indexOf(arr_job.format.arrStep) < 0){
// 				arr_step = steplist[0]
// 			}else{arr_step = arr_job.format.arrStep}
// 			GEN.openJob({job:job});
// 			GEN.openJob({job:arr_job.file.baseName});
// 			GEN.copyStep({  // 合并
// 				source_job:arr_job.file.baseName,
// 				source_name:arr_step,
// 				dest_job:job,
// 				dest_name:arr_step,
// 			});
// 			GEN.closeJob({ job: arr_job.file.baseName });
// 			GEN.deleteJob({ job: arr_job.file.baseName });  // 合并后 删除array的料号
// 		}
// 		importOk = true
// 	}

// 	if(/gerber/ig.test(jobInfo.type)){  // gerber导入
// 		GEN.createJob({name:job,db:custCfg.db})
// 		GEN.createStep({job:job,name:step})
// 		var gerberInfo = jobInfo.data.map(function(item){
// 			var gerberCfg = JSON.parse(JSON.stringify(item.format));
// 			var file = item.file
// 			gerberCfg.layer = file.name.toLowerCase()
// 			gerberCfg.path = file.path
// 			gerberCfg.format = item.type
// 			gerberCfg.job = job
// 			gerberCfg.step = step
// 			return gerberCfg
// 		})
// 		GEN.COM("input_manual_reset")
// 		gerberInfo.forEach(function(v){
// 			GEN.COM("input_manual_set",v)
// 		})
// 		GEN.COM("input_manual")
// 		importOk = true
// 	}

// 	if(!importOk){throw "import error"}

// 	// 导入结束
// 	var matrix = GEN.getMatrix({job:job}); 
// 	comp = Object.keys(matrix).filter(function(v){  // 如果有comp层 删除
// 		return /^comp_\+_/ig.test(v)
// 	})
// 	if (comp.length > 0){
// 		var tmp = GEN.getStepList({job:job})
// 		GEN.openStep({job:job,name:tmp[0]})
// 		GEN.COM("delete_comp")
// 		GEN.closeStep()
// 	}

// 	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};
// }

// function delSameJob(props){
//     var job = props.job
//     var delSame = props.delSame
//     if(GEN.isJobExists({job:job})){   
//         if(/^yes$/ig.test(delSame)){
//             if(GEN.isJobOpen({job:job})){ GEN.closeJob({job:job}) }
//             GEN.deleteJob({job:job})
//         } else {
//             return "job "+ job+ " exist"
//         }
//     }
// }
// function analyJobFiles(props){
// 	var files = props.jobFiles;
// 	var config = props.config;
// 	var job = props.job;
// 	var custCfg = props.custCfg;
// 	var rules = custCfg.rules;
// 	var paramsFile = files.filter(function(file){return /param/ig.test(file.baseName)})
// 	var tmp_hash = {}
// 	paramsFile.forEach(function(file){
// 		tmp_hash[file.baseName] = (function(){
// 			var tmparr = fs.readFile(file.path).split("\n")
// 			var tmp = {}
// 			tmparr.map(function(v){
// 				var arr = v.split(/\b\s+\b/)
// 				tmp[arr[0].replace(/-/ig,"_")] = arr[1]
// 			})
// 			return tmp
// 		})() 
// 	})
	
// 	var fileInfo = files.reduce(function(a,file){
// 		try {
// 			rules.forEach(function(item){
// 				if(item.valid({file:file, job:job})){
// 					var format = config.formats[item.format]
// 					var format1;
// 					if(item.format_params){
// 						format1 = item.format_params({params:tmp_hash})
// 					}
// 					if(format1){
// 						throw {file:file,type:item.type,format:format1}
// 					}else{
// 						throw {file:file,type:item.type,format:format}
// 					}
// 				}
// 			})
// 		} catch (info) { 
// 			a.push(info)
// 		}
// 		return a
// 	},[])
// 	var odbfiles = fileInfo.filter(function(item){return /odb/ig.test(item.type)})
// 	if(odbfiles.length == 0){
// 		return {
// 			type: "Gerber",
// 			data: fileInfo
// 		}
// 	} else {
// 		return {
// 			type: "ODB",
// 			data: odbfiles
// 		}
// 	}
// }
// function importJob(props,delSameJob){
//     var name = props.name
//     if (GEN.isJobExists({ job: name })) {
//         if (/^yes$/ig.test(delSameJob)) {
//             if(GEN.isJobOpen({ job: name })){ GEN.closeJob({ job: name }); }
//             GEN.deleteJob({ job: name });
//         }
//         else {
//             return "job "+name+" exist";
//         }
//     }
//     GEN.importJob(props);
// }