using Aitex.Core.Util; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.TwinCat { public class ScalingManager : Singleton { /// /// 字典 /// private Dictionary _scalingDic = new Dictionary(); /// /// 初始化 /// /// /// public void Initialize(string name,string strScaling) { if (!string.IsNullOrEmpty(strScaling)) { string[] str = strScaling.Split(','); double x1 = 0, y1 = 0, x2 = 0, y2 = 0; if (str.Length == 2) { var result1 = Analyse(str[0]); if (result1.Item1) { x1 = result1.Item3; y1 = result1.Item2; } else { return; } var result2 = Analyse(str[1]); if (result2.Item1) { x2 = result2.Item3; y2 = result2.Item2; Scaling scaling = new Scaling(x1, x2, y1, y2); _scalingDic[name] = scaling; } } } } /// /// 解析 /// /// /// private (bool,double,double) Analyse(string str) { string[] strOperator = str.Split('='); if (strOperator.Length == 2) { bool xResult = double.TryParse(strOperator[0],out var x); bool yResult = double.TryParse(strOperator[1],out var y); if (xResult && yResult) { return (true, x, y); } else { return (false, x, y); } } return (false,0,0); } /// /// 是否包含 /// /// /// public bool IsContained(string name) { return _scalingDic.ContainsKey(name); } /// /// 通过Twincat数值计算 /// /// /// /// public (bool,double) CalculateValueByTwincatVariable(string name,double x) { if(_scalingDic.ContainsKey(name)) { return (true, _scalingDic[name].CalculateYByX(x)); } else { return (false, 0); } } /// /// 计算Twincat数值 /// /// /// /// public (bool,int) CalculateTwincatValueByInput(string name,double y) { if (_scalingDic.ContainsKey(name)) { return (true, _scalingDic[name].CaculateXByY(y)); } else { return (false, 0); } } } }