ParticlesGridDevidePage.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using OTSIncAReportApp;
  4. using OTSIncAReportApp.DataOperation.DataAccess;
  5. using OTSIncAReportApp.DataOperation.Model;
  6. using OTSIncAReportApp.SysMgrTools;
  7. using OTSIncAReportGraph.Class;
  8. using OTSIncAReportGrids.OTSIncAReportGridsFuncation;
  9. using OTSIncAReportMailInterface;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Threading;
  19. using System.Windows.Forms;
  20. namespace OTSIncAReportGrids
  21. {
  22. public partial class ParticlesGridDevidePage : UserControl
  23. {
  24. #region 枚举定义
  25. /// <summary>
  26. /// 样品台X轴方向
  27. /// </summary>
  28. enum OTS_X_AXIS_DIRECTION
  29. {
  30. LEFT_TOWARD = 0,
  31. RIGHT_TOWARD = 1
  32. }
  33. /// <summary>
  34. /// 样品台Y轴方向
  35. /// </summary>
  36. enum OTS_Y_AXIS_DIRECTION
  37. {
  38. UP_TOWARD = 0,
  39. DOWN_TOWARD = 1
  40. }
  41. #endregion
  42. //设置模拟数据表
  43. public DataTable m_dt = new DataTable();
  44. //底层操作类
  45. OTSIncAReportGridsFun m_OTSIncAReportGridsFun = null;
  46. //进度条窗体
  47. public Frm_UserProgress m_frm_userprogress;
  48. //移动SEM到指定位置发生线程
  49. private Thread m_mythread;
  50. //记录线程是否已经运行完成的状态
  51. private bool m_mythread_state = false;
  52. //导出图像文件的路径
  53. private string m_imagefilepath = "";
  54. //显示元素列表
  55. public List<string> m_list_elementscolname = null;
  56. //单个数据源所拥有的列数,这里保存下来,供导出模块使用
  57. public int m_oneresult_columncount = 0;
  58. //多个数据源,每个数据源拥有的列数,这里保存下来,供分组使用
  59. public List<int> m_list_oneresult_columncount;
  60. //国际化
  61. Language lan;
  62. Hashtable table;
  63. //测量结果
  64. ResultFile result = null;
  65. FieldData fieldData;
  66. private string m_filename = "";
  67. #region 分页器相关
  68. #region 分页字段和属性
  69. string condition = "";
  70. public string FileName = null;
  71. private int pageIndex = 1;
  72. /// <summary>
  73. /// 当前页面
  74. /// </summary>
  75. public virtual int PageIndex
  76. {
  77. get { return pageIndex; }
  78. set { pageIndex = value; }
  79. }
  80. private int pageSize = 20;
  81. /// <summary>
  82. /// 每页记录数
  83. /// </summary>
  84. public virtual int PageSize
  85. {
  86. get { return pageSize; }
  87. set { pageSize = value; }
  88. }
  89. string OrderFunction = "fieldid,particleid";
  90. private int recordCount = 0;
  91. /// <summary>
  92. /// 总记录数
  93. /// </summary>
  94. public virtual int RecordCount
  95. {
  96. get { return recordCount; }
  97. set { recordCount = value; }
  98. }
  99. private int pageCount = 0;
  100. /// <summary>
  101. /// 总页数
  102. /// </summary>
  103. public int PageCount
  104. {
  105. get
  106. {
  107. pageCount = GetPageCount();
  108. return pageCount;
  109. }
  110. }
  111. /// <summary>
  112. /// 计算总页数
  113. /// </summary>
  114. /// <returns></returns>
  115. private int GetPageCount()
  116. {
  117. if (PageSize == 0)
  118. {
  119. return 0;
  120. }
  121. int pageCount = RecordCount / PageSize;
  122. if (RecordCount % PageSize == 0)
  123. {
  124. pageCount = RecordCount / PageSize;
  125. }
  126. else
  127. {
  128. pageCount = RecordCount / PageSize + 1;
  129. }
  130. return pageCount;
  131. }
  132. #endregion
  133. frmReportApp m_ReportApp;
  134. #endregion
  135. public ParticlesGridDevidePage(frmReportApp ReportApp)
  136. {
  137. m_ReportApp = ReportApp;
  138. result = m_ReportApp.resultFilesList[m_ReportApp.WorkingResult];
  139. InitializeComponent();
  140. m_frm_userprogress = new Frm_UserProgress();
  141. m_mythread_state = false;
  142. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  143. lan = new Language(this);
  144. table = lan.GetNameTable(this.Name);
  145. fieldData = new FieldData(result.FilePath);
  146. }
  147. private void ParticlesGridDevidePage_Load(object sender, EventArgs e)
  148. {
  149. //初始化底层操作类
  150. m_OTSIncAReportGridsFun = new OTSIncAReportGridsFun(m_ReportApp, this);
  151. if (Language.ReadDefaultLanguage() == "EN")
  152. {
  153. cbB_order.Items.Add("Default");
  154. cbB_order.Items.Add("ParticlesName");
  155. cbB_order.Items.Add("Dmax Ascend");
  156. cbB_order.Items.Add("Dmax Descend");
  157. cbB_order.Items.Add("Area Ascend");
  158. cbB_order.Items.Add("Area Descend");
  159. }
  160. else
  161. {
  162. cbB_order.Items.Add("默认");
  163. cbB_order.Items.Add("颗粒名称");
  164. cbB_order.Items.Add("最大直径 升序");
  165. cbB_order.Items.Add("最大直径 降序");
  166. cbB_order.Items.Add("面积 升序");
  167. cbB_order.Items.Add("面积 降序");
  168. }
  169. cbB_order.SelectedIndex = 0;
  170. cbB_PageSize.Items.Add("20");
  171. cbB_PageSize.Items.Add("100");
  172. cbB_PageSize.Items.Add("500");
  173. cbB_PageSize.Items.Add("1000");
  174. cbB_PageSize.Items.Add("5000");
  175. cbB_PageSize.Items.Add("All");
  176. cbB_PageSize.SelectedIndexChanged -= new System.EventHandler(this.cbB_PageSize_SelectedIndexChanged);
  177. cbB_PageSize.SelectedIndex = 0;
  178. cbB_PageSize.SelectedIndexChanged += new System.EventHandler(this.cbB_PageSize_SelectedIndexChanged);
  179. }
  180. #region 自定义方法
  181. /// <summary>
  182. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  183. /// </summary>
  184. /// <param name="in_cotsparticleclr"></param>
  185. /// <returns></returns>
  186. public Bitmap GetBitmapByParticle(string fieldImage, Rectangle offset_rect)
  187. {
  188. //然后将取出的数据,转换成Bitmap对象
  189. Bitmap ls_bt = DrawFunction.ReadImageFile(fieldImage);
  190. //为了能把整个颗粒显示完整
  191. offset_rect.X = offset_rect.X - 5;
  192. offset_rect.Y = offset_rect.Y - 5;
  193. offset_rect.Width = offset_rect.Width + 10;
  194. offset_rect.Height = offset_rect.Height + 10;
  195. //防止计算偏差后,有坐标溢出现象
  196. if (offset_rect.X < 0)
  197. offset_rect.X = 0;
  198. if (offset_rect.Y < 0)
  199. offset_rect.Y = 0;
  200. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  201. {
  202. offset_rect.Width = ls_bt.Width - offset_rect.X;
  203. }
  204. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  205. {
  206. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  207. }
  208. //
  209. Bitmap new_ret_bp;
  210. //防止为0后面计算出错
  211. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  212. {
  213. //最后通过list_showsegment组建成新的图片,进行返回
  214. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  215. }
  216. else
  217. {
  218. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  219. }
  220. return new_ret_bp;
  221. }
  222. public Bitmap GetBitmapForBig(string sub, ParticleData fielddata, double xs)
  223. {
  224. string vs = "," + sub.Replace(':', '-') + ",";
  225. DataTable dataTable = fielddata.GetParticleAll(vs);
  226. if (dataTable.Rows.Count == 0)
  227. {
  228. return null;
  229. }
  230. string path = result.FilePath;
  231. //内接矩形
  232. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  233. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  234. double min_Y = max_Y;
  235. double min_X = max_X;
  236. //拼接field矩形
  237. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  238. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  239. double MIN_Y = MAX_Y;
  240. double MIN_X = MAX_X;
  241. foreach (DataRow item in dataTable.Rows)
  242. {
  243. //颗粒外接矩形
  244. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  245. if (lefttopX < min_X)
  246. {
  247. min_X = lefttopX;
  248. }
  249. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  250. {
  251. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  252. }
  253. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  254. if (max_Y < lrfttopY)
  255. {
  256. max_Y = lrfttopY;
  257. }
  258. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  259. {
  260. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  261. }
  262. //画布
  263. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  264. if (lefttopXH > MAX_X)
  265. {
  266. MAX_X = lefttopXH;
  267. }
  268. if (lefttopXH < MIN_X)
  269. {
  270. MIN_X = lefttopXH;
  271. }
  272. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  273. if (MAX_Y < lrfttopYH)
  274. {
  275. MAX_Y = lrfttopYH;
  276. }
  277. if (MIN_Y > lrfttopYH)
  278. {
  279. MIN_Y = lrfttopYH;
  280. }
  281. }
  282. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + 1024;
  283. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + 768;
  284. //构造最终的图片白板
  285. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  286. Graphics graph = Graphics.FromImage(tableChartImage);
  287. //初始化这个大图
  288. graph.DrawImage(tableChartImage, 0, 0);
  289. int width = Convert.ToInt32(max_X - min_X);
  290. int height = Convert.ToInt32(max_Y - min_Y);
  291. int X = Convert.ToInt32(min_X - MIN_X);
  292. int Y = Convert.ToInt32(MAX_Y - max_Y);
  293. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  294. foreach (DataRow item in dataTable.Rows)
  295. {
  296. string filePath = path + "\\FIELD_FILES\\";
  297. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  298. //然后将取出的数据,转换成Bitmap对象
  299. Bitmap ls_bt = DrawFunction.ReadImageFile(imagePath);
  300. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  301. int y = System.Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MAX_Y));
  302. try
  303. {
  304. graph.DrawImage(ls_bt, x, y);
  305. }
  306. catch (Exception e)
  307. {
  308. throw;
  309. }
  310. }
  311. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  312. return bmap;
  313. }
  314. private void BindDataGridView()
  315. {
  316. #region 加载显示进度条部份
  317. this.Cursor = Cursors.WaitCursor;
  318. m_frm_userprogress = new Frm_UserProgress();
  319. //显示进度条,计算进度条应该显示的位置和宽度
  320. Form ls_main_form = this.ParentForm.ParentForm;//取出父窗体
  321. if (ls_main_form == null)
  322. {
  323. m_frm_userprogress.Visible = false;
  324. }
  325. else
  326. {
  327. string str7 = table["str7"].ToString();
  328. m_frm_userprogress.Width = (int)(MyPrimaryScreen.DESKTOP.Width / MyPrimaryScreen.ScaleX * 0.9);
  329. m_frm_userprogress.Location = new Point(ls_main_form.Location.X + 80, ls_main_form.Location.Y + (int)(MyPrimaryScreen.DESKTOP.Height / MyPrimaryScreen.ScaleX) / 2);
  330. m_frm_userprogress.Show();
  331. m_frm_userprogress.SetProgressValueAndText(0, str7);
  332. }
  333. #endregion
  334. dgV_ParticlesDevidePage.Rows.Clear();
  335. dgV_ParticlesDevidePage.Columns.Clear();
  336. //从报告xml文件中加载,显示计算列,显示元素信息
  337. string str_DefaultComputedColName = "", str_ElementsColName = "";
  338. DataSet ds = OTSIncAReportApp.DataOperation.DataAccess.XMLoperate.GetXmlData(Application.StartupPath + m_ReportApp.m_OTSReportMgrParamFile, "XMLData");
  339. DataTable dt = ds.Tables["Member"];
  340. foreach (DataRow element in dt.Rows)
  341. {
  342. string RegName = element["RegName"].ToString();
  343. if (RegName == "DefaultComputedColName")
  344. {
  345. str_DefaultComputedColName = element["strValue"].ToString();
  346. }
  347. if (RegName == "ElementsColName")
  348. {
  349. str_ElementsColName = element["strValue"].ToString();
  350. }
  351. }
  352. //获取需要显示的计算列
  353. string[] strs = str_DefaultComputedColName.Split(',');
  354. //列名
  355. Dictionary<string, string> keyValues = new Dictionary<string, string>() { };
  356. keyValues.Add("rowid", table["str4"].ToString());
  357. keyValues.Add("TypeName", table["str6"].ToString());
  358. keyValues.Add("ParticleImage", table["str5"].ToString());
  359. keyValues.Add("SEMPosX", "SEMPosX");
  360. keyValues.Add("SEMPosY", "SEMPosY");
  361. for (int i = 0; i < strs.Count(); i++)
  362. {
  363. if (strs[i] == "Area")
  364. {
  365. keyValues.Add("Area", table["str21"].ToString());
  366. }
  367. if (strs[i] == "EquivalentCircleDiameter")
  368. {
  369. keyValues.Add("Equivalent", table["str22"].ToString());
  370. }
  371. if (strs[i] == "MaxDiameter")
  372. {
  373. keyValues.Add("DMAX", table["str23"].ToString());
  374. }
  375. if (strs[i] == "MinDiameter")
  376. {
  377. keyValues.Add("DMIN", table["str24"].ToString());
  378. }
  379. if (strs[i] == "DiameterRatio")
  380. {
  381. keyValues.Add("DiameterRatio", table["str25"].ToString());
  382. }
  383. if (strs[i] == "FerretDiameter")
  384. {
  385. keyValues.Add("DFERET", table["str26"].ToString());
  386. }
  387. if (strs[i] == "PERP")
  388. {
  389. keyValues.Add("DPERP", table["str27"].ToString());
  390. }
  391. if (strs[i] == "PERI")
  392. {
  393. keyValues.Add("PERIMETER", table["str28"].ToString());
  394. }
  395. if (strs[i] == "INSCR")
  396. {
  397. keyValues.Add("DINSCR", table["str29"].ToString());
  398. }
  399. if (strs[i] == "MEAN")
  400. {
  401. keyValues.Add("DMEAN", table["str30"].ToString());
  402. }
  403. if (strs[i] == "ELONG")
  404. {
  405. keyValues.Add("DELONG", table["str31"].ToString());
  406. }
  407. if (strs[i] == "ASPECT_ELONG")
  408. {
  409. keyValues.Add("ASPECT_ELONG", table["str32"].ToString());
  410. }
  411. if (strs[i] == "Orientation")
  412. {
  413. keyValues.Add("ORIENTATION", table["str33"].ToString());
  414. }
  415. }
  416. keyValues.Add("Element", "Element");
  417. Dictionary<string, string>.Enumerator en = keyValues.GetEnumerator();
  418. for (int irow = 0; irow < keyValues.Count; irow++)
  419. {
  420. if (en.MoveNext())
  421. {
  422. if (en.Current.Key == "ParticleImage")
  423. {
  424. DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
  425. iconColumn.Name = en.Current.Key;
  426. iconColumn.HeaderText = en.Current.Value;
  427. dgV_ParticlesDevidePage.Columns.Add(iconColumn);
  428. }
  429. else if (en.Current.Key == "Element")
  430. {
  431. }
  432. else
  433. {
  434. int columndid = dgV_ParticlesDevidePage.Columns.Add(en.Current.Key, en.Current.Value);
  435. }
  436. }
  437. }
  438. ParticleData fielddata = new ParticleData(result.FilePath);
  439. DataTable particles = fielddata.GetInfoForPartucleDevidePage(PageIndex, pageSize, OrderFunction, condition);
  440. if(PageSize==-1)
  441. {
  442. PageSize = particles.Rows.Count;
  443. }
  444. if (particles == null)
  445. {
  446. return;
  447. }
  448. string particleM = "";
  449. //获取需要显示的元素名
  450. List<string> ElementTypeSort = new List<string>(str_ElementsColName.Split(',').ToList());//去重
  451. for (int i = 0; i < ElementTypeSort.Count; i++)
  452. {
  453. dgV_ParticlesDevidePage.Columns.Add(ElementTypeSort[i], ElementTypeSort[i]);
  454. }
  455. double jd = 95f / (double)particles.Rows.Count;//计算进度刻度
  456. string filePath = result.FilePath + "\\FIELD_FILES\\";
  457. KeyValuePair<string, Bitmap> FieldImage = new KeyValuePair<string, Bitmap>();
  458. int DelParticlesCount = 0; //此值= 合成拼接颗粒的所有子颗粒数目-拼接颗粒数目 , 颗粒总数 = 所有子颗粒数目 - 合成拼接颗粒的所有子颗粒数目 + 拼接颗粒数目;
  459. for (int i = 0; i < particles.Rows.Count; i++)
  460. {
  461. //更新进度,每100条记录加载完,更新一次进度
  462. if (i % 5 == 0)
  463. m_frm_userprogress.SetProgressValueAndText((int)(jd * i), "loading..");
  464. Dictionary<string, string>.Enumerator enl = keyValues.GetEnumerator();
  465. int add_rowindex = dgV_ParticlesDevidePage.Rows.Add();
  466. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[0].Value = (pageSize * (PageIndex - 1) + add_rowindex + 1).ToString();
  467. for (int k = 0; k < keyValues.Count; k++)
  468. {
  469. if (enl.MoveNext())
  470. {
  471. if (enl.Current.Key == "rowid")
  472. {
  473. //dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = i + 1;
  474. }
  475. if (enl.Current.Key == "ParticleImage")
  476. {
  477. string subt = particles.Rows[i]["SubParticles"].ToString();
  478. if (subt != null && subt != "")
  479. {
  480. double ScanFieldSize = Convert.ToDouble(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["Sample"])["Members"])["SEMDataMsr"])["ScanFieldSize"]);
  481. string filePatht = result.FilePath + "\\FIELD_FILES\\";
  482. string imagePatht = filePatht + "Field" + subt.Split(',')[0].Split(':')[0].ToString() + ".bmp";
  483. //然后将取出的数据,转换成Bitmap对象
  484. Bitmap tempbit = fielddata.ReadImageFile(imagePatht);
  485. int pixw = tempbit.Width;
  486. double xs = pixw / ScanFieldSize;
  487. particleM = particleM + "," + subt;
  488. Bitmap bmap = fielddata.GetBitmapForBig(subt, xs, result.FilePath);
  489. if (bmap != null)
  490. {
  491. string[] str = subt.Split(',');
  492. bmap.Tag = new List<string>() { str[0].Split(':')[0], str[0].Split(':')[1], particles.Rows[i]["TypeId"].ToString() };
  493. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  494. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  495. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  496. dgV_ParticlesDevidePage.Rows[add_rowindex].DefaultCellStyle.ForeColor = Color.Chocolate;
  497. DelParticlesCount+= str.Length-1;
  498. //在颗粒总数中去除合成拼接颗粒的所有子颗粒数目
  499. foreach (var part in str)
  500. {
  501. DataRow[] foundRow;
  502. foundRow = particles.Select("fieldid = "+part.Split(':')[0]+ "and particleid = "+ part.Split(':')[1], "");
  503. foreach (DataRow row in foundRow)
  504. {
  505. particles.Rows.Remove(row);
  506. }
  507. }
  508. }
  509. else
  510. {
  511. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  512. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  513. }
  514. }
  515. else
  516. {
  517. if (FieldImage.Key != particles.Rows[i]["fieldid"].ToString() || FieldImage.Value == null)
  518. {
  519. string imagePath = filePath + "Field" + particles.Rows[i]["fieldid"].ToString() + ".bmp";
  520. FieldImage = new KeyValuePair<string, Bitmap>(particles.Rows[i]["fieldid"].ToString(), fielddata.ReadImageFile(imagePath));
  521. }
  522. Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(particles.Rows[i]["RectLeft"]), Y = Convert.ToInt32(particles.Rows[i]["RectTop"]), Width = Convert.ToInt32(particles.Rows[i]["RectWidth"]), Height = Convert.ToInt32(particles.Rows[i]["RectHeight"]) };
  523. Bitmap bmap = fielddata.GetBitmapByParticle(FieldImage.Value, rectangle);
  524. bmap.Tag = new List<string>() { particles.Rows[i]["FieldId"].ToString(), particles.Rows[i]["ParticleId"].ToString(), particles.Rows[i]["TypeId"].ToString() };
  525. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  526. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20;
  527. }
  528. }
  529. if (enl.Current.Key == "DiameterRatio")
  530. {
  531. double d = Convert.ToDouble(particles.Rows[i]["DMAX"]) / Convert.ToDouble(particles.Rows[i]["DMIN"]);
  532. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  533. }
  534. if (enl.Current.Key == "ASPECT_ELONG")
  535. {
  536. double d = Convert.ToDouble(particles.Rows[i]["DELONG"]) / Convert.ToDouble(particles.Rows[i]["DMEAN"]);
  537. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  538. }
  539. if (particles.Columns.Contains(enl.Current.Key))
  540. {
  541. double num = 0;
  542. if (double.TryParse(particles.Rows[i][enl.Current.Key].ToString(), out num))
  543. {
  544. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(num, 2);
  545. }
  546. else if (enl.Current.Key == "Element")
  547. {
  548. List<string> elementtemp = new List<string>(ElementTypeSort);
  549. string[] strcbo = particles.Rows[i][enl.Current.Key].ToString().Split(';');
  550. for (int j = 0; j < strcbo.Length; j++)
  551. {
  552. string[] str = strcbo[j].Split('-');
  553. if (ElementTypeSort.Contains(str[0]))
  554. { dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = Math.Round(double.Parse(str[1]), 2).ToString(); }
  555. elementtemp.Remove(str[0].ToString());
  556. }
  557. foreach (var ele in elementtemp)
  558. {
  559. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[ele].Value = "0";
  560. }
  561. }
  562. else
  563. {
  564. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = particles.Rows[i][enl.Current.Key];
  565. }
  566. }
  567. if (enl.Current.Key == "TypeName")
  568. {
  569. if (particles.Rows[i]["TypeId"].ToString() == "9")
  570. {
  571. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = "Not Identified";
  572. }
  573. }
  574. if (enl.Current.Key == "Equivalent")
  575. {
  576. double dSize = Convert.ToDouble(particles.Rows[i]["Area"]);
  577. double Diameter = Math.Sqrt(dSize / Math.PI) * 2;
  578. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(Diameter, 2);
  579. }
  580. }
  581. }
  582. }
  583. RecordCount = fielddata.GetCountOfPsrticles(condition)- DelParticlesCount;
  584. #region 加载进度条进度部份结束
  585. //加载完成设置鼠标为默认
  586. this.Cursor = Cursors.Default;
  587. string str8 = table["str8"].ToString();
  588. m_frm_userprogress.SetProgressValueAndText(100, str8);
  589. //加载完成,关闭进度条
  590. m_frm_userprogress.Close();
  591. #endregion
  592. }
  593. /// <summary>
  594. /// 设置DataGridView样式
  595. /// </summary>
  596. private void SetDataGridViewStyle()
  597. {
  598. //用户不能调整标题的高度
  599. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  600. //用户不能调整 行高
  601. dgV_ParticlesDevidePage.AllowUserToResizeRows = false;
  602. //改变行的高度;
  603. //Gview_gz.RowTemplate.Height = 20;
  604. //点击选择整行
  605. dgV_ParticlesDevidePage.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  606. //居中显示
  607. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  608. dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  609. dgV_ParticlesDevidePage.DefaultCellStyle = dataGridViewCellStyle1;
  610. dgV_ParticlesDevidePage.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  611. //再次重覆禁用拖动表头高度,居然有效果了
  612. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  613. //设置grid可以复制
  614. dgV_ParticlesDevidePage.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  615. //设置每列的宽度
  616. dgV_ParticlesDevidePage.Columns[0].Width = 40;//第一列序号的宽度设置一下吧,要不太丑
  617. dgV_ParticlesDevidePage.Columns[1].Width = 150;
  618. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].Width = 450;
  619. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  620. //设置序号列不排序
  621. dgV_ParticlesDevidePage.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  622. //设置序号列不可以设置宽度
  623. dgV_ParticlesDevidePage.Columns[0].Resizable = DataGridViewTriState.False;
  624. dgV_ParticlesDevidePage.RowsDefaultCellStyle.BackColor = Color.Azure;
  625. }
  626. #endregion
  627. #region 相关事件
  628. private void ToolStripMenuItem1_Click(object sender, EventArgs e)
  629. {
  630. //复制整个表
  631. CopyAll();
  632. }
  633. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  634. {
  635. //复制选择区域
  636. CopySelected();
  637. }
  638. /// <summary>
  639. /// 复制选择区域
  640. /// </summary>
  641. public void CopySelected()
  642. {
  643. //复制选择区域
  644. object oo = dgV_ParticlesDevidePage.GetClipboardContent();
  645. if (oo != null)
  646. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  647. }
  648. /// <summary>
  649. /// 复制所有
  650. /// </summary>
  651. public void CopyAll()
  652. {
  653. dgV_ParticlesDevidePage.SelectAll();
  654. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  655. }
  656. /// <summary>
  657. /// 以图像的方式将GridView进行截图
  658. /// </summary>
  659. public void CopyImage()
  660. {
  661. int height, width;
  662. width = dgV_ParticlesDevidePage.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 2;
  663. height = this.Height;
  664. Bitmap image = new Bitmap(width, height);
  665. dgV_ParticlesDevidePage.DrawToBitmap(image, new Rectangle(0, 0, width, height));
  666. Clipboard.SetImage(image);
  667. }
  668. private void toolStripMenuItem4_Click(object sender, EventArgs e)
  669. {
  670. //对gridview进行截图
  671. CopyImage();
  672. }
  673. private void dgV_ParticlesDevidePage_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  674. {
  675. //排序中
  676. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag.ToString() == "统计行")
  677. {
  678. //ROW1>ROW2
  679. e.SortResult = 1;
  680. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  681. e.SortResult = -1;
  682. e.Handled = true;
  683. return;
  684. }
  685. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag.ToString() == "统计行")
  686. {
  687. //ROW1<ROW2
  688. e.SortResult = -1;
  689. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  690. e.SortResult = 1;
  691. e.Handled = true;
  692. return;
  693. }
  694. }
  695. /// <summary>
  696. /// 将OTS坐标转换为Sem 坐标
  697. /// </summary>
  698. /// <param name="POTSCoord"></param>
  699. /// <returns></returns>
  700. public Point ChangeOTSToSemCoord(Point POTSCoord)
  701. {
  702. //first if m_semstagedata is null to get stage inforation
  703. Convert.ToDouble(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["scanFieldSize"]);
  704. //after obtaining stage info,calc stage point data
  705. Point ret_SEM_point = new Point();
  706. // get center point, um
  707. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  708. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  709. long xCenter = (xStart + xEnd) / 2;
  710. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  711. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  712. long yCenter = (yStart + yEnd) / 2;
  713. // delte = SEM - OTSa
  714. long deltex = xCenter - 0;
  715. long deltey = yCenter - 0;
  716. int xdir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["xAxisDir"]);
  717. int ydir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["yAxisDir"]);
  718. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  719. {
  720. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  721. }
  722. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  723. {
  724. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  725. }
  726. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  727. {
  728. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  729. }
  730. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  731. {
  732. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  733. }
  734. return ret_SEM_point;
  735. }
  736. #endregion
  737. #region 获取向导出模块提供的DataTable和GridView对象
  738. /// <summary>
  739. /// 获取到该模块输出后形成的DataTable,和GridView
  740. /// </summary>
  741. /// <param name="out_dt"></param>
  742. /// <param name="out_dg"></param>
  743. public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
  744. {
  745. out_dt = m_dt;
  746. out_dg = this.dgV_ParticlesDevidePage;//同样通过 dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[2].Value 来获取bitmap对象
  747. }
  748. #endregion
  749. #region 连接电镜相关
  750. /// <summary>
  751. /// 移动SEM到指定位置线程函数
  752. /// </summary>
  753. private void Thread_GO(object in_obj)
  754. {
  755. if (m_mythread_state == false)
  756. {
  757. m_mythread_state = true;
  758. Point sem_point = (Point)in_obj;
  759. //第一步,连接电镜
  760. m_OTSIncAReportGridsFun.Connection_ForParticlesGrid();
  761. Thread.Sleep(500);
  762. //第二步,移动到指定位置,先读取再设置
  763. if (m_OTSIncAReportGridsFun.m_SEMConnectionState == true)
  764. {
  765. m_OTSIncAReportGridsFun.MoveSemToPointXY_ForParticlesGrid(sem_point.X, sem_point.Y);
  766. }
  767. Thread.Sleep(1500);
  768. //第三步,断开电镜连接
  769. m_OTSIncAReportGridsFun.DisConnectSEM_ForParticlesGrid();
  770. //Thread.Sleep(500);
  771. m_mythread_state = false;
  772. }
  773. }
  774. #endregion
  775. private void ToolStripMenuItem_exportzoomimagefile_Click(object sender, EventArgs e)
  776. {
  777. //导出放大图像
  778. FolderBrowserDialog fbd = new FolderBrowserDialog();
  779. if (fbd.ShowDialog() == DialogResult.OK)
  780. {
  781. m_imagefilepath = fbd.SelectedPath;
  782. for (int i = 0; i < m_dt.Rows.Count; i++)
  783. {
  784. Bitmap ls_bp = new Bitmap(1, 1);
  785. ls_bp = (Bitmap)m_dt.Rows[i][1];
  786. string strPath = m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg";
  787. string strPath2 = m_imagefilepath + "\\" + (i + 1).ToString() + "s.jpg";
  788. ls_bp.Save(strPath);
  789. //m_COTSReportProjFileMgrClr.ReZoom(strPath, strPath2);
  790. System.IO.File.Delete(strPath);//再将文件删除
  791. }
  792. string str1 = table["str9"].ToString();
  793. MessageBox.Show(str1 + m_dt.Rows.Count.ToString());
  794. }
  795. }
  796. /// <summary>
  797. /// 导出二次放大图像
  798. /// </summary>
  799. /// <param name="str_srcPathName"></param>
  800. /// <param name="str_outPathName"></param>
  801. public void ParticleReZoom(string str_srcPathName, string str_outPathName)
  802. {
  803. //m_COTSReportProjFileMgrClr.ReZoom(str_srcPathName, str_outPathName);
  804. }
  805. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  806. {
  807. //恢复至初始状态
  808. dgV_ParticlesDevidePage.Rows.Clear();
  809. dgV_ParticlesDevidePage.Columns.Clear();
  810. BindDataGridView();
  811. SetDataGridViewStyle();
  812. }
  813. private void ToolStripMenuItem5_Click(object sender, EventArgs e)
  814. {
  815. /*注意:多数据源情况下,只获取第一个数据源的选择了多行的情况下,只获取最后一个选择行的,没有办法,这是为了符合李阳博士提出的要求*/
  816. //先获取鼠标所选择的行里的颗粒的FieldID和ParticleID
  817. string i_ls_fieldid = "";
  818. string i_ls_particleid = "";
  819. int cou = dgV_ParticlesDevidePage.SelectedRows.Count;
  820. if (cou > 0)
  821. {
  822. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[cou - 1];
  823. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  824. List<string> list_str = (List<string>)ls_bp.Tag;
  825. if (list_str != null)
  826. {
  827. i_ls_fieldid = list_str[0];
  828. i_ls_particleid = list_str[1];
  829. }
  830. }
  831. ParticleData particleData = new ParticleData(result.FilePath);
  832. Particle particle = particleData.GetParticleByFidAndPid(i_ls_fieldid, i_ls_particleid);
  833. ////然后遍历所有的Field
  834. //Point ls_ots_point = new Point() { X = particle.PosX, Y = particle.PosY };
  835. ////将该坐标转成SEM坐标
  836. //Point ls_sem_point = ChangeOTSToSemCoord(ls_ots_point);
  837. //取得Field的Position,然后执行线程移动SEM到当前Field的位置
  838. if (m_mythread == null)
  839. {
  840. return;
  841. }
  842. if (m_mythread.ThreadState == ThreadState.Running || m_mythread.ThreadState == ThreadState.WaitSleepJoin)
  843. {
  844. return;
  845. }
  846. if (m_mythread.ThreadState == ThreadState.Stopped)
  847. {
  848. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  849. }
  850. //改为线程调用,先判断线程状态
  851. m_mythread.Start(new Point(particle.SEMPosX, particle.SEMPosY));
  852. }
  853. private void ToolStripMenuItem_selectparticle_Click(object sender, EventArgs e)
  854. {
  855. string path = result.FilePath;
  856. ParticleData fielddata = new ParticleData(result.FilePath);
  857. List<Particle> particles = fielddata.GetParticleAllList();
  858. List<Particle> ls_list_cotsparticleclr = new List<Particle>();
  859. for (int l = 0; l < dgV_ParticlesDevidePage.SelectedRows.Count; l++)
  860. {
  861. //从事先加载的Bitmap对象的Tag中取出List<string>
  862. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[l];
  863. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  864. List<string> list_str = (List<string>)ls_bp.Tag;
  865. if (list_str != null)
  866. {
  867. int i_ls_fieldid = Convert.ToInt32(list_str[0]);
  868. int i_ls_particleid = Convert.ToInt32(list_str[1]);
  869. foreach (Particle item in particles)
  870. {
  871. if (i_ls_fieldid == item.FieldId && i_ls_particleid == item.ParticleId)
  872. {
  873. ls_list_cotsparticleclr.Add(item);
  874. break;
  875. }
  876. }
  877. }
  878. }
  879. m_ReportApp.SelectedParticles = ls_list_cotsparticleclr;
  880. string str1 = table["str1"].ToString();
  881. //然后循环得出,list cotsparticleclr列表,返回给框架
  882. //m_sec.SendCall(str1, "", "", ls_list_cotsparticleclr);
  883. string str2 = table["str2"].ToString();
  884. string str3 = table["str3"].ToString();
  885. MessageBox.Show(str2 + ls_list_cotsparticleclr.Count.ToString() + str3);
  886. }
  887. private void ToolStripMenuItem_exportimagefile_Click(object sender, EventArgs e)
  888. {
  889. FolderBrowserDialog fbd = new FolderBrowserDialog();
  890. if (fbd.ShowDialog() == DialogResult.OK)
  891. {
  892. m_imagefilepath = fbd.SelectedPath;
  893. for (int i = 0; i < dgV_ParticlesDevidePage.Rows.Count; i++)
  894. {
  895. Bitmap ls_bp = new Bitmap(1, 1);
  896. ls_bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i].Cells[2].Value;
  897. ls_bp.Save(m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg");
  898. }
  899. string str9 = table["str9"].ToString();
  900. MessageBox.Show(str9 + dgV_ParticlesDevidePage.Rows.Count.ToString());
  901. }
  902. }
  903. private void testToolStripMenuItem_Click(object sender, EventArgs e)
  904. {
  905. ////测试导出指定颗粒的能谱图,测试相关代码
  906. //OTSIncAReportGraph.DParticle ls_dparticle = new OTSIncAReportGraph.DParticle();
  907. //for (int i = 0; i < dgV_ParticlesDevidePage.SelectedRows.Count; i++)
  908. //{
  909. // //获取选择行颗粒的fieldid,和particleid
  910. // string str_fieldid = "";
  911. // string str_particleid = "";
  912. // string str_stdtypeid = "";
  913. // DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[i];
  914. // Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  915. // List<string> list_str = (List<string>)ls_bp.Tag;
  916. // if (list_str != null)
  917. // {
  918. // str_fieldid = list_str[0];
  919. // str_particleid = list_str[1];
  920. // str_stdtypeid = list_str[2];
  921. // }
  922. // ls_dparticle.CLRFieldID = Convert.ToInt32(str_fieldid);
  923. // ls_dparticle.CLRTagID = Convert.ToInt32(str_particleid);
  924. // ls_dparticle.STDTypeID = Convert.ToInt32(str_stdtypeid);
  925. //}
  926. ////显示xray能谱
  927. //ShowXRay(ls_dparticle);
  928. //Bitmap ls_bpex = new Bitmap(control_XRayTable1.Width, control_XRayTable1.Height);
  929. //control_XRayTable1.DrawToBitmap(ls_bpex, new Rectangle(0, 0, control_XRayTable1.Width, control_XRayTable1.Height));
  930. ////Bitmap ls_bpex = control_XRayTable1.ExportXRayImage();
  931. //ls_bpex.Save("e:\\test222.jpg");
  932. }
  933. /// <summary>
  934. /// 判断条件输入 前<后
  935. /// </summary>
  936. /// <param name="min"></param>
  937. /// <param name="max"></param>
  938. /// <returns></returns>
  939. bool CompareInput(string min,string max)
  940. {
  941. int imax = 0;
  942. int imin = 0;
  943. double dmax = 0;
  944. double dmin = 0;
  945. if (int.TryParse(max, out imax))
  946. {
  947. dmax = (double)imax;
  948. }
  949. else
  950. {
  951. dmax = Convert.ToDouble(max);
  952. }
  953. if (int.TryParse(min, out imin))
  954. {
  955. dmin = (double)imin;
  956. }
  957. else
  958. {
  959. dmin = Convert.ToDouble(min);
  960. }
  961. if(dmin<=dmax)
  962. {
  963. return true;
  964. }
  965. else
  966. {
  967. return false;
  968. }
  969. }
  970. private void btn_Sel_Click(object sender, EventArgs e)
  971. {
  972. condition = "";
  973. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text))
  974. {
  975. double dnum = 0;
  976. int inum = 0;
  977. if (double.TryParse(tBx_AreaMin.Text, out dnum) && dnum >= 0)
  978. {
  979. condition += " and Area" + " > " + tBx_AreaMin.Text;
  980. }
  981. else if(int.TryParse(tBx_AreaMin.Text, out inum) && inum >= 0)
  982. {
  983. condition += " and Area" + " > " + tBx_AreaMin.Text;
  984. }
  985. else
  986. {
  987. MessageBox.Show(table["str10"].ToString());
  988. return;
  989. }
  990. }
  991. if (!string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  992. {
  993. double dnum = 0;
  994. int inum = 0;
  995. if (double.TryParse(Tbx_AreaMax.Text, out dnum) && dnum >= 0)
  996. {
  997. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  998. }
  999. else if (int.TryParse(Tbx_AreaMax.Text, out inum) && inum >= 0)
  1000. {
  1001. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  1002. }
  1003. else
  1004. {
  1005. MessageBox.Show(table["str10"].ToString());
  1006. return;
  1007. }
  1008. }
  1009. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text) && !string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  1010. {
  1011. if (!CompareInput(tBx_AreaMin.Text, Tbx_AreaMax.Text))
  1012. {
  1013. MessageBox.Show(table["str11"].ToString());
  1014. return;
  1015. }
  1016. }
  1017. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text))
  1018. {
  1019. double dnum = 0;
  1020. int inum = 0;
  1021. if (double.TryParse(tbx_DmaxMin.Text, out dnum) && dnum >= 0)
  1022. {
  1023. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1024. }
  1025. else if(int.TryParse(tbx_DmaxMin.Text, out inum)&& inum >= 0)
  1026. {
  1027. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1028. }
  1029. else
  1030. {
  1031. MessageBox.Show(table["str10"].ToString());
  1032. return;
  1033. }
  1034. }
  1035. if (!string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1036. {
  1037. double dnum = 0;
  1038. int inum = 0;
  1039. if (double.TryParse(tbx_DmaxMax.Text, out dnum) && dnum > 0)
  1040. {
  1041. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1042. }
  1043. else if(int.TryParse(tbx_DmaxMax.Text, out inum)&& inum >= 0)
  1044. {
  1045. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1046. }
  1047. else
  1048. {
  1049. MessageBox.Show(table["str10"].ToString());
  1050. return;
  1051. }
  1052. }
  1053. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text) && !string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1054. {
  1055. if (!CompareInput(tbx_DmaxMin.Text, tbx_DmaxMax.Text))
  1056. {
  1057. MessageBox.Show(table["str11"].ToString());
  1058. return;
  1059. }
  1060. }
  1061. if (!string.IsNullOrWhiteSpace(tbx_Type.Text))
  1062. {
  1063. condition += " and TypeName Like \"%" + tbx_Type.Text + "%\" ";
  1064. }
  1065. lnkFirst_Click(null, null);
  1066. SetDataGridViewStyle();
  1067. }
  1068. private void cbB_order_SelectedIndexChanged(object sender, EventArgs e)
  1069. {
  1070. int ordernum = cbB_order.SelectedIndex;
  1071. if (ordernum == 0)
  1072. {
  1073. OrderFunction = "fieldid,particleid";
  1074. }
  1075. else if (ordernum == 1)
  1076. {
  1077. OrderFunction = "TypeName";
  1078. }
  1079. else if (ordernum == 2)
  1080. {
  1081. OrderFunction = "DMAX";
  1082. }
  1083. else if (ordernum == 3)
  1084. {
  1085. OrderFunction = "DMAX desc";
  1086. }
  1087. else if (ordernum == 4)
  1088. {
  1089. OrderFunction = "Area";
  1090. }
  1091. else if (ordernum == 5)
  1092. {
  1093. OrderFunction = "Area desc";
  1094. }
  1095. lnkFirst_Click(null, null);
  1096. SetDataGridViewStyle();
  1097. }
  1098. /// <summary>
  1099. /// 页面控件呈现
  1100. /// </summary>
  1101. private void DrawControl(bool callEvent)
  1102. {
  1103. BindDataGridView();
  1104. lblTotalCount.Text = RecordCount.ToString();
  1105. lblPageCount.Text = PageCount.ToString();
  1106. txtPageNum.Text = PageIndex.ToString();
  1107. SetFormCtrEnabled();
  1108. if (PageCount == 0 || PageCount == 1)//有且仅有一页
  1109. {
  1110. lnkFirst.Enabled = false;
  1111. lnkPrev.Enabled = false;
  1112. lnkNext.Enabled = false;
  1113. lnkLast.Enabled = false;
  1114. btnGo.Enabled = false;
  1115. }
  1116. else if (PageIndex == 1)//第一页
  1117. {
  1118. lnkFirst.Enabled = false;
  1119. lnkPrev.Enabled = false;
  1120. }
  1121. else if (PageIndex == PageCount)//最后一页
  1122. {
  1123. lnkNext.Enabled = false;
  1124. lnkLast.Enabled = false;
  1125. }
  1126. }
  1127. private void SetFormCtrEnabled()
  1128. {
  1129. lnkFirst.Enabled = true;
  1130. lnkPrev.Enabled = true;
  1131. lnkNext.Enabled = true;
  1132. lnkLast.Enabled = true;
  1133. btnGo.Enabled = true;
  1134. }
  1135. /// <summary>
  1136. /// 分页属性改变了。
  1137. /// </summary>
  1138. private void txtPageSize_TextChanged(object sender, EventArgs e)
  1139. {
  1140. int num = 0;
  1141. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1142. {
  1143. }
  1144. else { txtPageNum.Text = "1"; }
  1145. }
  1146. private void lnkFirst_Click(object sender, EventArgs e)
  1147. {
  1148. PageIndex = 1;
  1149. DrawControl(true);
  1150. }
  1151. private void lnkPrev_Click(object sender, EventArgs e)
  1152. {
  1153. PageIndex = Math.Max(1, PageIndex - 1);
  1154. DrawControl(true);
  1155. }
  1156. private void lnkNext_Click(object sender, EventArgs e)
  1157. {
  1158. PageIndex = Math.Min(PageCount, PageIndex + 1);
  1159. DrawControl(true);
  1160. }
  1161. private void lnkLast_Click(object sender, EventArgs e)
  1162. {
  1163. PageIndex = PageCount;
  1164. DrawControl(true);
  1165. }
  1166. /// <summary>
  1167. /// 跳转
  1168. /// </summary>
  1169. /// <param name="sender"></param>
  1170. /// <param name="e"></param>
  1171. private void btnGo_Click(object sender, EventArgs e)
  1172. {
  1173. int num = 0;
  1174. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1175. {
  1176. if (num > PageCount)
  1177. {
  1178. num = PageCount;
  1179. txtPageNum.Text = PageCount.ToString();
  1180. }
  1181. PageIndex = num;
  1182. DrawControl(true);
  1183. }
  1184. }
  1185. private void cbB_PageSize_SelectedIndexChanged(object sender, EventArgs e)
  1186. {
  1187. if (cbB_PageSize.SelectedItem.ToString() == "All")
  1188. {
  1189. pageSize = -1;
  1190. }
  1191. else
  1192. {
  1193. pageSize = int.Parse(cbB_PageSize.SelectedItem.ToString());
  1194. }
  1195. lnkFirst_Click(null, null);
  1196. }
  1197. /// <summary>
  1198. /// 在帧图上标记颗粒的位置矩形,并保存到FIELD_FILES_MARK文件夹中
  1199. /// </summary>
  1200. public void SaveMarkParticleRectangleOnFieldFile(DataTable dataTable)
  1201. {
  1202. string str_path_FIELD_FILES = result.FilePath + "\\FIELD_FILES\\";
  1203. string str_path_FIELD_FILES_MARK = result.FilePath + "\\FIELD_FILES_MARK\\";
  1204. ////判断是否已经有导出过的标记帧图文件夹,如果已经存在,则直接返回不输出,先不加
  1205. DirectoryInfo theFolder_FieldMark = new DirectoryInfo(str_path_FIELD_FILES_MARK);
  1206. if (!theFolder_FieldMark.Exists)
  1207. {
  1208. //创建
  1209. theFolder_FieldMark.Create();
  1210. }
  1211. else
  1212. {
  1213. //已经存在的话,直接返回
  1214. //return;
  1215. }
  1216. //查找该field下对应所有的颗粒
  1217. DataTable dt_AllParticle = fieldData.GetAllParticle_DataTable();
  1218. DirectoryInfo theFolder = new DirectoryInfo(str_path_FIELD_FILES);
  1219. if (theFolder.Exists)
  1220. {
  1221. //循环帧图,再循环各帧图中的颗粒,进行标记,保存
  1222. foreach (FileInfo nextifile in theFolder.GetFiles())
  1223. {
  1224. //找出所有的用户库文件,在这个目录中
  1225. if (nextifile.Name.Contains(".bmp") == true || nextifile.Name.Contains(".BMP") == true)
  1226. {
  1227. //确认对应的帧图名
  1228. string str_fieldid = Path.GetFileNameWithoutExtension(nextifile.Name);
  1229. str_fieldid = str_fieldid.Substring(5, str_fieldid.Length - 5);//减去field字符长度
  1230. if (dataTable.Select(" fieldid = " + str_fieldid).Count() > 0)
  1231. {
  1232. Image img = Image.FromFile(nextifile.FullName);
  1233. Bitmap ls_fieldbp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  1234. using (Graphics g = Graphics.FromImage(ls_fieldbp))
  1235. {
  1236. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  1237. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  1238. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  1239. g.DrawImage(img, 0, 0);
  1240. }
  1241. img.Dispose();
  1242. img = null;
  1243. //循环将颗粒,进行标记到bitmap图上
  1244. DataRow[] drlist = dt_AllParticle.Select(" fieldid = " + str_fieldid);
  1245. for (int i_row = 0; i_row < drlist.Count(); i_row++)
  1246. {
  1247. //string str_fieldid = drlist[i_row]["fieldid"].ToString();
  1248. string str_particleid = drlist[i_row]["particleid"].ToString();
  1249. string str_stdtypeid = drlist[i_row]["typeid"].ToString();
  1250. string str_offset_rect_X = drlist[i_row]["RectLeft"].ToString();
  1251. string str_offset_rect_Y = drlist[i_row]["RectTop"].ToString();
  1252. string str_offset_rect_Width = drlist[i_row]["RectWidth"].ToString();
  1253. string str_offset_rect_Height = drlist[i_row]["RectHeight"].ToString();
  1254. //确定需要标记颗粒的定位
  1255. Rectangle offset_rect = new Rectangle(Convert.ToInt32(str_offset_rect_X), Convert.ToInt32(str_offset_rect_Y),
  1256. Convert.ToInt32(str_offset_rect_Width), Convert.ToInt32(str_offset_rect_Height));
  1257. //向帧图进行标记颗粒位置
  1258. Graphics g = Graphics.FromImage(ls_fieldbp);
  1259. g.DrawRectangle(new Pen(Color.Aquamarine), offset_rect);
  1260. //判断是否超出了图像外面,是的话,让文字在左侧显示,默认在右侧显示
  1261. int ls_offsetx = offset_rect.X + offset_rect.Width;
  1262. if (ls_offsetx > ls_fieldbp.Width - 10)
  1263. {
  1264. ls_offsetx = offset_rect.X - 10;
  1265. }
  1266. g.DrawString(str_fieldid + "" + str_particleid.ToString(),
  1267. new Font("黑体", 8), new SolidBrush(Color.Aqua), new PointF(ls_offsetx, offset_rect.Y));
  1268. //保存带有标记的帧图
  1269. ls_fieldbp.Save(str_path_FIELD_FILES_MARK + str_fieldid.ToString() + ".bmp");
  1270. //ls_fieldbp.Dispose();
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. //
  1277. GC.Collect();
  1278. GC.WaitForPendingFinalizers();
  1279. }
  1280. private void testToolStripMenuItem1_Click(object sender, EventArgs e)
  1281. {
  1282. //SaveMarkParticleRectangleOnFieldFile();
  1283. MessageBox.Show("导出完成!");
  1284. }
  1285. private void EXCELToolStripMenuItem_Click(object sender, EventArgs e)
  1286. {
  1287. //将所有的数据导出到EXCEL中
  1288. SaveFileDialog sfd = new SaveFileDialog();
  1289. sfd.Filter = "Excel文件(*.xls)|*.xls";
  1290. //设置默认文件类型显示顺序
  1291. sfd.FilterIndex = 1;
  1292. //保存对话框是否记忆上次打开的目录
  1293. sfd.RestoreDirectory = true;
  1294. if (sfd.ShowDialog() == DialogResult.OK)
  1295. {
  1296. IWorkbook workbook = new HSSFWorkbook(); //用于创建.xls office2003开始以前的
  1297. //IWorkbook workbook = new XSSFWorkbook(); //用于创建.xlsx office2007开始以后的
  1298. ISheet sheet;
  1299. //创建Excel文件
  1300. FileStream fs = File.Create(sfd.FileName);
  1301. fs.Close();
  1302. sheet = workbook.CreateSheet("Particles");//创建工作表
  1303. //创建表格边框样式风格
  1304. ICellStyle cellStyle = workbook.CreateCellStyle();
  1305. cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  1306. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1307. //设置上4边
  1308. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1309. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1310. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1311. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1312. //设置字符大小和颜色
  1313. IFont font = workbook.CreateFont();
  1314. font.FontName = "黑体";
  1315. font.FontHeightInPoints = 10;
  1316. cellStyle.SetFont(font);
  1317. //设置颗粒名列宽
  1318. sheet.SetColumnWidth(1, 30 * 256);//夹杂物名列宽
  1319. sheet.SetColumnWidth(2, 3 * 256);//图像列宽
  1320. IRow row;
  1321. ICell cell;
  1322. //插入表头
  1323. row = sheet.CreateRow(1);//从第15行添加一行
  1324. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1325. {
  1326. cell = row.CreateCell(i_cell);
  1327. cell.CellStyle = cellStyle;
  1328. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i_cell].Name);
  1329. }
  1330. //插入表内容
  1331. for (int i_row = 0; i_row < dgV_ParticlesDevidePage.Rows.Count; i_row++)
  1332. {
  1333. row = sheet.CreateRow(2 + i_row);
  1334. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1335. {
  1336. cell = row.CreateCell(i_cell);
  1337. cell.CellStyle = cellStyle;
  1338. if (dgV_ParticlesDevidePage[i_cell, i_row].Value == null)
  1339. continue;
  1340. if (i_cell == 2)
  1341. {
  1342. //颗粒图像列
  1343. Bitmap bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i_row].Cells[2].Value;
  1344. byte[] bytes = ImageConvertToBytes(bp);
  1345. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  1346. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  1347. //第三步,在sheet中创建画布
  1348. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  1349. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  1350. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2, i_cell, i_row + 2, i_cell + 1, i_row + 3);//终止比开始位置大1,会自动缩放到一个单元格内的
  1351. //第五步,创建图片
  1352. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  1353. //图像效果不好,自己另外导出吧,这里对该列宽进行了设置0
  1354. }
  1355. else
  1356. {
  1357. //非图像列
  1358. cell.SetCellValue(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString());
  1359. }
  1360. }
  1361. }
  1362. //完成后,对Excel进行保存
  1363. FileStream file = new FileStream(sfd.FileName, FileMode.Create);
  1364. workbook.Write(file);
  1365. file.Close();
  1366. MessageBox.Show("导出完成!");
  1367. //导出完成后,打开Excel文件
  1368. if (File.Exists(sfd.FileName))
  1369. {
  1370. //打开刚才导出的文件
  1371. System.Diagnostics.Process.Start(sfd.FileName);
  1372. }
  1373. }
  1374. }
  1375. /// <summary>
  1376. /// 将image转成bytes
  1377. /// </summary>
  1378. /// <param name="in_img"></param>
  1379. /// <returns></returns>
  1380. public byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  1381. {
  1382. MemoryStream ms = new MemoryStream();
  1383. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  1384. return ms.ToArray();
  1385. }
  1386. }
  1387. }