ATS_rout_optimize_connect.js 10.1 KB
Newer Older
Scott Sun's avatar
s  
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
/*
NAME: 
DESCRIPTION: 连接位修复;
PARAMETER:
	[
		{
			name : 'step',
			title : 'step',
			type : 'LineEdit',
			property : {tool_tip : '目标step,默认是orig'}
		},
        {
			name : 'workLayers',
			title : 'workLayers',
			type : 'LineEdit',
			property : {tool_tip : '工作层'}
		},
		{
			name : 'auto_save',
			title : '自动保存',
			type : 'RadioBox',
			property : {
				item_list:[
					{name:'yes',text:'YES'},
					{name:'no',text:'NO'},
				],
				tool_tip:'是否自动保存料号开关'
			}
		}
	]
	
 VERSION_HISTORY:
	V1.00 2021-04-08 template
		1.新版本
		
 HELP:
	  <html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		<p> template </ 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>	
*/

// 引入模块 包 外部参数
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var _ = require('lodash');
var mode = $.ikm ? "topcam" : "aimdfm";
var IKM = $.ikm ? $.ikm : require('topcam.ikm6')($);
var GEN = $.gen;
var Job = $.job || $.job_name;
var JobId = $.job_id;
var db = $.db || IKM.db;
var plugin = require('topsin.gengui');
var GUI = plugin.newGui(plugin.__dirname);
var PAR = {};
if ($.par) {
	PAR = $.par;
} else if ($.hasOwnProperty('script_parameter')) {
	PAR = JSON.parse($.script_parameter);
}
if (mode === "aimdfm") {
	IKM.crud("updateRow", {
		table: "pdm_aimdfm_task",
		data: { current_process_title: $.process_title },
		where: { id: $.task_id }
	});
}
var Status = 'ok';
var resultData = [];
var par = PAR; // 接收参数
var default_par = { // 设置默认参数
	step: "cad",
	auto_save: "No",
    workLayers:"out",
	units: "mm"
};
for (var key in default_par) {
	if (!par.hasOwnProperty(key) || par[key] == "") {
		par[key] = default_par[key];
	}
}

// 定义变量
var job = Job.toLowerCase();
require("topsin.genmath");
var genMath = new GenMath();
try {
	// 常规验证 准备工作
	if (!GEN.isJobExists({ job: job })) { throw "料号" + job + "不存在" }
	if (!GEN.isJobOpen({ job: job })) { GEN.openJob({ job: job }) }
	if (mode == "aimdfm") {
		if (GEN.checkInout({job: job,mode: "test"}) != 0) {
			throw "the job check"
		}
		GEN.checkInout({job: job,mode: "out"});
	}
	// 主体
	var stepList = GEN.getStepList({ job: job });
	stepList = stepList.filter(function (v) {
		var tmpreg = new RegExp(par.step, "ig");
		return tmpreg.test(v);
	});
	if (stepList.length == 0) {
		throw "未找到" + par.step
	}
    
    var workLayers = par.workLayers.split(";")
    workLayers = workLayers.filter(function(l){return GEN.isLayerExists({job:job, layer:l})})
    if(workLayers.length == 0) {
        throw "没有找到工作层"
    }

    var func = 'function(props){return GUI.showForm({\
		title: "连接位公差(um)",\
		size: "400x220",\
		use_core_engine: true,\
		include_hidden_items: true,\
		items: {\
			type: "ScrollArea",\
			property: {\
				widget_resizable: true,\
				frame_shape: "NoFrame"\
			},\
			child: [{\
				type: "FormGridLayout",\
				property: {\
				},\
				child: [{\
					name : "tolerance",\
					title: "tolerance",\
					type : "LineEdit",\
					property : {tool_tip : "tolerance"}\
				}]\
			}]\
		},\
		values: { tolerance: 200 },\
	}) }';


	var data = IKM.command(func, {}, 1).data;
	if(data && data.tolerance && /\d/.test(data.tolerance) ){
        stepList.forEach(function (step) {
            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: par.units }); //单位初始化
            GEN.zoomHome();  //窗口显示回到原始位置
            GEN.selClearFeature()
            
            workLayers.forEach(function(layer){
                GEN.workLayer({name:layer, clear_before:"yes"})
                var feats = GEN.getFeatures({job:job, step:step, layer:layer, units:"mm", options:"feat_index"})
                feats = feats.filter(function (feat) {
                    return feat.type == "line" || feat.type == "arc"
                })
                var ret = [];
Scott Sun's avatar
s  
Scott Sun committed
165 166 167 168 169 170 171
				var indexObj = {}
				var keys = {}
				feats.forEach(function (feat) {
					var x = feat.xs + (feat.xe - feat.xs) / 2
					var y = feat.ys + (feat.ye - feat.ys) / 2
					keys[x+ "_" + y] = true 
				})
Scott Sun's avatar
s  
Scott Sun committed
172 173 174
                feats.forEach(function (feat1, i) {
                    for (var i2 = i+1; i2 < feats.length; i2++) {
                        var feat2 = feats[i2]
Scott Sun's avatar
s  
Scott Sun committed
175
                        if(feat2 && lineLength(feat1) > 0.2 && lineLength(feat2) > 0.2) {
Scott Sun's avatar
s  
Scott Sun committed
176 177 178 179 180
                            var len1 = p2p({x1: feat1.xs, y1:feat1.ys, x2:feat2.xs ,y2:feat2.ys})
                            var len2 = p2p({x1: feat1.xs, y1:feat1.ys, x2:feat2.xe ,y2:feat2.ye})
                            var len3 = p2p({x1: feat1.xe, y1:feat1.ye, x2:feat2.xs ,y2:feat2.ys})
                            var len4 = p2p({x1: feat1.xe, y1:feat1.ye, x2:feat2.xe ,y2:feat2.ye})
                            if(FlagTolerance(len1) || FlagTolerance(len2) || FlagTolerance(len3) || FlagTolerance(len4)) {
Scott Sun's avatar
s  
Scott Sun committed
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
                                var flag = true
								var midx;
								var midy;
								if (FlagTolerance(len1)) {
									midx = feat1.xs + (feat2.xs - feat1.xs)/2
									midy = feat1.ys + (feat2.ys - feat1.ys)/2
								} else if (FlagTolerance(len2)) {
									midx = feat1.xs + (feat2.xe - feat1.xs)/2
									midy = feat1.ys + (feat2.ye - feat1.ys)/2
								} else if (FlagTolerance(len3)) {
									midx = feat1.xe + (feat2.xs - feat1.xe)/2
									midy = feat1.ye + (feat2.ys - feat1.ye)/2
								} else if (FlagTolerance(len4)) {
									midx = feat1.xe + (feat2.xe - feat1.xe)/2
									midy = feat1.ye + (feat2.ye - feat1.ye)/2
								}
								var key = midx + "_" + midy
								
								if (!indexObj[feat1.index] && !indexObj[feat2.index] && flag && !keys[key]) {
									indexObj[feat1.index] = true
									indexObj[feat2.index] = true
									ret.push({
										feat1: feat1,
										feat2: feat2,
									})
								}
Scott Sun's avatar
s  
Scott Sun committed
207 208 209 210 211 212 213 214 215 216 217
                            }
                        }
                    }
                })

                if(ret.length > 0) {
                    // 1. 判断两个线是否有角度 或者是否有弧
                    // 选中执行命令
                    ret.forEach(function (item) {
                        var feat1 = item.feat1
                        var feat2 = item.feat2
Scott Sun's avatar
s  
Scott Sun committed
218 219 220 221 222 223
                        var flag = true;
                        // if(feat1.type == "arc" || feat2.type == "arc") {
                        //     flag = true
                        // } else if (Math.abs((feat1.xe - feat1.xs)/(feat1.ye-feat1.ye) - (feat2.xe - feat2.xs)/(feat2.ye-feat2.ye)) <= 0.001) {
                        //     flag = true
                        // }
Scott Sun's avatar
s  
Scott Sun committed
224 225 226 227 228
                        
                        if(flag) {
                            GEN.selLayerFeat({layer:layer, index:feat1.index,operation:"select"})
                            GEN.selLayerFeat({layer:layer, index:feat2.index,operation:"select"})
                            if(GEN.getSelectCount() == 2) {
Scott Sun's avatar
s  
Scott Sun committed
229 230 231
								// GEN.PAUSE(0)
                                // GEN.COM("sel_design2rout,det_tol="+data.tolerance+",con_tol="+data.tolerance+",rad_tol=10")
								// GEN.PAUSE(1)
Scott Sun's avatar
s  
Scott Sun committed
232
                            } 
Scott Sun's avatar
s  
Scott Sun committed
233
                            // GEN.selClearFeature()
Scott Sun's avatar
s  
Scott Sun committed
234 235
                        }
                    })
Scott Sun's avatar
s  
Scott Sun committed
236
					GEN.PAUSE(0)
Scott Sun's avatar
s  
Scott Sun committed
237 238 239 240 241 242 243 244 245 246 247
                }
            })
    
    
            GEN.affectedLayer({ mode: 'all', affected: 'no' }); //设置影响层全部不影响
            GEN.clearLayers();  //清除层显示
        });
    }
    

    function FlagTolerance(len) {
Scott Sun's avatar
s  
Scott Sun committed
248
        return len > 0 && len <= data.tolerance/1000
Scott Sun's avatar
s  
Scott Sun committed
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
    }

	// 保存料号
	if (/yes/ig.test(par.auto_save)) {
		GEN.checkInout({ job: job, mode: "out" });
		GEN.saveJob({ job: job });
		GEN.checkInout({ job: job, mode: "in" });
	}

	// 结尾返回  固定写法
	var Return = "Done";
	if (mode === "aimdfm") {
		IKM.crud("updateRow", {
			table: "pdm_aimdfm_task",
			data: { progress: 100 },
			where: { id: $.task_id }
		});
		var tmperr = { type: "info", title: "操作完成, 请注意检查!" };
		if (GEN.hasError()) {
			Status = 'error';
			tmperr = { type: "error", title: "GEN错误!", detail: [{ desc: GEN.STATUS.join("\n") }] };
		}
		resultData.push(tmperr);
		Return = { status: Status, result_data: resultData };
	}
	return Return;
} catch (e) {
	var showGUI = true;
	if (GEN.STATUS.length > 0) {
		if (GEN.STATUS.indexOf("Pause Exit:0") >= 0) {
			showGUI = false;
		}
		e = _.toString(e) + "\n" + GEN.STATUS.join("\n");
	}
	if (showGUI) {
		IKM.msg(e);
	}
	Status = 'error';
	resultData.push({ type: "error", title: "脚本执行出错!", detail: [{ desc: _.toString(e) }] });
	return mode === "aimdfm" ? { status: Status, result_data: resultData } : "Error";
}

Scott Sun's avatar
s  
Scott Sun committed
291 292 293 294
function lineLength(line) {
	return Math.sqrt((line.xe - line.xs)*(line.xe - line.xs) + (line.ye - line.ys)*(line.ye - line.ys))
}

Scott Sun's avatar
s  
Scott Sun committed
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
/**
 * 获取层别
 * @param {Object} 过滤选项
 * @returns {Array}
 */
function getLayer(props) {
	if (!props) {
		props = {};
	}
	var matrix = GEN.getMatrix({ job: job });
	return Object.keys(matrix).reduce(function (a, b) {
		var info = matrix[b];
		var ret = true;
		for (var _key in props) {
			if (info[_key]) {
				if (/object string|object number/ig.test({}.toString.call(props[_key]))) {
					if (props[_key] != info[_key]) {
						ret = false;
					}
				}
				if (/object RegExp/ig.test({}.toString.call(props[_key]))) {
					if (!props[_key].test(info[_key])) {
						ret = false;
					}
				}
				if (/object function/ig.test({}.toString.call(props[_key]))) {
					if (!props[_key](info[_key])) {
						ret = false;
					}
				}
			}
		}
		if (ret) {
			a.push(props.res == "info" ? info : b);
		}
		return a;
	}, []);
}

function p2p(props) {
    var x1 = props.x1
    var y1 = props.y1
    var x2 = props.x2
    var y2 = props.y2
    return Math.sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)) 
}