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(); }
/// <summary> /// 關閉 Minitab 程式 /// </summary> public static void Quit() { try { App.Quit(); System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("Mtb"); for (int i = proc.Length; i-- > 0;) { if (proc[i].Id == appID) { proc[i].Kill(); //只將目前使用的 Minitab 關掉 } } } catch { } finally { App = null; GC.Collect(); } }
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); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Mtb.Application mtbApp = new Mtb.Application(); mtbApp.UserInterface.DisplayAlerts = false; mtbApp.UserInterface.Visible = true; Mtb.Project proj = mtbApp.ActiveProject; Mtb.Worksheet ws; try { proj.Worksheets.Open(@"C:\Users\Yalibuda\Desktop\data_16.mtw"); ws = proj.ActiveWorksheet; } catch { mtbApp.Quit(); return; } Mtb.Column rawDataCol = ws.Columns.Item("VALUE"); double mean = Mtblib.Tools.MtbTools.MISSINGVALUE; double stdev = Mtblib.Tools.MtbTools.MISSINGVALUE; double maximum = Mtblib.Tools.MtbTools.MISSINGVALUE; double minimum = Mtblib.Tools.MtbTools.MISSINGVALUE; try { mean = ((double[])rawDataCol.GetData()).Where(x => x < Mtblib.Tools.MtbTools.MISSINGVALUE).Average(); stdev = ((double[])rawDataCol.GetData()).Where(x => x < Mtblib.Tools.MtbTools.MISSINGVALUE).StdDev(); maximum = ((double[])rawDataCol.GetData()).Where(x => x < Mtblib.Tools.MtbTools.MISSINGVALUE).Max(); minimum = ((double[])rawDataCol.GetData()).Where(x => x < Mtblib.Tools.MtbTools.MISSINGVALUE).Min(); } catch (Exception ee) { MessageBox.Show(ee.Message); } Console.WriteLine("Stdev:{0}", stdev); //double sstdev = ((double[])ws.Columns.Item("C10").GetData()).StdDev(); //Console.Write(sstdev); //Chart barchart = new Chart(proj,ws); //barchart.Variables = "C10 C11"; //barchart.BarsRepresent = Chart.ChartRepresent.TWO_WAY_TABLE; //barchart.GroupingVariables = "C2 C3"; //barchart.Bar.GroupingBy = "C2 C3"; //barchart.NoMissing = true; //barchart.NoEmpty = true; //string path2 = MtbTools.BuildTemporaryMacro("mycode.mtb", barchart.GetCommand()); //proj.ExecuteCommand(string.Format("% \"{0}\" 1", path2)); //proj.Worksheets.Open(@"D:\Dropbox\Workspace\Test\test20160611.mtw"); //proj.Worksheets.Open(@"D:\Dropbox\Workspace\03.PFG\06.Dataset\熔爐資料\FurnWall.mtw"); //proj.Worksheets.Open(@"D:\Dropbox\Workspace\Test\test20160810.mtw"); //ws = proj.ActiveWorksheet; //Mtb.Column[] cols = new Mtb.Column[] { // ws.Columns.Item("C1"), // ws.Columns.Item("C2"), // ws.Columns.Item("C3"), // ws.Columns.Item("C4"), // ws.Columns.Item("C5"), // ws.Columns.Item("C6") //}; //ws.Columns.Add().Name="xxx"; //ws.Columns.Item("xxx").SetData("a", 1, 10); //System.Data.DataTable dt = Mtblib.Tools.MtbTools.GetDataTableFromMtbCols(cols); //System.Data.DataTable result = Mtblib.Tools.MtbTools.Apply( // "C3", Mtblib.Tools.Arithmetic.NMiss, new string[] { "C1", "C2" }, dt); //proj.Worksheets.Open(@"D:\Dropbox\Workspace\03.PFG\06.Dataset\熔爐資料\FurnWall.mtw"); //ws = proj.ActiveWorksheet; //double[] data = ws.Columns.Item("C5").GetData(); //string[] name = ws.Columns.Item("C3").GetData(); //DateTime[] timeorder = ws.Columns.Item("C4").GetData(); //var data1 = data.Zip(name, (x, y) => new { Value = x, Name = y }); //var datas = data1.Zip(timeorder, (x, y) => new { Value = x.Value, Name = x.Name, Time = y }).ToArray(); //string num = "17"; //var subdata = datas.Where(x => x.Name == num).Select(x => x.Value).ToArray(); //Mtblib.Stat.ChangePoints.ChangePointInfo[] changePts = Mtblib.Stat.ChangePoints.Run(subdata); //System.Text.StringBuilder cmnd = new System.Text.StringBuilder(); //cmnd.AppendLine("plot c5*c4;"); //cmnd.AppendLine("symb;"); //cmnd.AppendLine("conn;"); //cmnd.AppendLine("Include;"); //cmnd.AppendFormat("where \"c3=\"\"{0}\"\" \";\r\n",num); //dynamic xvalue = datas.Where(x => x.Name == num).Select(x => x.Time).ToArray(); //double[] dxvalue; //if (xvalue is DateTime[]) //{ // dxvalue = ((DateTime[])xvalue).Select(x => x.ToOADate()).ToArray(); //} //else //{ // dxvalue = xvalue; //} //if (changePts != null) //{ // cmnd.AppendLine("refe 1 &"); // foreach (var item in changePts) // { // Console.WriteLine("Change Points={0}, Confidence level={1}", item.Index, item.ConfidenceLevel); // cmnd.AppendFormat("{0} &\r\n", dxvalue[item.Index + 1].ToString()); // } // cmnd.AppendLine(";"); //} //cmnd.AppendFormat("title \"爐區={0}\";\r\n", num); //cmnd.AppendLine("."); //proj.ExecuteCommand(cmnd.ToString()); //List<double[]> d = new List<double[]>(); //d.Add(ws.Columns.Item("C3").GetData()); //d.Add(ws.Columns.Item("C4").GetData()); //d.Add(ws.Columns.Item("C5").GetData()); //Mtblib.Tools.GScale[] gg = Mtblib.Tools.MtbTools.GetMinitabGScaleInfo(d, proj, ws); // Test //Mtb.Application mtbApp = new Mtb.Application(); //mtbApp.UserInterface.Visible = true; //mtbApp.UserInterface.DisplayAlerts = false; //Mtb.Project proj = mtbApp.ActiveProject; // summary data //proj.Worksheets.Open(@"D:\Dropbox\Workspace\01.chipMOS\06.Dataset\Top 3 Cust.mtw"); // stacked data //proj.Worksheets.Open(@"D:\Dropbox\Workspace\Test\test20160611.mtw"); //Mtb.Worksheet ws = proj.ActiveWorksheet; // count data //proj.Worksheets.Open(@"D:\Dropbox\Workspace\Test\test20160628.mtw"); //Mtb.Worksheet ws = proj.ActiveWorksheet; ////Scatter plot //Plot p = new Plot(proj, ws); //p.YVariables = "C4 C5"; //p.XVariables = "C3"; //p.GraphType = Plot.MultipleGraphType.Overlay; //p.YScale.SecScale.Variable = "C5"; //p.YScale.SecScale.Label.Text= "HEHEHE"; //p.YScale.Label.Text = "AAAAA"; //p.XScale.Ticks.SetLabels(new string[] { "\"A\"","\"B\"" }); //p.Connectline.Visible = true; //Console.WriteLine(p.GetCommand()); //proj.ExecuteCommand(p.GetCommand()); //HighlevelBarLinePlot hchart = new HighlevelBarLinePlot(proj,ws); //hchart.chart.Variables = "C3"; //hchart.chart.GroupingVariables = "C1 C2"; //hchart.chart.Bar.GroupingBy = "C2"; //hchart.chart.FuncType = Chart.ChartFunctionType.MEAN; //hchart.chart.Title.Text = "AAA"; //hchart.chart.XScale.Ticks.TShow = 1; //hchart.Run(); // Chart //using (Chart barchart = new Chart(proj, ws)) //{ // // Chart 的基本設定 (Two-way table) // //barchart.BarsRepresent = Chart.ChartRepresent.TWO_WAY_TABLE; // //barchart.Variables = "C4-C7"; // //barchart.GroupingVariables = "C1"; // //Chart 的基本設定 (stacked data) // //barchart.FuncType = Chart.ChartFunctionType.MEAN; // //barchart.BarsRepresent = Chart.ChartRepresent.A_FUNCTION_OF_A_VARIABLE; // //barchart.Variables = "C3"; // //barchart.GroupingVariables = "C1-C2"; // //Chart 的基本設定 (count data) // barchart.BarsRepresent = Chart.ChartRepresent.COUNT_OF_UNIQUE_VALUES; // barchart.Variables = "C1"; // barchart.GroupingVariables = "C2"; // barchart.AdjDatlabAtStackBar = true; // barchart.DataLabel.Visible = true; // barchart.DataLabel.DatlabType = Datlab.DisplayType.Column; // barchart.StackType = Chart.ChartStackType.Stack; // barchart.XScale.Label.Text = "AAAAA"; // barchart.XScale.Ticks.TShow = 1; // barchart.YScale.Label.Text = "BBBB"; // barchart.Bar.AssignAttributeByVariables = true; // //barchart.Panel.PaneledBy = "C1"; // //barchart.Panel.RowColumn = new int[] { 3, 1 }; // barchart.FootnoteLst.Add(new Footnote() { Text = "CCCCC" }); // //barchart.FigureRegion.SetCoordinate(0, 1, 0, 0.6); // barchart.Title.Visible = false; // barchart.Title.Text = "xxxx"; // barchart.ShowSeparateSubTitle = false; // barchart.ShowPersonalSubTitle = false; // barchart.XScale.Refes.Values = 1.5; // barchart.XScale.Refes.Labels = "\"AAAA\""; // barchart.YScale.Refes.Values = new double[]{100,120}; // barchart.YScale.Refes.Color = new int[] { 3, 4 }; // string path1 = MtbTools.BuildTemporaryMacro("testchart.mac", barchart.GetCommand()); // System.Text.StringBuilder cmnd = new System.Text.StringBuilder(); // cmnd.AppendFormat("%\"{0}\" {1};\r\n", path1, // string.Join(" &\r\n", ((Mtb.Column[])barchart.Variables).Select(x => x.SynthesizedName).ToArray()) // ); // if (barchart.GroupingVariables != null) // { // cmnd.AppendFormat("group {0};\r\n", // string.Join(" &\r\n", ((Mtb.Column[])barchart.GroupingVariables).Select(x => x.SynthesizedName).ToArray()) // ); // } // if (barchart.Panel.PaneledBy != null) // { // cmnd.AppendFormat("pane {0};\r\n", // string.Join(" &\r\n", (string[])barchart.Panel.PaneledBy) // ); // } // if (barchart.DataLabel.LabelColumn != null) // { // cmnd.AppendFormat("datlab {0};\r\n", barchart.DataLabel.LabelColumn); // } // cmnd.Append("."); // string path2 = MtbTools.BuildTemporaryMacro("mycode.mtb", cmnd.ToString()); // proj.ExecuteCommand(string.Format("exec \"{0}\" 1", path2)); //} ////Boxplot //BoxPlot boxplot = new BoxPlot(proj, ws); //boxplot.Mean.Visible = true; //boxplot.Mean.Type = 6; //boxplot.Mean.Color = MtbColor.DarkSkyBlue; //boxplot.CMean.Visible = true; //boxplot.Individual.Visible = true; //boxplot.IQRBox.Visible = false; //boxplot.Whisker.Visible = false; //boxplot.Outlier.Visible = false; //boxplot.Variables = "C3"; //boxplot.GroupingVariables = "C1 C2"; //boxplot.XScale.Label.Visible = false; //boxplot.XScale.Ticks.TShow = new int[] { 1, 2 }; //boxplot.XScale.Ticks.HideAllTick = true; //boxplot.FootnoteLst.Add( // new Footnote() // { // Text = "AAAAAA" // }); //boxplot.MeanDatlab.Visible = true; //LabelPosition lpos = new LabelPosition(1, ""); //boxplot.MeanDatlab.PosititionList.Add(lpos); ////boxplot.GraphPath = @"D:\test.jpg"; //boxplot.GraphRegion.SetCoordinate(10, 4); //boxplot.Title.Text = "BoxxxxxPlot"; //boxplot.FigureRegion.SetCoordinate(0, 1, 0.6, 1); //System.Text.StringBuilder cmnd = new System.Text.StringBuilder(); //cmnd.AppendLine("layout."); //cmnd.Append(barchart.GetCommand()); //cmnd.Append(boxplot.GetCommand()); //cmnd.AppendLine("endl"); //proj.ExecuteCommand(cmnd.ToString()); //proj.ExecuteCommand(boxplot.GetCommand() + "."); //Datlab dd = new Datlab(); //string text = "C1-C5"; //Regex regex = new Regex(@"('.*?'|C[\d]+)-('.*?'|C[\d]+)"); //if (regex.IsMatch(text)) //{ // MatchCollection mm = regex.Matches(text); // foreach (Match m in mm) // { // for (int i = 0; i < m.Groups.Count; i++) // { // Console.WriteLine(m.Groups[i].Value); // } // } //} Application.Run(new Form1()); }