DlgStageMgr.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 CStageParam 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(CStageParam 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. CStage 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 (findIndex < 0)
  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 (a_bUpdateData)
  268. {
  269. UpdateData(true);
  270. }
  271. // if there is sample on the stage, the working stage can't be changed.
  272. m_pStageFile.SetWorkingStageId(m_nListBoxStageListIndex);
  273. }
  274. public bool UpdateData(bool bSaveAndValidate)
  275. {
  276. return true;
  277. }
  278. public void DrawStage()
  279. {
  280. if (m_nListBoxStageListIndex < 0 || m_nListBoxStageListIndex > (int)(m_pStageFile.GetStagesList().Count))
  281. {
  282. return;
  283. }
  284. Rectangle rc = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size);
  285. Rectangle re = new Rectangle(m_ctrlStagePicture.Location, m_ctrlStagePicture.Size);
  286. int pWnd = GetDlgItem(otsdataconst.IDC_PIC_STAGE);
  287. int nWidth = (int)rc.Width;
  288. int nHeight = (int)rc.Height;
  289. Object pDC = new Object();
  290. bool DeleteObject = false;
  291. //paint the DC with white
  292. Brush pOldBrush = new SolidBrush(Color.FromArgb(50, Color.White));
  293. //Draw stage
  294. if (m_pStageFile.GetStagesList().Count > 0)
  295. {
  296. m_ctrlStagePicture.Refresh();
  297. //Image img = (Image)new Bitmap(m_ctrlStagePicture.Width, m_ctrlStagePicture.Height);
  298. //Graphics graphics = m_ctrlStagePicture.CreateGraphics();// Graphics.FromImage(img);
  299. //graphics.FillRectangle(new SolidBrush(Color.Beige), re);
  300. pDC = m_ctrlStagePicture.CreateGraphics();
  301. //消除锯齿
  302. ((Graphics)pDC).SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式选择
  303. ((Graphics)pDC).InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
  304. ((Graphics)pDC).CompositingQuality = CompositingQuality.HighQuality;//再加一点
  305. //get stage information
  306. List<CStage> pStage = m_pStageFile.GetStagesList();//get curretn working stage
  307. CDomain pBoundery = pStage[m_nListBoxStageListIndex].GetBoundary();// GetBoundary();
  308. System.Drawing.Rectangle BounderyRect = pBoundery.GetDomainRect();
  309. int nBounderyWidth = (int)(BounderyRect.Width);//um,,pixle is (nWidth - PIC_EDGE * 2 )
  310. int nBounderyHeight = (int)(BounderyRect.Height);
  311. double PixSize = 0;
  312. if (nBounderyWidth > nBounderyHeight)
  313. {
  314. PixSize = (long)((double)nBounderyWidth / (double)(nWidth - StageDisplayHelp.PIC_EDGE * 2));
  315. }
  316. else
  317. {
  318. PixSize = (long)((double)nBounderyHeight / (double)(nHeight - StageDisplayHelp.PIC_EDGE * 2));
  319. }
  320. //draw boundery
  321. Brush pLTGrayBrush = new SolidBrush(Color.FromArgb(255, 0, 0));
  322. stageDisplayHelp.DrawStageBoundery(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize);
  323. ////draw STD
  324. //Brush pBlackBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ;
  325. //DrawStageSTD(m_pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize);
  326. ////draw holes
  327. //Brush pWriteBrush = new SolidBrush(Color.FromArgb(255, 0, 0)); ;
  328. stageDisplayHelp.DrawStageHole(pStage[m_nListBoxStageListIndex], nWidth, nHeight, pDC, PixSize);
  329. // draw ratio
  330. stageDisplayHelp.DrawRatio(nBounderyWidth, nWidth, nHeight, pDC, (IntPtr)pWnd, rc);
  331. pLTGrayBrush.Dispose();
  332. //pBlackBrush.Dispose();
  333. //pWriteBrush.Dispose();
  334. //graphics.Dispose();
  335. //((Graphics)pDC).Dispose();
  336. }
  337. else
  338. {
  339. // paint the stage
  340. m_bShowFlag = true;
  341. }
  342. pOldBrush.Dispose();
  343. }
  344. public int GetDlgItem(int nID)
  345. {
  346. return nID;
  347. }
  348. //// stage file get and set
  349. //public CStageParam GetStageFile()
  350. //{
  351. // return m_pStageFile;
  352. //}
  353. //public void SetStageFile(CStageParam a_pStageFile)
  354. //{
  355. // m_pStageFile = a_pStageFile;
  356. //}
  357. //// soft pack id get and set
  358. //public otsdataconst.OTS_SysType_ID GetPackId() { return m_nPackId; }
  359. //public void SetPackId(otsdataconst.OTS_SysType_ID a_nPackId) { m_nPackId = a_nPackId; }
  360. public void DrawStageSTD(CStage a_pStage, int a_nWidth, int a_nHeight, Object pDC, double a_dPixSize)
  361. {
  362. if (a_pStage == null)
  363. {
  364. return;
  365. }
  366. CDomain pSTD = a_pStage.GetSTD();
  367. otsdataconst.DOMAIN_SHAPE nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  368. nShape = pSTD.GetShape();
  369. Rectangle PixRect = new Rectangle();
  370. stageDisplayHelp.GetPixRect(pSTD, a_nWidth, a_nHeight, a_dPixSize,ref PixRect);
  371. stageDisplayHelp.DrawShape(PixRect, pDC, nShape, false);
  372. }
  373. //public Brush GetStockObject(int i) {
  374. // BrushConverter brushConverter = new BrushConverter(Color.Brown);
  375. // return (Brush)brushConverter.ConvertFromString(LTGRAY_BRUSH.ToString());
  376. //}
  377. public Brush GetStockObject(int i)
  378. {
  379. return null;
  380. }
  381. // boundary
  382. public CDomain GetBoundary()
  383. {
  384. return m_poBourary;
  385. }
  386. public Brush SelectObject(Brush pBrush)
  387. {
  388. return pBrush;
  389. }
  390. public void DrawRatio(int a_nBounderyWidth, int a_nWidth, int a_nHeight, int pDC, int pWnd, Rectangle rc)
  391. {
  392. if (pDC == 0)
  393. {
  394. return;
  395. }
  396. if (pWnd == 0)
  397. {
  398. return;
  399. }
  400. int nRatio = a_nBounderyWidth * (a_nWidth / 2 - StageDisplayHelp.RATIO_RECT_LEFT - 10) / (a_nWidth - StageDisplayHelp.PIC_EDGE * 2) / 1000; //显示在标尺上的mm数
  401. String.Format("%dcm", nRatio);
  402. System.Drawing.Point oRatioStart = new System.Drawing.Point();
  403. oRatioStart.X = StageDisplayHelp.RATIO_RECT_LEFT;
  404. oRatioStart.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_HEIGHT - StageDisplayHelp.RATIO_RECT_DOWN;
  405. System.Drawing.Point oRatioEnd = new System.Drawing.Point();
  406. oRatioEnd.X = a_nWidth / 2;
  407. oRatioEnd.Y = a_nHeight - StageDisplayHelp.RATIO_RECT_DOWN;
  408. Control control = new Control();
  409. //标尺
  410. FontDialog fontdialog = new FontDialog();
  411. fontdialog.ShowDialog();
  412. control.Font = fontdialog.Font;
  413. control.Font = new System.Drawing.Font("华文新魏", 22.2f, FontStyle.Bold);
  414. System.Drawing.Font def_font = new System.Drawing.Font(control.Font, FontStyle.Regular);
  415. BLENDFUNCTION bf = new BLENDFUNCTION();
  416. bf.BlendOp = 1;
  417. bf.BlendFlags = 0;
  418. bf.AlphaFormat = 0;
  419. bf.SourceConstantAlpha = 10;
  420. }
  421. private void button_edit_Click(object sender, EventArgs e)
  422. {
  423. DlgStageEdit dlgStageEdit = new DlgStageEdit(m_pStageFile.GetStagesList()[m_ctrlListBoxStageList.SelectedIndex]);
  424. if (dlgStageEdit.ShowDialog() == DialogResult.OK)
  425. {
  426. List<CStage> cli = m_pStageFile.GetStagesList();
  427. cli[m_ctrlListBoxStageList.SelectedIndex]=dlgStageEdit.m_pStage;
  428. //m_pStageFile.SetStagesList(cli, true);
  429. m_pStageFile.SetModify(true);
  430. SetStageListControlData();
  431. }
  432. else
  433. {
  434. }
  435. }
  436. private void button_New_Click(object sender, EventArgs e)
  437. {
  438. DlgStageEdit dlgStageEdit = new DlgStageEdit();
  439. if (dlgStageEdit.ShowDialog() == DialogResult.OK)
  440. {
  441. List<CStage> cli = m_pStageFile.GetStagesList();
  442. cli.Add(dlgStageEdit.m_pStage);
  443. //m_pStageFile.SetStagesList(cli, true);
  444. m_pStageFile.SetModify(true);
  445. SetStageListControlData();
  446. }
  447. else
  448. {
  449. }
  450. }
  451. //public CStageParam ShowStageMgrDialog(otsdataconst.OTS_SysType_ID a_nPackId, CStageParam a_pStageFile)
  452. //{
  453. // if (a_pStageFile == null)
  454. // {
  455. // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog:invalid stage file."));
  456. // loger.Error("ShowStageMgrDialog:invalid stage file.");
  457. // return null;
  458. // }
  459. // //show the dialog
  460. // DlgStageMgr dlg = new DlgStageMgr(a_pStageFile);
  461. // dlg.SetStageFile(a_pStageFile);
  462. // dlg.SetPackId(a_nPackId);
  463. // dlg.ShowDialog();
  464. // CStageParam pStageFile = dlg.GetStageFile();
  465. // //ASSERT(pStageFile);
  466. // if (pStageFile == null)
  467. // {
  468. // //LogErrorTrace(__FILE__, __LINE__, _T("ShowStageMgrDialog: Load stage from stage manager dlg failed."));
  469. // loger.Error("ShowStageMgrDialog: Load stage from stage manager dlg failed.");
  470. // return null;
  471. // }
  472. // return pStageFile;
  473. //}
  474. }
  475. }