private void MapSetup_Load(object sender, EventArgs e) { // Prepare cmbMapConfigName datasource List<string> mapConfigs = new List<string>(); DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.MapConfigFolder); FileInfo[] files = dirInfo.GetFiles("*.xml"); foreach (FileInfo file in files) { mapConfigs.Add(file.Name.ToString().Substring(0, file.Name.ToString().LastIndexOf("."))); } // Binding datasource for cmbMapConfigName and set default value. cmbMapConfigName.DataSource = mapConfigs; ConfigHelper ch = new ConfigHelper(); cmbMapConfigName.SelectedItem = ch.GetDefaultMapConfigName().Trim(); // Initialize nudImageColumns, nudImageRows value. nudImageColumns.Value = ch.GettlpFlawImagesColumns(cmbMapConfigName.SelectedItem.ToString()); nudImageRows.Value = ch.GettlpFlawImagesRows(cmbMapConfigName.SelectedItem.ToString()); // Initialize rdoMapGridOn,rdoMapGridOff rdoMapGridOn.Checked = ch.GetIsDisplayMapGrid(cmbMapConfigName.SelectedItem.ToString()); rdoMapGridOff.Checked = !ch.GetIsDisplayMapGrid(cmbMapConfigName.SelectedItem.ToString()); // Initialize rdoFixCellSize, rdoCountSize rdoFixCellSize.Checked = ch.GetIsFixCellSizeMode(cmbMapConfigName.SelectedItem.ToString()); rdoCountSize.Checked = !ch.GetIsFixCellSizeMode(cmbMapConfigName.SelectedItem.ToString()); if (rdoFixCellSize.Checked) { txtFixSizeCD.Text = ch.GetFixCellSizeCD(cmbMapConfigName.SelectedItem.ToString()).ToString(); txtFixSizeCD.Enabled = true; txtFixSizeMD.Text = ch.GetFixCellSizeMD(cmbMapConfigName.SelectedItem.ToString()).ToString(); txtFixSizeMD.Enabled = true; txtCountSizeCD.Text = ""; txtCountSizeCD.Enabled = false; txtCountSizeMD.Text = ""; txtCountSizeMD.Enabled = false; lblSCMD.Text = ch.GetFixCellSizeSmybol(cmbMapConfigName.SelectedItem.ToString()); lblSCCD.Text = ch.GetFixCellSizeSmybol(cmbMapConfigName.SelectedItem.ToString()); } else if (rdoCountSize.Checked) { txtCountSizeCD.Text = ch.GetCountSizeCD(cmbMapConfigName.SelectedItem.ToString()).ToString(); txtCountSizeCD.Enabled = true; txtCountSizeMD.Text = ch.GetCountSizeMD(cmbMapConfigName.SelectedItem.ToString()).ToString(); txtCountSizeMD.Enabled = true; txtFixSizeCD.Text = ""; txtFixSizeCD.Enabled = false; txtFixSizeMD.Text = ""; txtFixSizeMD.Enabled = false; } else { TextBox[] txts = { txtFixSizeCD, txtFixSizeMD, txtCountSizeCD, txtCountSizeMD }; foreach (TextBox txt in txts) { txt.Enabled = false; txt.Text = ""; } } // Initialize cmbBottomAxes default selected cmbBottomAxes.SelectedItem = ch.GetBottomAxes(cmbMapConfigName.SelectedItem.ToString()); // Initialize chkCDInverse, chkMDInverse chkCDInverse.Checked = ch.IsCdInver_X(cmbMapConfigName.SelectedItem.ToString()); chkMDInverse.Checked = ch.IsMdInver_Y(cmbMapConfigName.SelectedItem.ToString()); // Initialize cmbMapSize default. (x:y) int x = ch.GetMapProportion_X(cmbMapConfigName.SelectedItem.ToString()); int y = ch.GetMapProportion_Y(cmbMapConfigName.SelectedItem.ToString()); cmbMapSize.SelectedItem = String.Format("{0}:{1}", x, y); // prepare shape list Dictionary<string, string> shapes = new Dictionary<string, string>(); shapes.Add("Triangle", "▲"); shapes.Add("InvertedTriangle", "▼"); shapes.Add("Square", "■"); shapes.Add("Circle", "●"); shapes.Add("Plus", "✚"); shapes.Add("Cross", "✖"); shapes.Add("Star", "★"); // Initialize FlawLegend (dgvFlawLegends) List<Column> columns = new List<Column>(); Column flawType = new Column(0, "FlawType", 60); Column name = new Column(1, "Name", 120); Column shape = new Column(2, "Shape", 80); Column color = new Column(3, "Color", 80); columns.Add(flawType); columns.Add(name); columns.Add(shape); columns.Add(color); foreach (Column c in columns) { if (c.Name == "Shape") { DataGridViewComboBoxColumn cmbShape = new DataGridViewComboBoxColumn(); cmbShape.HeaderText = c.Name; cmbShape.DisplayIndex = c.Index; cmbShape.DataPropertyName = c.Name; cmbShape.Width = c.Width; cmbShape.DataSource = new BindingSource(shapes, null); cmbShape.DisplayMember = "Value"; cmbShape.ValueMember = "Key"; dgvFlawLegends.Columns.Add(cmbShape); } else { DataGridViewCell cell = new DataGridViewTextBoxCell(); DataGridViewColumn column = new DataGridViewColumn(); column.CellTemplate = cell; column.Name = c.Name; column.HeaderText = c.Name; column.Width = c.Width; column.DataPropertyName = c.Name; column.SortMode = DataGridViewColumnSortMode.Automatic; if (c.Name == "FlawType" || c.Name == "Name" || c.Name == "Color") { column.ReadOnly = true; } dgvFlawLegends.Columns.Add(column); } } dgvFlawLegends.MultiSelect = false; dgvFlawLegends.AutoGenerateColumns = false; // Get datatable of _dtbFlawLegends _dtbFlawLegends = ch.GetDataTablePrevFlawLegend(cmbMapConfigName.SelectedItem.ToString()); dgvFlawLegends.DataSource = _dtbFlawLegends; }
private void btnSave_Click(object sender, EventArgs e) { // Save default config file to system config ConfigHelper ch = new ConfigHelper(); if (String.IsNullOrEmpty(cmbMapConfigName.Text)) { cmbMapConfigName.Text = DateTime.Now.ToShortDateString(); } if (!ch.SaveMapSetupConfigFile(cmbMapConfigName.Text.Trim())) { return; } // initialize map xml sechma string sechma_path = PathHelper.SystemConfigFolder + "map_sechma.xml"; XmlDocument document = new XmlDocument(); document.Load(sechma_path); XPathNavigator navigator = document.CreateNavigator(); // save image_grid_rows , image_grid_columns navigator.SelectSingleNode("//pxptab/image_grid_rows").SetValue(nudImageRows.Value.ToString()); navigator.SelectSingleNode("//pxptab/image_grid_columns").SetValue(nudImageColumns.Value.ToString()); // save map_proportion string[] proportion = cmbMapSize.SelectedItem.ToString().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); navigator.SelectSingleNode("//map_window/map_chart/map_proportion[@name='x']").SetValue(proportion[0]); navigator.SelectSingleNode("//map_window/map_chart/map_proportion[@name='y']").SetValue(proportion[1]); // save grid line display if (rdoMapGridOn.Checked) { navigator.SelectSingleNode("//map_window/map_chart/grid_line_display").SetValue("true"); } else { navigator.SelectSingleNode("//map_window/map_chart/grid_line_display").SetValue("false"); } // save grid line mode :Map 底格線間隔模式 0: FixSize , 1: EachCellCount if (rdoFixCellSize.Checked) { navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("0"); // save grid line value of mode double md = Double.TryParse(txtFixSizeMD.Text, out md) ? md : 1; double cd = Double.TryParse(txtFixSizeCD.Text, out cd) ? cd : 1; navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/fix/md").SetValue(md.ToString()); navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/fix/cd").SetValue(cd.ToString()); } else if (rdoCountSize.Checked) { navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("1"); // save grid line value of mode int md = Int32.TryParse(txtCountSizeMD.Text, out md) ? md : 2; int cd = Int32.TryParse(txtCountSizeCD.Text, out cd) ? cd : 2; navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/md").SetValue(md.ToString()); navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/cd").SetValue(cd.ToString()); } else { navigator.SelectSingleNode("//map_window/map_chart/grid_line_mode").SetValue("1"); // save grid line value of mode int md = Int32.TryParse(txtCountSizeMD.Text, out md) ? md : 2; int cd = Int32.TryParse(txtCountSizeCD.Text, out cd) ? cd : 2; navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/md").SetValue(md.ToString()); navigator.SelectSingleNode("//map_window/map_chart/grid_line_set/count/cd").SetValue(cd.ToString()); } // save bottom axes string bottom = cmbBottomAxes.SelectedItem.ToString().Trim(); navigator.SelectSingleNode("//map_window/map_chart/bottom_axes").SetValue(bottom); // save md_inver if (chkMDInverse.Checked) { navigator.SelectSingleNode("//map_window/map_chart/md_inver").SetValue("true"); } else { navigator.SelectSingleNode("//map_window/map_chart/md_inver").SetValue("false"); } // save cd_inver if (chkCDInverse.Checked) { navigator.SelectSingleNode("//map_window/map_chart/cd_inver").SetValue("true"); } else { navigator.SelectSingleNode("//map_window/map_chart/cd_inver").SetValue("false"); } // save flawlegends // remove old flawlegend for add records if (navigator.Select("//map_window/flaw_legend").Count > 0 && _dtbFlawLegends.Rows.Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//map_window/flaw_legend[1]"); XPathNavigator last = navigator.SelectSingleNode("//map_window/flaw_legend[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbFlawLegends to xml for (int i = 0; i < _dtbFlawLegends.Rows.Count; i++) { string flawType = _dtbFlawLegends.Rows[i]["FlawType"].ToString(); string flawName = _dtbFlawLegends.Rows[i]["Name"].ToString(); string shape = _dtbFlawLegends.Rows[i]["Shape"].ToString(); string color = _dtbFlawLegends.Rows[i]["Color"].ToString(); navigator.SelectSingleNode("//config/map_window").AppendChildElement(string.Empty, "flaw_legend", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "flaw_type", string.Empty, flawType); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "name", string.Empty, flawName); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "color", string.Empty, color); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "shape", string.Empty, shape); } // save string map_path = PathHelper.MapConfigFolder + cmbMapConfigName.Text.Trim() + ".xml"; try { document.Save(map_path); // Re binding cmbMapConfigName datasource List<string> mapConfigs = new List<string>(); DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.MapConfigFolder); FileInfo[] files = dirInfo.GetFiles("*.xml"); foreach (FileInfo file in files) { mapConfigs.Add(file.Name.ToString().Substring(0, file.Name.ToString().LastIndexOf("."))); } // Binding datasource for cmbMapConfigName and set default value. cmbMapConfigName.DataSource = mapConfigs; cmbMapConfigName.SelectedItem = ch.GetDefaultMapConfigName().Trim(); MessageBox.Show("Success."); } catch { //MessageBox.Show("Fail."); } }
private void btnMapSetting_Click(object sender, EventArgs e) { // Deal Flaw Legends datasoure ConfigHelper ch = new ConfigHelper(); XYDiagram diagram = null; if ((XYDiagram)chartControl.Diagram != null) { diagram = (XYDiagram)chartControl.Diagram; } MapSetup ms = new MapSetup(); ms.ShowDialog(); if (_legend != null && _legend.Count > 0) { // Reload flawlegend _dtbFlawLegends.Rows.Clear(); // Add jobloaded records foreach (FlawLegend f in _legend) { DataRow dr = _dtbFlawLegends.NewRow(); dr["Display"] = f.VisibleFlags; dr["FlawType"] = f.ClassID; dr["Name"] = f.Name; dr["Shape"] = "Cone"; dr["Color"] = String.Format("#{0:X2}{1:X2}{2:X2}", ColorTranslator.FromWin32((int)f.Color).R, ColorTranslator.FromWin32((int)f.Color).G, ColorTranslator.FromWin32((int)f.Color).B); dr["PieceDoffNum"] = 0; dr["JobDoffNum"] = 0; _dtbFlawLegends.Rows.Add(dr); } string mapConfig = ch.GetDefaultMapConfigName().Trim(); DataTable dtbLegendFromConfig = ch.GetDataTablePrevFlawLegend(mapConfig); foreach (DataRow d in dtbLegendFromConfig.Rows) { string expr = String.Format("FlawType={0} AND Name='{1}'", d["FlawType"].ToString().Trim(), d["Name"].ToString().Trim()); DataRow[] drs = _dtbFlawLegends.Select(expr); if (drs.Length > 0) { drs[0]["Shape"] = d["Shape"].ToString().Trim(); drs[0]["Color"] = d["Color"].ToString().Trim(); } } dgvFlawLegend.ClearSelection(); dgvFlawLegendDetial.ClearSelection(); // Re-configure Chart if (diagram != null) { double width = Convert.ToDouble(diagram.AxisX.Range.ScrollingRange.MaxValue); double height = Convert.ToDouble(diagram.AxisY.Range.ScrollingRange.MaxValue); InitChart(width, height); DrawChartPoint(); } } // Refresh pxptab tablelayout _pnl.ColumnCount = ch.GettlpFlawImagesColumns(); _pnl.RowCount = ch.GettlpFlawImagesRows(); _pnl.ColumnStyles.Clear(); int phdHeight = _pnl.Height / _pnl.RowCount; int phdrWidth = _pnl.Width / _pnl.ColumnCount; for (int i = 0; i < _pnl.RowCount; i++) { _pnl.RowStyles.Add(new RowStyle(SizeType.Percent, 50)); } for (int i = 0; i < _pnl.ColumnCount; i++) { _pnl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50)); } // 呼叫 RefreshtlpImagesControls 重新加入控制項, 然後重新計算總頁數 if (_dtbFlaws != null) { _pxp.RefreshtlpImagesControls(1); int dataCount = _dtbFlaws.Select(_dtbFlaws.DefaultView.RowFilter).Length; int totalPage = dataCount % (_pnl.ColumnCount * _pnl.RowCount) == 0 ? dataCount / (_pnl.ColumnCount * _pnl.RowCount) : dataCount / (_pnl.ColumnCount * _pnl.RowCount) + 1; _pxp.Controls["lblTotalPage"].Text = totalPage.ToString(); } }
// Initial flaw legend datagridview public void InitFlawLegendGrid() { _dtbFlawLegends.Rows.Clear(); // Add jobloaded records foreach (FlawLegend f in _legend) { DataRow dr = _dtbFlawLegends.NewRow(); dr["Display"] = f.VisibleFlags; dr["FlawType"] = f.ClassID; dr["Name"] = f.Name; dr["Shape"] = "Cone"; dr["Color"] = String.Format("#{0:X2}{1:X2}{2:X2}", ColorTranslator.FromWin32((int)f.Color).R, ColorTranslator.FromWin32((int)f.Color).G, ColorTranslator.FromWin32((int)f.Color).B); dr["PieceDoffNum"] = 0; dr["JobDoffNum"] = 0; _dtbFlawLegends.Rows.Add(dr); } // Deal Flaw Legends datasoure ConfigHelper ch = new ConfigHelper(); string mapConfig = ch.GetDefaultMapConfigName().Trim(); DataTable dtbLegendFromConfig = ch.GetDataTablePrevFlawLegend(mapConfig); foreach (DataRow d in dtbLegendFromConfig.Rows) { string expr = String.Format("FlawType={0} AND Name='{1}'", d["FlawType"].ToString().Trim(), d["Name"].ToString().Trim()); DataRow[] drs = _dtbFlawLegends.Select(expr); if (drs.Length > 0) { drs[0]["Shape"] = d["Shape"].ToString().Trim(); drs[0]["Color"] = d["Color"].ToString().Trim(); } } // Save new record to xml string map_path = PathHelper.MapConfigFolder + mapConfig + ".xml"; XmlDocument document = new XmlDocument(); document.Load(map_path); XPathNavigator navigator = document.CreateNavigator(); XPathNodeIterator node = navigator.Select("//map_window/flaw_legend"); // remove old flawlegend for add records if (navigator.Select("//map_window/flaw_legend").Count > 0 && _dtbFlawLegends.Rows.Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//map_window/flaw_legend[1]"); XPathNavigator last = navigator.SelectSingleNode("//map_window/flaw_legend[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbFlawLegends to xml for (int i = 0; i < _dtbFlawLegends.Rows.Count; i++) { string flawType = _dtbFlawLegends.Rows[i]["FlawType"].ToString(); string flawName = _dtbFlawLegends.Rows[i]["Name"].ToString(); string shape = _dtbFlawLegends.Rows[i]["Shape"].ToString(); string color = _dtbFlawLegends.Rows[i]["Color"].ToString(); navigator.SelectSingleNode("//config/map_window").AppendChildElement(string.Empty, "flaw_legend", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "flaw_type", string.Empty, flawType); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "name", string.Empty, flawName); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "color", string.Empty, color); navigator.SelectSingleNode("//config/map_window/flaw_legend[last()]").AppendChildElement(string.Empty, "shape", string.Empty, shape); } try { document.Save(map_path); } catch { MessageBox.Show("Fail."); } }
// Initial flaw chart public void InitChart(double width, double height) { ConfigHelper ch = new ConfigHelper(); string mapConfigFile = ch.GetDefaultMapConfigName(); bool isDisplayGrid = ch.GetIsDisplayMapGrid(mapConfigFile); bool isCdInvert = ch.IsCdInver_X(mapConfigFile); bool isMdInvert = ch.IsMdInver_Y(mapConfigFile); bool isFixCellSize = ch.GetIsFixCellSizeMode(mapConfigFile); double fixCdSize = ch.GetFixCellSizeCD(mapConfigFile); double fixMdSize = ch.GetFixCellSizeMD(mapConfigFile); double countCdSize = ch.GetCountSizeCD(mapConfigFile); double countMdSize = ch.GetCountSizeMD(mapConfigFile); string bottomAxes = ch.GetBottomAxes(mapConfigFile); if (chartControl.Series != null) { chartControl.Series.Clear(); } Series series = new Series(); chartControl.RuntimeHitTesting = true; chartControl.Legend.Visible = false; chartControl.Series.Add(series); XYDiagram diagram = (XYDiagram)chartControl.Diagram; // Setting AxisX format diagram.EnableAxisXZooming = true; diagram.EnableAxisXScrolling = true; diagram.AxisX.Range.MinValue = 0; diagram.AxisX.Range.MaxValue = width; diagram.AxisX.Range.ScrollingRange.SetMinMaxValues(0, width); diagram.AxisX.NumericOptions.Format = NumericFormat.Number; diagram.AxisX.NumericOptions.Precision = 6; diagram.AxisX.Reverse = isCdInvert; diagram.AxisX.GridLines.Visible = isDisplayGrid; diagram.AxisX.GridLines.LineStyle.DashStyle = DashStyle.Dash; diagram.AxisX.GridSpacingAuto = false; // Setting AxisY format diagram.EnableAxisYZooming = true; diagram.EnableAxisYScrolling = true; diagram.AxisY.Range.MinValue = 0; diagram.AxisY.Range.MaxValue = height; diagram.AxisY.Range.ScrollingRange.SetMinMaxValues(0, height); diagram.AxisY.NumericOptions.Format = NumericFormat.Number; diagram.AxisY.NumericOptions.Precision = 6; diagram.AxisY.Reverse = isMdInvert; diagram.AxisY.GridLines.Visible = isDisplayGrid; diagram.AxisY.GridLines.LineStyle.DashStyle = DashStyle.Dash; diagram.AxisY.GridSpacingAuto = false; // Setting chart cell size if (isFixCellSize) // Specify cell size { diagram.AxisX.GridSpacing = fixCdSize; diagram.AxisY.GridSpacing = fixMdSize; } else // Equal cell count { diagram.AxisX.GridSpacing = width / countCdSize; diagram.AxisY.GridSpacing = height / countMdSize; } // Setting Axes position if (isCdInvert) { if (isMdInvert) { diagram.AxisX.Alignment = AxisAlignment.Far; diagram.AxisY.Alignment = AxisAlignment.Far; } else { diagram.AxisX.Alignment = AxisAlignment.Near; diagram.AxisY.Alignment = AxisAlignment.Far; } } else { if (isMdInvert) { diagram.AxisX.Alignment = AxisAlignment.Far; diagram.AxisY.Alignment = AxisAlignment.Near; } else { diagram.AxisX.Alignment = AxisAlignment.Near; diagram.AxisY.Alignment = AxisAlignment.Near; } } // Rotate chart when bottom axes is MD if (bottomAxes == "CD") { diagram.Rotated = false; } else { diagram.Rotated = true; } DrawSubPiece(); }
private void btnCreateGrid_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Are you sure you want to create new subpiece setting?", "Create Setting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { int x = int.TryParse(txtColumns.Text, out x) ? x : 1; int y = int.TryParse(txtRows.Text, out y) ? y : 1; _dtbColumns.Clear(); for (int i = 0; i < x; i++) { DataRow dr = _dtbColumns.NewRow(); dr["Name"] = i + 1; _dtbColumns.Rows.Add(dr); } _dtbRows.Clear(); for (int i = 0; i < y; i++) { DataRow dr = _dtbRows.NewRow(); dr["Name"] = i + 1; _dtbRows.Rows.Add(dr); } // SubpieceName Reset _pointsSubpieceNames = new List<string>(); _marksSubpieceNames = new List<string>(); _pointsSubpieceNames.Add("All"); _marksSubpieceNames.Add("All"); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { string name = String.Format("ROI-{0}{1}", i + 1, j + 1); _pointsSubpieceNames.Add(name); _marksSubpieceNames.Add(name); } } cmbSubMarks.DataSource = null; cmbSubMarks.DataSource = _marksSubpieceNames; cmbSubPoints.DataSource = null; cmbSubPoints.DataSource = _pointsSubpieceNames; // Add Points set ConfigHelper ch = new ConfigHelper(); string map_path = ch.GetDefaultMapConfigName(); Dictionary<int, string> legends = ch.GetPrevFlawLegendDictionary(map_path); _dtbPoints.Rows.Clear(); foreach (string subpiece in _pointsSubpieceNames) { foreach (KeyValuePair<int, string> l in legends) { // SubpieceName, ClassName, Score DataRow dr = _dtbPoints.NewRow(); dr["SubpieceName"] = subpiece; dr["ClassName"] = l.Value; dr["Score"] = 0; _dtbPoints.Rows.Add(dr); } } // Refresh Mark foreach (string subpiece in _marksSubpieceNames) { string expr = String.Format("SubpieceName='{0}'", subpiece); DataRow[] drs = _dtbGrades.Select(expr); if (drs.Length < 1) { DataRow dr = _dtbGrades.NewRow(); dr["SubpieceName"] = subpiece; dr["GradeName"] = "A"; dr["Score"] = 0; _dtbGrades.Rows.Add(dr); } } } }
// Save xml private void btnSaveGradeConfigFile_Click(object sender, EventArgs e) { // if some data wrong will tip. if (_dtbColumns.Rows.Count < 1 || _dtbRows.Rows.Count < 1 || _dtbPoints.Rows.Count < 1 || _dtbGrades.Rows.Count < 1) { MessageBox.Show("Input has null value."); return; } DataHelper dh = new DataHelper(); if (dh.HasNull(_dtbColumns) || dh.HasNull(_dtbRows) || dh.HasNull(_dtbPoints) || dh.HasNull(_dtbGrades)) { MessageBox.Show("Input has null value."); return; } // Save default config file to system config ConfigHelper ch = new ConfigHelper(); if (String.IsNullOrEmpty(cmbConfig.Text)) { cmbConfig.Text = DateTime.Now.ToShortDateString(); } // if save error break; if (!ch.SaveGradeSetupConfigFile(cmbConfig.Text.Trim())) { MessageBox.Show("File name is empty."); return; } // ROI-Setting //----------------------------------------------------------------------------------// // initialize grade xml sechma string sechma_path = PathHelper.SystemConfigFolder + "grade_sechma.xml"; XmlDocument document = new XmlDocument(); document.Load(sechma_path); XPathNavigator navigator = document.CreateNavigator(); // save roi mode RadioButton[] rdos = { rdoNoRoi, rdoSymmetrical }; foreach (RadioButton rdo in rdos) { if (rdo.Checked) { navigator.SelectSingleNode("//roi/mode").SetValue(rdo.Text.Trim()); } } // Save roi columns , rows size navigator.SelectSingleNode("//roi/columns/size").SetValue(_dtbColumns.Rows.Count.ToString()); navigator.SelectSingleNode("//roi/rows/size").SetValue(_dtbRows.Rows.Count.ToString()); // save roi data (start, end) of columns , rows. // Remove old columns for add new record if (navigator.Select("//roi/columns/column").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//roi/columns/column[1]"); XPathNavigator last = navigator.SelectSingleNode("//roi/columns/column[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dgvColumns data to xml for (int i = 0; i < _dtbColumns.Rows.Count; i++) { string name = _dtbColumns.Rows[i]["Name"].ToString(); string start = _dtbColumns.Rows[i]["Start"].ToString(); string end = _dtbColumns.Rows[i]["End"].ToString(); navigator.SelectSingleNode("//roi/columns").AppendChildElement(string.Empty, "column", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "name", string.Empty, name); navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "start", string.Empty, start); navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "end", string.Empty, end); } // Remove old rows for add new record. if (navigator.Select("//roi/rows/row").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//roi/rows/row[1]"); XPathNavigator last = navigator.SelectSingleNode("//roi/rows/row[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbRows data to xml for (int i = 0; i < _dtbRows.Rows.Count; i++) { string name = _dtbRows.Rows[i]["Name"].ToString(); string start = _dtbRows.Rows[i]["Start"].ToString(); string end = _dtbRows.Rows[i]["End"].ToString(); navigator.SelectSingleNode("//roi/rows").AppendChildElement(string.Empty, "row", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "name", string.Empty, name); navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "start", string.Empty, start); navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "end", string.Empty, end); } // Grade - Setting //----------------------------------------------------------------------------------// // Points // Save is points enable? navigator.SelectSingleNode("//grade/points/enable").SetValue(chkEnablePonit.Checked.ToString()); // Remove old points/sub_piece for add new record. (_dtbPoints) if (navigator.Select("//grade/points/sub_piece").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//grade/points/sub_piece[1]"); XPathNavigator last = navigator.SelectSingleNode("//grade/points/sub_piece[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbPoints List<string> pointsSubpieces = _pointsSubpieceNames; // prepare flawtype convert dictionary string map_config_path = ch.GetDefaultMapConfigName(); Dictionary<string, int> flawlegends = ch.GetPrevFlawLegendDictionaryID(map_config_path); foreach (string subpieceName in pointsSubpieces) { navigator.SelectSingleNode("//grade/points").AppendChildElement(string.Empty, "sub_piece", string.Empty, null); // Move to last column element and add name value. navigator.SelectSingleNode("//grade/points/sub_piece[last()]").AppendChildElement(string.Empty, "name", string.Empty, subpieceName); string expr = String.Format("SubpieceName='{0}'", subpieceName); // check all same if (chkAllSameOfPoint.Checked) { expr = String.Format("SubpieceName='{0}'", "All"); } DataRow[] drs = _dtbPoints.Select(expr); foreach (DataRow dr in drs) { string className = dr["ClassName"].ToString(); int classID = flawlegends[className]; string score = dr["Score"].ToString(); navigator.SelectSingleNode("//grade/points/sub_piece[last()]").AppendChildElement(string.Empty, "flawtype_score", string.Empty, score); navigator.SelectSingleNode("//grade/points/sub_piece[last()]/flawtype_score[last()]").CreateAttribute(string.Empty, "id", string.Empty, classID.ToString()); } } // Remove old grade(marks)/subpiece for add new record . (_dtbGrades) if (navigator.Select("//grade/marks/sub_piece").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//grade/marks/sub_piece[1]"); XPathNavigator last = navigator.SelectSingleNode("//grade/marks/sub_piece[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // Marks // Save is grade(marks) enable? navigator.SelectSingleNode("//grade/marks/enable").SetValue(chkEnableGrade.Checked.ToString()); // save _dtbGrades List<string> marksSubpieces = _marksSubpieceNames; foreach (string subpieceName in marksSubpieces) { navigator.SelectSingleNode("//grade/marks").AppendChildElement(string.Empty, "sub_piece", string.Empty, null); //Move to last column element and add name value. navigator.SelectSingleNode("//grade/marks/sub_piece[last()]").AppendChildElement(string.Empty, "name", string.Empty, subpieceName); string expr = String.Format("SubpieceName='{0}'", subpieceName); // check all same if (chkAllSameOfGrade.Checked) { expr = String.Format("SubpieceName='{0}'", "All"); } DataRow[] drs = _dtbGrades.Select(expr); foreach (DataRow dr in drs) { string className = dr["GradeName"].ToString(); string score = dr["Score"].ToString(); navigator.SelectSingleNode("//grade/marks/sub_piece[last()]").AppendChildElement(string.Empty, "mark", string.Empty, score); navigator.SelectSingleNode("//grade/marks/sub_piece[last()]/mark[last()]").CreateAttribute(string.Empty, "id", string.Empty, className); } } // Pass or Fail // Save filter score is enable? navigator.SelectSingleNode("//grade/pass_fail/enable").SetValue(chkEnablePFS.Checked.ToString()); int filterScore = int.TryParse(txtFilterScore.Text, out filterScore) ? filterScore : 0; navigator.SelectSingleNode("//grade/pass_fail/score").SetValue(filterScore.ToString()); // Finish Save //----------------------------------------------------------------------------------// string grade_path = PathHelper.GradeConfigFolder + cmbConfig.Text.Trim() + ".xml"; try { document.Save(grade_path); // Re binding cmbMapConfigName datasource List<string> graeConfigs = new List<string>(); DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.GradeConfigFolder); FileInfo[] files = dirInfo.GetFiles("*.xml"); foreach (FileInfo file in files) { graeConfigs.Add(file.Name.ToString().Substring(0, file.Name.ToString().LastIndexOf("."))); } // Binding datasource for cmbMapConfigName and set default value. cmbConfig.DataSource = graeConfigs; cmbConfig.SelectedItem = ch.GetDefaultGradeConfigName().Trim(); MessageBox.Show("Success."); } catch { MessageBox.Show("Fail."); } // Refresh datas // Refresh dgvPoint datasource _dtbPoints.Clear(); DataTable tmpPoints = ch.GetDataTabledgvPoints(cmbConfig.SelectedItem.ToString().Trim()); foreach (DataRow dr in tmpPoints.Rows) { DataRow d = _dtbPoints.NewRow(); d["SubpieceName"] = dr["SubpieceName"]; d["ClassName"] = dr["ClassName"]; d["Score"] = dr["Score"]; _dtbPoints.Rows.Add(d); } DataView dvPoints = _dtbPoints.DefaultView; dvPoints.RowFilter = String.Format("SubpieceName='{0}'", cmbSubPoints.SelectedItem.ToString().Trim()); tmpPoints.Dispose(); // Refresh dgvGrade datasource _dtbGrades.Clear(); DataTable tmpGrades = ch.GetDataTabledgvGrade(cmbConfig.SelectedItem.ToString().Trim()); foreach (DataRow dr in tmpGrades.Rows) { DataRow d = _dtbGrades.NewRow(); d["SubpieceName"] = dr["SubpieceName"]; d["GradeName"] = dr["GradeName"]; d["Score"] = dr["Score"]; _dtbGrades.Rows.Add(d); } DataView dvGrade = _dtbGrades.DefaultView; dvGrade.RowFilter = String.Format("SubpieceName='{0}'", cmbSubMarks.SelectedItem.ToString().Trim()); tmpGrades.Dispose(); }