using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using OTSDataType; using static OTSModelSharp.ResourceManage.ResourceID; using static OTSModelSharp.ResourceManage.ResourceData; using System.Drawing.Drawing2D; using OTSMeasureApp._7_OTSProgMgrInfo.Stage; namespace OTSModelSharp.Measure.GetStageInfo { public partial class DlgStageMgr : Form { public static NLog.Logger loger = NLog.LogManager.GetCurrentClassLogger(); otsdataconst model = new otsdataconst(); // stage file public CStageParam m_pStageFile; StageDisplayHelp stageDisplayHelp; // stage list value int m_nListBoxStageListIndex=0; // flag if the stage should be re-paint bool m_bShowFlag; // boundary CDomain m_poBourary; const int LTGRAY_BRUSH = 1; const int BLACK_BRUSH = 4; const int WHITE_BRUSH = 0; const int ROUND_VALUE = 20; //round rect angle public struct BLENDFUNCTION { public Byte BlendOp; public Byte BlendFlags; public Byte SourceConstantAlpha; public Byte AlphaFormat; } public DlgStageMgr(CStageParam m_cstagefile) { InitializeComponent(); m_pStageFile = m_cstagefile; stageDisplayHelp = new StageDisplayHelp(); } private void DlgStageMgr_Load(object sender, EventArgs e) { // 加载样品台文件; \Config\SysData\OTSStage.stg if (!m_pStageFile.Load( true, false)) { return; } SetStageListControlData(); m_ctrlListBoxStageList.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_StageList); m_ctrlBtnApply.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Apply); m_ctrlBtnDelete.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Delete); m_ctrlBtnRename.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Rename); IDC_BTN_IMPORT.Text= XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Import); m_ctrlBtnExport.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Export); this.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Title); } // CDlgStageMgr message map public void OnClickedBtnImport(object sender, EventArgs e) { loger.Trace("OnClickedBtnImport: import a stage from txt file."); String strDlgTitle = ""; if (strDlgTitle == "") { // file open dialog OpenFileDialog openFileDialog = new OpenFileDialog(); // get file pathname openFileDialog.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*"; strDlgTitle = openFileDialog.FileName; DialogResult dresult = openFileDialog.ShowDialog(); if (dresult == DialogResult.OK) { String strPathName = openFileDialog.FileName; if (strPathName == "") { loger.Error("OnClickedBtnImport: input an empty path."); return; } CSEMStageData a_pCSEMStageData = new CSEMStageData(); //获取配置文件中 StageData 内容 COTSDefaultParam m_DefaultParam = new COTSDefaultParam(); m_DefaultParam.LoadInfoFromProgMgrFile(); a_pCSEMStageData = m_DefaultParam.GetStageDataParam(); // load a stage from txt file if (!m_pStageFile.LoadStageFromTextFile(strPathName, a_pCSEMStageData)) { loger.Error("OnClickedBtnImport: failed to import a stage."); return; } // set modify flag m_pStageFile.SetModify(true); // the new stage insert to stage list SetStageListControlData(); loger.Trace("OnClickedBtnImport: a stage has been import."); } } else { loger.Error("OnClickedBtnImport: import action has been cancled."); return; } // show the stage m_bShowFlag = false; } public void OnClickedBtnAply(object sender, EventArgs e) { loger.Info("OnClickedBtnAply: the selected stage will be the working stage."); UpdateStageFileData(true); //create stage manager file if (m_pStageFile.IsModified()) { if (!m_pStageFile.Save()) { loger.Error("OnClickedBtnAply: save a stage file failed."); return; } } this.Close(); //CDialog::OnOK(); } public void OnClickedBtnDel(object sender, EventArgs e) { loger.Info("OnClickedBtnDel: the selected stage will be deleted."); if (DialogResult.Cancel == MessageBox.Show("Are you sure to delete this sample station?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { loger.Info("OnClickedBtnDel: user cancled."); return; } UpdateStageFileData(true); // remove the stage if (m_pStageFile.GetStagesList().Count() > 0) { m_pStageFile.GetStagesList().RemoveAt(m_nListBoxStageListIndex); // set new working stage m_nListBoxStageListIndex = 0; m_pStageFile.SetWorkingStageId(m_nListBoxStageListIndex); m_pStageFile.SetModify(); //m_nListBoxStageList--; Invalidate(); } SetStageListControlData(); } public void OnClickedBtnExport(object sender, EventArgs e) { loger.Info("OnClickedBtnExport: the selected stage will be export stage into a txt."); UpdateStageFileData(true); // export a stage into text file SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.DefaultExt = null; saveFileDialog.InitialDirectory = null; saveFileDialog.CheckPathExists = true; saveFileDialog.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*"; DialogResult dr = saveFileDialog.ShowDialog(); if (dr == System.Windows.Forms.DialogResult.OK) { string strPathName = saveFileDialog.FileName; if (string.IsNullOrEmpty(strPathName)) { loger.Error("OnClickedBtnExport: empty path."); return; } m_pStageFile.SaveStageIntoTextFile(m_nListBoxStageListIndex, strPathName); } else { loger.Error("OnClickedBtnExport: export action is cancled."); return; } SetStageListControlData(); } public void OnClickedBtnRename(object sender, EventArgs e) { loger.Trace("OnClickedBtnRename: the selected stage will be renamed."); UpdateStageFileData(true); CStage pStage = m_pStageFile.GetStagesList()[m_nListBoxStageListIndex]; DlgStageRename dlg = new DlgStageRename(); dlg.m_sEditName = pStage.GetName(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { pStage.SetName(dlg.m_sStageName); m_pStageFile.SetModify(); } else { loger.Error("OnClickedBtnRename: rename action is cancled."); return; } SetStageListControlData(); } void OnSelchangeListStage(object sender, EventArgs e) { m_nListBoxStageListIndex = m_ctrlListBoxStageList.SelectedIndex; //UpdateStageFileData(true); 为避免关闭界面仍然保存当前选择的样品台,此处注销了 m_ctrlStagePicture.Refresh(); DrawStage(); } void OnPaint(object sender, PaintEventArgs e) { //CPaintDC dc(this); // device context for painting if (!m_bShowFlag) { DrawStage(); } } public void SetStageListControlData() { if (m_ctrlListBoxStageList.Items.Count > 0) { m_ctrlListBoxStageList.Items.Clear(); } // there is stage in the stage file? if (m_pStageFile.GetStagesList().Count > 0) { // there is sample on the work stage //if (m_pStageFile.GetInUse() == true) //{ // // can't change working stage // m_ctrlBtnApply.Enabled = false; // // if working stage id == selected id // if (m_nListBoxStageListIndex == m_pStageFile.GetWorkingStageId()) // { // // can't delete and rename the woking stage // m_ctrlBtnDelete.Enabled = false; // m_ctrlBtnRename.Enabled = false; // } // else // { // m_ctrlBtnDelete.Enabled = true; // m_ctrlBtnRename.Enabled = true; // } //} //else //{ m_ctrlBtnApply.Enabled = true; m_ctrlBtnDelete.Enabled = true; m_ctrlBtnRename.Enabled = true; //} foreach (var pStage in m_pStageFile.GetStagesList()) { string sCurrentName = pStage.GetName(); int findIndex = m_ctrlListBoxStageList.FindString(sCurrentName); if (findIndex < 0) { m_ctrlListBoxStageList.Items.Add(sCurrentName); } } //default is the first item m_ctrlListBoxStageList.SelectedIndex = m_nListBoxStageListIndex; m_ctrlBtnExport.Enabled = true; } else { // the control can't be used because there is no stage in the file m_ctrlBtnApply.Enabled = false; m_ctrlBtnApply.Enabled = false; m_ctrlBtnDelete.Enabled = false; m_ctrlBtnExport.Enabled = false; m_ctrlBtnRename.Enabled = false; } } public void UpdateStageFileData(bool a_bUpdateData/* = TRUE*/) { if (a_bUpdateData) { UpdateData(true); } // if there is sample on the stage, the working stage can't be changed. m_pStageFile.SetWorkingStageId(m_nListBoxStageListIndex); } public bool UpdateData(bool bSaveAndValidate) { return true; } public void DrawStage() { if (m_nListBoxStageListIndex < 0 || m_nListBoxStageListIndex > (int)(m_pStageFile.GetStagesList().Count)) { return; } Rectangle rc = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size); Rectangle re = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size); int pWnd = GetDlgItem(otsdataconst.IDC_PIC_STAGE); int nWidth = (int)rc.Width; int nHeight = (int)rc.Height; Object pDC = new Object(); bool DeleteObject = false; //paint the DC with white Brush pOldBrush = new SolidBrush(Color.FromArgb(50, Color.White)); //Draw stage if (m_pStageFile.GetStagesList().Count > 0) { m_ctrlStagePicture.Refresh(); //Image img = (Image)new Bitmap(m_ctrlStagePicture.Width, m_ctrlStagePicture.Height); //Graphics graphics = m_ctrlStagePicture.CreateGraphics();// Graphics.FromImage(img); //graphics.FillRectangle(new SolidBrush(Color.Beige), re); pDC = m_ctrlStagePicture.CreateGraphics(); //消除锯齿 ((Graphics)pDC).SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式选择 ((Graphics)pDC).InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量 ((Graphics)pDC).CompositingQuality = CompositingQuality.HighQuality;//再加一点 //get stage information List pStage = m_pStageFile.GetStagesList();//get curretn working stage CDomain pBoundery = pStage[m_nListBoxStageListIndex].GetBoundary();// GetBoundary(); System.Drawing.Rectangle BounderyRect = pBoundery.GetDomainRect(); int nBounderyWidth = (int)(BounderyRect.Width);//um,,pixle is (nWidth - PIC_EDGE * 2 ) int nBounderyHeight = (int)(BounderyRect.Height); double PixSize = 0; if (nBounderyWidth > nBounderyHeight) { PixSize = (long)((double)nBounderyWidth / (double)(nWidth - StageDisplayHelp.PIC_EDGE * 2)); } else { PixSize = (long)((double)nBounderyHeight / (double)(nHeight - StageDisplayHelp.PIC_EDGE * 2)); } //draw boundery Brush pLTGrayBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); stageDisplayHelp.DrawStageBoundery(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize); ////draw STD //Brush pBlackBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ; //DrawStageSTD(m_pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize); ////draw holes //Brush pWriteBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ; stageDisplayHelp.DrawStageHole(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize); // draw ratio stageDisplayHelp.DrawRatio(nBounderyWidth, nWidth, nHeight, pDC, (IntPtr)pWnd, rc); pLTGrayBrush.Dispose(); //pBlackBrush.Dispose(); //pWriteBrush.Dispose(); //graphics.Dispose(); //((Graphics)pDC).Dispose(); } else { // paint the stage m_bShowFlag = true; } pOldBrush.Dispose(); } public int GetDlgItem(int nID) { return nID; } //// stage file get and set //public CStageParam GetStageFile() //{ // return m_pStageFile; //} //public void SetStageFile(CStageParam a_pStageFile) //{ // m_pStageFile = a_pStageFile; //} //// soft pack id get and set //public otsdataconst.OTS_SysType_ID GetPackId() { return m_nPackId; } //public void SetPackId(otsdataconst.OTS_SysType_ID a_nPackId) { m_nPackId = a_nPackId; } public void DrawStageSTD(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSize) { if (a_pStage == null) { return; } CDomain pSTD = a_pStage.GetSTD(); otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1); nShape = pSTD.GetShape(); Rectangle PixRect = new Rectangle(); stageDisplayHelp.GetPixRect(pSTD, a_nWidth, a_nHeight, a_dPixSize,ref PixRect); stageDisplayHelp.DrawShape(PixRect, pDC, nShape, false); } //public Brush GetStockObject(int i) { // BrushConverter brushConverter = new BrushConverter(Color.Brown); // return (Brush)brushConverter.ConvertFromString(LTGRAY_BRUSH.ToString()); //} public Brush GetStockObject(int i) { return null; } // boundary public CDomain GetBoundary() { return m_poBourary; } public Brush SelectObject(Brush pBrush) { return pBrush; } public void DrawRatio(int a_nBounderyWidth, int a_nWidth, int a_nHeight, int pDC, int pWnd, Rectangle rc) { if (pDC == 0) { return; } if (pWnd == 0) { return; } int nRatio = a_nBounderyWidth * (a_nWidth / 2 - StageDisplayHelp.RATIO_RECT_LEFT - 10) / (a_nWidth - StageDisplayHelp.PIC_EDGE * 2) / 1000; //显示在标尺上的mm数 String.Format("%dcm", nRatio); System.Drawing.Point oRatioStart = new System.Drawing.Point(); oRatioStart.X = StageDisplayHelp.RATIO_RECT_LEFT; oRatioStart.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_HEIGHT - StageDisplayHelp.RATIO_RECT_DOWN; System.Drawing.Point oRatioEnd = new System.Drawing.Point(); oRatioEnd.X = a_nWidth / 2; oRatioEnd.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_DOWN; Control control = new Control(); //标尺 FontDialog fontdialog = new FontDialog(); fontdialog.ShowDialog(); control.Font = fontdialog.Font; control.Font = new System.Drawing.Font("华文新魏", 22.2f, FontStyle.Bold); System.Drawing.Font def_font = new System.Drawing.Font(control.Font, FontStyle.Regular); BLENDFUNCTION bf = new BLENDFUNCTION(); bf.BlendOp = 1; bf.BlendFlags = 0; bf.AlphaFormat = 0; bf.SourceConstantAlpha = 10; } private void button_edit_Click(object sender, EventArgs e) { DlgStageEdit dlgStageEdit = new DlgStageEdit(m_pStageFile.GetStagesList()[m_ctrlListBoxStageList.SelectedIndex]); if (dlgStageEdit.ShowDialog() == DialogResult.OK) { List cli = m_pStageFile.GetStagesList(); cli[m_ctrlListBoxStageList.SelectedIndex]=dlgStageEdit.m_pStage; //m_pStageFile.SetStagesList(cli, true); m_pStageFile.SetModify(true); SetStageListControlData(); } else { } } private void button_New_Click(object sender, EventArgs e) { DlgStageEdit dlgStageEdit = new DlgStageEdit(); if (dlgStageEdit.ShowDialog() == DialogResult.OK) { List cli = m_pStageFile.GetStagesList(); cli.Add(dlgStageEdit.m_pStage); //m_pStageFile.SetStagesList(cli, true); m_pStageFile.SetModify(true); SetStageListControlData(); } else { } } //public CStageParam ShowStageMgrDialog(otsdataconst.OTS_SysType_ID a_nPackId, CStageParam a_pStageFile) //{ // if (a_pStageFile == null) // { // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog:invalid stage file.")); // loger.Error("ShowStageMgrDialog:invalid stage file."); // return null; // } // //show the dialog // DlgStageMgr dlg = new DlgStageMgr(a_pStageFile); // dlg.SetStageFile(a_pStageFile); // dlg.SetPackId(a_nPackId); // dlg.ShowDialog(); // CStageParam pStageFile = dlg.GetStageFile(); // //ASSERT(pStageFile); // if (pStageFile == null) // { // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog: Load stage from stage manager dlg failed.")); // loger.Error("ShowStageMgrDialog: Load stage from stage manager dlg failed."); // return null; // } // return pStageFile; //} } }