123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 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<ScalingManager>
- {
- /// <summary>
- /// 字典
- /// </summary>
- private Dictionary<string, Scaling> _scalingDic = new Dictionary<string, Scaling>();
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="name"></param>
- /// <param name="strScaling"></param>
- 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;
- }
- }
- }
- }
- /// <summary>
- /// 解析
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 是否包含
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public bool IsContained(string name)
- {
- return _scalingDic.ContainsKey(name);
- }
- /// <summary>
- /// 通过Twincat数值计算
- /// </summary>
- /// <param name="name"></param>
- /// <param name="x"></param>
- /// <returns></returns>
- public (bool,double) CalculateValueByTwincatVariable(string name,double x)
- {
- if(_scalingDic.ContainsKey(name))
- {
- return (true, _scalingDic[name].CalculateYByX(x));
- }
- else
- {
- return (false, 0);
- }
- }
- /// <summary>
- /// 计算Twincat数值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="y"></param>
- /// <returns></returns>
- public (bool,int) CalculateTwincatValueByInput(string name,double y)
- {
- if (_scalingDic.ContainsKey(name))
- {
- return (true, _scalingDic[name].CaculateXByY(y));
- }
- else
- {
- return (false, 0);
- }
- }
- }
- }
|