mytest.js 13.1 KB
Newer Older
Scott Sun's avatar
Scott Sun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 147 148 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 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
// 引入模块 包
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
var IKM = $.ikm;
var GEN = $.gen;
var GUI = $.gui;
var Job = $.job;
var JobId = $.job_id;
    
try {
	var cfg = JSON.parse(IKM.select_value({table:'pub_conf',field:'json_data',where:{path : 'cam/input_data'}}));  // 读取配置文件
	var useCfg = cfg.ats;    // todo 选择使用哪个客户的配置 
    addDefaultCfg(cfg.def, useCfg) // 添加默认配置
    /*  useCfg 测试配置
        "job": "1",
        "path": "C:/Users/Administrator/Desktop/Customer_1",
        "step": "cad",
        "ODB": {
            "db": "genesis",
            "files": ["tgz"],
            "arrStep": "stp",
            "isMerge": "yes",
            "jobNameRule": "(job)-a"
        },
        "Gerber": {
            "db": "genesis",
            "files": ["drd", "drl", "phd", "phl"],
            "editFile": [{
                "filter": "drd",
                "callback": "function(props){}"
            }],
            "format": {
                "art": "Gerber274x",
                "drd": "Excellon1",
                "drl": "Wheel",
                "ipc": "IPC356",
                "phd": "Gerber274x",
                "phl": "Ascii"
            }
        },
        "autoSave": "yes",
        "isDelComp": "yes",
        "delSameJob": "yes"
    */
		// todo job来源 
    var job = "1" || "demo", path = useCfg.path, step = useCfg.step, err;
    err = delSameJob({job:job, delSame:useCfg.delSameJob});  if(err){throw err}
	// 判断导入方式 处理path
	var jobPath = path + "/" + job
	if(!fs.exists(jobPath)){throw "path error"}
	var pathInfo = getPathInfo({path:jobPath, cfg: useCfg})  // 分析path下的文件
	if(!pathInfo.type){throw "file error"}   // pathInfo.type    "ODB"  or  "Gerber"
	var input_ok = false   // 判断是不是导入成功
	// 分支  导入ODB
	if(pathInfo.type == "ODB"){
		var odb = useCfg.ODB
		var fileInfo = getFileInfo({inputConfig:odb, path:jobPath, files:pathInfo.files}) // 获取导入文件信息
		if(/^yes$/ig.test(odb.isMerge) && fileInfo.length > 1){
            var err = mergeJob({odb:odb,job:job,fileInfo:fileInfo,delSameJob:useCfg.delSameJob})
            if(err){throw err}
        } else {
            var jobInfo = fileInfo.filter(function(v){return v.baseName == job})[0]
            if(jobInfo.length==0){throw "file error"}
            var err = importJob({name:job,db:odb.db,path:jobInfo.path},useCfg.delSameJob)
            if(err){throw err}
		}
		input_ok = true
	}
	// 分支  导入Gerber
	if(pathInfo.type == "Gerber"){
		var gerber = useCfg.Gerber
		// 如果要修改文件
		if(gerber.editFile && gerber.editFile.length){
			// 将文件拷贝一份到_tmp
			var tmpDir = path + "/" + job + "_tmp"
			if(!fs.exists(tmpDir)){fs.mkdir(tmpDir)}
			var srcFiles = []
			pathInfo.files.forEach(function(v){
				srcFiles.push(v)
				fs.copyFile(v.path,tmpDir + "/" + v.name,true)
			})
			jobPath = tmpDir
			pathInfo.files = fs.listDir(tmpDir,1)
			// 配置操作
			gerber.editFile.forEach(function(item){
				var filter = item.filter
				var fn = eval(item.callback)
				pathInfo.files.forEach(function(file){
					var reg = new RegExp(file.suffix, "ig")
					if(reg.test(filter)){
						fn.call({},{fs: fs, nowFile: file, srcFiles: srcFiles})
					}
				})
			})
		}
		var fileInfo = getFileInfo({inputConfig:gerber, path:jobPath, files:pathInfo.files}) // 获取导入文件信息
		// 导入gerber数据
		importGerber({job:job,step:step,gerber:gerber,fileInfo:fileInfo})
		input_ok = true
	}

	if(!input_ok){throw "input failed"}
	// end
	if(/^yes$/ig.test(useCfg.isDelComp)){  // 删除comp层
		var matrix = GEN.getMatrix({job:job}); 
		comp = Object.keys(matrix).filter(function(v){
			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()
		}
	}
	if(/^yes$/ig.test(useCfg.autoSave)){  // 自动保存
		GEN.checkInout({job:job,mode:"out"})
		GEN.saveJob({ job: job });
	}
	GEN.checkInout({job:job,mode:"in"})
	GEN.closeJob({job:job})
	
	GUI.msg("end")
    return 'Done';
}
catch (error) {
    GUI.msg(error)
    return 'Error';
}

function addDefaultCfg(def, use) {
    for (var key in def) {
        var val = def[key]
        if (typeof val == "string" && !use.hasOwnProperty(key)) {
            use[key] = val
        } else if (typeof val == "object" && use.hasOwnProperty(key)) {
            if (!use[key].hasOwnProperty("db")) {
                use[key].db = val.db
            }
            if (!use[key].hasOwnProperty("files")) {
                use[key].files = val.files
            }
        }
        if (key == "Gerber" && use.hasOwnProperty(key)) {
            for (var gkey in val.format) {
                if (!use[key].hasOwnProperty("format")) {
                    use[key].format = val.format
                } else if (!use[key].format.hasOwnProperty(gkey)) {
                    use[key].format[gkey] = val.format[gkey]
                }
            }
        }
    }
}
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 getPathInfo(props){
	var path = props.path
	var cfg = props.cfg
	var res = {}
	res.files = fs.listDir(path)
	res.fileSuffix = res.files.reduce(function(a,b){
		if(b.isFile && a.indexOf(b.suffix)<0){
			a.push(b.suffix)
		}
		return a
	},[])
	if(cfg.Gerber){
		res.type = cfg.Gerber.files.reduce(function(a,b){
			if(res.fileSuffix.indexOf(b) < 0){
				a = false
			}
			return a 
		}, true)? "Gerber" : undefined
	}
	if(cfg.ODB){
		res.type = cfg.ODB.files.reduce(function(a,b){
			if(res.fileSuffix.indexOf(b) < 0){
				a = false
			}
			return a 
		}, true)? "ODB" : res.type
	}
	return res
}
function getFileInfo(props){
	var inputConfig = props.inputConfig, path = props.path, files = props.files
	var fileInfo = files.reduce(function(a,b){
		if(inputConfig.files.indexOf(b.suffix)>=0){
			a.push({
				name:b.name,
				baseName: b.baseName,
				completeBaseName: b.completeBaseName,
				completeSuffix: b.completeSuffix,
				path: b.path,
				suffix: b.suffix
			})
		}
		return a
	},[])
	return fileInfo
}

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

function mergeJob(props){
    var odb = props.odb
    var job = props.job
    var fileInfo = props.fileInfo
    var arrJobName = odb.jobNameRule.replace("(job)", job)
    var jobInfo = fileInfo.filter(function(v){return v.baseName == job})[0]
    var arrJobInfo = fileInfo.filter(function(v){return v.baseName == arrJobName})[0]
    if(!jobInfo){ return "file error" }
    if(!arrJobInfo){ return "file error" }
    var jobInfos = [jobInfo, arrJobInfo]
    for(var i = 0;i<jobInfos.length;i++){
        var err = importJob({name:jobInfos[i].baseName,db:odb.db,path:jobInfos[i].path},props.delSameJob)
        if(err){return err}
    }
    // 合并操作
    jobInfos.forEach(function(v){GEN.openJob({job:v.baseName})})
    GEN.copyStep({
        source_job:arrJobInfo.baseName,
        source_name:odb.arrStep,
        dest_job:jobInfo.baseName,
        dest_name:odb.arrStep,
    });
    GEN.closeJob({ job: arrJobInfo.baseName });
    GEN.deleteJob({ job: arrJobInfo.baseName });
}
function importGerber(props){
	var fileInfo = props.fileInfo
	var gerber = props.gerber
	var job = props.job
	var step = props.step
	var gerberFiles = fileInfo.filter(function(file){return gerber.gerberConfig.hasOwnProperty(file.suffix)})
	var gerberInfo = gerberFiles.map(function(item){
		var gerberConfig = gerber.gerberConfig[item.suffix] || {}
		var gerberItem = {
			layer: item.name.toLowerCase(), path: item.path,
			format: gerber.format[item.suffix] || "Excellon2",
			job:job,step:step
		}
		for (var key in gerberConfig) {
			gerberItem[key] = gerberConfig[key]
		}
		return gerberItem
	})
	// 创建料号和step
	GEN.createJob({name:job,db:gerber.db})
	GEN.createStep({job:job,name:step})
	// 分析geber配置
	GEN.COM("input_manual_reset")
	gerberInfo.forEach(function(v){
	    GEN.COM("input_manual_set",v)
	})
	GEN.COM("input_manual")
}


/*
{
    "def": {  																		
		"step": "pcs",
		"ODB": {																			
            "db": "genesis",
            "files": ["tgz"]
        },
        "Gerber": {
            "db": "genesis",
            "files": ["drl", "art"],
            "format": {
                "art": "Gerber274x",
                "drd": "Excellon1",
                "drl": "Excellon2",
                "ipc": "IPC356",
                "phd": "Gerber274x",
                "phl": "Ascii"
            }
		},
        "autoSave": "yes",
        "isDelComp": "yes",
        "delSameJob": "yes"
    },
    "ats": {
        "path": "C:/Users/Administrator/Desktop/Customer_1",
		"step": "cad",
		"ODB": {
            "db": "genesis",
            "files": ["tgz"],
            "arrStep": "stp",
            "isMerge": "yes",
            "jobNameRule": "(job)-a"
        },
        "Gerber": {
            "db": "genesis",
            "files": ["drd", "drl", "phd", "phl"],
            "editFile": [{
                "filter": "drd",
                "callback": "(function(){ return function(props){  
					var fs=props.fs,nowFile=props.nowFile,srcFiles=props.srcFiles;
					var srcFile = srcFiles.filter(function(v){return v.name == nowFile.name})[0];
					var str = fs.readFile(srcFile.path).replace(/:/g,'');
					fs.writeFile(nowFile.path,str.match(/T\\d+|G\\d+|X\\d+\\.\\d+Y\\d+\\.\\d+/ig).join('\\n'));
				}})()"
			}],
			"format": {
                "art": "Gerber274x",
                "drd": "Excellon1",
                "drl": "Wheel",
                "ipc": "IPC356",
                "phd": "Gerber274x",
                "phl": "Ascii"
			},
			"gerberConfig": {
				"drd": {
					"data_type":"ascii",
					"units":"mm",
					"coordinates":"absolute",
					"zeroes":"leading",
					"nf1":3,
					"nf2":0,
					"decimal":"yes",
					"separator":"nl",
					"tool_units":"mm",
					"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
				},
				"phd": {
					"data_type":"ascii",
					"units":"mm",
					"coordinates":"absolute",
					"zeroes":"leading",
					"nf1":3,
					"nf2":4,
					"decimal":"no",
					"separator":"*",
					"tool_units":"inch",
					"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
				}
			}
        }
    }
}

*/

/*

{
	"ats": {
		"ODB": {
			"db": "genesis",
			"files": ["tgz"],
			"arrStep": "stp",
			"isMerge": "yes",
			"jobNameRule": "(job)-a"
		},
		"path": "C:/Users/Administrator/Desktop/Customer_1",
		"step": "cad",
		"Gerber": {
			"db": "genesis",
			"files": ["drd", "drl", "phd", "phl"],
			"format": {
				"art": "Gerber274x",
				"drd": "Excellon1",
				"drl": "Wheel",
				"phd": "Gerber274x",
				"phl": "Ascii"
			},
			"editFile": [{
				"filter": "drd",
				"callback": "(function(){ return function(props){ var fs=props.fs,nowFile=props.nowFile,srcFiles=props.srcFiles; var srcFile = srcFiles.filter(function(v){return v.name == nowFile.name})[0]; var str = fs.readFile(srcFile.path).replace(/:/g,''); fs.writeFile(nowFile.path,str.match(/T\\d+|G\\d+|X\\d+\\.\\d+Y\\d+\\.\\d+/ig).join('\\n')); }})()"
			}],
			"gerberConfig": {
				"drd": {
					"nf1": 3,
					"nf2": 0,
					"units": "mm",
					"zeroes": "leading",
					"decimal": "yes",
					"nf_comp": 0,
					"break_sr": "yes",
					"data_type": "ascii",
					"separator": "nl",
					"threshold": 200,
					"drill_only": "no",
					"multiplier": 1,
					"resolution": 3,
					"tool_units": "mm",
					"coordinates": "absolute",
					"merge_by_rule": "no",
					"signed_coords": "no",
					"text_line_width": 0.0024
				},
				"phd": {
					"nf1": 3,
					"nf2": 4,
					"units": "mm",
					"zeroes": "leading",
					"decimal": "no",
					"nf_comp": 0,
					"break_sr": "yes",
					"data_type": "ascii",
					"separator": "*",
					"threshold": 200,
					"drill_only": "no",
					"multiplier": 1,
					"resolution": 3,
					"tool_units": "inch",
					"coordinates": "absolute",
					"merge_by_rule": "no",
					"signed_coords": "no",
					"text_line_width": 0.0024
				}
			}
		}
	},
	"def": {
		"ODB": {
			"db": "genesis",
			"files": ["tgz"]
		},
		"step": "pcs",
		"Gerber": {
			"db": "genesis",
			"files": ["drl", "art"],
			"format": {
				"art": "Gerber274x",
				"drd": "Excellon1",
				"drl": "Excellon2",
				"ipc": "IPC356",
				"phd": "Gerber274x",
				"phl": "Ascii"
			}
		},
		"autoSave": "yes",
		"isDelComp": "yes",
		"delSameJob": "yes"
	}
}
*/