Esempio n. 1
0
        /// <summary>
        /// 演算設定処理
        /// </summary>
        /// <returns></returns>
        public bool Init()
        {
            bool bRet = false;
            calcCount = 0;

            try
            {
                int index = 0;
                int pos = -1;
                int calcCount_o = 0;
                string temp = string.Empty;
                string variable = string.Empty;

                calc = new Calc();
                if (calc == null)
                {
                    return bRet;
                }

                if (dataTagSetting == null || constantSetting == null)
                {
                    return bRet;
                }

                //call eval method
                if (dataTagSetting != null && dataTagSetting.DataTagList != null)
                {
                    for (index = 0; index < dataTagSetting.DataTagList.Length; index++)
                    {
                        if (dataTagSetting.DataTagList[index].TagKind == 2)
                        {
                            // 演算タグがグラフに割り当てられていれば対象。
                            if (!IsAssignedGraph(dataTagSetting.DataTagList[index].TagNo))
                                continue;

                            //演算2
                            calcCount++;
                        }
                    }

                    //evaluate expression
                    strErrorMessage = string.Empty;
                    valConstant = new double[constantSetting.ConstantList.Length];
                    strConstantName = new string[constantSetting.ConstantList.Length];
                    valVariable = new double[dataTagSetting.DataTagList.Length];
                    strVariableName = new string[dataTagSetting.DataTagList.Length];

                    valCalcResult = new double[calcCount];
                    strCalcName = new string[calcCount];
                    strExpression = new string[calcCount];
                    iCalcIndex = new int[calcCount];

                    //////////////////////////////////////////////////////////////////////////////////////////////////////////
                    dataHandleVar = GCHandle.Alloc(valVariable, GCHandleType.Pinned);
                    IntPtr dataPtrVar = dataHandleVar.AddrOfPinnedObject();

                    dataHandleCalc = GCHandle.Alloc(valCalcResult, GCHandleType.Pinned);
                    IntPtr dataPtrCale = dataHandleCalc.AddrOfPinnedObject();
                    //////////////////////////////////////////////////////////////////////////////////////////////////////////

                    string Tag = string.Empty;

                    //定数リスト
                    for (int i = 0; i < constantSetting.ConstantList.Length; i++)
                    {
                        string s = constantSetting.ConstantList[i].Value.ToString();
                        valConstant[i] = double.Parse(s);
                        Tag = string.Format("CONST{0}", i + 1);
                        strConstantName[i] = Tag;
                    }
                    //変数リスト
                    for (int i = 0; i < dataTagSetting.DataTagList.Length; i++)
                    {
                        Tag = string.Format("TAG{0}", i + 1);
                        strVariableName[i] = Tag;
                        valVariable[i] = 0.0F;
                    }

                    //演算式リスト
                    calcCount_o = 0;
                    int iFind1 = 0;
                    int iFind2 = 0;
                    string stTarget = string.Empty;

                    for (index = 0; index < dataTagSetting.DataTagList.Length; index++)
                    {
                        if (dataTagSetting.DataTagList[index].TagKind == 2)
                        {
                            // 演算タグがグラフに割り当てられていれば対象。
                            if (!IsAssignedGraph(dataTagSetting.DataTagList[index].TagNo))
                                continue;

                            //演算2
                            temp = dataTagSetting.DataTagList[index].Expression;

                            //call eval method
                            stTarget = GetReplace(temp);
                            string tempA = stTarget.Replace("@", "TAG");
                            temp = tempA;
                            //if (dataTagSetting != null && dataTagSetting.DataTagList != null)
                            //{
                            //    for (int k = 0; k < dataTagSetting.DataTagList.Length; k++)
                            //    {
                            //        //"@1[変位右上]"
                            //        variable = string.Format("@{0}[{1}]", k + 1, dataTagSetting.DataTagList[k].GetSystemTagName());
                            //        pos = temp.IndexOf(variable);
                            //        if (pos >= 0)
                            //        {
                            //            string tag = string.Format("TAG{0}", k + 1);
                            //            string tempV = temp.Replace(variable, tag);
                            //            temp = tempV;
                            //        }
                            //    }
                            //}

                            if (constantSetting != null && constantSetting.ConstantList != null)
                            {
                                for (int j = 0; j < constantSetting.ConstantList.Length; j++)
                                {
                                    variable = string.Format("@C{0}[{1}]", j + 1, constantSetting.ConstantList[j].GetSystemConstantName());
                                    pos = temp.IndexOf(variable);
                                    if (pos > 0)
                                    {
                                        string tempC = temp.Replace(variable, constantSetting.ConstantList[index].Value.ToString());
                                        temp = tempC;
                                    }
                                }
                            }

                            Tag = string.Format("CALC{0}", calcCount_o + 1);
                            strCalcName[calcCount_o] = Tag;
                            strExpression[calcCount_o] = temp;
                            valCalcResult[calcCount_o] = 0.0F;

                            iCalcIndex[calcCount_o] = index;
                            calcCount_o++;
                        }

                    }
                }

                //定数設定
                calc.SetConstantVal(strConstantName, valConstant);
                //変数設定
                calc.SetVariableVal(strVariableName, valVariable);
                //演算式設定
                if (calc.CalcFormulaJudge(strCalcName, strExpression, valCalcResult, ref strErrorMessage) == false)
                {
                    //error
                    //                    MessageBox.Show(strErrorMessage + "\n" + AppResource.GetString("MSG_DATA_CALCERR_MSG1"), "演算初期設定", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
                else
                {
                    //演算設定完了
                    this.calcinitFlag = true;
                    bRet = true;
                }

            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
            return bRet;
        }
Esempio n. 2
0
        /// <summary>
        /// evaluate expression
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private string EvaluateExpression(string input)
        {
            string retString = string.Empty;
            bool eval = false;
            int index = 0;
            int pos = -1;
            string temp = string.Empty;
            string variable = string.Empty;
            List<string> tagList = new List<string>();
            List<int> tagIndexList = new List<int>();
            try
            {
                temp = input;
                eval = true;
                if (eval)
                {

                    //call eval method
                    if (this.dataTagSetting != null && this.dataTagSetting.DataTagList != null)
                    {
                        bool match = false;
                        int foundIndex = 0;
                        string varName = string.Empty;
                        char[] delimiters = { '+', '-', '*', '/', '^', '(', ')' };
                        string[] data = temp.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        int indexOfHardBracket = -1;
                        int indexOfAmps = -1;
                        int strLength = 0;
                        string evalData = string.Empty;

                        string trimTemp = temp.Trim();

                        //項が2個以下の場合(四則演算としてはあり得ない)
                        if (data.Length < 2)
                        {
                            return AppResource.GetString("MSG_TAG_SELECT_INVALID") + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                        }

                        //最後が演算子で終わっている
                        string tmpconpare = trimTemp.Substring(temp.Trim().Length - 1);

                        if(tmpconpare == "+" || tmpconpare == "-" || tmpconpare == "*" || tmpconpare == "/" || tmpconpare == "^")
                        {
                            return AppResource.GetString("MSG_TAG_CALC_FINISHED_OPE") + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                        }

                        //括弧が終わっていない
                        int openCount = trimTemp.Count((x) => (x == '('));
                        int closeCount = trimTemp.Count((x) => (x == ')'));

                        if (openCount != closeCount)
                        {
                            return AppResource.GetString("MSG_TAG_CALC_PARENTHESIS_INVALID") + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                        }

                        for (int k = 0; k < data.Length; k++)
                        {
                            varName = string.Empty;
                            match = false;
                            foundIndex = -1;

                            if (data[k] == "@")
                            {
                                return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_MEAS_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                            }

                            if (data[k].Length > 1)
                            {
                                if (data[k].Substring(0, 2) == "@C")
                                {
                                    if (!Regex.IsMatch(data[k], @"@C([0-9]{1,3})"))
                                    {
                                        return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_CONST_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                    }
                                    else
                                    {
                                        if (Regex.Match(data[k], @"@C([0-9]{1,3})").Value.Length != data[k].Length)
                                        {
                                            return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_CONST_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                        }
                                    }
                                }
                                else if (data[k].Substring(0, 1) == "@")
                                {

                                    if (!Regex.IsMatch(data[k], @"@([0-9]{1,3})"))
                                    {
                                        return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_MEAS_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                    }
                                    else
                                    {
                                        if (Regex.Match(data[k], @"@([0-9]{1,3})").Value.Length != data[k].Length)
                                        {
                                            return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_MEAS_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                        }
                                    }

                                    foreach (Match mt in Regex.Matches(data[k], @"@([0-9]{1,3})"))
                                    {
                                        if (mt.Success)
                                        {
                                            //check tag No that over than 300
                                            varName = mt.Value.Substring(1);
                                            if (Convert.ToInt32(varName) > 300)
                                            {
                                                return string.Format(AppResource.GetString("MSG_TAG_CALC_INVALID_MEAS_TAGNAME"), data[k]) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                            }
                                            //get index of close bracket & next '@'
                                            indexOfHardBracket = data[k].IndexOf(']', mt.Index + 1);
                                            indexOfAmps = data[k].IndexOf('@', mt.Index + 1);
                                            for (int m = 0; m < this.dataTagSetting.DataTagList.Length; m++)
                                            {
                                                variable = string.Format("@{0}", m + 1);
                                                if (mt.Value.Equals(variable))
                                                {
                                                    //keep tag info and tag index to list
                                                    if (indexOfHardBracket > 0)
                                                    {
                                                        if (indexOfAmps > 0)
                                                        {
                                                            strLength = (indexOfAmps > indexOfHardBracket ? indexOfHardBracket : indexOfAmps) - mt.Index + 1;
                                                        }
                                                        else
                                                        {
                                                            strLength = indexOfHardBracket - mt.Index + 1;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (indexOfAmps > 0)
                                                        {
                                                            strLength = indexOfAmps - mt.Index + 1;
                                                        }
                                                        else
                                                        {
                                                            strLength = -1;
                                                        }
                                                    }
                                                    if (strLength > 0)
                                                    {
                                                        tagList.Add(data[k].Substring(mt.Index, strLength));
                                                    }
                                                    else
                                                    {
                                                        tagList.Add(data[k].Substring(mt.Index));
                                                    }
                                                    tagIndexList.Add(m);
                                                    match = true;
                                                    varName = mt.Value;
                                                    foundIndex = m;

                                                    if (match)
                                                    {
                                                        match = false;

                                                        DataTag tmptag = this.dataTagSetting.DataTagList[foundIndex];

                                                        if (tmptag.TagKind == 2)
                                                        {
                                                            return AppResource.GetString("MSG_TAG_CALC_SELECTED_INCALC") + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                                        }

                                                        //if (this.currentTag != null)
                                                        //{
                                                        //    if (this.currentTag.TagNo == this.dataTagSetting.DataTagList[foundIndex].TagNo)
                                                        //    {
                                                        //        match = true;
                                                        //    }
                                                        //    //check edition tag with other calc tag
                                                        //    else if (this.currentTag.TagKind == 2 || (!IsMeasure && this.currentTag.IsBlank))
                                                        //    {
                                                        //        if (this.dataTagSetting.DataTagList[foundIndex].TagKind == 2 || (!IsMeasure && this.dataTagSetting.DataTagList[foundIndex].IsBlank))
                                                        //        {
                                                        //            match = true;
                                                        //        }
                                                        //    }
                                                        //if (match)
                                                        //{
                                                        //CalcBtmEnabled(true);
                                                        //return AppResource.GetString("MSG_TAG_CALC_SELECTED_INCALC") + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                                                        //}
                                                        //}
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    for (int n = 0; n < tagList.Count; n++)
                    {
                        temp = temp.Replace(tagList[n], string.Format("TAG{0}", tagIndexList[n] + 1));
                    }
                    if (this.constantSetting != null && this.constantSetting.ConstantList != null)
                    {
                        for (index = 0; index < this.constantSetting.ConstantList.Length; index++)
                        {
                            variable = string.Format("@C{0}[{1}]", index + 1, this.constantSetting.ConstantList[index].GetSystemConstantName());
                            pos = temp.IndexOf(variable);
                            if (pos > 0)
                            {
                                string tempC = temp.Replace(variable, this.constantSetting.ConstantList[index].Value.ToString());
                                temp = tempC;
                            }
                            else
                            {
                                variable = string.Format("@C{0}", index + 1);
                                pos = temp.IndexOf(variable);
                                if (pos > 0)
                                {
                                    string tempC = temp.Replace(variable, this.constantSetting.ConstantList[index].Value.ToString());
                                    temp = tempC;
                                }
                            }
                        }
                    }

                    //evaluate expression
                    string strErrorMessage = string.Empty;
                    double[] valConstant = new double[this.constantSetting.ConstantList.Length];
                    string[] strConstantName = new string[this.constantSetting.ConstantList.Length];
                    string[] strVariableName = new string[this.dataTagSetting.DataTagList.Length];
                    string[] strCalcName = new string[1];
                    string[] strExpression = new string[1];
                    Calc calc = new Calc();
                    string Tag = string.Empty;

                    for (int i = 0; i < this.constantSetting.ConstantList.Length; i++)
                    {
                        string s = this.constantSetting.ConstantList[i].Value.ToString();
                        valConstant[i] = double.Parse(s);
                        Tag = string.Format("CONST{0}", i + 1);
                        strConstantName[i] = Tag;
                    }
                    for (int i = 0; i < this.dataTagSetting.DataTagList.Length; i++)
                    {
                        Tag = string.Format("TAG{0}", i + 1);
                        strVariableName[i] = Tag;
                    }
                    strCalcName[0] = string.Format("CALC1");
                    strExpression[0] = temp;

                    calc.SetConstantVal(strConstantName, valConstant);
                    calc.SetVariableVal(strVariableName, 1.0F);
                    if (calc.CalcFormulaJudge(strCalcName, strExpression, ref strErrorMessage) == false)
                    {
                        retString =
                            CalcCommon.Calc.GetErrString(strErrorMessage) + "\n" + AppResource.GetString("MSG_TAGSETTING_NG_EXPRESSION");
                    }
                    else
                    {
                        // expression is OK - AppResource.GetString("MSG_TAGSETTING_OK_EXPRESSION")
                        retString = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                retString = ex.Message;
            }

            return retString;
        }