/*
NAME: 
DESCRIPTION: ;
PARAMETER:
    [
		{
            name : 'auto_save',
			title : '自动保存',
            type : 'RadioBox',
            property : {
				item_list:[
					{name:'yes',text:'YES'},
					{name:'no',text:'NO'},
				],
				tool_tip:'是否自动保存料号开关'
			}
        }
	]
	
 VERSION_HISTORY:
	V1.00 2020-06-10 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("==============================>li bei xiao yin start");

// 引入模块 包
var $ = require('topcam.scriptfunc').argv();
var fs = require('fs');
var genmath = require('topcam.genmath')
var _ = require('lodash');
var mode = $.ikm ? "topcam" : "aimdfm";
var IKM = $.ikm;
if (IKM==undefined ) { IKM = require('topcam.ikm6')($) }
var GEN = $.gen;
var GUI = $.gui || {};
var Job = $.job || $.job_name;
Job = Job.toLowerCase()
var job = Job
var JobId = $.job_id;
var db = $.db || {}
var PAR = {}
if ($.par) {
	PAR = $.par
} else if ($.hasOwnProperty('script_parameter')){
	PAR = JSON.parse($.script_parameter);
}
var par = PAR;
if (mode === "aimdfm") {
	var database = require("topsin.database");
	database.addConnection($.conf.database_conf, "DFM");
	var QDfm = database.query("DFM");
	$.QDfm = QDfm;
	if ($.conf.product_type == "aimdfm") {
		QDfm.updateRow({
			table: "pdm_aimdfm_task",
			data: {
				current_process_title: $.process_title
			},
			where: { id: $.task_id }
		});
	}
}
var Status = 'ok';
var resultData = [];
var debug = true;
var Step = par.step_fliter

var default_par = {  // par默认参数？
	auto_save: "no"
}

for(var key in default_par){  // 设置默认属性
    if (!par.hasOwnProperty(key) || par[key] == ""){
        par[key] = default_par[key]
    }
}

try {

	var layer = "000"
	var step = "cad"
	var layer = "000"
	// GEN.workLayer({name:layer,display_number:2,clear_before:'yes'})
	// var feautres = GEN.getFeatures({job:job,step:step,layer:layer,units:"mm"})
	var data = {"type":"arc","xs":"-5.1252","ys":"7.685785","xe":"-6.5392","ye":"7.1001","xc":"-5.1250975","yc":"5.685835","symbol":"r25.4","polarity":"positive","direction":"ccw","attributes":[]}
	// -5.1252 7.685785 -6.5392 7.1001 -5.1250975 5.685835
	IKM.msg(genmath.arcLength(data))
	// // 根据feautres 算出周长
	// var milling = Object.keys(feautres).reduce(function(a,b){
	// 	var value = feautres[b]
	// 	if (value.type=="line"){
	// 		length = Math.sqrt((value.xe-value.xs)*(value.xe-value.xs) + (value.ye-value.ys)*(value.ye-value.ys))
	// 		a += length
	// 	} else if (value.type=="arc") {
	// 		a += arcLength(Number(value.xs),Number(value.ys),Number(value.xe),Number(value.ye),Number(value.xc),Number(value.yc))
	// 	}
	// 	return a 
	// }, 0)
	// IKM.msg(milling)

	if (mode === "aimdfm") {
		$.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
			};
		}
	}else {
		return "Done"
	}
} catch (e) {
	IKM.msg(e)
	Status = 'error';
	resultData.push({type: "error", title: "脚本执行出错!", detail: [{desc: _.toString(e)}]});
	return (mode === "aimdfm") ? {status: Status, result_data: resultData} : "Error";
}

function arcLength(x1,y1,x2,y2,x3,y3){
	var cos0 =  +((x3-x1)*(x2-x3) + (y3-y1)*(y2-y3)) / Math.pow((Math.pow(x2-x3,2) + Math.pow(y2-y3,2)),0.5) * Math.pow((Math.pow(x3-x1,2) + Math.pow(y3-y1,2)),0.5)
	var tmp = Math.acos(cos0)
	var sin0 = Math.sin(tmp)
	var length = +Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2)) / sin0 * tmp
	return length
}
