BatteryBallAndCrystalDialog.cs 63 KB

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