BatterySectionPorosityDialog.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. using OpenCvSharp;
  2. using PaintDotNet.Annotation;
  3. using PaintDotNet.Annotation.Enum;
  4. using PaintDotNet.Base.CommTool;
  5. using PaintDotNet.Base.Functionodel;
  6. using PaintDotNet.Base.SettingModel;
  7. using PaintDotNet.CustomControl;
  8. using PaintDotNet.DbOpreate.DbBll;
  9. using PaintDotNet.DbOpreate.DbModel;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Configuration;
  15. using System.Data;
  16. using System.Drawing;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. namespace PaintDotNet.DedicatedAnalysis.Battery
  22. {
  23. internal partial class BatterySectionPorosityDialog : PdnBaseForm
  24. {
  25. #region 属性
  26. //二值化集成1
  27. BinaryClassForB binaryClass;
  28. private int menuId;
  29. /// <summary>
  30. /// 选中图片的bitmap
  31. /// </summary>
  32. private Bitmap bitmap;
  33. /// <summary>
  34. /// 存在视场标记
  35. /// </summary>
  36. private bool existViewFlag = false;
  37. private int changeCount;
  38. /// <summary>
  39. /// 主空间
  40. /// </summary>
  41. private AppWorkspace appWorkspace;
  42. /// <summary>
  43. /// 图像面板
  44. /// </summary>
  45. private DocumentWorkspaceWindow documentWorkspace;
  46. /// <summary>
  47. /// 公共控件
  48. /// </summary>
  49. private GeneralAnalysisCommonControlButtons commonControlButtons;
  50. /// <summary>
  51. /// 图片测量记录
  52. /// </summary>
  53. private List<DataTable> pictureDataTables = new List<DataTable>();
  54. /// <summary>
  55. /// 网格Xml
  56. /// </summary>
  57. private GrainSizePointCountingMethodModel grainSizePointCountingMethodModel;
  58. /// <summary>
  59. /// 数据变化
  60. /// </summary>
  61. private bool saveSuccess = false;
  62. private BinaryControlSmaller bc;
  63. #endregion 属性
  64. #region 数据属性
  65. /// <summary>
  66. /// 折线点的集合
  67. /// </summary>
  68. public List<System.Drawing.Point> pointArray = new List<System.Drawing.Point>();
  69. /// <summary>
  70. /// 存储所有轮廓线的点集合
  71. /// </summary>
  72. private List<PointF> pointList = new List<PointF>();
  73. /// <summary>
  74. /// 存储所有轮廓线集合
  75. /// </summary>
  76. private List<List<PointF>> lineList = new List<List<PointF>>();
  77. /// <summary>
  78. /// 存储所有孔隙线的点集合
  79. /// </summary>
  80. private List<PointF> kongPointList = new List<PointF>();
  81. /// <summary>
  82. /// 存储所有孔隙线集合
  83. /// </summary>
  84. private List<List<PointF>> kongLineList = new List<List<PointF>>();
  85. /// <summary>
  86. /// 存储所有删除孔隙线的点集合
  87. /// </summary>
  88. private List<PointF> delPointList = new List<PointF>();
  89. /// <summary>
  90. /// 存储所有删除孔隙线集合
  91. /// </summary>
  92. private List<List<PointF>> delLineList = new List<List<PointF>>();
  93. //轮廓修改状态
  94. bool isLunKuo = false;
  95. //添加孔隙状态
  96. bool isAddKong = false;
  97. //删除孔隙状态
  98. bool isDelKong = false;
  99. //绘制轮廓
  100. bool isDrwKuo = false;
  101. //绘制轮廓结束
  102. bool DrwDone = false;
  103. #endregion
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. /// <param name="appWorkspace"></param>
  108. public BatterySectionPorosityDialog(AppWorkspace appWorkspace, int menuId)
  109. {
  110. this.KeyPreview = true;
  111. this.KeyDown += OnKeyDown;
  112. this.menuId = menuId;
  113. binaryClass = new BinaryClassForB(menuId);
  114. InitializeComponent();
  115. InitializeLanguageText();
  116. ColumnHeader header0 = new ColumnHeader();
  117. header0.Text = "";
  118. header0.Width = 180;
  119. this.appWorkspace = appWorkspace;
  120. this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
  121. this.documentWorkspace.Dock = DockStyle.Fill;
  122. this.documentWorkspace.HookMouseEvents();
  123. this.documentWorkspace.AuxiliaryLineEnabled = false;
  124. this.documentWorkspace.Visible = false;
  125. this.documentWorkspace.panel.Paint += Panel_Paint;
  126. this.documentWorkspace.panel.MouseDown += OnMouseDown;
  127. this.documentWorkspace.panel.MouseUp += OnMouseUp;
  128. this.documentWorkspace.panel.Paint += Panel_Paint;
  129. this.documentWorkspace.panel.MouseMove += onMouseMove;
  130. this.documentWorkspace.activeTool = DrawToolType.Pointer;
  131. this.groupBox7.Controls.Add(this.documentWorkspace);
  132. this.commonControlButtons = new GeneralAnalysisCommonControlButtons();
  133. this.commonControlButtons.Dock = DockStyle.Top;
  134. this.commonControlButtons.Height = 30;
  135. this.commonControlButtons.HideZoomToWindowAndActualSize();
  136. this.commonControlButtons.trackLabel.Text = "单击开始画线,右键停止。";
  137. this.commonControlButtons.trackLabel.Visible = true;
  138. this.commonControlButtons.trackLabel.Width = 300;
  139. this.commonControlButtons.Visible = false;
  140. this.groupBox7.Controls.Add(commonControlButtons);
  141. //获取
  142. //Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out ruleValue);
  143. //double ruleValue = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
  144. ////double physical_area_length_Micron = getCurrentWorkspace().GetRuler(MeasurementUnit.Micron);
  145. //二值化集成2
  146. bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);//初始化原图勾选改变事件
  147. binaryClass.imageType = 1;
  148. binaryClass.createDocumentItemsSmaller(new string[] { "bp1" } , this.bc, this.appWorkspace, this.documentWorkspace, this.listView1);//初始化相的工作结构
  149. binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
  150. InitCommonButtonEvent();
  151. InitGridStyle();
  152. InitPicList();
  153. // SetAnalyzeModelFromXml(getModelName());
  154. }
  155. private void ShowImgEvent(object sender, EventArgs e)
  156. {
  157. if (this.listView1.Items.Count == 0)
  158. {
  159. return;
  160. }
  161. listView1.Focus();
  162. this.listView1.Items[0].Focused = true;
  163. this.listView1.Items[0].Selected = true;
  164. }
  165. #region 二值化相关方法
  166. private void InclusionsStandardDialog_FormClosing(object sender, FormClosingEventArgs e)
  167. {
  168. binaryClass.saveParams();
  169. }
  170. private void ShownChoiseItemAndInitData(object sender, EventArgs e)
  171. {
  172. binaryClass.RefreshHistogramControl1Values();
  173. }
  174. /// <summary>
  175. /// 添加参数改变的监听
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. private void GrainSizePointCountingMethodDialog_Load(object sender, EventArgs e)
  180. {
  181. for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
  182. {
  183. this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  184. }
  185. this.dataGridView4.SortCompare += DataGridView4_SortCompare;
  186. this.binaryClass.loadParams();
  187. }
  188. private bool bcBinaryChecked()
  189. {
  190. return bc != null && bc.BinaryChecked;
  191. }
  192. private bool bcOriginChecked()
  193. {
  194. return bc != null && bc.OriginChecked;
  195. }
  196. /// <summary>
  197. /// 显示原图/原图+二值图
  198. /// </summary>
  199. /// <param name="sender"></param>
  200. /// <param name="e"></param>
  201. private void bcOriginCheckedChanged(object sender, EventArgs e)
  202. {
  203. if (!bcOriginChecked())
  204. {
  205. if (bcBinaryChecked())
  206. this.documentWorkspace.PhaseModels[0].choise = true;
  207. }
  208. else
  209. this.documentWorkspace.PhaseModels[0].choise = false;
  210. this.documentWorkspace.Refresh();
  211. }
  212. #endregion
  213. /// <summary>
  214. /// 初始化画面数据
  215. /// </summary>
  216. private void InitGridStyle()
  217. {
  218. grainSizePointCountingMethodModel = XmlSerializeHelper.DESerializer<GrainSizePointCountingMethodModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\GrainSizePointCountingMethodModel.xml", FileMode.Open));
  219. if (grainSizePointCountingMethodModel.GridStyleList != null)
  220. {
  221. ArrayList arrayList = new ArrayList();
  222. foreach (var GridStyle in grainSizePointCountingMethodModel.GridStyleList)
  223. {
  224. arrayList.Add(GridStyle.gridName);
  225. }
  226. if (grainSizePointCountingMethodModel.GridStyleList.Count == 0)
  227. {
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 初始化图片列表数据
  233. /// </summary>
  234. public void InitPicList()
  235. {
  236. //初始化图片列表
  237. for (int i = 0; i < this.appWorkspace.DocumentWorkspaces.Length; i++)
  238. {
  239. this.imageList1.Images.Add("img" + i, this.appWorkspace.DocumentWorkspaces[i].BinarizationThumbnail);
  240. this.listView1.Items.Add("", i);
  241. this.listView1.Items[i].ImageIndex = i;
  242. this.listView1.Items[i].Text = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
  243. this.listView1.Items[i].Name = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
  244. }
  245. this.Shown += ShowImgEvent;
  246. }
  247. #region 初始化
  248. private void InitializeLanguageText()
  249. {
  250. this.bc = new PaintDotNet.CustomControl.BinaryControlSmaller();
  251. //
  252. // bc
  253. //
  254. this.bc.BinaryBackColor = System.Drawing.Color.Red;
  255. this.bc.BinaryChecked = false;
  256. this.bc.BinaryStyle = 1;
  257. this.bc.Location = new System.Drawing.Point(152, 7);
  258. this.bc.Name = "bc";
  259. this.bc.OriginChecked = false;
  260. this.bc.scope1End = 0D;
  261. this.bc.scope1Start = 0D;
  262. this.bc.scope2End = 0D;
  263. this.bc.scope2Start = 0D;
  264. this.bc.scope3End = 0D;
  265. this.bc.scope3Start = 0D;
  266. this.bc.Size = new System.Drawing.Size(360, 247);
  267. this.bc.TabIndex = 3;
  268. this.Controls.Add(this.bc);
  269. this.Controls.SetChildIndex(this.bc, 0);
  270. }
  271. #endregion
  272. #region 公共按钮
  273. private void InitCommonButtonEvent()
  274. {
  275. this.commonControlButtons.zoomInButton.Click += new EventHandler(zoomInButton_Click);
  276. this.commonControlButtons.zoomOutButton.Click += new EventHandler(zoomOutButton_Click);
  277. this.commonControlButtons.zoomToWindowButton.Click += new EventHandler(zoomToWindowButton_Click);
  278. this.commonControlButtons.actualSizeButton.Click += new EventHandler(actualSizeButton_Click);
  279. this.commonControlButtons.pointerButton.Click += new EventHandler(pointerButton_Click);
  280. this.commonControlButtons.mobileModeButton.Click += new EventHandler(mobileModeButton_Click);
  281. }
  282. private void zoomInButton_Click(object sender, EventArgs e)
  283. {
  284. this.documentWorkspace.ZoomIn();
  285. }
  286. private void zoomOutButton_Click(object sender, EventArgs e)
  287. {
  288. this.documentWorkspace.ZoomOut();
  289. }
  290. private void zoomToWindowButton_Click(object sender, EventArgs e)
  291. {
  292. this.documentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
  293. }
  294. private void actualSizeButton_Click(object sender, EventArgs e)
  295. {
  296. this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
  297. this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
  298. }
  299. private void pointerButton_Click(object sender, EventArgs e)
  300. {
  301. this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.Pointer;
  302. }
  303. private void mobileModeButton_Click(object sender, EventArgs e)
  304. {
  305. this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
  306. }
  307. #endregion
  308. private void listView1_MouseDown(object sender, MouseEventArgs e)
  309. {
  310. this.changeCount = 0;
  311. }
  312. private void DataGridView4_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  313. {
  314. // 如果是数据的列,则按浮点数处理
  315. if (e.Column.Index > 1)//###20186
  316. {
  317. e.SortResult = (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) > 0) ? 1 : (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) < 0) ? -1 : 0;
  318. }
  319. //否则,按字符串比较
  320. else
  321. {
  322. e.SortResult = System.String.Compare(Convert.ToString(e.CellValue1), Convert.ToString(e.CellValue2));
  323. }
  324. e.Handled = true;//不能省掉,不然没效果
  325. }
  326. private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  327. {
  328. bool change = false;
  329. if (this.changeCount == 0)
  330. {
  331. if (this.dataGridView1.Rows.Count > 0)
  332. {
  333. if (this.saveSuccess)
  334. {
  335. DialogResult dt = MessageBox.Show(PdnResources.GetString("Menu.Whethertosavetherrentpicture.text") + "?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  336. if (dt == DialogResult.Yes)
  337. {
  338. string imgName = this.appWorkspace.DocumentWorkspaces[e.ItemIndex].GetFriendlyName();
  339. string tag = this.imageList1.Images.Keys[e.ItemIndex];
  340. if (this.dataGridView1.Rows.Count == 0)
  341. {
  342. MessageBox.Show(PdnResources.GetString("Menu.Pleaseaddtype.text") + "!");
  343. return;
  344. }
  345. }
  346. }
  347. }
  348. }
  349. else
  350. {
  351. if (change)
  352. SelectedIndexChanged();
  353. }
  354. this.changeCount++;
  355. }
  356. /// <summary>
  357. /// 切换图片
  358. /// </summary>
  359. /// <param name="sender"></param>
  360. /// <param name="e"></param>
  361. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  362. {
  363. SelectedIndexChanged();
  364. }
  365. List<PointF> list = new List<PointF>();
  366. /// <summary>
  367. /// 切换图片
  368. /// </summary>
  369. private void SelectedIndexChanged()
  370. {
  371. if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Selected)
  372. {
  373. existViewFlag = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList.IsExsitView();
  374. this.bitmap = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  375. lineList.Clear();
  376. pointList.Clear();
  377. kongLineList.Clear();
  378. kongPointList.Clear();
  379. delLineList.Clear();
  380. delPointList.Clear();
  381. isLunKuo = false;
  382. isAddKong = false;
  383. isDelKong = false;
  384. isDrwKuo = false;
  385. DrwDone = false;
  386. this.documentWorkspace.Refresh();
  387. this.dataGridView1.Rows.Clear();
  388. this.dataGridView4.Rows.Clear();
  389. Init();
  390. this.documentWorkspace.GridRectangleEnabled = false;
  391. //getimage();
  392. //getbian(bitmap);
  393. binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
  394. //二值化集成4
  395. binaryClass.listView1_SelectedIndexChanged(this.bitmap);
  396. binaryClass.bcDefault();
  397. // btnCircleDrawing_Click(null, null);
  398. this.documentWorkspace.Refresh();
  399. }
  400. }
  401. #region 原始事件
  402. private void Init()
  403. {
  404. this.commonControlButtons.Visible = true;
  405. this.dataGridView1.Rows.Clear();
  406. this.dataGridView4.Rows.Clear();
  407. this.documentWorkspace.phaseModels.Clear();
  408. }
  409. /// <summary>
  410. /// 画布绘制
  411. /// </summary>
  412. /// <param name="sender"></param>
  413. /// <param name="e"></param>
  414. private void Panel_Paint(object sender, PaintEventArgs e)
  415. {
  416. if (this.documentWorkspace.CompositionSurface != null)
  417. {
  418. //
  419. // 以下是计算绘制图片的位置和大小并绘制图片
  420. //
  421. Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
  422. int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
  423. int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
  424. int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
  425. int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
  426. //if (pointArray.Count > 1)
  427. //{
  428. // e.Graphics.DrawLines(new Pen(Color.FromArgb(0, 0, 0), 2), pointArray.ToArray());
  429. //}
  430. //
  431. // 以下是绘制网格、标注、测量、视场等开始
  432. //
  433. e.Graphics.TranslateTransform(x, y);
  434. e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
  435. Draw(e.Graphics);
  436. //e.Graphics.DrawLines(new Pen(this.panel1.BackColor, Convert.ToInt32(this.txt_LeftLineWeight.Text)), list.ToArray());
  437. e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
  438. e.Graphics.TranslateTransform(-x, -y);
  439. }
  440. if (bc.isedit)
  441. {
  442. bc.isedit = false;
  443. List<List<int>> CircleDate = bc.data;
  444. bindGrid(CircleDate);
  445. }
  446. }
  447. /// <summary>
  448. /// 绘制
  449. /// </summary>
  450. private void Draw(Graphics graphics)
  451. {
  452. //轮廓手动干预线
  453. if (lineList.Count > 0)
  454. {
  455. for (int i = 0; i < lineList.Count; i++)
  456. {
  457. graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), lineList[i].ToArray());
  458. }
  459. }
  460. if (pointList.Count > 1)
  461. {
  462. graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), pointList.ToArray());
  463. }
  464. //孔隙手动干预线
  465. Color color = bc.BinaryBackColor;
  466. if (kongLineList.Count > 0)
  467. {
  468. for (int i = 0; i < kongLineList.Count; i++)
  469. {
  470. graphics.DrawLines(new Pen(color, 2), kongLineList[i].ToArray());
  471. }
  472. }
  473. if (kongPointList.Count > 1)
  474. {
  475. graphics.DrawLines(new Pen(color, 2), kongPointList.ToArray());
  476. }
  477. //删除孔隙
  478. //if (delLineList.Count > 0)
  479. //{
  480. // for (int i = 0; i < delLineList.Count; i++)
  481. // {
  482. // graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 0), 2), delLineList[i].ToArray());
  483. // }
  484. //}
  485. //if (delPointList.Count > 1)
  486. //{
  487. // graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 0), 2), delPointList.ToArray());
  488. //}
  489. }
  490. /// <summary>
  491. /// 画同心圆
  492. /// </summary>
  493. /// <param name="sender"></param>
  494. /// <param name="e"></param>
  495. private void btnCircleDrawing_Click(object sender, EventArgs e)
  496. {
  497. if (this.listView1.Items.Count == 0)
  498. {
  499. return;
  500. }
  501. if (rb_RightCircle.Checked)
  502. {
  503. List<List<int>> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
  504. bindGrid(CircleDate);
  505. }
  506. else
  507. {
  508. List<List<int>> CircleDate = binaryClass.getCircleDate(0);
  509. bindGrid(CircleDate);
  510. }
  511. }
  512. private void bindGrid(List<List<int>> dataList)
  513. {
  514. if (dataList==null)
  515. {
  516. return;
  517. }
  518. if (dataList.Count < 1)
  519. {
  520. return;
  521. }
  522. List<int> CircleDate = dataList[0];
  523. if (CircleDate.Count < 2)
  524. {
  525. return;
  526. }
  527. //标尺 微米/像素
  528. double d = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
  529. //图像自动识别标尺x微米/像素
  530. //double q= binaryClass.GetRuler();
  531. //孔隙率信息列表
  532. dataGridView1.Rows.Clear();
  533. List<Color> colorList = new List<Color>() { Color.FromArgb(255, 0, 0), Color.FromArgb(0, 255, 0), Color.FromArgb(139, 0, 0), Color.FromArgb(128, 0, 128), Color.FromArgb(122, 160, 255) };
  534. double maxArea =(double)CircleDate[CircleDate.Count - 1];
  535. double k = (double)CircleDate[CircleDate.Count - 2] / maxArea;
  536. double totalArea = CircleDate[CircleDate.Count - 2] * d * d;
  537. int newrow = dataGridView1.Rows.Add();
  538. dataGridView1.Rows[newrow].Cells[0].Value = "整体";
  539. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(k*100, 2).ToString()+"%";
  540. dataGridView1.Rows[newrow].Cells[2].Value = Math.Round(totalArea, 2).ToString();
  541. int n = CircleDate.Count - 2;
  542. //方差
  543. double sd = 0;
  544. //标准方差
  545. double rsd = 0;
  546. //平均值
  547. double x = 0;
  548. //半径
  549. int r = binaryClass.r;
  550. List<double> tlist = new List<double>();
  551. double nArea = 0;
  552. for (int i = 0; i < n; i++)
  553. {
  554. double t = 0;
  555. double ar = Math.PI * r * r * ((i + 1) * (i + 1) - i * i);
  556. if (i == n - 1)
  557. {
  558. ar = maxArea - Math.PI * i * r * i * r;
  559. }
  560. t = (double)CircleDate[i] / ar;
  561. if (i == n - 1)
  562. {
  563. t = (CircleDate[CircleDate.Count - 2] - nArea) / ar;
  564. }
  565. double curArea = (double)CircleDate[i] * d * d;
  566. if (i == n - 1)
  567. {
  568. curArea = (CircleDate[CircleDate.Count - 2] - nArea) * d * d;
  569. }
  570. newrow = dataGridView1.Rows.Add();
  571. if (i == n - 1)
  572. {
  573. dataGridView1.Rows[newrow].Cells[0].Value = "剩余部分";
  574. }
  575. else
  576. {
  577. dataGridView1.Rows[newrow].Cells[0].Value = "同心圆" + (i + 1).ToString();
  578. }
  579. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(t * 100, 2).ToString() + "%";
  580. dataGridView1.Rows[newrow].Cells[2].Value = Math.Round(curArea, 2).ToString();
  581. dataGridView1.Rows[newrow].DefaultCellStyle.ForeColor = colorList[i];
  582. tlist.Add(t);
  583. nArea = nArea + (double)CircleDate[i];
  584. x = x + t;
  585. }
  586. x = x / n;
  587. for (int i = 0; i < n; i++)
  588. {
  589. double t = tlist[i];
  590. sd = sd + (t - x) * (t - x);
  591. }
  592. sd = Math.Sqrt(sd / (n - 1));
  593. rsd = sd / x;
  594. newrow = dataGridView1.Rows.Add();
  595. dataGridView1.Rows[newrow].Cells[0].Value = "同心圆孔隙率RSD";
  596. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(rsd * 100, 0).ToString() + "%";
  597. dataGridView1.Rows[newrow].Cells[2].Value ="";
  598. //孔隙信息列表
  599. dataGridView4.Rows.Clear();
  600. for (int i = 1; i < dataList.Count-1; i++)
  601. {
  602. if(dataList[i].Count<6)
  603. { continue; }
  604. int newPo = dataGridView4.Rows.Add();
  605. //序号
  606. dataGridView4.Rows[newPo].Cells[0].Value = (newPo+1).ToString();
  607. //X
  608. dataGridView4.Rows[newPo].Cells[1].Value = dataList[i][0];
  609. //Y
  610. dataGridView4.Rows[newPo].Cells[2].Value = dataList[i][1];
  611. //长
  612. dataGridView4.Rows[newPo].Cells[3].Value =Math.Round( Convert.ToDouble( dataList[i][2])*d,2).ToString();
  613. //宽
  614. dataGridView4.Rows[newPo].Cells[4].Value = Math.Round(Convert.ToDouble(dataList[i][3]) * d, 2).ToString();
  615. //面积
  616. dataGridView4.Rows[newPo].Cells[5].Value = Math.Round(Convert.ToDouble(dataList[i][4]) * d*d, 2).ToString();
  617. //标识
  618. dataGridView4.Rows[newPo].Cells[6].Value = dataList[i][5];
  619. }
  620. }
  621. private Mat getbian(Bitmap img)
  622. {
  623. // Bitmap img= this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  624. Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(img);
  625. Mat g_grayImage = new Mat();
  626. Mat g_dstImage = new Mat();
  627. Mat g_bwImage = new Mat();
  628. Mat mask = Mat.Zeros(mat.Size(), MatType.CV_8UC1);
  629. Mat dst = new Mat();
  630. //缩放
  631. //Cv2.Resize(mat, mat,new OpenCvSharp.Size(mat.Cols / 4, mat.Rows / 4), (0.0), (0.0), OpenCvSharp.InterpolationFlags.Area);
  632. //灰度图
  633. Cv2.CvtColor(mat, g_grayImage, ColorConversionCodes.BGR2GRAY);
  634. //算子
  635. InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5), new OpenCvSharp.Point(2, 2));
  636. //闭操作
  637. Cv2.MorphologyEx(g_grayImage, g_dstImage, MorphTypes.Close, kernel, new OpenCvSharp.Point(-1, -1));
  638. Scalar scalar = Cv2.Mean(g_dstImage);
  639. double val = scalar.Val0 + 11;
  640. //bmc.scope1Start = val;
  641. //bmc.Refresh();
  642. // bmc.UpdateVerticalBarWithOneScope(0, Convert.ToInt32(bmc.scope1Start));
  643. //图像二值化
  644. Cv2.Threshold(g_dstImage, g_bwImage, val, 255, ThresholdTypes.Binary);
  645. OpenCvSharp.Point[][] contours = new OpenCvSharp.Point[][] { };
  646. HierarchyIndex[] hierarchyIndex = new HierarchyIndex[] { };
  647. //轮廓
  648. Cv2.FindContours(g_bwImage, out contours, out hierarchyIndex, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  649. //截面面积
  650. double maxArea = 0;
  651. int maxcontourIdx = 0;
  652. OpenCvSharp.Point[] maxContour = new OpenCvSharp.Point[] { };
  653. for (int i = 0; i < contours.Length; i++)
  654. {
  655. double area = Cv2.ContourArea(contours[i]);
  656. if (area > maxArea)
  657. {
  658. maxArea = area;
  659. maxContour = contours[i];
  660. maxcontourIdx = i;
  661. }
  662. }
  663. Mat matret = new Mat(mat.Rows, mat.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  664. // Cv2.DrawContours(matret, contours, maxcontourIdx, new Scalar(255, 0, 255, 255), 2);
  665. OpenCvSharp.Point center = new OpenCvSharp.Point();
  666. double dist = 0;
  667. double maxdist = 0;
  668. for (int i = (matret.Cols*18)/40; i < (matret.Cols * 22) / 40; i++)
  669. {
  670. for (int j = (matret.Rows * 19) / 40; j < (matret.Rows * 21) / 40; j++)
  671. {
  672. OpenCvSharp.Point point = new OpenCvSharp.Point(i, j);
  673. dist = Cv2.PointPolygonTest(maxContour, point, true);
  674. if (dist > maxdist)
  675. {
  676. maxdist = dist;
  677. center = point;
  678. }
  679. }
  680. }
  681. Cv2.Circle(matret, center,Convert.ToInt32( maxdist), new Scalar(255, 0, 255, 255));
  682. List<OpenCvSharp.Point> maxContour1 = new List<OpenCvSharp.Point>(){};
  683. for (int i = 0; i < maxContour.Length; i++)
  684. {
  685. double distance;
  686. distance =Math.Pow((maxContour[i].X - center.X), 2) + Math.Pow((maxContour[i].Y - center.Y), 2);
  687. distance = Math.Sqrt(distance);
  688. if (distance <= maxdist+70)
  689. {
  690. maxContour1.Add( maxContour[i]);
  691. }
  692. }
  693. List< List<OpenCvSharp.Point>> maxContour2 = new List<List<OpenCvSharp.Point>>() { };
  694. maxContour2.Add(maxContour1);
  695. Cv2.DrawContours(matret, maxContour2, 0, new Scalar(255, 0, 255, 255), 2);
  696. Cv2.ImShow("yuan", matret);
  697. Cv2.DrawContours(mask, contours, maxcontourIdx, new Scalar(255, 255, 255, 255), -1);
  698. mat.CopyTo(dst, mask);
  699. for (int i = 0; i < maxContour.Length; i++)
  700. {
  701. PointF pointF = new PointF() { X = maxContour[i].X, Y = maxContour[i].Y };
  702. list.Add(pointF);
  703. }
  704. return dst;
  705. }
  706. #region 手动干预
  707. /// <summary>
  708. /// 修改轮廓
  709. /// </summary>
  710. /// <param name="sender"></param>
  711. /// <param name="e"></param>
  712. private void btnLunkuo_Click(object sender, EventArgs e)
  713. {
  714. isLunKuo = true;
  715. isAddKong = false;
  716. isDelKong = false;
  717. isDrwKuo = false;
  718. //DrwDone = false;
  719. }
  720. /// <summary>
  721. /// 添加孔隙
  722. /// </summary>
  723. /// <param name="sender"></param>
  724. /// <param name="e"></param>
  725. private void btnAddKong_Click(object sender, EventArgs e)
  726. {
  727. isLunKuo = false;
  728. isAddKong = true;
  729. isDelKong = false;
  730. isDrwKuo = false;
  731. //DrwDone = false;
  732. }
  733. /// <summary>
  734. /// 删除孔隙
  735. /// </summary>
  736. /// <param name="sender"></param>
  737. /// <param name="e"></param>
  738. private void btnDelKong_Click(object sender, EventArgs e)
  739. {
  740. isLunKuo = false;
  741. isAddKong = false;
  742. isDelKong = true;
  743. isDrwKuo = false;
  744. //DrwDone = false;
  745. }
  746. /// <summary>
  747. /// 绘制轮廓
  748. /// </summary>
  749. /// <param name="sender"></param>
  750. /// <param name="e"></param>
  751. private void btnDrwKuo_Click(object sender, EventArgs e)
  752. {
  753. isLunKuo = false;
  754. isAddKong = false;
  755. isDelKong = false;
  756. isDrwKuo = true;
  757. DrwDone = true;
  758. }
  759. /// <summary>
  760. /// 重新统计
  761. /// </summary>
  762. /// <param name="sender"></param>
  763. /// <param name="e"></param>
  764. private void btnReDrw_Click(object sender, EventArgs e)
  765. {
  766. if (pointList.Count > 1)
  767. {
  768. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  769. lineList.Add(pointFs);
  770. pointList.Clear();
  771. }
  772. if (kongPointList.Count > 1)
  773. {
  774. List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
  775. kongLineList.Add(pointFs);
  776. kongPointList.Clear();
  777. }
  778. if (delPointList.Count > 1)
  779. {
  780. }
  781. List<List<int>> CircleDate = new List<List<int>>();
  782. if (DrwDone)
  783. {
  784. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  785. }
  786. else
  787. {
  788. CircleDate = binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  789. }
  790. bindGrid(CircleDate);
  791. isLunKuo = false;
  792. isAddKong = false;
  793. isDelKong = false;
  794. isDrwKuo = false;
  795. // DrwDone = false;
  796. }
  797. /// <summary>
  798. /// 移动
  799. /// </summary>
  800. /// <param name="sender"></param>
  801. /// <param name="e"></param>
  802. private void onMouseMove(object sender, MouseEventArgs e)
  803. {
  804. //if (e.Button == MouseButtons.Left)
  805. //{
  806. if (isLunKuo)
  807. {
  808. int cou = pointList.Count;
  809. if (cou > 0)
  810. { // 换算后的点
  811. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  812. pointList[cou - 1] = movePoint;
  813. }
  814. }
  815. if (isAddKong)
  816. {
  817. if (kongPointList.Count > 0)
  818. { // 换算后的点
  819. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  820. kongPointList[kongPointList.Count - 1] = movePoint;
  821. }
  822. }
  823. if (isDelKong)
  824. {
  825. //if (delPointList.Count > 0)
  826. //{ // 换算后的点
  827. // PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  828. // delPointList[delPointList.Count - 1] = movePoint;
  829. //}
  830. }
  831. if (isDrwKuo)
  832. {
  833. int cou = pointList.Count;
  834. if (cou > 0)
  835. { // 换算后的点
  836. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  837. pointList[cou - 1] = movePoint;
  838. }
  839. }
  840. this.documentWorkspace.Refresh();
  841. // }
  842. }
  843. /// <summary>
  844. /// 鼠标抬起
  845. /// </summary>
  846. /// <param name="sender"></param>
  847. /// <param name="e"></param>
  848. private void OnMouseUp(object sender, MouseEventArgs e)
  849. {
  850. }
  851. /// <summary>
  852. /// 鼠标按下
  853. /// </summary>
  854. /// <param name="drawArea"></param>
  855. /// <param name="e"></param>
  856. private void OnMouseDown(object sender, MouseEventArgs e)
  857. {
  858. PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
  859. if (e.Button == MouseButtons.Left)
  860. {
  861. if (isLunKuo)
  862. {
  863. if (pointList.Count == 0)
  864. {
  865. pointList.Add(downPoint);
  866. pointList.Add(downPoint);
  867. }
  868. else
  869. {
  870. pointList.Add(downPoint);
  871. }
  872. }
  873. if (isAddKong)
  874. {
  875. if (kongPointList.Count == 0)
  876. {
  877. kongPointList.Add(downPoint);
  878. kongPointList.Add(downPoint);
  879. }
  880. else
  881. {
  882. kongPointList.Add(downPoint);
  883. }
  884. }
  885. if (isDelKong)
  886. {
  887. //if (delPointList.Count == 0)
  888. //{
  889. // delPointList.Add(downPoint);
  890. // delPointList.Add(downPoint);
  891. //}
  892. //else
  893. //{
  894. // delPointList.Add(downPoint);
  895. //}
  896. delPointList.Add(downPoint);
  897. List<List<int>> CircleDate = new List<List<int>>();
  898. if (DrwDone)
  899. {
  900. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  901. }
  902. else
  903. {
  904. binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  905. }
  906. bindGrid(CircleDate);
  907. }
  908. if (isDrwKuo)
  909. {
  910. if (pointList.Count == 0)
  911. {
  912. pointList.Add(downPoint);
  913. pointList.Add(downPoint);
  914. }
  915. else
  916. {
  917. pointList.Add(downPoint);
  918. }
  919. }
  920. }
  921. if (e.Button == MouseButtons.Right)
  922. {
  923. if (isLunKuo)
  924. {
  925. if (pointList.Count > 2)
  926. {
  927. pointList.RemoveAt(pointList.Count - 1);
  928. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  929. lineList.Add(pointFs);
  930. }
  931. pointList.Clear();
  932. }
  933. if (isAddKong)
  934. {
  935. if (kongPointList.Count > 2)
  936. {
  937. kongPointList.RemoveAt(kongPointList.Count - 1);
  938. List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
  939. kongLineList.Add(pointFs);
  940. }
  941. kongPointList.Clear();
  942. }
  943. if (isDelKong)
  944. {
  945. //if (delPointList.Count > 2)
  946. //{
  947. // delPointList.RemoveAt(delPointList.Count - 1);
  948. // List<PointF> pointFs = new List<PointF>(delPointList.ToArray());
  949. // delLineList.Add(pointFs);
  950. //}
  951. //delPointList.Clear();
  952. }
  953. if (isDrwKuo)
  954. {
  955. if (pointList.Count > 2)
  956. {
  957. pointList.RemoveAt(pointList.Count - 1);
  958. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  959. lineList.Add(pointFs);
  960. }
  961. pointList.Clear();
  962. }
  963. isLunKuo = false;
  964. isAddKong = false;
  965. isDelKong = false;
  966. isDrwKuo = false;
  967. }
  968. }
  969. /// <summary>
  970. /// Ctrl+z
  971. /// </summary>
  972. private void OnKeyDown(object sender, KeyEventArgs e)
  973. {
  974. if (e.Control && e.KeyCode == Keys.Z)
  975. {
  976. if (isLunKuo)
  977. {
  978. int cou = pointList.Count;
  979. if (cou > 1)
  980. {
  981. if (cou == 2)
  982. {
  983. pointList.Clear();
  984. }
  985. else
  986. {
  987. pointList.RemoveAt(cou - 1);
  988. }
  989. }
  990. else if (lineList.Count > 0)
  991. {
  992. pointList = lineList[lineList.Count - 1];
  993. lineList.RemoveAt(lineList.Count - 1);
  994. pointList.RemoveAt(pointList.Count - 1);
  995. }
  996. }
  997. if (isAddKong)
  998. {
  999. int cou =kongPointList.Count;
  1000. if (cou > 1)
  1001. {
  1002. if (cou == 2)
  1003. {
  1004. kongPointList.Clear();
  1005. }
  1006. else
  1007. {
  1008. kongPointList.RemoveAt(cou - 1);
  1009. }
  1010. }
  1011. else if (kongLineList.Count > 0)
  1012. {
  1013. kongPointList = kongLineList[kongLineList.Count - 1];
  1014. kongLineList.RemoveAt(kongLineList.Count - 1);
  1015. kongPointList.RemoveAt(kongPointList.Count - 1);
  1016. }
  1017. }
  1018. if (isDelKong)
  1019. {
  1020. int cou = delPointList.Count;
  1021. if (cou > 0)
  1022. {
  1023. delPointList.RemoveAt(cou - 1);
  1024. }
  1025. List<List<int>> CircleDate = new List<List<int>>();
  1026. if (DrwDone)
  1027. {
  1028. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  1029. }
  1030. else
  1031. {
  1032. binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  1033. }
  1034. bindGrid(CircleDate);
  1035. //if (cou > 1)
  1036. //{
  1037. // if (cou == 2)
  1038. // {
  1039. // delPointList.Clear();
  1040. // }
  1041. // else
  1042. // {
  1043. // delPointList.RemoveAt(cou - 1);
  1044. // }
  1045. //}
  1046. //else if (delLineList.Count > 0)
  1047. //{
  1048. // delPointList = delLineList[delLineList.Count - 1];
  1049. // delLineList.RemoveAt(delLineList.Count - 1);
  1050. // delPointList.RemoveAt(delPointList.Count - 1);
  1051. //}
  1052. }
  1053. this.documentWorkspace.Refresh();
  1054. }
  1055. }
  1056. #endregion
  1057. /// <summary>
  1058. /// 导出孔隙信息
  1059. /// </summary>
  1060. /// <param name="sender"></param>
  1061. /// <param name="e"></param>
  1062. private void button14_Click(object sender, EventArgs e)
  1063. {
  1064. SaveFileDialog exe = new SaveFileDialog();
  1065. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  1066. exe.FilterIndex = 0;
  1067. exe.RestoreDirectory = true;
  1068. //exe.CreatePrompt = true;
  1069. exe.Title = "Export Excel File";
  1070. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1071. exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") +"孔隙信息";
  1072. DialogResult dr = exe.ShowDialog();
  1073. if (dr != DialogResult.OK)
  1074. {
  1075. return;
  1076. }
  1077. DataTable dtb = new DataTable();
  1078. foreach (DataGridViewColumn c in this.dataGridView4.Columns)
  1079. {
  1080. dtb.Columns.Add(c.HeaderText);
  1081. }
  1082. for (int r = 0; r < this.dataGridView4.Rows.Count; r++)
  1083. {
  1084. DataRow dataRow = dtb.NewRow();
  1085. for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count; c++)
  1086. {
  1087. dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
  1088. }
  1089. dtb.Rows.Add(dataRow);
  1090. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1091. }
  1092. List<DataTable> list = new List<DataTable>();
  1093. list.Add(dtb);
  1094. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  1095. }
  1096. /// <summary>
  1097. /// 不可点击空白区域
  1098. /// </summary>
  1099. /// <param name="sender"></param>
  1100. /// <param name="e"></param>
  1101. private void listView1_MouseUp(object sender, MouseEventArgs e)
  1102. {
  1103. if (e.Button == MouseButtons.Left)
  1104. {
  1105. if (listView1.SelectedItems.Count > 0)
  1106. {
  1107. }
  1108. else if (listView1.SelectedItems.Count <= 0)//点击空白区
  1109. {
  1110. if (this.listView1.FocusedItem != null)
  1111. {
  1112. ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);
  1113. if (item == null)
  1114. {
  1115. this.listView1.FocusedItem.Selected = true;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. /// <summary>
  1122. /// 生成孔隙率
  1123. /// </summary>
  1124. /// <param name="sender"></param>
  1125. /// <param name="e"></param>
  1126. private void button13_Click(object sender, EventArgs e)
  1127. {
  1128. //string path = string.Empty;
  1129. //System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
  1130. //fbd.Description = "请选择文件夹";
  1131. //String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
  1132. //if (!string.IsNullOrEmpty(str))
  1133. //{
  1134. // fbd.SelectedPath = str;
  1135. //}
  1136. //if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1137. //{
  1138. // path = fbd.SelectedPath;
  1139. // PrintReport(path);
  1140. // Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  1141. // cfa.AppSettings.Settings["FolderSelectedPath"].Value = path;
  1142. // cfa.Save();
  1143. // System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  1144. //}
  1145. string name = this.listView1.FocusedItem.Name.Split('.')[0] + "-";
  1146. SaveFileDialog exe = new SaveFileDialog();
  1147. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  1148. exe.FilterIndex = 0;
  1149. exe.RestoreDirectory = true;
  1150. //exe.CreatePrompt = true;
  1151. exe.Title = "Export Excel File";
  1152. // exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1153. exe.FileName = name;
  1154. //String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
  1155. DialogResult dr = exe.ShowDialog();
  1156. if (dr != DialogResult.OK)
  1157. {
  1158. return;
  1159. }
  1160. DataTable dtb = new DataTable();
  1161. foreach (DataGridViewColumn c in this.dataGridView1.Columns)
  1162. {
  1163. dtb.Columns.Add(c.HeaderText);
  1164. }
  1165. for (int r = 0; r < this.dataGridView1.Rows.Count; r++)
  1166. {
  1167. DataRow dataRow = dtb.NewRow();
  1168. for (int c = 0; c < this.dataGridView1.Rows[r].Cells.Count; c++)
  1169. {
  1170. dataRow[this.dataGridView1.Columns[c].HeaderText] = (string)this.dataGridView1.Rows[r].Cells[c].Value.ToString();
  1171. }
  1172. dtb.Rows.Add(dataRow);
  1173. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1174. }
  1175. int index = exe.FileName.LastIndexOf('\\');
  1176. List<DataTable> list = new List<DataTable>();
  1177. list.Add(dtb);
  1178. DataTable dtb1 = new DataTable();
  1179. foreach (DataGridViewColumn c in this.dataGridView4.Columns)
  1180. {
  1181. dtb1.Columns.Add(c.HeaderText);
  1182. }
  1183. for (int r = 0; r < this.dataGridView4.Rows.Count; r++)
  1184. {
  1185. DataRow dataRow = dtb1.NewRow();
  1186. for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count; c++)
  1187. {
  1188. dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
  1189. }
  1190. dtb1.Rows.Add(dataRow);
  1191. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1192. }
  1193. list.Add(dtb1);
  1194. //保存处理后的图片
  1195. double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
  1196. List<Bitmap> tempBit = new List<Bitmap>();
  1197. Bitmap originalBit = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  1198. originalBit.Tag = pantographRatio;
  1199. tempBit.Add(originalBit);
  1200. Bitmap processedBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
  1201. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  1202. Graphics graphics = Graphics.FromImage(newBit);
  1203. graphics.DrawImage(processedBit, new PointF(0, 0));
  1204. Draw(graphics);
  1205. newBit.Tag = pantographRatio;
  1206. tempBit.Add(newBit);
  1207. string GR_Path = exe.FileName.Remove(index) + "\\" + name + ".jpg";
  1208. newBit.Save(GR_Path);
  1209. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  1210. }
  1211. /// <summary>
  1212. /// 导出报告
  1213. /// </summary>
  1214. private void PrintReport(string savePath)
  1215. {
  1216. List<List<string>> analysisContent = new List<List<string>>();
  1217. for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
  1218. {
  1219. List<string> content1 = new List<string>();
  1220. if (i == 0)
  1221. {
  1222. content1.Add(this.dataGridView1.Columns[0].HeaderText);
  1223. content1.Add(this.dataGridView1.Columns[1].HeaderText);
  1224. content1.Add(this.dataGridView1.Columns[2].HeaderText);
  1225. analysisContent.Add(content1);
  1226. }
  1227. content1 = new List<string>();
  1228. content1.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
  1229. content1.Add(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
  1230. content1.Add(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
  1231. analysisContent.Add(content1);
  1232. }
  1233. List<string> content2 = new List<string>();
  1234. analysisContent.Add(content2);
  1235. for (int i = 0; i < this.dataGridView4.Rows.Count; i++)
  1236. {
  1237. List<string> content1 = new List<string>();
  1238. if (i == 0)
  1239. {
  1240. content1.Add(this.dataGridView4.Columns[0].HeaderText);
  1241. content1.Add(this.dataGridView4.Columns[1].HeaderText);
  1242. content1.Add(this.dataGridView4.Columns[2].HeaderText);
  1243. content1.Add(this.dataGridView4.Columns[3].HeaderText);
  1244. content1.Add(this.dataGridView4.Columns[4].HeaderText);
  1245. content1.Add(this.dataGridView4.Columns[5].HeaderText);
  1246. content1.Add(this.dataGridView4.Columns[6].HeaderText);
  1247. analysisContent.Add(content1);
  1248. }
  1249. content1 = new List<string>();
  1250. content1.Add(this.dataGridView4.Rows[i].Cells[0].Value.ToString());
  1251. content1.Add(this.dataGridView4.Rows[i].Cells[1].Value.ToString());
  1252. content1.Add(this.dataGridView4.Rows[i].Cells[2].Value.ToString());
  1253. content1.Add(this.dataGridView4.Rows[i].Cells[3].Value.ToString());
  1254. content1.Add(this.dataGridView4.Rows[i].Cells[4].Value.ToString());
  1255. content1.Add(this.dataGridView4.Rows[i].Cells[5].Value.ToString());
  1256. content1.Add(this.dataGridView4.Rows[i].Cells[6].Value.ToString());
  1257. analysisContent.Add(content1);
  1258. }
  1259. //保存处理后的图片
  1260. double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
  1261. List<Bitmap> tempBit = new List<Bitmap>();
  1262. Bitmap originalBit = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  1263. originalBit.Tag = pantographRatio;
  1264. tempBit.Add(originalBit);
  1265. Bitmap processedBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
  1266. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  1267. Graphics graphics = Graphics.FromImage(newBit);
  1268. graphics.DrawImage(processedBit, new PointF(0, 0));
  1269. Draw(graphics);
  1270. newBit.Tag = pantographRatio;
  1271. tempBit.Add(newBit);
  1272. List<Bitmap> bitList = new List<Bitmap>();
  1273. bitList.Add(tempBit[0]);
  1274. bitList.Add(tempBit[1]);
  1275. Dictionary<string, string> tagInfos = new Dictionary<string, string>();
  1276. // this.analyzeSettingModel.analyzeClassify
  1277. AnalyzeSettingModel model = new AnalyzeSettingModel();
  1278. string filePath = Application.StartupPath + "\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";//获取文档路径
  1279. string filePath2 = "E:\\图形处理\\Metisvision20210204\\RBDC\\Metisvision\\bin\\Debug\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";
  1280. model.modulePath = filePath;
  1281. model.savePath = savePath;
  1282. //model.itemNumber = this.textBox2.Text;
  1283. //model.ratingDate = this.dateTimePicker1.Value;
  1284. //model.inspectionPerson = this.textBox4.Text;
  1285. model.contact = "";
  1286. //model.inspectionDepartment = this.textBox5.Text;
  1287. //model.inspectionDate = this.dateTimePicker2.Value;
  1288. string name = this.listView1.FocusedItem.Name.Split('.')[0];
  1289. model.analyzeClassify = name;
  1290. this.analyzeSettingModel = new AnalyzeSettingModel();
  1291. //this.analyzeSettingModel.modelItems = new List<AnalyzeSavedModel.ModelItem>();
  1292. //this.analyzeSettingModel = new AnalyzeSavedModel.ModelItem();
  1293. //this.analyzeSettingModel.languageName = this.languageName;
  1294. this.analyzeSettingModel = model;
  1295. //this.analyzeSettingModel.modelItems.Add(analyzeModel);
  1296. this.appWorkspace.CreateAnalysisReport(this.analyzeSettingModel, analysisContent, bitList, tagInfos);
  1297. }
  1298. /// 导出图片
  1299. /// </summary>
  1300. /// <param name="sender"></param>
  1301. /// <param name="e"></param>
  1302. private void btnImageOut_Click(object sender, EventArgs e)
  1303. {
  1304. SaveFileDialog exe = new SaveFileDialog();
  1305. exe.Filter = "Image Files (*.jpg)|*.jpg";
  1306. exe.FilterIndex = 0;
  1307. exe.RestoreDirectory = true;
  1308. //exe.CreatePrompt = true;
  1309. exe.Title = "Image Files";
  1310. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1311. exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "孔隙率";
  1312. DialogResult dr = exe.ShowDialog();
  1313. if (dr != DialogResult.OK)
  1314. {
  1315. return;
  1316. }
  1317. Mat mat = binaryClass.MargMat();
  1318. if (Cv2.ImWrite(exe.FileName, mat))
  1319. {
  1320. MessageBox.Show("保存成功!");
  1321. }
  1322. //List<DataTable> list = new List<DataTable>();
  1323. //list.Add(dtb);
  1324. //this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  1325. }
  1326. /// <summary>
  1327. /// 准备用来处理单个颗粒的选中事件
  1328. /// </summary>
  1329. /// <param name="sender"></param>
  1330. /// <param name="e"></param>
  1331. private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
  1332. {
  1333. //当点击表头部的列时,e.RowIndex==-1
  1334. if (e.RowIndex > -1)
  1335. {
  1336. int index= Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[6].Value);
  1337. int x = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[0].Value);
  1338. int y = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[1].Value);
  1339. PointF select = new PointF() { X = x, Y = y };
  1340. binaryClass.DrwLunKuoSel(index);
  1341. // binaryClass.DrwLunKuoLie1Selected(select);
  1342. this.documentWorkspace.Refresh();
  1343. }
  1344. }
  1345. #region 内部类
  1346. internal class LocalListViewItem
  1347. {
  1348. public DocumentWorkspace Value { get; }
  1349. public string Display { get; }
  1350. public LocalListViewItem(DocumentWorkspace Value, string Display)
  1351. {
  1352. this.Value = Value;
  1353. this.Display = Display;
  1354. }
  1355. }
  1356. internal class DocumentItem
  1357. {
  1358. public Bitmap bitmap;
  1359. public GraphicsList graphicsList;
  1360. public List<PhaseModel> phaseModels;
  1361. //public AnalysisResult analysisResult;
  1362. public DocumentItem(Bitmap bitmap, GraphicsList graphicsList, List<PhaseModel> phaseModels)
  1363. {
  1364. this.bitmap = bitmap;
  1365. this.graphicsList = graphicsList;
  1366. this.phaseModels = phaseModels;
  1367. }
  1368. }
  1369. #endregion
  1370. #endregion
  1371. }
  1372. }