ParticlesGridDevidePage.cs 64 KB

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