private void buttonCancel_Click(object sender, EventArgs e)
        {
            TreeNode      node = treeViewWeightSortNode.SelectedNode;
            WeightFormula wf   = (WeightFormula)node.Tag;

            textBoxFormula.Text     = wf.Formula;
            textBoxFormula.Modified = false;
            SetEditMode(1);
        }
        private void treeViewWeightSortNode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            SetEditMode(1);

            TreeNode node = e.Node;

            if (node.Nodes.Count == 0)
            {
                WeightFormula wf = (WeightFormula)node.Tag;

                if (wf == null)
                {
                    MessageBox.Show("异常错误!节点无对应公式!");
                    return;
                }
                textBoxFormula.Text = wf.Formula;

                SetParaList(wf.ParaList);

                buttonEdit.Enabled = true;

                buttonSelect.Enabled = (listBoxCandidateFormula.Items.Count > 0);
            }
            else
            {
                listViewPara.Items.Clear();

                List <TreeNode> ret = new List <TreeNode>();
                GetFinalNodeList(node, ref ret);

                HashSet <WeightParameter> wpSet = new HashSet <WeightParameter>();

                foreach (TreeNode finalnode in ret)
                {
                    WeightFormula wf = (WeightFormula)finalnode.Tag;
                    if (wf == null)
                    {
                        MessageBox.Show("异常错误!节点无对应公式!");
                        return;
                    }
                    foreach (WeightParameter wp in wf.ParaList)
                    {
                        wpSet.Add(wp);
                    }
                }
                SetParaList(wpSet);

                textBoxFormula.Text  = "组合节点无公式";
                buttonEdit.Enabled   = false;
                buttonSelect.Enabled = false;
            }
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            TreeNode      node = treeViewWeightSortNode.SelectedNode;
            NodeFormula   nf   = (NodeFormula)node.Tag;
            WeightFormula wf   = nf[nEditFormulaIndex];

            if (nEditFormulaIndex == 0)
            {
                textBoxFormulaX.Text     = wf.Formula;
                textBoxFormulaX.Modified = false;
            }
            else
            {
                textBoxFormulaY.Text     = wf.Formula;
                textBoxFormulaY.Modified = false;
            }

            SetEditMode(1);
        }
        private void buttonEdit_Click(object sender, EventArgs e)
        {
            TreeNode      node = treeViewWeightSortNode.SelectedNode;
            WeightFormula wf   = (WeightFormula)node.Tag;

            dictTempPara.Clear();

            foreach (WeightParameter wp in wf.ParaList)
            {
                //if (WeightArithmetic.FindGlobleParameters(wp.ParaName, null).Count > 1)
                //{
                dictTempPara.Add(wp.ParaName, wp.ParaUnit);
                //}
            }

            SetEditMode(2);
            textBoxFormula.Focus();
            textBoxFormula.SelectionStart = textBoxFormula.TextLength;
        }
        private void OnCurWaChanged()
        {
            curExprList = new List <CExpression>();

            treeViewClass.Nodes[0].Nodes.Clear();

            string errmsg;

            foreach (NodeFormula nf in curWa.FormulaList)
            {
                treeViewClass.Nodes[0].Nodes.Add(nf.NodeName);

                for (int i = 0; i < 2; ++i)
                {
                    WeightFormula wf   = nf[i];
                    CExpression   expr = CExpression.Parse(wf.Formula, out errmsg);
                    if (expr == null)
                    {
                        string outmsg = "公式\"" + wf.Formula + "\"错误:" + errmsg;
                        XLog.Write(outmsg);
                        MessageBox.Show(outmsg);
                        return;
                    }
                    curExprList.Add(expr);
                }
            }

            treeViewClass.Nodes[0].Expand();

            curWaParas = curWa.GetParaList();

            dataGridViewParaInput.Rows.Clear();

            foreach (WeightParameter wp in curWaParas)
            {
                dataGridViewParaInput.Rows.Add(new object[] { wp.ParaName, wp.ParaValue, wp.ParaUnit, WeightParameter.ParaTypeList[wp.ParaType], wp.ParaRemark });
            }

            btnCompute.Enabled = true;
            flowLayoutPanelParaImport.Enabled = true;
            flowLayoutPanelParaExport.Enabled = true;
        }
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (!bEditProject)
            {
                txtWeightEstName.Text = txtWeightEstName.Text.Trim();
                if (txtWeightEstName.Text.Length == 0)
                {
                    MessageBox.Show("设计数据名称不能为空!");
                    return;
                }

                if (Verification.IsCheckString(txtWeightEstName.Text))
                {
                    MessageBox.Show("设计数据名称含有非法字符");
                    return;
                }

                curWa.DataName = txtWeightEstName.Text;
            }

            for (int i = 0; i < curWa.FormulaList.Count; ++i)
            {
                CExpression expr = curExprList[2 * i];
                foreach (WeightParameter wp in curWa.FormulaList[i].XFormula.ParaList)
                {
                    expr.SetVariableValue(wp.ParaName, wp.ParaValue);
                }
                curWa.FormulaList[i].XFormula.Value = expr.Run();

                expr = curExprList[2 * i + 1];
                foreach (WeightParameter wp in curWa.FormulaList[i].YFormula.ParaList)
                {
                    expr.SetVariableValue(wp.ParaName, wp.ParaValue);
                }
                curWa.FormulaList[i].YFormula.Value = expr.Run();
            }

            // curWa.WriteArithmeticFile("test.xml", true);

            if (mainForm.designProjectData == null)
            {
                CoreDesignProject coreForm = new CoreDesignProject(mainForm, "new");
                coreForm.ShowDialog();
            }

            if (mainForm.designProjectData.lstCoreEnvelopeDesign == null)
            {
                mainForm.designProjectData.lstCoreEnvelopeDesign = new List <Model.CoreEnvelopeArithmetic>();
            }


            Dictionary <WeightParameter, WeightParameter> wpDict = new Dictionary <WeightParameter, WeightParameter>();

            List <WeightFormula> FormulaList = new List <WeightFormula>();

            foreach (NodeFormula ndformula in curWa.FormulaList)
            {
                for (int i = 0; i < 2; ++i)
                {
                    WeightFormula          formula  = ndformula[i];
                    List <WeightParameter> ParaList = new List <WeightParameter>();
                    foreach (WeightParameter para in formula.ParaList)
                    {
                        WeightParameter tempWp = null;
                        if (!wpDict.ContainsKey(para))
                        {
                            tempWp = new WeightParameter(para);
                            wpDict.Add(para, tempWp);
                        }
                        else
                        {
                            tempWp = wpDict[para];
                        }
                        ParaList.Add(tempWp);
                    }
                    formula.ParaList = ParaList;
                }
            }
            if (!bEditProject)
            {
                mainForm.designProjectData.lstCoreEnvelopeDesign.Add(curWa);
                mainForm.BindProjectTreeData(mainForm.designProjectData);
                mainForm.SetCoreEnvelopeDesignTab(curWa, mainForm.designProjectData.lstCoreEnvelopeDesign.Count - 1);
                XLog.Write("设计数据\"" + txtWeightEstName.Text + "\"计算完成!");
            }
            else
            {
                //设置页面数据
                mainForm.SetCoreEnvelopeDesignResult(curWa);
                XLog.Write("设计数据\"" + txtWeightEstName.Text + "\"重新计算完成!");
            }

            DialogResult = DialogResult.OK;
            Close();
        }
        private void buttonApply_Click(object sender, EventArgs e)
        {
            string errmsg;

            ZstExpression.CExpression expr = ZstExpression.CExpression.Parse(textBoxFormulaEdit.Text, out errmsg);

            if (expr == null)
            {
                MessageBox.Show(errmsg);
                return;
            }
            textBoxFormulaEdit.Text = expr.GetExpression();

            TreeNode      node = treeViewWeightSortNode.SelectedNode;
            NodeFormula   nf   = (NodeFormula)node.Tag;
            WeightFormula wf   = nf[nEditFormulaIndex];

            wf.Formula = textBoxFormulaEdit.Text;
            List <string> paras = new List <string>();

            expr.GetVariables(ref paras);

            wf.ParaList.Clear();

            foreach (string paraname in paras)
            {
                WeightParameter wp = null;
                if (dictTempPara.ContainsKey(paraname))
                {
                    wp = FindParameter(paraname, dictTempPara[paraname]);
                }
                else
                {
                    wp = FindParameter(paraname, null);
                }
                if (wp == null)
                {
                    wp          = new WeightParameter();
                    wp.ParaName = paraname;
                    wp.ParaType = 10;
                }
                wf.ParaList.Add(wp);
            }
            HashSet <WeightParameter> wpSet = new HashSet <WeightParameter>();

            foreach (WeightParameter wp in nf.XFormula.ParaList)
            {
                wpSet.Add(wp);
            }
            foreach (WeightParameter wp in nf.YFormula.ParaList)
            {
                wpSet.Add(wp);
            }
            SetParaList(wpSet);

            if (nEditFormulaIndex == 0)
            {
                textBoxFormulaX.Text     = wf.Formula;
                textBoxFormulaX.Modified = false;
            }
            else
            {
                textBoxFormulaY.Text     = wf.Formula;
                textBoxFormulaY.Modified = false;
            }

            SetEditMode(1);

            dateTimePickerLastModifyTime.Value = DateTime.Today;
        }
        private void comboBoxWeightSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nwsort = comboBoxWeightSort.SelectedIndex;

            if (nwsort == -1)
            {
                return;
            }
            List <WeightSortData> wsDataList = WeightSortManageForm.GetListWeightSortData();

            WeightSortEditForm.BindTreeList(treeViewWeightSortNode, wsDataList[nwsort]);

            List <TreeNode> FinalNodes = new List <TreeNode>();

            foreach (TreeNode node in treeViewWeightSortNode.Nodes)
            {
                GetFinalNodeList(node, ref FinalNodes);
            }

            if (strType == "jynew")
            {
                waData.FormulaList.RemoveRange(mapCandidateFormula.Count, waData.FormulaList.Count - mapCandidateFormula.Count);

                foreach (KeyValuePair <WeightFormula, string> wfpair in mapCandidateFormula)
                {
                    wfpair.Key.nAttach = 0;
                    foreach (TreeNode node in FinalNodes)
                    {
                        if (node.Name == wfpair.Value)
                        {
                            wfpair.Key.nAttach = 1;
                            node.Tag           = wfpair.Key;
                            break;
                        }
                    }
                }

                listBoxCandidateFormula.Items.Clear();
                foreach (KeyValuePair <WeightFormula, string> wfpair in mapCandidateFormula)
                {
                    if (wfpair.Key.nAttach == 0)
                    {
                        listBoxCandidateFormula.Items.Add(wfpair.Value);
                    }
                }
            }
            else if (waData.FormulaList.Count > 0)
            {
                foreach (WeightFormula wf in waData.FormulaList)
                {
                    wf.nAttach = 0;
                    foreach (TreeNode node in FinalNodes)
                    {
                        if (node.Name == wf.NodePath)
                        {
                            wf.nAttach = 1;
                            node.Tag   = wf;
                            break;
                        }
                    }
                }
            }

            foreach (TreeNode node in FinalNodes)
            {
                if (node.Tag == null)
                {
                    WeightFormula wf = new WeightFormula();
                    wf.NodePath = node.Name;
                    wf.nAttach  = 1;
                    node.Tag    = wf;
                    waData.FormulaList.Add(wf);
                }
            }

            treeViewWeightSortNode.ExpandAll();
            SetEditMode(1);
            textBoxFormula.Text     = "";
            textBoxFormula.Modified = false;
            buttonEdit.Enabled      = false;
            buttonSelect.Enabled    = false;
            listViewPara.Items.Clear();
        }
        private void buttonApply_Click(object sender, EventArgs e)
        {
            WeightFormula wf = null;

            if (groupBoxSelectFormula.Visible)
            {
                TreeNode node = treeViewWeightSortNode.SelectedNode;

                WeightFormula oldwf = (WeightFormula)node.Tag;
                oldwf.nAttach       = 0;
                wf                  = mapCandidateFormula.First(temppair => temppair.Value == (string)listBoxCandidateFormula.SelectedItem).Key;
                node.Tag            = wf;
                wf.NodePath         = node.Name;
                wf.nAttach          = 1;
                textBoxFormula.Text = wf.Formula;
                listBoxCandidateFormula.Items.Remove(listBoxCandidateFormula.SelectedItem);

                if (mapCandidateFormula.ContainsKey(oldwf))
                {
                    listBoxCandidateFormula.Items.Add(mapCandidateFormula[oldwf]);
                }
            }
            else
            {
                string errmsg;
                ZstExpression.CExpression expr = ZstExpression.CExpression.Parse(textBoxFormula.Text, out errmsg);

                if (expr == null)
                {
                    MessageBox.Show(errmsg);
                    return;
                }
                textBoxFormula.Text = expr.GetExpression();

                TreeNode node = treeViewWeightSortNode.SelectedNode;
                wf = (WeightFormula)node.Tag;

                wf.Formula = textBoxFormula.Text;
                List <string> paras = new List <string>();
                expr.GetVariables(ref paras);

                wf.ParaList.Clear();

                foreach (string paraname in paras)
                {
                    WeightParameter wp = null;
                    if (dictTempPara.ContainsKey(paraname))
                    {
                        wp = FindParameter(paraname, dictTempPara[paraname]);
                    }
                    else
                    {
                        wp = FindParameter(paraname, null);
                    }
                    if (wp == null)
                    {
                        wp          = new WeightParameter();
                        wp.ParaName = paraname;
                        wp.ParaType = 10;
                    }
                    wf.ParaList.Add(wp);
                }
            }
            SetParaList(wf.ParaList);
            textBoxFormula.Modified = false;
            SetEditMode(1);

            dateTimePickerLastModifyTime.Value = DateTime.Today;
        }
        private void comboBoxWeightSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nwsort = comboBoxWeightSort.SelectedIndex;
            if (nwsort == -1)
            {
                return;
            }
            List<WeightSortData> wsDataList = WeightSortManageForm.GetListWeightSortData();

            WeightSortEditForm.BindTreeList(treeViewWeightSortNode, wsDataList[nwsort]);

            List<TreeNode> FinalNodes = new List<TreeNode>();
            foreach (TreeNode node in treeViewWeightSortNode.Nodes)
            {
                GetFinalNodeList(node, ref FinalNodes);
            }

            if (strType == "jynew")
            {
                waData.FormulaList.RemoveRange(mapCandidateFormula.Count, waData.FormulaList.Count - mapCandidateFormula.Count);

                foreach (KeyValuePair<WeightFormula, string> wfpair in mapCandidateFormula)
                {
                    wfpair.Key.nAttach = 0;
                    foreach (TreeNode node in FinalNodes)
                    {
                        if (node.Name == wfpair.Value)
                        {
                            wfpair.Key.nAttach = 1;
                            node.Tag = wfpair.Key;
                            break;
                        }
                    }
                }

                listBoxCandidateFormula.Items.Clear();
                foreach (KeyValuePair<WeightFormula, string> wfpair in mapCandidateFormula)
                {
                    if (wfpair.Key.nAttach == 0)
                    {
                        listBoxCandidateFormula.Items.Add(wfpair.Value);
                    }
                }
            }
            else if (waData.FormulaList.Count > 0)
            {
                foreach (WeightFormula wf in waData.FormulaList)
                {
                    wf.nAttach = 0;
                    foreach (TreeNode node in FinalNodes)
                    {
                        if (node.Name == wf.NodePath)
                        {
                            wf.nAttach = 1;
                            node.Tag = wf;
                            break;
                        }
                    }
                }
            }

            foreach (TreeNode node in FinalNodes)
            {
                if (node.Tag == null)
                {
                    WeightFormula wf = new WeightFormula();
                    wf.NodePath = node.Name;
                    wf.nAttach = 1;
                    node.Tag = wf;
                    waData.FormulaList.Add(wf);
                }
            }

            treeViewWeightSortNode.ExpandAll();
            SetEditMode(1);
            textBoxFormula.Text = "";
            textBoxFormula.Modified = false;
            buttonEdit.Enabled = false;
            buttonSelect.Enabled = false;
            listViewPara.Items.Clear();
        }