ParticlesGridDevidePage.cs 71 KB

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