public void GeneratePictures(Models.RowData modelRowData, System.Windows.Forms.TextBox textBox)
        {
            try
            {
                foreach (Process proc in Process.GetProcessesByName("Mtb"))
                {
                    proc.Kill();
                }
            }
            catch (Exception ex)
            {
            }

            Mtb.Application MtbApp = new Mtb.Application();
            MtbApp.UserInterface.Visible = true;
            //创建图片文件夹
            textBox.AppendText("Creating picture folder:" + modelRowData.filePath + "\r\n");
            if (!Directory.Exists(modelRowData.filePath))         //判断文件夹是否已经存在
            {
                Directory.CreateDirectory(modelRowData.filePath); //创建文件夹
            }
            textBox.AppendText("Generating pictures...\r\n");
            foreach (Models.Instance modelInstance in modelRowData.instances)
            {
                try
                {
                    textBox.AppendText("    " + modelInstance.title + "    ");
                    GeneratePicturesInstance(MtbApp, modelInstance, modelRowData);
                    textBox.AppendText("Succeed\r\n");
                }
                catch
                {
                    Mtb.Project MtbProj = MtbApp.ActiveProject;
                    MtbProj.Delete();
                    MtbApp.New();
                    textBox.AppendText("Failed\r\n");
                }
            }
            textBox.AppendText("Generate pictures succeed\r\n");
            MtbApp.Quit();
        }
        public void GeneratePicturesInstance(Mtb.Application MtbApp, Models.Instance modelInstance, Models.RowData modelRowData)
        {
            Mtb.Project MtbProj = MtbApp.ActiveProject;

            parseColumn(modelInstance, modelRowData);
            //计算标准差
            CalculateSTDVE(column3, modelInstance);

            Mtb.Columns MtbColumns = MtbProj.ActiveWorksheet.Columns;
            Mtb.Column  MtbColumn1 = MtbColumns.Add(null, null, 1);
            MtbColumn1.SetData(column1.ToArray());

            Mtb.Column MtbColumn2 = MtbColumns.Add(null, null, 1);
            MtbColumn2.SetData(column2.ToArray());

            Mtb.Column MtbColumn3 = MtbColumns.Add(null, null, 1);
            MtbColumn3.SetData(column3.ToArray());

            try
            {
                string imgPath = Path.Combine(modelRowData.filePath, parseFileName(modelInstance.title) + " Process Capability");
                string lspec   = Double.IsNaN(modelInstance.LCL) ? "" : "Lspec " + modelInstance.LCL.ToString("f3") + ";";
                string uspec   = Double.IsNaN(modelInstance.UCL) ? "" : "Uspec " + modelInstance.UCL.ToString("f3") + ";";
                MtbProj.ExecuteCommand(" Capa C3 " + column1.Count + ";  " + lspec + uspec + "   Pooled;   AMR;   UnBiased;   OBiased;   Toler 6;   Within;   Percent;   Title \"" + getPictureTitle(0, modelInstance) + "\";   CStat.");
                Mtb.Graph MtbGraph = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1).Graph;
                MtbGraph.SaveAs(imgPath, true, Mtb.MtbGraphFileTypes.GFPNGHighColor, 768, 531);
                modelInstance.pictures.Add(imgPath + ".png");
            }
            catch
            {
                modelInstance.pictures.Add(null);
            }

            try
            {
                string imgPath = Path.Combine(modelRowData.filePath, parseFileName(modelInstance.title) + " Individual Polt");
                MtbProj.ExecuteCommand("  Indplot ( C3 ) * C1;   Title \"" + getPictureTitle(1, modelInstance) + "\";   Individual.");
                Mtb.Graph MtbGraph2 = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1).Graph;
                MtbGraph2.SaveAs(imgPath, true, Mtb.MtbGraphFileTypes.GFPNGHighColor, 768, 531);
                modelInstance.pictures.Add(imgPath + ".png");
            }
            catch
            {
                modelInstance.pictures.Add(null);
            }
            try
            {
                string imgPath = Path.Combine(modelRowData.filePath, parseFileName(modelInstance.title) + " Scatter Plot");
                MtbProj.ExecuteCommand("  Plot C3*C2;   Symbol C1;   Title \"" + getPictureTitle(2, modelInstance) + "\";   JITTER.");
                Mtb.Graph MtbGraph3 = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1).Graph;
                MtbGraph3.SaveAs(imgPath, true, Mtb.MtbGraphFileTypes.GFPNGHighColor, 768, 531);
                modelInstance.pictures.Add(imgPath + ".png");
            }
            catch
            {
                modelInstance.pictures.Add(null);
            }
            try
            {
                string imgPath = Path.Combine(modelRowData.filePath, parseFileName(modelInstance.title) + " Probability Plot");
                MtbProj.ExecuteCommand(" PPlot C3;   Normal;   Symbol;   FitD;     NoCI;   Grid 2;   Grid 1;   MGrid 1;   Title \"" + getPictureTitle(3, modelInstance) + "\".");
                Mtb.Graph MtbGraph4 = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1).Graph;
                MtbGraph4.SaveAs(imgPath, true, Mtb.MtbGraphFileTypes.GFPNGHighColor, 768, 531);
                modelInstance.pictures.Add(imgPath + ".png");
            }
            catch
            {
                modelInstance.pictures.Add(null);
            }
            MtbProj.Delete();
            MtbApp.New();
        }
        public Dictionary <string, object> MtbAnalysis(string chartType, Dictionary <string, string> rules)
        {
            try
            {
                foreach (Process proc in Process.GetProcessesByName("Mtb"))
                {
                    proc.Kill();
                }
            }
            catch (Exception ex)
            {}
            System.IO.FileInfo imgFile = new System.IO.FileInfo(imgDir + this.controlId + "_" + chartType + ".PNG");
            if (imgFile.Exists)
            {
                imgFile.Delete();
            }

            Mtb.Application             MtbApp       = new Mtb.Application();
            Dictionary <string, object> resultObject = new Dictionary <string, object>();

            try
            {
                MtbApp.UserInterface.Visible = false;
                Mtb.Project MtbProj = MtbApp.ActiveProject;

                Mtb.Columns MtbColumns = MtbProj.ActiveWorksheet.Columns;
                //加载dt所有数据到mtb
                for (int i = 0; i < endColumn; i++)
                {
                    Mtb.Column MtbColumn1 = MtbColumns.Add(null, null, 1);
                    ArrayList  columnData = new ArrayList();
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt.Rows[j][i].ToString().Length < 1)
                        {
                            columnData.Add(DBNull.Value);
                        }
                        else
                        {
                            columnData.Add(dt.Rows[j][i]);
                        }
                    }
                    MtbColumn1.SetData(columnData.ToArray());
                }

                int rawColumnCount = MtbColumns.Count;

                //Minitab 控制图 or 能力分析命令。
                //命令先生成,先调用checkRule()方法检查规则数量,以及对应生成的检验结果列数
                string execAnalysis = this.GetExcuteCommands(chartType, rules);

                //Minitab 分析结果存储列命令
                string varCommand = this.GetVarCommands(chartType);

                if (!string.IsNullOrEmpty(varCommand))
                {
                    MtbProj.ExecuteCommand(varCommand);
                }
                if (!string.IsNullOrEmpty(execAnalysis))
                {
                    MtbProj.ExecuteCommandAsync(execAnalysis);
                }

                Mtb.Output MtbOut = null;
                try {
                    MtbOut = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1);
                    Console.WriteLine("Normal output!Normal output!Normal output!");
                }
                catch (Exception ex)
                {
                    MtbOut = MtbProj.Commands.Item(MtbProj.Commands.Count).Outputs.Item(1);
                    Console.WriteLine("Unormal output!Unormal output!Unormal output!");
                }

                //Minitab 图形保存
                Mtb.Graph MtbGraph = MtbOut.Graph;
                MtbGraph.SaveAs(imgDir + this.controlId + "_" + chartType, true, Mtb.MtbGraphFileTypes.GFPNGHighColor, 790, 500);

                //Minitab 工作区表保存
                Mtb.Worksheet MtbSheet = MtbProj.ActiveWorksheet;
                MtbSheet.SaveAs(imgDir + this.controlId + "_" + chartType, true);

                for (int i = 1; i <= MtbSheet.Columns.Count; i++)
                {
                    resultObject.Add(MtbSheet.Columns.Item(i).Name, MtbSheet.Columns.Item(i).GetData());
                }
                if (chartType.Contains("Chart"))
                {
                    resultObject.Add("RuleOut", MtbProj.Commands.OutputDocument.Text);
                }
                MtbProj.Delete();
                MtbApp.Quit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally { MtbApp.Quit(); }
            return(resultObject);
        }