/// <summary> /// 将编辑后得内容保存到工站配置中 /// </summary> /// <returns></returns> public bool SaveEditChange() { string[] axisNames = _station.AxisNames; for (int i = 0; i < dgvPos.Rows.Count; i++) { DataGridViewRow row = dgvPos.Rows[i]; string posName = row.Cells[0].Value as string; JFMultiAxisPosition ap = _station.GetWorkPosition(posName); for (int j = 0; j < axisNames.Length; j++) { DataGridViewCheckBoxCell enableCell = row.Cells[j * 2 + 1] as DataGridViewCheckBoxCell; //是否使能轴位置 DataGridViewTextBoxCell posCell = row.Cells[j * 2 + 2] as DataGridViewTextBoxCell; if ((bool)enableCell.Value) { double pos = 0; if (!double.TryParse(posCell.Value as string, out pos)) { string[] standardAxisName = new string[] { "X", "Y", "Z", "R" }; string axisName = j < _station.StandardAxisCount ? standardAxisName[j] : _station.AxisNames[j]; MessageBox.Show(string.Format("保存失败!请检查位置参数格式,必须为数字\n点位名称:{0} 轴名称:{1}", posName, axisName)); posCell.Style.BackColor = Color.Red; return(false); } ap.SetAxisPos(axisNames[j], pos); } else { ap.RemoveAxis(axisNames[j]); } } } _station.SaveCfg(); return(true); }
/// <summary> /// 将当前位置保存为指定工位 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btSaveCurr2WorkPos_Click(object sender, EventArgs e) { if (cbSaveWorkPos.SelectedIndex < 0) { MessageBox.Show("请选择需要存储的工作点位"); return; } JFMultiAxisPosition pos = _station.GetWorkPosition(cbSaveWorkPos.Text); if (null == pos) { MessageBox.Show(" 未找到点位信息,Name = " + cbSaveWorkPos.Text); return; } string[] axisNames = pos.AxisNames; if (null == axisNames || 0 == axisNames.Length) { MessageBox.Show("未能保存,点位中不包含轴/电机!"); return; } List <double> lstAxisPos = new List <double>(); foreach (string axisName in axisNames) { double apos = 0; string errorInfo; if (!_station.GetAxisPosition(axisName, out apos, out errorInfo)) { MessageBox.Show("获取轴 = \"" + axisName + "\"位置失败,ErrorInfo :" + errorInfo); return; } lstAxisPos.Add(apos); } if (DialogResult.Cancel == MessageBox.Show("确定将当前位置保存为点位:" + cbSaveWorkPos.Text, "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { return; } for (int i = 0; i < axisNames.Length; i++) { pos.SetAxisPos(pos.AxisNames[i], lstAxisPos[i]); } _station.SaveCfg(); MessageBox.Show("点位坐标已保存"); }
/// <summary> /// 添加新点位 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btAdd_Click(object sender, EventArgs e) { BenameDialog nameDialog = new BenameDialog(); nameDialog.Text = "添加新点位"; if (nameDialog.ShowDialog() == DialogResult.OK) { string posName = nameDialog.GetName(); if (_station.ContianPositionName(posName)) { MessageBox.Show("不能添加已存在的点位名称:" + posName); return; } JFMultiAxisPosition newPos = new JFMultiAxisPosition(); newPos.Name = posName; _station.AddWorkPosition(newPos); DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cellName = new DataGridViewTextBoxCell(); cellName.Value = posName; row.Cells.Add(cellName); foreach (string axisName in _station.AxisNames) { DataGridViewCheckBoxCell chkEnable = new DataGridViewCheckBoxCell(); chkEnable.Value = false; row.Cells.Add(chkEnable); DataGridViewTextBoxCell cellPos = new DataGridViewTextBoxCell(); cellPos.Style.BackColor = SystemColors.ControlDark; row.Cells.Add(cellPos); } DataGridViewButtonCell btCell = new DataGridViewButtonCell(); btCell.Value = "更新"; row.Cells.Add(btCell); dgvPos.Rows.Add(row); btDel.Enabled = true; } }
/// <summary> /// 根据工站点位更新界面布局 /// </summary> void UpdateStation2UI() { if (InvokeRequired) { Invoke(new Action(UpdateStation2UI)); return; } dgvPos.Columns.Clear(); if (null == _station) { lbTips.Text = "工站未设置"; btDel.Enabled = false; btAdd.Enabled = false; btEdit.Enabled = false; btCancel.Enabled = false; dgvPos.Enabled = false; return; } string[] axisNames = _station.AxisNames; if (null == axisNames || 0 == axisNames.Length) { lbTips.Text = "工站:" + _station.Name + " 无可用轴!"; btDel.Enabled = false; btAdd.Enabled = false; btEdit.Enabled = false; btCancel.Enabled = false; dgvPos.Enabled = false; return; } btEdit.Enabled = true; lbTips.Text = "工站:" + _station.Name + " 点位设置"; DataGridViewColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = "工作点位名称"; col.Width = 150; dgvPos.Columns.Add(col); for (int i = 0; i < axisNames.Length; i++) { col = new DataGridViewCheckBoxColumn(); //是否使能轴位置//new DataGridViewColumn(); col.HeaderText = "使能"; col.Width = 40; dgvPos.Columns.Add(col); col = new DataGridViewTextBoxColumn(); col.HeaderText = "位置"; col.Width = 120; dgvPos.Columns.Add(col); } col = new DataGridViewButtonColumn(); col.HeaderText = "使用当前位置"; col.Width = 100; dgvPos.Columns.Add(col); string[] posNames = _station.WorkPositionNames; if (null == posNames || 0 == posNames.Length) { lbTips.Text += "无点位"; return; } foreach (string posName in posNames) { JFMultiAxisPosition pos = _station.GetWorkPosition(posName); DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cellName = new DataGridViewTextBoxCell(); cellName.Value = posName; row.Cells.Add(cellName); for (int i = 0; i < axisNames.Length; i++) { DataGridViewCheckBoxCell cellEnable = new DataGridViewCheckBoxCell(); //本轴是否使能 row.Cells.Add(cellEnable); DataGridViewTextBoxCell cellAP = new DataGridViewTextBoxCell(); if (!pos.ContainAxis(axisNames[i])) { cellEnable.Value = false; cellAP.Style.BackColor = SystemColors.ControlDark; cellAP.Value = "未指定"; } else { cellEnable.Value = true; cellAP.Style.BackColor = Color.White; cellAP.Value = pos.GetAxisPos(axisNames[i]).ToString(); } row.Cells.Add(cellAP); } DataGridViewButtonCell btCell = new DataGridViewButtonCell(); btCell.Value = "更新"; row.Cells.Add(btCell); dgvPos.Rows.Add(row); } string[] standardAxisName = new string[] { "X", "Y", "Z", "R" }; for (int i = 0; i < axisNames.Length; i++) { string axisName = axisNames[i]; if (i < _station.StandardAxisCount) { axisName = standardAxisName[i]; } dgvPos.ColumnHeadersHeight = 40; dgvPos.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; dgvPos.AddSpanHeader(i * 2 + 1, 2, _station.GetDecChnAliasName(NamedChnType.Axis, axisName)); //dgvPos.AddSpanHeader(i*2+1, 2, axisName); } }