ParticlesGridDevidePage.cs 64 KB

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