DlgStageMgr.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using OTSDataType;
  11. using static OTSModelSharp.ResourceManage.ResourceID;
  12. using static OTSModelSharp.ResourceManage.ResourceData;
  13. using System.Drawing.Drawing2D;
  14. using OTSMeasureApp._7_OTSProgMgrInfo.Stage;
  15. namespace OTSModelSharp.Measure.GetStageInfo
  16. {
  17. public partial class DlgStageMgr : Form
  18. {
  19. public static NLog.Logger loger = NLog.LogManager.GetCurrentClassLogger();
  20. otsdataconst model = new otsdataconst();
  21. // stage file
  22. public CStageHolderParam m_pStageFile;
  23. StageDisplayHelp stageDisplayHelp;
  24. // stage list value
  25. int m_nListBoxStageListIndex=0;
  26. // flag if the stage should be re-paint
  27. bool m_bShowFlag;
  28. // boundary
  29. //CDomain m_poBourary;
  30. const int LTGRAY_BRUSH = 1;
  31. const int BLACK_BRUSH = 4;
  32. const int WHITE_BRUSH = 0;
  33. const int ROUND_VALUE = 20; //round rect angle
  34. public struct BLENDFUNCTION
  35. {
  36. public Byte BlendOp;
  37. public Byte BlendFlags;
  38. public Byte SourceConstantAlpha;
  39. public Byte AlphaFormat;
  40. }
  41. public DlgStageMgr(CStageHolderParam m_cstagefile)
  42. {
  43. InitializeComponent();
  44. m_pStageFile = m_cstagefile;
  45. stageDisplayHelp = new StageDisplayHelp();
  46. }
  47. private void DlgStageMgr_Load(object sender, EventArgs e)
  48. {
  49. // 加载样品台文件; \Config\SysData\OTSStage.stg
  50. if (!m_pStageFile.Load( true, false))
  51. {
  52. return;
  53. }
  54. SetStageListControlData();
  55. m_ctrlListBoxStageList.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_StageList);
  56. m_ctrlBtnApply.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Apply);
  57. m_ctrlBtnDelete.Text = XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Delete);
  58. m_ctrlBtnRename.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Rename);
  59. IDC_BTN_IMPORT.Text= XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Import);
  60. m_ctrlBtnExport.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Export);
  61. this.Text=XmlResourceData.GetInstance().GetStringByKey(GrpOtherParam, DlgStageMgr_Title);
  62. }
  63. // CDlgStageMgr message map
  64. public void OnClickedBtnImport(object sender, EventArgs e)
  65. {
  66. loger.Trace("OnClickedBtnImport: import a stage from txt file.");
  67. String strDlgTitle = "";
  68. if (strDlgTitle == "")
  69. {
  70. // file open dialog
  71. OpenFileDialog openFileDialog = new OpenFileDialog();
  72. // get file pathname
  73. openFileDialog.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
  74. strDlgTitle = openFileDialog.FileName;
  75. DialogResult dresult = openFileDialog.ShowDialog();
  76. if (dresult == DialogResult.OK)
  77. {
  78. String strPathName = openFileDialog.FileName;
  79. if (strPathName == "")
  80. {
  81. loger.Error("OnClickedBtnImport: input an empty path.");
  82. return;
  83. }
  84. CSEMStageData a_pCSEMStageData /*= new CSEMStageData()*/;
  85. //获取配置文件中 StageData 内容
  86. COTSDefaultParam m_DefaultParam = new COTSDefaultParam();
  87. m_DefaultParam.LoadInfoFromProgMgrFile();
  88. a_pCSEMStageData = m_DefaultParam.GetStageDataParam();
  89. // load a stage from txt file
  90. if (!m_pStageFile.LoadStageFromTextFile(strPathName, a_pCSEMStageData))
  91. {
  92. loger.Error("OnClickedBtnImport: failed to import a stage.");
  93. return;
  94. }
  95. // set modify flag
  96. m_pStageFile.SetModify(true);
  97. // the new stage insert to stage list
  98. SetStageListControlData();
  99. loger.Trace("OnClickedBtnImport: a stage has been import.");
  100. }
  101. }
  102. else
  103. {
  104. loger.Error("OnClickedBtnImport: import action has been cancled.");
  105. return;
  106. }
  107. // show the stage
  108. m_bShowFlag = false;
  109. }
  110. public void OnClickedBtnAply(object sender, EventArgs e)
  111. {
  112. loger.Info("OnClickedBtnAply: the selected stage will be the working stage.");
  113. UpdateStageFileData(true);
  114. //create stage manager file
  115. if (m_pStageFile.IsModified())
  116. {
  117. if (!m_pStageFile.Save())
  118. {
  119. loger.Error("OnClickedBtnAply: save a stage file failed.");
  120. return;
  121. }
  122. }
  123. this.Close(); //CDialog::OnOK();
  124. }
  125. public void OnClickedBtnDel(object sender, EventArgs e)
  126. {
  127. loger.Info("OnClickedBtnDel: the selected stage will be deleted.");
  128. if (DialogResult.Cancel == MessageBox.Show("Are you sure to delete this sample station?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
  129. {
  130. loger.Info("OnClickedBtnDel: user cancled.");
  131. return;
  132. }
  133. UpdateStageFileData(true);
  134. // remove the stage
  135. if (m_pStageFile.GetStagesList().Count() > 0)
  136. {
  137. m_pStageFile.GetStagesList().RemoveAt(m_nListBoxStageListIndex);
  138. // set new working stage
  139. m_nListBoxStageListIndex = 0;
  140. m_pStageFile.SetWorkingStageId(m_nListBoxStageListIndex);
  141. m_pStageFile.SetModify();
  142. //m_nListBoxStageList--;
  143. Invalidate();
  144. }
  145. SetStageListControlData();
  146. }
  147. public void OnClickedBtnExport(object sender, EventArgs e)
  148. {
  149. loger.Info("OnClickedBtnExport: the selected stage will be export stage into a txt.");
  150. UpdateStageFileData(true);
  151. // export a stage into text file
  152. SaveFileDialog saveFileDialog = new SaveFileDialog();
  153. saveFileDialog.DefaultExt = null;
  154. saveFileDialog.InitialDirectory = null;
  155. saveFileDialog.CheckPathExists = true;
  156. saveFileDialog.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
  157. DialogResult dr = saveFileDialog.ShowDialog();
  158. if (dr == System.Windows.Forms.DialogResult.OK)
  159. {
  160. string strPathName = saveFileDialog.FileName;
  161. if (string.IsNullOrEmpty(strPathName))
  162. {
  163. loger.Error("OnClickedBtnExport: empty path.");
  164. return;
  165. }
  166. m_pStageFile.SaveStageIntoTextFile(m_nListBoxStageListIndex, strPathName);
  167. }
  168. else
  169. {
  170. loger.Error("OnClickedBtnExport: export action is cancled.");
  171. return;
  172. }
  173. SetStageListControlData();
  174. }
  175. public void OnClickedBtnRename(object sender, EventArgs e)
  176. {
  177. loger.Trace("OnClickedBtnRename: the selected stage will be renamed.");
  178. UpdateStageFileData(true);
  179. CSampleHolder pStage = m_pStageFile.GetStagesList()[m_nListBoxStageListIndex];
  180. DlgStageRename dlg = new DlgStageRename();
  181. dlg.m_sEditName = pStage.GetName();
  182. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  183. {
  184. pStage.SetName(dlg.m_sStageName);
  185. m_pStageFile.SetModify();
  186. }
  187. else
  188. {
  189. loger.Error("OnClickedBtnRename: rename action is cancled.");
  190. return;
  191. }
  192. SetStageListControlData();
  193. }
  194. void OnSelchangeListStage(object sender, EventArgs e)
  195. {
  196. m_nListBoxStageListIndex = m_ctrlListBoxStageList.SelectedIndex;
  197. //UpdateStageFileData(true); 为避免关闭界面仍然保存当前选择的样品台,此处注销了
  198. m_ctrlStagePicture.Refresh();
  199. DrawStage();
  200. }
  201. void OnPaint(object sender, PaintEventArgs e)
  202. {
  203. //CPaintDC dc(this); // device context for painting
  204. if (!m_bShowFlag)
  205. {
  206. DrawStage();
  207. }
  208. }
  209. public void SetStageListControlData()
  210. {
  211. if (m_ctrlListBoxStageList.Items.Count > 0)
  212. {
  213. m_ctrlListBoxStageList.Items.Clear();
  214. }
  215. // there is stage in the stage file?
  216. if (m_pStageFile.GetStagesList().Count > 0)
  217. {
  218. // there is sample on the work stage
  219. //if (m_pStageFile.GetInUse() == true)
  220. //{
  221. // // can't change working stage
  222. // m_ctrlBtnApply.Enabled = false;
  223. // // if working stage id == selected id
  224. // if (m_nListBoxStageListIndex == m_pStageFile.GetWorkingStageId())
  225. // {
  226. // // can't delete and rename the woking stage
  227. // m_ctrlBtnDelete.Enabled = false;
  228. // m_ctrlBtnRename.Enabled = false;
  229. // }
  230. // else
  231. // {
  232. // m_ctrlBtnDelete.Enabled = true;
  233. // m_ctrlBtnRename.Enabled = true;
  234. // }
  235. //}
  236. //else
  237. //{
  238. m_ctrlBtnApply.Enabled = true;
  239. m_ctrlBtnDelete.Enabled = true;
  240. m_ctrlBtnRename.Enabled = true;
  241. //}
  242. foreach (var pStage in m_pStageFile.GetStagesList())
  243. {
  244. string sCurrentName = pStage.GetName();
  245. //int findIndex = m_ctrlListBoxStageList.FindString(sCurrentName);
  246. if (!m_ctrlListBoxStageList.Items.Contains(sCurrentName))
  247. {
  248. m_ctrlListBoxStageList.Items.Add(sCurrentName);
  249. }
  250. }
  251. //default is the first item
  252. m_ctrlListBoxStageList.SelectedIndex = m_nListBoxStageListIndex;
  253. m_ctrlBtnExport.Enabled = true;
  254. }
  255. else
  256. {
  257. // the control can't be used because there is no stage in the file
  258. m_ctrlBtnApply.Enabled = false;
  259. m_ctrlBtnApply.Enabled = false;
  260. m_ctrlBtnDelete.Enabled = false;
  261. m_ctrlBtnExport.Enabled = false;
  262. m_ctrlBtnRename.Enabled = false;
  263. }
  264. }
  265. public void UpdateStageFileData(bool a_bUpdateData/* = TRUE*/)
  266. {
  267. // if there is sample on the stage, the working stage can't be changed.
  268. m_pStageFile.SetWorkingStageId(m_nListBoxStageListIndex);
  269. }
  270. public void DrawStage()
  271. {
  272. if (m_nListBoxStageListIndex < 0 || m_nListBoxStageListIndex > (int)(m_pStageFile.GetStagesList().Count))
  273. {
  274. return;
  275. }
  276. Rectangle rc = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size);
  277. //Rectangle re = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size);
  278. int pWnd = GetDlgItem(otsdataconst.IDC_PIC_STAGE);
  279. int nWidth = (int)rc.Width;
  280. int nHeight = (int)rc.Height;
  281. Object pDC ;
  282. //bool DeleteObject = false;
  283. //paint the DC with white
  284. Brush pOldBrush = new SolidBrush(Color.FromArgb(50, Color.White));
  285. //Draw stage
  286. if (m_pStageFile.GetStagesList().Count > 0)
  287. {
  288. m_ctrlStagePicture.Refresh();
  289. //Image img = (Image)new Bitmap(m_ctrlStagePicture.Width, m_ctrlStagePicture.Height);
  290. //Graphics graphics = m_ctrlStagePicture.CreateGraphics();// Graphics.FromImage(img);
  291. //graphics.FillRectangle(new SolidBrush(Color.Beige), re);
  292. pDC = m_ctrlStagePicture.CreateGraphics();
  293. //消除锯齿
  294. ((Graphics)pDC).SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式选择
  295. ((Graphics)pDC).InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
  296. ((Graphics)pDC).CompositingQuality = CompositingQuality.HighQuality;//再加一点
  297. //get stage information
  298. List<CSampleHolder> pStage = m_pStageFile.GetStagesList();//get curretn working stage
  299. CDomain pBoundery = pStage[m_nListBoxStageListIndex].GetBoundary();// GetBoundary();
  300. System.Drawing.RectangleF BounderyRect = pBoundery.GetDomainRect();
  301. int nBounderyWidth = (int)(BounderyRect.Width);//um,,pixle is (nWidth - PIC_EDGE * 2 )
  302. int nBounderyHeight = (int)(BounderyRect.Height);
  303. double PixSizeX = 0;
  304. double PixSizeY = 0;
  305. PixSizeX = (long)((double)nBounderyWidth / (double)(nWidth - StageDisplayHelp.PIC_EDGE * 2));
  306. PixSizeY = (long)((double)nBounderyHeight / (double)(nHeight - StageDisplayHelp.PIC_EDGE * 2));
  307. //draw boundery
  308. Brush pLTGrayBrush = new SolidBrush(Color.FromArgb(255, 0, 0));
  309. stageDisplayHelp.DrawStageBoundery(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSizeX, PixSizeY);
  310. ////draw STD
  311. //Brush pBlackBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ;
  312. //DrawStageSTD(m_pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize);
  313. ////draw holes
  314. //Brush pWriteBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ;
  315. stageDisplayHelp.DrawStageHole(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSizeX, PixSizeY);
  316. // draw ratio
  317. stageDisplayHelp.DrawRatio(nBounderyWidth, nWidth, nHeight, pDC, (IntPtr)pWnd, rc);
  318. pLTGrayBrush.Dispose();
  319. //pBlackBrush.Dispose();
  320. //pWriteBrush.Dispose();
  321. //graphics.Dispose();
  322. //((Graphics)pDC).Dispose();
  323. }
  324. else
  325. {
  326. // paint the stage
  327. m_bShowFlag = true;
  328. }
  329. pOldBrush.Dispose();
  330. }
  331. public int GetDlgItem(int nID)
  332. {
  333. return nID;
  334. }
  335. //// stage file get and set
  336. //public CStageParam GetStageFile()
  337. //{
  338. // return m_pStageFile;
  339. //}
  340. //public void SetStageFile(CStageParam a_pStageFile)
  341. //{
  342. // m_pStageFile = a_pStageFile;
  343. //}
  344. //// soft pack id get and set
  345. //public otsdataconst.OTS_SysType_ID GetPackId() { return m_nPackId; }
  346. //public void SetPackId(otsdataconst.OTS_SysType_ID a_nPackId) { m_nPackId = a_nPackId; }
  347. //public Brush GetStockObject(int i) {
  348. // BrushConverter brushConverter = new BrushConverter(Color.Brown);
  349. // return (Brush)brushConverter.ConvertFromString(LTGRAY_BRUSH.ToString());
  350. //}
  351. public Brush GetStockObject(int i)
  352. {
  353. return null;
  354. }
  355. // boundary
  356. //public CDomain GetBoundary()
  357. //{
  358. // return m_poBourary;
  359. //}
  360. public Brush SelectObject(Brush pBrush)
  361. {
  362. return pBrush;
  363. }
  364. public void DrawRatio(int a_nBounderyWidth, int a_nWidth, int a_nHeight, int pDC, int pWnd, Rectangle rc)
  365. {
  366. if (pDC == 0)
  367. {
  368. return;
  369. }
  370. if (pWnd == 0)
  371. {
  372. return;
  373. }
  374. int nRatio = a_nBounderyWidth * (a_nWidth / 2 - StageDisplayHelp.RATIO_RECT_LEFT - 10) / (a_nWidth - StageDisplayHelp.PIC_EDGE * 2) / 1000; //显示在标尺上的mm数
  375. String.Format("%dcm", nRatio);
  376. System.Drawing.Point oRatioStart = new System.Drawing.Point();
  377. oRatioStart.X = StageDisplayHelp.RATIO_RECT_LEFT;
  378. oRatioStart.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_HEIGHT - StageDisplayHelp.RATIO_RECT_DOWN;
  379. System.Drawing.Point oRatioEnd = new System.Drawing.Point();
  380. oRatioEnd.X = a_nWidth / 2;
  381. oRatioEnd.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_DOWN;
  382. Control control = new Control();
  383. //标尺
  384. FontDialog fontdialog = new FontDialog();
  385. fontdialog.ShowDialog();
  386. control.Font = fontdialog.Font;
  387. control.Font = new System.Drawing.Font("华文新魏", 22.2f, FontStyle.Bold);
  388. System.Drawing.Font def_font = new System.Drawing.Font(control.Font, FontStyle.Regular);
  389. BLENDFUNCTION bf = new BLENDFUNCTION();
  390. bf.BlendOp = 1;
  391. bf.BlendFlags = 0;
  392. bf.AlphaFormat = 0;
  393. bf.SourceConstantAlpha = 10;
  394. }
  395. private void button_edit_Click(object sender, EventArgs e)
  396. {
  397. List<string> StageNames = new List<string>();
  398. foreach (var it in m_ctrlListBoxStageList.Items)
  399. {
  400. StageNames.Add(it.ToString());
  401. }
  402. DlgStageEdit dlgStageEdit = new DlgStageEdit(m_pStageFile.GetStagesList()[m_ctrlListBoxStageList.SelectedIndex], StageNames);
  403. DialogResult dialogResult = dlgStageEdit.ShowDialog();
  404. if (dialogResult == DialogResult.OK)
  405. {
  406. List<CSampleHolder> cli = m_pStageFile.GetStagesList();
  407. cli[m_ctrlListBoxStageList.SelectedIndex]=dlgStageEdit.Stage;
  408. //m_pStageFile.SetStagesList(cli, true);
  409. m_pStageFile.SetModify(true);
  410. SetStageListControlData();
  411. }
  412. else if(dialogResult == DialogResult.No)
  413. {
  414. List<CSampleHolder> cli = m_pStageFile.GetStagesList();
  415. cli.Add(dlgStageEdit.Stage);
  416. //m_pStageFile.SetStagesList(cli, true);
  417. m_pStageFile.SetModify(true);
  418. SetStageListControlData();
  419. }
  420. }
  421. private void button_New_Click(object sender, EventArgs e)
  422. {
  423. List<string> StageNames = new List<string>();
  424. foreach(var it in m_ctrlListBoxStageList.Items)
  425. {
  426. StageNames.Add(it.ToString());
  427. }
  428. DlgStageEdit dlgStageEdit = new DlgStageEdit(StageNames);
  429. if (dlgStageEdit.ShowDialog() == DialogResult.OK)
  430. {
  431. List<CSampleHolder> cli = m_pStageFile.GetStagesList();
  432. cli.Add(dlgStageEdit.Stage);
  433. //m_pStageFile.SetStagesList(cli, true);
  434. m_pStageFile.SetModify(true);
  435. SetStageListControlData();
  436. }
  437. else
  438. {
  439. }
  440. }
  441. //public CStageParam ShowStageMgrDialog(otsdataconst.OTS_SysType_ID a_nPackId, CStageParam a_pStageFile)
  442. //{
  443. // if (a_pStageFile == null)
  444. // {
  445. // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog:invalid stage file."));
  446. // loger.Error("ShowStageMgrDialog:invalid stage file.");
  447. // return null;
  448. // }
  449. // //show the dialog
  450. // DlgStageMgr dlg = new DlgStageMgr(a_pStageFile);
  451. // dlg.SetStageFile(a_pStageFile);
  452. // dlg.SetPackId(a_nPackId);
  453. // dlg.ShowDialog();
  454. // CStageParam pStageFile = dlg.GetStageFile();
  455. // //ASSERT(pStageFile);
  456. // if (pStageFile == null)
  457. // {
  458. // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog: Load stage from stage manager dlg failed."));
  459. // loger.Error("ShowStageMgrDialog: Load stage from stage manager dlg failed.");
  460. // return null;
  461. // }
  462. // return pStageFile;
  463. //}
  464. }
  465. }