BatterySectionPorosityDialog.cs 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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 BatterySectionPorosityDialog : 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. //绘制轮廓
  101. bool isDrwKuo = false;
  102. //绘制轮廓结束
  103. bool DrwDone = false;
  104. //设置圆心
  105. bool DrwCenter = false;
  106. //画圆
  107. bool DrwCircle = false;
  108. /// <summary>
  109. /// 0:添加 1:选择 2:删除
  110. /// </summary>
  111. private int operationK = -1;
  112. /// <summary>
  113. /// 被选择的线
  114. /// </summary>
  115. private int selected = -1;
  116. /// <summary>
  117. /// 起止点
  118. /// </summary>
  119. private System.Drawing.PointF startPoint, endPoint;
  120. /// <summary>
  121. /// 存储所有线的点集合
  122. /// </summary>
  123. private List<PointF[]> pointList1 = new List<PointF[]>();
  124. /// <summary>
  125. /// 存储当前线的点集合
  126. /// </summary>
  127. private PointF[] nowLine;
  128. /// <summary>
  129. /// 辅助计算偏移量-移动线两端的点
  130. /// </summary>
  131. private PointF lastPoint = new PointF(0, 0);
  132. /// <summary>
  133. /// 辅助计算偏移量-移动线中间的点
  134. /// </summary>
  135. private PointF tempPoint = new PointF(0, 0);
  136. /// <summary>
  137. /// 判断图形是否可移动
  138. /// </summary>
  139. private bool canMove = false;
  140. /// <summary>
  141. /// 测量点
  142. /// </summary>
  143. public List<RectangleF> rectangleFList = new List<RectangleF>();
  144. /// <summary>
  145. /// 移动线上的哪一个点
  146. /// </summary>
  147. private int movePointIndex = -1;
  148. /// <summary>
  149. /// 线的总数
  150. /// </summary>
  151. private int lineCount = 0;
  152. /// <summary>
  153. /// 单位标尺
  154. /// </summary>
  155. private double unitLength = 1;
  156. /// <summary>
  157. /// 画圆外点
  158. /// </summary>
  159. private PointF tempCirclePoint = new PointF(0, 0);
  160. /// <summary>
  161. /// 同心圆半径
  162. /// </summary>
  163. private List<int> ListRadius = new List<int>();
  164. /// <summary>
  165. /// 同心圆颜色
  166. /// </summary>
  167. private Dictionary<int, Color> ListRadiusColor = new Dictionary<int, Color>();
  168. private 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) };
  169. #endregion
  170. /// <summary>
  171. ///
  172. /// </summary>
  173. /// <param name="appWorkspace"></param>
  174. public BatterySectionPorosityDialog(AppWorkspace appWorkspace, int menuId)
  175. {
  176. this.KeyPreview = true;
  177. this.KeyDown += OnKeyDown;
  178. this.menuId = menuId;
  179. binaryClass = new BinaryClassForB(menuId);
  180. InitializeComponent();
  181. InitializeLanguageText();
  182. ColumnHeader header0 = new ColumnHeader();
  183. header0.Text = "";
  184. header0.Width = 180;
  185. this.appWorkspace = appWorkspace;
  186. this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
  187. this.documentWorkspace.Dock = DockStyle.Fill;
  188. this.documentWorkspace.HookMouseEvents();
  189. this.documentWorkspace.AuxiliaryLineEnabled = false;
  190. this.documentWorkspace.Visible = false;
  191. this.documentWorkspace.panel.Paint += Panel_Paint;
  192. this.documentWorkspace.panel.MouseDown += OnMouseDown;
  193. this.documentWorkspace.panel.MouseUp += OnMouseUp;
  194. this.documentWorkspace.panel.Paint += Panel_Paint;
  195. this.documentWorkspace.panel.MouseMove += onMouseMove;
  196. this.documentWorkspace.activeTool = DrawToolType.Pointer;
  197. this.groupBox7.Controls.Add(this.documentWorkspace);
  198. this.commonControlButtons = new GeneralAnalysisCommonControlButtons();
  199. this.commonControlButtons.Dock = DockStyle.Top;
  200. this.commonControlButtons.Height = 30;
  201. this.commonControlButtons.HideZoomToWindowAndActualSize();
  202. this.commonControlButtons.trackLabel.Text = "单击开始画线,右键停止。";
  203. this.commonControlButtons.trackLabel.Visible = true;
  204. this.commonControlButtons.trackLabel.Width = 300;
  205. this.commonControlButtons.Visible = false;
  206. this.groupBox7.Controls.Add(commonControlButtons);
  207. //获取
  208. //Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out ruleValue);
  209. //double ruleValue = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
  210. ////double physical_area_length_Micron = getCurrentWorkspace().GetRuler(MeasurementUnit.Micron);
  211. //二值化集成2
  212. bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);//初始化原图勾选改变事件
  213. rb_RightCircle.CheckedChanged += new EventHandler(this.RightCircleCheckedChanged);
  214. txt_RightLineWeight.SelectedIndexChanged += new EventHandler(this.RightCircleCheckedChanged);
  215. numericMax.ValueChanged += new EventHandler(this.RightCircleCheckedChanged);
  216. binaryClass.imageType = 1;
  217. binaryClass.createDocumentItemsSmaller(new string[] { "bp1" }, this.bc, this.appWorkspace, this.documentWorkspace, this.listView1);//初始化相的工作结构
  218. binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
  219. InitCommonButtonEvent();
  220. InitGridStyle();
  221. InitPicList();
  222. // SetAnalyzeModelFromXml(getModelName());
  223. }
  224. private void ShowImgEvent(object sender, EventArgs e)
  225. {
  226. if (this.listView1.Items.Count == 0)
  227. {
  228. return;
  229. }
  230. listView1.Focus();
  231. this.listView1.Items[0].Focused = true;
  232. this.listView1.Items[0].Selected = true;
  233. }
  234. #region 二值化相关方法
  235. private void InclusionsStandardDialog_FormClosing(object sender, FormClosingEventArgs e)
  236. {
  237. binaryClass.saveParams();
  238. }
  239. private void ShownChoiseItemAndInitData(object sender, EventArgs e)
  240. {
  241. binaryClass.RefreshHistogramControl1Values();
  242. }
  243. /// <summary>
  244. /// 添加参数改变的监听
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void GrainSizePointCountingMethodDialog_Load(object sender, EventArgs e)
  249. {
  250. for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
  251. {
  252. this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  253. }
  254. this.dataGridView4.SortCompare += DataGridView4_SortCompare;
  255. this.binaryClass.loadParams();
  256. }
  257. private bool bcBinaryChecked()
  258. {
  259. return bc != null && bc.BinaryChecked;
  260. }
  261. private bool bcOriginChecked()
  262. {
  263. return bc != null && bc.OriginChecked;
  264. }
  265. /// <summary>
  266. /// 显示原图/原图+二值图
  267. /// </summary>
  268. /// <param name="sender"></param>
  269. /// <param name="e"></param>
  270. private void bcOriginCheckedChanged(object sender, EventArgs e)
  271. {
  272. if (!bcOriginChecked())
  273. {
  274. if (bcBinaryChecked())
  275. this.documentWorkspace.PhaseModels[0].choise = true;
  276. }
  277. else
  278. this.documentWorkspace.PhaseModels[0].choise = false;
  279. this.documentWorkspace.Refresh();
  280. }
  281. #endregion
  282. /// <summary>
  283. /// 初始化画面数据
  284. /// </summary>
  285. private void InitGridStyle()
  286. {
  287. grainSizePointCountingMethodModel = XmlSerializeHelper.DESerializer<GrainSizePointCountingMethodModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\GrainSizePointCountingMethodModel.xml", FileMode.Open));
  288. if (grainSizePointCountingMethodModel.GridStyleList != null)
  289. {
  290. ArrayList arrayList = new ArrayList();
  291. foreach (var GridStyle in grainSizePointCountingMethodModel.GridStyleList)
  292. {
  293. arrayList.Add(GridStyle.gridName);
  294. }
  295. if (grainSizePointCountingMethodModel.GridStyleList.Count == 0)
  296. {
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// 初始化图片列表数据
  302. /// </summary>
  303. public void InitPicList()
  304. {
  305. //初始化图片列表
  306. for (int i = 0; i < this.appWorkspace.DocumentWorkspaces.Length; i++)
  307. {
  308. this.imageList1.Images.Add("img" + i, this.appWorkspace.DocumentWorkspaces[i].BinarizationThumbnail);
  309. this.listView1.Items.Add("", i);
  310. this.listView1.Items[i].ImageIndex = i;
  311. this.listView1.Items[i].Text = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
  312. this.listView1.Items[i].Name = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
  313. }
  314. this.Shown += ShowImgEvent;
  315. }
  316. #region 初始化
  317. private void InitializeLanguageText()
  318. {
  319. this.bc = new PaintDotNet.CustomControl.BinaryControlSmaller();
  320. //
  321. // bc
  322. //
  323. this.bc.BinaryBackColor = System.Drawing.Color.Red;
  324. this.bc.BinaryChecked = false;
  325. this.bc.BinaryStyle = 1;
  326. this.bc.Location = new System.Drawing.Point(152, 7);
  327. this.bc.Name = "bc";
  328. this.bc.OriginChecked = false;
  329. this.bc.scope1End = 0D;
  330. this.bc.scope1Start = 0D;
  331. this.bc.scope2End = 0D;
  332. this.bc.scope2Start = 0D;
  333. this.bc.scope3End = 0D;
  334. this.bc.scope3Start = 0D;
  335. this.bc.Size = new System.Drawing.Size(360, 247);
  336. this.bc.TabIndex = 3;
  337. this.Controls.Add(this.bc);
  338. this.Controls.SetChildIndex(this.bc, 0);
  339. }
  340. #endregion
  341. #region 公共按钮
  342. private void InitCommonButtonEvent()
  343. {
  344. this.commonControlButtons.zoomInButton.Click += new EventHandler(zoomInButton_Click);
  345. this.commonControlButtons.zoomOutButton.Click += new EventHandler(zoomOutButton_Click);
  346. this.commonControlButtons.zoomToWindowButton.Click += new EventHandler(zoomToWindowButton_Click);
  347. this.commonControlButtons.actualSizeButton.Click += new EventHandler(actualSizeButton_Click);
  348. this.commonControlButtons.pointerButton.Click += new EventHandler(pointerButton_Click);
  349. this.commonControlButtons.mobileModeButton.Click += new EventHandler(mobileModeButton_Click);
  350. }
  351. private void zoomInButton_Click(object sender, EventArgs e)
  352. {
  353. this.documentWorkspace.ZoomIn();
  354. }
  355. private void zoomOutButton_Click(object sender, EventArgs e)
  356. {
  357. this.documentWorkspace.ZoomOut();
  358. }
  359. private void zoomToWindowButton_Click(object sender, EventArgs e)
  360. {
  361. this.documentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
  362. }
  363. private void actualSizeButton_Click(object sender, EventArgs e)
  364. {
  365. this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
  366. this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
  367. }
  368. private void pointerButton_Click(object sender, EventArgs e)
  369. {
  370. this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.Pointer;
  371. }
  372. private void mobileModeButton_Click(object sender, EventArgs e)
  373. {
  374. this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
  375. }
  376. #endregion
  377. private void listView1_MouseDown(object sender, MouseEventArgs e)
  378. {
  379. this.changeCount = 0;
  380. }
  381. private void DataGridView4_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  382. {
  383. // 如果是数据的列,则按浮点数处理
  384. if (e.Column.Index > 1)//###20186
  385. {
  386. e.SortResult = (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) > 0) ? 1 : (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) < 0) ? -1 : 0;
  387. }
  388. //否则,按字符串比较
  389. else
  390. {
  391. e.SortResult = System.String.Compare(Convert.ToString(e.CellValue1), Convert.ToString(e.CellValue2));
  392. }
  393. e.Handled = true;//不能省掉,不然没效果
  394. }
  395. private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  396. {
  397. bool change = false;
  398. if (this.changeCount == 0)
  399. {
  400. if (this.dataGridView1.Rows.Count > 0)
  401. {
  402. if (this.saveSuccess)
  403. {
  404. DialogResult dt = MessageBox.Show(PdnResources.GetString("Menu.Whethertosavetherrentpicture.text") + "?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  405. if (dt == DialogResult.Yes)
  406. {
  407. string imgName = this.appWorkspace.DocumentWorkspaces[e.ItemIndex].GetFriendlyName();
  408. string tag = this.imageList1.Images.Keys[e.ItemIndex];
  409. if (this.dataGridView1.Rows.Count == 0)
  410. {
  411. MessageBox.Show(PdnResources.GetString("Menu.Pleaseaddtype.text") + "!");
  412. return;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. else
  419. {
  420. if (change)
  421. SelectedIndexChanged();
  422. }
  423. if (unitLength == 1)
  424. unitLength = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
  425. this.changeCount++;
  426. }
  427. /// <summary>
  428. /// 切换图片
  429. /// </summary>
  430. /// <param name="sender"></param>
  431. /// <param name="e"></param>
  432. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  433. {
  434. SelectedIndexChanged();
  435. }
  436. List<PointF> list = new List<PointF>();
  437. /// <summary>
  438. /// 切换图片
  439. /// </summary>
  440. private void SelectedIndexChanged()
  441. {
  442. if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Selected)
  443. {
  444. existViewFlag = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList.IsExsitView();
  445. this.bitmap = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  446. lineList.Clear();
  447. pointList.Clear();
  448. kongLineList.Clear();
  449. kongPointList.Clear();
  450. delLineList.Clear();
  451. delPointList.Clear();
  452. pointList1.Clear();
  453. binaryClass.SetCenter = new OpenCvSharp.Point(0, 0);
  454. binaryClass.R = 0;
  455. isLunKuo = false;
  456. isAddKong = false;
  457. isDelKong = false;
  458. isDrwKuo = false;
  459. DrwDone = false;
  460. DrwCenter = false;
  461. DrwCircle = false;
  462. this.documentWorkspace.Refresh();
  463. this.dataGridView1.Rows.Clear();
  464. this.dataGridView4.Rows.Clear();
  465. Init();
  466. this.documentWorkspace.GridRectangleEnabled = false;
  467. //getimage();
  468. //getbian(bitmap);
  469. binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
  470. //二值化集成4
  471. binaryClass.listView1_SelectedIndexChanged(this.bitmap);
  472. binaryClass.bcDefault();
  473. // btnCircleDrawing_Click(null, null);
  474. this.documentWorkspace.Refresh();
  475. }
  476. }
  477. #region 原始事件
  478. private void Init()
  479. {
  480. this.commonControlButtons.Visible = true;
  481. this.dataGridView1.Rows.Clear();
  482. this.dataGridView4.Rows.Clear();
  483. this.documentWorkspace.phaseModels.Clear();
  484. }
  485. /// <summary>
  486. /// 画布绘制
  487. /// </summary>
  488. /// <param name="sender"></param>
  489. /// <param name="e"></param>
  490. private void Panel_Paint(object sender, PaintEventArgs e)
  491. {
  492. if (this.documentWorkspace.CompositionSurface != null)
  493. {
  494. //
  495. // 以下是计算绘制图片的位置和大小并绘制图片
  496. //
  497. Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
  498. int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
  499. int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
  500. int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
  501. int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
  502. //if (pointArray.Count > 1)
  503. //{
  504. // e.Graphics.DrawLines(new Pen(Color.FromArgb(0, 0, 0), 2), pointArray.ToArray());
  505. //}
  506. //
  507. // 以下是绘制网格、标注、测量、视场等开始
  508. //
  509. e.Graphics.TranslateTransform(x, y);
  510. e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
  511. Draw(e.Graphics);
  512. //e.Graphics.DrawLines(new Pen(this.panel1.BackColor, Convert.ToInt32(this.txt_LeftLineWeight.Text)), list.ToArray());
  513. e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
  514. e.Graphics.TranslateTransform(-x, -y);
  515. }
  516. if (bc.isedit)
  517. {
  518. bc.isedit = false;
  519. List<List<int>> CircleDate = bc.data;
  520. bindGrid(CircleDate);
  521. }
  522. }
  523. // <summary>
  524. /// 存储字号与颜色字典
  525. /// </summary>
  526. public Dictionary<string, Color> colourFont = new Dictionary<string, Color>() { { "20", Color.White } };
  527. /// <summary>
  528. /// 绘制
  529. /// </summary>
  530. private void Draw(Graphics graphics)
  531. {
  532. //轮廓手动干预线
  533. if (lineList.Count > 0)
  534. {
  535. for (int i = 0; i < lineList.Count; i++)
  536. {
  537. //graphics.DrawLines(new Pen(Color.FromArgb(30, 144,255), 2), lineList[i].ToArray());
  538. graphics.DrawLines(new Pen(Color.FromArgb(30, 144,255), 2), lineList[i].ToArray());
  539. }
  540. }
  541. if (pointList.Count > 1)
  542. {
  543. graphics.DrawLines(new Pen(Color.FromArgb(30, 144,255), 2), pointList.ToArray());
  544. //Pen _pen = new Pen(Color.FromArgb(99, 99, 99), 2);
  545. //_pen.DashStyle = DashStyle.Dash;
  546. //_pen.DashCap = DashCap.Triangle;
  547. //graphics.DrawLines(_pen, pointList.ToArray());
  548. }
  549. //孔隙手动干预线
  550. Color color = bc.BinaryBackColor;
  551. if (kongLineList.Count > 0)
  552. {
  553. for (int i = 0; i < kongLineList.Count; i++)
  554. {
  555. graphics.DrawLines(new Pen(color, 2), kongLineList[i].ToArray());
  556. }
  557. }
  558. if (kongPointList.Count > 1)
  559. {
  560. graphics.DrawLines(new Pen(color, 2), kongPointList.ToArray());
  561. }
  562. //手动画圆
  563. if(!rb_RightCircle.Checked)
  564. {
  565. if (ListRadius.Count > 0)
  566. {
  567. for (int i = 0; i < ListRadius.Count; i++)
  568. {
  569. Rectangle rect = new Rectangle(Convert.ToInt32(binaryClass.SetCenter.X - ListRadius[i]), Convert.ToInt32(binaryClass.SetCenter.Y - ListRadius[i]), Convert.ToInt32(ListRadius[i] * 2), Convert.ToInt32(ListRadius[i] * 2));
  570. graphics.DrawEllipse(new Pen(colorList[i], 2), rect);
  571. }
  572. }
  573. }
  574. if (DrwCircle && tempCirclePoint != null)
  575. {
  576. PointF diff = new PointF(tempCirclePoint.X - binaryClass.SetCenter.X, tempCirclePoint.Y - binaryClass.SetCenter.Y);
  577. double culength =Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
  578. Rectangle rect = new Rectangle(Convert.ToInt32(binaryClass.SetCenter.X - culength), Convert.ToInt32(binaryClass.SetCenter.Y - culength), Convert.ToInt32(culength * 2), Convert.ToInt32(culength * 2));
  579. graphics.DrawLines(new Pen(colorList[ListRadius.Count], 2), new PointF[] { new PointF(binaryClass.SetCenter.X, binaryClass.SetCenter.Y), tempCirclePoint });
  580. graphics.DrawEllipse(new Pen(colorList[ListRadius.Count], 2),rect);
  581. }
  582. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  583. Pen standardPen = new Pen(Color.FromArgb(0, 255, 0), 4);//基准线画笔
  584. Pen surfacePen = new Pen(Color.FromArgb(255, 0, 0), 4);//钢样表面画笔
  585. Pen completePen = new Pen(Color.FromArgb(0, 0, 255), 4);//完全脱碳层画笔
  586. Pen validPen = new Pen(Color.FromArgb(255, 255, 255), 4);//有效脱碳层画笔
  587. Pen partPen = new Pen(Color.FromArgb(0, 0, 0), 2);//部分脱碳层画笔
  588. Pen borderPen = new Pen(Color.Black);//填充点外圈
  589. SolidBrush insideBrush = new SolidBrush(Color.White);//填充点内圈
  590. Pen rectPen = new Pen(Color.Black);
  591. rectPen.DashStyle = DashStyle.Custom;
  592. float[] dashArray = { 2.0f, 3.0f };
  593. rectPen.DashPattern = dashArray;
  594. if (pointList1.Count > 0)
  595. {
  596. foreach (PointF[] pointFs in pointList1)
  597. {
  598. PointF[] pf = pointFs;
  599. if (pointFs.Count() > 1 && pointFs[0] != pointFs[1])
  600. {
  601. float lineLength = 12;
  602. double angle = Math.Round(BasicCalculationHelper.AngleText(pointFs[0], pointFs[1], new PointF(pointFs[0].X + lineLength, pointFs[0].Y)), 10);
  603. //判断第二个点相对于第一个点的象限
  604. int x2 = (int)(pointFs[1].X - pointFs[0].X);
  605. if (x2 == 0)
  606. x2 = 1;
  607. int y2 = (int)(pointFs[1].Y - pointFs[0].Y);
  608. if (y2 == 0)
  609. y2 = 1;
  610. int i2 = 0;
  611. if (x2 > 0 && y2 > 0) //第4象限
  612. {
  613. i2 = 4;
  614. }
  615. else if (x2 > 0 && y2 < 0) //第1象限
  616. {
  617. i2 = 1;
  618. }
  619. else if (x2 < 0 && y2 < 0) //第2象限
  620. {
  621. i2 = 2;
  622. }
  623. else if (x2 < 0 && y2 > 0) //第3象限
  624. {
  625. i2 = 3;
  626. }
  627. double sAngle1;
  628. double eAngle1;
  629. double sAngle2;
  630. double eAngle2;
  631. if (i2 == 1 || i2 == 2)
  632. {
  633. sAngle1 = 360 - angle;
  634. eAngle1 = 180 - angle;
  635. sAngle2 = 270 - angle;
  636. eAngle2 = 90 - angle;
  637. }
  638. else
  639. {
  640. sAngle1 = angle;
  641. eAngle1 = 180 + angle;
  642. sAngle2 = 270 + angle;
  643. eAngle2 = 90 + angle;
  644. }
  645. graphics.DrawLine(standardPen, pointFs[0], pointFs[1]);
  646. if (!double.IsNaN(sAngle1) && !double.IsNaN(eAngle1) && !double.IsNaN(sAngle2) && !double.IsNaN(eAngle2))
  647. {
  648. PointF assistPoint1 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[0].X + lineLength), pointFs[0].Y), pointFs[0], sAngle2);
  649. PointF assistPoint2 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[0].X + lineLength), pointFs[0].Y), pointFs[0], eAngle2);
  650. PointF assistPoint3 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[1].X + lineLength), pointFs[1].Y), pointFs[1], sAngle2);
  651. PointF assistPoint4 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[1].X + lineLength), pointFs[1].Y), pointFs[1], eAngle2);
  652. graphics.DrawLine(surfacePen, assistPoint1, assistPoint2);
  653. graphics.DrawLine(partPen, assistPoint3, assistPoint4);
  654. //PointF[] pf = pointFs;
  655. if (i2 == 1 || i2 == 2)
  656. {
  657. graphics.DrawString(Math.Round((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength), 2).ToString()+"μm", 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);
  658. }
  659. else
  660. {
  661. graphics.DrawString(Math.Round((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength), 2).ToString()+ "μm", 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);
  662. }
  663. }
  664. }
  665. }
  666. if (selected > -1)
  667. {
  668. int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1));
  669. int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1));
  670. graphics.DrawRectangle(borderPen, new Rectangle((int)(pointList1[selected][0].X - tempLess), (int)(pointList1[selected][0].Y - tempLess), tempPlus, tempPlus));
  671. graphics.FillRectangle(insideBrush, new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus));
  672. graphics.DrawRectangle(borderPen, new Rectangle((int)(pointList1[selected][1].X - tempLess), (int)(pointList1[selected][1].Y - tempLess), tempPlus, tempPlus));
  673. graphics.FillRectangle(insideBrush, new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus));
  674. graphics.DrawRectangle(rectPen, rectangleFList[selected].X, rectangleFList[selected].Y, rectangleFList[selected].Width, rectangleFList[selected].Height);
  675. }
  676. }
  677. standardPen.Dispose();
  678. surfacePen.Dispose();
  679. completePen.Dispose();
  680. validPen.Dispose();
  681. partPen.Dispose();
  682. borderPen.Dispose();
  683. insideBrush.Dispose();
  684. rectPen.Dispose();
  685. }
  686. /// <summary>
  687. /// 画同心圆
  688. /// </summary>
  689. /// <param name="sender"></param>
  690. /// <param name="e"></param>
  691. private void btnCircleDrawing_Click(object sender, EventArgs e)
  692. {
  693. if (this.listView1.Items.Count == 0)
  694. {
  695. return;
  696. }
  697. if (rb_RightCircle.Checked)
  698. {
  699. binaryClass.R = Convert.ToInt32(numericMax.Value);
  700. List<List<int>> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
  701. bindGrid(CircleDate);
  702. }
  703. else
  704. {
  705. List<List<int>> CircleDate = binaryClass.getCircleDate(0);
  706. bindGrid(CircleDate);
  707. }
  708. }
  709. /// <summary>
  710. /// 自动绘制
  711. /// </summary>
  712. /// <param name="sender"></param>
  713. /// <param name="e"></param>
  714. private void RightCircleCheckedChanged(object sender, EventArgs e)
  715. {
  716. if (this.listView1.Items.Count == 0)
  717. {
  718. return;
  719. }
  720. if (rb_RightCircle.Checked)
  721. ListRadius.Clear();
  722. ListRadiusColor.Clear();
  723. if (!rb_RightCircle.Checked)
  724. {
  725. List<List<int>> CircleDate = binaryClass.getCircleDate(0);
  726. bindGrid(CircleDate);
  727. }
  728. else
  729. {
  730. binaryClass.R = Convert.ToInt32(numericMax.Value);
  731. List<List<int>> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
  732. bindGrid(CircleDate);
  733. }
  734. }
  735. private void bindGrid(List<List<int>> dataList)
  736. {
  737. if (dataList==null)
  738. {
  739. return;
  740. }
  741. if (dataList.Count < 1)
  742. {
  743. return;
  744. }
  745. List<int> CircleDate = dataList[0];
  746. if (CircleDate.Count < 2)
  747. {
  748. return;
  749. }
  750. //半径
  751. numericMax.Value = binaryClass.R;
  752. //标尺 微米/像素
  753. double d = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
  754. //图像自动识别标尺x微米/像素
  755. //double q= binaryClass.GetRuler();
  756. //孔隙率信息列表
  757. dataGridView1.Rows.Clear();
  758. 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) };
  759. //最大面积
  760. double maxArea =(double)CircleDate[CircleDate.Count - 1];
  761. //孔隙率
  762. double k = (double)CircleDate[CircleDate.Count - 2] / maxArea;
  763. //孔面积
  764. double totalArea = CircleDate[CircleDate.Count - 2] * d * d;
  765. int newrow = dataGridView1.Rows.Add();
  766. dataGridView1.Rows[newrow].Cells[0].Value = "整体";
  767. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(totalArea, 2).ToString();
  768. dataGridView1.Rows[newrow].Cells[2].Value = Math.Round(maxArea * d * d, 2).ToString();
  769. dataGridView1.Rows[newrow].Cells[3].Value = Math.Round(k * 100, 2).ToString() + "%";
  770. int n = CircleDate.Count - 2;
  771. //方差
  772. double sd = 0;
  773. //标准方差
  774. double rsd = 0;
  775. //平均值
  776. double x = 0;
  777. //半径
  778. int r = binaryClass.r;
  779. List<double> tlist = new List<double>();
  780. double nArea = 0;
  781. Dictionary<int, Color> listradiuscolor = ListRadiusColor.OrderBy(c => c.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
  782. //圆环面积
  783. List<double> ListArea = new List<double>();
  784. for (int i = 0; i < listradiuscolor.Count; i++)
  785. {
  786. if (i == listradiuscolor.Count - 1)
  787. {
  788. double Ar = listradiuscolor.ElementAt(i - 1).Key ;
  789. double ar = maxArea - Math.PI * Ar * Ar;
  790. ListArea.Add(ar * d * d);
  791. }
  792. else if (i > 0)
  793. {
  794. double Ar = listradiuscolor.ElementAt(i - 1).Key * d;
  795. double Br = listradiuscolor.ElementAt(i).Key * d;
  796. double ar = Math.PI * (Br + Ar) * (Br - Ar);
  797. ListArea.Add(ar);
  798. }
  799. else if (i == 0)
  800. {
  801. double Ar = listradiuscolor.ElementAt(i).Key * d;
  802. double ar = Math.PI * Ar * Ar;
  803. ListArea.Add(ar);
  804. }
  805. }
  806. if (listradiuscolor.Count > 0)
  807. {
  808. for (int i = 0; i < n; i++)
  809. {
  810. double t = (double)CircleDate[i] / maxArea;
  811. if (i == n - 1)
  812. {
  813. t = (CircleDate[CircleDate.Count - 2] - nArea) / maxArea;
  814. }
  815. double curArea = (double)CircleDate[i] * d * d;
  816. if (i == n - 1)
  817. {
  818. curArea = (CircleDate[CircleDate.Count - 2] - nArea) * d * d;
  819. }
  820. newrow = dataGridView1.Rows.Add();
  821. if (i == n - 1)
  822. {
  823. dataGridView1.Rows[newrow].Cells[0].Value = "剩余部分";
  824. }
  825. else
  826. {
  827. dataGridView1.Rows[newrow].Cells[0].Value = "同心圆" + (i + 1).ToString();
  828. }
  829. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(curArea, 2).ToString();
  830. dataGridView1.Rows[newrow].Cells[2].Value = Math.Round(ListArea[i], 2).ToString();
  831. dataGridView1.Rows[newrow].Cells[3].Value = Math.Round(t * 100, 2).ToString() + "%";
  832. dataGridView1.Rows[newrow].DefaultCellStyle.ForeColor = listradiuscolor.ElementAt(i).Value;
  833. tlist.Add(t);
  834. nArea = nArea + (double)CircleDate[i];
  835. x = x + t;
  836. }
  837. }
  838. else
  839. {
  840. for (int i = 0; i < n; i++)
  841. {
  842. double t = 0;
  843. double ar = Math.PI * r * r * ((i + 1) * (i + 1) - i * i);
  844. if (i == n - 1)
  845. {
  846. ar = maxArea - Math.PI * i * r * i * r;
  847. }
  848. t = (double)CircleDate[i] / maxArea;
  849. if (i == n - 1)
  850. {
  851. t = (CircleDate[CircleDate.Count - 2] - nArea) / maxArea;
  852. }
  853. double curArea = (double)CircleDate[i] * d * d;
  854. if (i == n - 1)
  855. {
  856. curArea = (CircleDate[CircleDate.Count - 2] - nArea) * d * d;
  857. }
  858. newrow = dataGridView1.Rows.Add();
  859. if (i == n - 1)
  860. {
  861. dataGridView1.Rows[newrow].Cells[0].Value = "剩余部分";
  862. }
  863. else
  864. {
  865. dataGridView1.Rows[newrow].Cells[0].Value = "同心圆" + (i + 1).ToString();
  866. }
  867. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(curArea, 2).ToString();
  868. dataGridView1.Rows[newrow].Cells[2].Value = Math.Round(ar * d * d, 2).ToString();
  869. dataGridView1.Rows[newrow].Cells[3].Value = Math.Round(t * 100, 2).ToString() + "%";
  870. dataGridView1.Rows[newrow].DefaultCellStyle.ForeColor = colorList[i];
  871. tlist.Add(t);
  872. nArea = nArea + (double)CircleDate[i];
  873. x = x + t;
  874. }
  875. }
  876. x = x / n;
  877. for (int i = 0; i < n; i++)
  878. {
  879. double t = tlist[i];
  880. sd = sd + (t - x) * (t - x);
  881. }
  882. sd = Math.Sqrt(sd / (n - 1));
  883. rsd = sd / x;
  884. newrow = dataGridView1.Rows.Add();
  885. dataGridView1.Rows[newrow].Cells[0].Value = "同心圆孔隙率RSD";
  886. dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(rsd * 100, 0).ToString() + "%";
  887. dataGridView1.Rows[newrow].Cells[2].Value ="";
  888. dataGridView1.Rows[newrow].Cells[3].Value = "";
  889. //孔隙信息列表
  890. dataGridView4.Rows.Clear();
  891. for (int i = 1; i < dataList.Count-1; i++)
  892. {
  893. if(dataList[i].Count<6)
  894. { continue; }
  895. int newPo = dataGridView4.Rows.Add();
  896. //序号
  897. dataGridView4.Rows[newPo].Cells[0].Value = (newPo+1).ToString();
  898. //X
  899. dataGridView4.Rows[newPo].Cells[1].Value = dataList[i][0];
  900. //Y
  901. dataGridView4.Rows[newPo].Cells[2].Value = dataList[i][1];
  902. //长
  903. dataGridView4.Rows[newPo].Cells[3].Value =Math.Round( Convert.ToDouble( dataList[i][2])*d,2).ToString();
  904. //宽
  905. dataGridView4.Rows[newPo].Cells[4].Value = Math.Round(Convert.ToDouble(dataList[i][3]) * d, 2).ToString();
  906. //面积
  907. dataGridView4.Rows[newPo].Cells[5].Value = Math.Round(Convert.ToDouble(dataList[i][4]) * d*d, 2).ToString();
  908. //标识
  909. dataGridView4.Rows[newPo].Cells[6].Value = dataList[i][5];
  910. }
  911. }
  912. #region 手动干预
  913. /// <summary>
  914. /// 修改轮廓
  915. /// </summary>
  916. /// <param name="sender"></param>
  917. /// <param name="e"></param>
  918. private void btnLunkuo_Click(object sender, EventArgs e)
  919. {
  920. isLunKuo = true;
  921. isAddKong = false;
  922. isDelKong = false;
  923. isDrwKuo = false;
  924. DrwCenter = false;
  925. DrwCircle = false;
  926. operationK = -1;
  927. }
  928. /// <summary>
  929. /// 添加孔隙
  930. /// </summary>
  931. /// <param name="sender"></param>
  932. /// <param name="e"></param>
  933. private void btnAddKong_Click(object sender, EventArgs e)
  934. {
  935. isLunKuo = false;
  936. isAddKong = true;
  937. isDelKong = false;
  938. isDrwKuo = false;
  939. DrwCenter = false;
  940. DrwCircle = false;
  941. operationK = -1;
  942. }
  943. /// <summary>
  944. /// 删除孔隙
  945. /// </summary>
  946. /// <param name="sender"></param>
  947. /// <param name="e"></param>
  948. private void btnDelKong_Click(object sender, EventArgs e)
  949. {
  950. isLunKuo = false;
  951. isAddKong = false;
  952. isDelKong = true;
  953. isDrwKuo = false;
  954. DrwCenter = false;
  955. DrwCircle = false;
  956. operationK = -1;
  957. }
  958. /// <summary>
  959. /// 绘制轮廓
  960. /// </summary>
  961. /// <param name="sender"></param>
  962. /// <param name="e"></param>
  963. private void btnDrwKuo_Click(object sender, EventArgs e)
  964. {
  965. isLunKuo = false;
  966. isAddKong = false;
  967. isDelKong = false;
  968. isDrwKuo = true;
  969. DrwDone = true;
  970. DrwCenter = false;
  971. DrwCircle = false;
  972. operationK = -1;
  973. lineList.Clear();
  974. }
  975. /// <summary>
  976. /// 设置圆心
  977. /// </summary>
  978. /// <param name="sender"></param>
  979. /// <param name="e"></param>
  980. private void btnbtnSetCenter_Click(object sender, EventArgs e)
  981. {
  982. isLunKuo = false;
  983. isAddKong = false;
  984. isDelKong = false;
  985. isDrwKuo = false;
  986. DrwDone = false;
  987. DrwCenter = true;
  988. DrwCircle = false;
  989. operationK = -1;
  990. }
  991. /// <summary>
  992. /// 画圆
  993. /// </summary>
  994. /// <param name="sender"></param>
  995. /// <param name="e"></param>
  996. private void btnDrwCircle_Click(object sender, EventArgs e)
  997. {
  998. if (rb_RightCircle.Checked)
  999. {
  1000. MessageBox.Show("请取消自动绘制!");
  1001. }
  1002. else
  1003. {
  1004. if (ListRadius.Count == 5)
  1005. {
  1006. MessageBox.Show("同心圆最多绘制5个!");
  1007. return;
  1008. }
  1009. isLunKuo = false;
  1010. isAddKong = false;
  1011. isDelKong = false;
  1012. isDrwKuo = false;
  1013. DrwDone = false;
  1014. DrwCenter = false;
  1015. DrwCircle = true;
  1016. operationK = -1;
  1017. }
  1018. }
  1019. /// <summary>
  1020. /// 重新统计
  1021. /// </summary>
  1022. /// <param name="sender"></param>
  1023. /// <param name="e"></param>
  1024. private void btnReDrw_Click(object sender, EventArgs e)
  1025. {
  1026. Recalculation();
  1027. }
  1028. private void Recalculation()
  1029. {
  1030. if (pointList.Count > 1)
  1031. {
  1032. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  1033. lineList.Add(pointFs);
  1034. pointList.Clear();
  1035. }
  1036. if (kongPointList.Count > 1)
  1037. {
  1038. List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
  1039. kongLineList.Add(pointFs);
  1040. kongPointList.Clear();
  1041. }
  1042. if (delPointList.Count > 1)
  1043. {
  1044. }
  1045. List<List<int>> CircleDate = new List<List<int>>();
  1046. if (DrwDone)
  1047. {
  1048. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  1049. }
  1050. else
  1051. {
  1052. CircleDate = binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  1053. }
  1054. bindGrid(CircleDate);
  1055. isLunKuo = false;
  1056. isAddKong = false;
  1057. isDelKong = false;
  1058. isDrwKuo = false;
  1059. DrwCenter = false;
  1060. DrwCircle = false;
  1061. operationK = -1;
  1062. }
  1063. /// <summary>
  1064. /// 计算外接矩形
  1065. /// </summary>
  1066. private RectangleF CalcOtherRectangle(PointF p1, PointF p2)
  1067. {
  1068. RectangleF rectangleF = new RectangleF();
  1069. if (p1.X > p2.X)
  1070. rectangleF.X = p2.X;
  1071. else
  1072. rectangleF.X = p1.X;
  1073. if (p1.Y > p2.Y)
  1074. rectangleF.Y = p2.Y;
  1075. else
  1076. rectangleF.Y = p1.Y;
  1077. rectangleF.Width = Math.Abs(p2.X - p1.X);
  1078. rectangleF.Height = Math.Abs(p2.Y - p1.Y);
  1079. int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1080. int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1081. return new RectangleF(rectangleF.X - tempLess, rectangleF.Y - tempLess, rectangleF.Width + tempPlus, rectangleF.Height + tempPlus);
  1082. }
  1083. /// <summary>
  1084. /// 移动
  1085. /// </summary>
  1086. /// <param name="sender"></param>
  1087. /// <param name="e"></param>
  1088. private void onMouseMove(object sender, MouseEventArgs e)
  1089. {
  1090. if (operationK >= 0 && e.Button == MouseButtons.Left)
  1091. {
  1092. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1093. if (operationK == 0)
  1094. {
  1095. this.endPoint = movePoint;
  1096. pointList1[lineCount][1] = endPoint;
  1097. }
  1098. else if (operationK == 1 && selected > -1 && canMove)
  1099. {
  1100. float dx = movePoint.X - lastPoint.X;
  1101. float dy = movePoint.Y - lastPoint.Y;
  1102. float dxTemp = movePoint.X - tempPoint.X;
  1103. float dyTemp = movePoint.Y - tempPoint.Y;
  1104. lastPoint.X = movePoint.X;
  1105. lastPoint.Y = movePoint.Y;
  1106. if (movePointIndex == -1)
  1107. {
  1108. RectangleF rectangleF = new RectangleF();
  1109. rectangleF = rectangleFList[selected];
  1110. if (rectangleF.Contains(movePoint))
  1111. this.documentWorkspace.panel.Cursor = Cursors.SizeAll;
  1112. rectangleF.X += dx;
  1113. rectangleF.Y += dy;
  1114. rectangleFList[selected] = rectangleF;
  1115. for (int i = 0; i < pointList1[selected].Count(); i++)
  1116. {
  1117. if (pointList1[selected][i] == PointF.Empty)
  1118. continue;
  1119. pointList1[selected][i].X += dx;
  1120. pointList1[selected][i].Y += dy;
  1121. }
  1122. }
  1123. else if (movePointIndex == 0)
  1124. {
  1125. pointList1[selected][0].X += dx;
  1126. pointList1[selected][0].Y += dy;
  1127. rectangleFList[selected] = CalcOtherRectangle(pointList1[selected][0], pointList1[selected][1]);
  1128. }
  1129. else if (movePointIndex == 1)
  1130. {
  1131. pointList1[selected][1].X += dx;
  1132. pointList1[selected][1].Y += dy;
  1133. rectangleFList[selected] = CalcOtherRectangle(pointList1[selected][0], pointList1[selected][1]);
  1134. }
  1135. }
  1136. this.documentWorkspace.Refresh();
  1137. }
  1138. else
  1139. {
  1140. if (isLunKuo)
  1141. {
  1142. int cou = pointList.Count;
  1143. if (cou > 0)
  1144. { // 换算后的点
  1145. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1146. pointList[cou - 1] = movePoint;
  1147. }
  1148. }
  1149. if (isAddKong)
  1150. {
  1151. if (kongPointList.Count > 0)
  1152. { // 换算后的点
  1153. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1154. kongPointList[kongPointList.Count - 1] = movePoint;
  1155. }
  1156. }
  1157. if (isDelKong)
  1158. {
  1159. //if (delPointList.Count > 0)
  1160. //{ // 换算后的点
  1161. // PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1162. // delPointList[delPointList.Count - 1] = movePoint;
  1163. //}
  1164. }
  1165. if (isDrwKuo)
  1166. {
  1167. int cou = pointList.Count;
  1168. if (cou > 0)
  1169. { // 换算后的点
  1170. PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1171. pointList[cou - 1] = movePoint;
  1172. }
  1173. }
  1174. if (DrwCircle)
  1175. {
  1176. tempCirclePoint = this.documentWorkspace.GetScalePoint(e.Location);
  1177. }
  1178. }
  1179. this.documentWorkspace.Refresh();
  1180. // }
  1181. }
  1182. /// <summary>
  1183. /// 鼠标抬起
  1184. /// </summary>
  1185. /// <param name="sender"></param>
  1186. /// <param name="e"></param>
  1187. private void OnMouseUp(object sender, MouseEventArgs e)
  1188. {
  1189. if (e.Button == MouseButtons.Left)
  1190. {
  1191. if (operationK == 0)
  1192. {
  1193. if (startPoint != null && endPoint != null && startPoint != endPoint)
  1194. {
  1195. RectangleF rectangleF = CalcOtherRectangle(pointList1[lineCount][0], pointList1[lineCount][1]);
  1196. rectangleFList.Add(rectangleF);
  1197. lineCount++;
  1198. // AddContentToDataGridView();
  1199. }
  1200. else
  1201. {
  1202. if (pointList1.Count > 0)
  1203. {
  1204. pointList1.RemoveAt(pointList1.Count - 1);
  1205. this.documentWorkspace.Refresh();
  1206. return;
  1207. }
  1208. }
  1209. lastPoint.X = endPoint.X;
  1210. lastPoint.Y = endPoint.Y;
  1211. tempPoint.X = endPoint.X;
  1212. tempPoint.Y = endPoint.Y;
  1213. for (int i = rectangleFList.Count - 1; i >= 0; i--)
  1214. {
  1215. if (rectangleFList[i].Contains(endPoint))
  1216. {
  1217. selected = i;
  1218. canMove = true;
  1219. }
  1220. }
  1221. if (selected > -1 && pointList1 != null && pointList1.Count > 0)
  1222. {
  1223. int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1224. int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1225. if (new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus).Contains(endPoint))
  1226. movePointIndex = 0;
  1227. else if (new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus).Contains(endPoint))
  1228. movePointIndex = 1;
  1229. else
  1230. movePointIndex = -1;
  1231. this.documentWorkspace.Refresh();
  1232. }
  1233. operationK = 1;
  1234. if (canMove)
  1235. return;
  1236. selected = -1;
  1237. //this.documentWorkspace.Refresh();
  1238. }
  1239. else if (operationK == 1)
  1240. {
  1241. canMove = false;
  1242. movePointIndex = -1;
  1243. // RefreshDataGridView1();
  1244. }
  1245. else if (isAddKong && kongLineList.Count > 0)
  1246. {
  1247. //if (kongLineList[kongLineList.Count - 1].Count > 3)
  1248. //{
  1249. // isAddKong = false;
  1250. // List<List<int>> CircleDate = binaryClass.DeleteCrystal(delPointList, kongLineList);
  1251. // bindGrid(CircleDate);
  1252. //}
  1253. //else
  1254. //{
  1255. // kongLineList.RemoveAt(kongLineList.Count - 1);
  1256. //}
  1257. }
  1258. }
  1259. if (operationK >= 0 && e.Button == MouseButtons.Left)
  1260. {
  1261. PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
  1262. if (operationK == 0)
  1263. {
  1264. //startPoint = endPoint = downPoint;
  1265. pointList1[lineCount - 1][1] = downPoint;
  1266. }
  1267. else if (operationK == 1)
  1268. {
  1269. lastPoint.X = downPoint.X;
  1270. lastPoint.Y = downPoint.Y;
  1271. tempPoint.X = downPoint.X;
  1272. tempPoint.Y = downPoint.Y;
  1273. for (int i = rectangleFList.Count - 1; i >= 0; i--)
  1274. {
  1275. if (rectangleFList[i].Contains(downPoint))
  1276. {
  1277. selected = i;
  1278. canMove = true;
  1279. }
  1280. }
  1281. if (selected > -1 && pointList1 != null && pointList1.Count > 0)
  1282. {
  1283. int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1284. int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1285. if (new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
  1286. movePointIndex = 0;
  1287. else if (new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
  1288. movePointIndex = 1;
  1289. else
  1290. movePointIndex = -1;
  1291. this.documentWorkspace.Refresh();
  1292. }
  1293. if (canMove)
  1294. return;
  1295. selected = -1;
  1296. }
  1297. }
  1298. }
  1299. /// <summary>
  1300. /// 鼠标按下
  1301. /// </summary>
  1302. /// <param name="drawArea"></param>
  1303. /// <param name="e"></param>
  1304. private void OnMouseDown(object sender, MouseEventArgs e)
  1305. {
  1306. PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
  1307. if (operationK >= 0 && e.Button == MouseButtons.Left)
  1308. {
  1309. if (operationK == 0)
  1310. {
  1311. startPoint = endPoint = downPoint;
  1312. nowLine = new PointF[4];
  1313. nowLine[0] = startPoint;
  1314. nowLine[1] = endPoint;
  1315. pointList1.Add(nowLine);
  1316. }
  1317. else if (operationK == 1)
  1318. {
  1319. lastPoint.X = downPoint.X;
  1320. lastPoint.Y = downPoint.Y;
  1321. tempPoint.X = downPoint.X;
  1322. tempPoint.Y = downPoint.Y;
  1323. for (int i = rectangleFList.Count - 1; i >= 0; i--)
  1324. {
  1325. if (rectangleFList[i].Contains(downPoint))
  1326. {
  1327. selected = i;
  1328. canMove = true;
  1329. }
  1330. }
  1331. if (selected > -1 && pointList1 != null && pointList1.Count > 0)
  1332. {
  1333. int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1334. int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
  1335. if (new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
  1336. movePointIndex = 0;
  1337. else if (new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
  1338. movePointIndex = 1;
  1339. else
  1340. movePointIndex = -1;
  1341. this.documentWorkspace.Refresh();
  1342. }
  1343. if (canMove)
  1344. return;
  1345. selected = -1;
  1346. }
  1347. }
  1348. else if (e.Button == MouseButtons.Left)
  1349. {
  1350. if (isLunKuo)
  1351. {
  1352. if (pointList.Count == 0)
  1353. {
  1354. pointList.Add(downPoint);
  1355. pointList.Add(downPoint);
  1356. }
  1357. else
  1358. {
  1359. pointList.Add(downPoint);
  1360. }
  1361. }
  1362. else if (isAddKong)
  1363. {
  1364. if (kongPointList.Count == 0)
  1365. {
  1366. kongPointList.Add(downPoint);
  1367. kongPointList.Add(downPoint);
  1368. }
  1369. else
  1370. {
  1371. kongPointList.Add(downPoint);
  1372. }
  1373. }
  1374. else if (isDelKong)
  1375. {
  1376. //if (delPointList.Count == 0)
  1377. //{
  1378. // delPointList.Add(downPoint);
  1379. // delPointList.Add(downPoint);
  1380. //}
  1381. //else
  1382. //{
  1383. // delPointList.Add(downPoint);
  1384. //}
  1385. delPointList.Add(downPoint);
  1386. List<List<int>> CircleDate = new List<List<int>>();
  1387. if (DrwDone)
  1388. {
  1389. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  1390. }
  1391. else
  1392. {
  1393. binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  1394. }
  1395. bindGrid(CircleDate);
  1396. }
  1397. else if (isDrwKuo)
  1398. {
  1399. if (pointList.Count == 0)
  1400. {
  1401. pointList.Add(downPoint);
  1402. pointList.Add(downPoint);
  1403. }
  1404. else
  1405. {
  1406. pointList.Add(downPoint);
  1407. }
  1408. }
  1409. else if (DrwCenter)
  1410. {
  1411. binaryClass.SetCenter = new OpenCvSharp.Point() { X =Convert.ToInt32( downPoint.X), Y = Convert.ToInt32(downPoint.Y) };
  1412. binaryClass.R = Convert.ToInt32(numericMax.Value);
  1413. if (!rb_RightCircle.Checked)
  1414. {
  1415. List<List<int>> CircleDate = binaryClass.getCircleDate(0);
  1416. bindGrid(CircleDate);
  1417. DrwCenter = false;
  1418. }
  1419. else
  1420. {
  1421. List<List<int>> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
  1422. bindGrid(CircleDate);
  1423. DrwCenter = false;
  1424. }
  1425. }
  1426. else if (DrwCircle)
  1427. {
  1428. //binaryClass.SetCenter = new OpenCvSharp.Point() { X = Convert.ToInt32(downPoint.X), Y = Convert.ToInt32(downPoint.Y) };
  1429. //binaryClass.R = Convert.ToInt32(numericMax.Value);
  1430. //if (rb_RightCircle.Checked)
  1431. //{
  1432. // List<List<int>> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
  1433. // bindGrid(CircleDate);
  1434. // DrwCircle = false;
  1435. //}
  1436. float x = Math.Abs(downPoint.X - binaryClass.SetCenter.X);
  1437. float y = Math.Abs(downPoint.Y - binaryClass.SetCenter.Y);
  1438. int radius = (int)Math.Sqrt(x * x + y * y);
  1439. ListRadius.Add(radius);
  1440. ListRadiusColor.Add(radius, colorList[ListRadius.Count-1]);
  1441. DrwCircle = false;
  1442. if (ListRadiusColor.Count > 1)
  1443. {
  1444. List<List<int>> CircleDate = binaryClass.getCircleDate(ListRadiusColor);
  1445. bindGrid(CircleDate);
  1446. }
  1447. }
  1448. }
  1449. if (e.Button == MouseButtons.Right)
  1450. {
  1451. if (isLunKuo)
  1452. {
  1453. if (pointList.Count > 2)
  1454. {
  1455. pointList.RemoveAt(pointList.Count - 1);
  1456. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  1457. lineList.Add(pointFs);
  1458. }
  1459. pointList.Clear();
  1460. Recalculation();
  1461. lineList.Clear();
  1462. }
  1463. if (isAddKong)
  1464. {
  1465. if (kongPointList.Count > 2)
  1466. {
  1467. kongPointList.RemoveAt(kongPointList.Count - 1);
  1468. List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
  1469. kongLineList.Add(pointFs);
  1470. }
  1471. kongPointList.Clear();
  1472. }
  1473. if (isDelKong)
  1474. {
  1475. //if (delPointList.Count > 2)
  1476. //{
  1477. // delPointList.RemoveAt(delPointList.Count - 1);
  1478. // List<PointF> pointFs = new List<PointF>(delPointList.ToArray());
  1479. // delLineList.Add(pointFs);
  1480. //}
  1481. //delPointList.Clear();
  1482. }
  1483. if (isDrwKuo)
  1484. {
  1485. if (pointList.Count > 2)
  1486. {
  1487. pointList.RemoveAt(pointList.Count - 1);
  1488. List<PointF> pointFs = new List<PointF>(pointList.ToArray());
  1489. lineList.Add(pointFs);
  1490. }
  1491. pointList.Clear();
  1492. Recalculation();
  1493. }
  1494. isLunKuo = false;
  1495. isAddKong = false;
  1496. isDelKong = false;
  1497. isDrwKuo = false;
  1498. }
  1499. }
  1500. /// <summary>
  1501. /// Ctrl+z
  1502. /// </summary>
  1503. private void OnKeyDown(object sender, KeyEventArgs e)
  1504. {
  1505. if (e.Control && e.KeyCode == Keys.Z)
  1506. {
  1507. if (isLunKuo)
  1508. {
  1509. int cou = pointList.Count;
  1510. if (cou > 1)
  1511. {
  1512. if (cou == 2)
  1513. {
  1514. pointList.Clear();
  1515. }
  1516. else
  1517. {
  1518. pointList.RemoveAt(cou - 1);
  1519. }
  1520. }
  1521. else if (lineList.Count > 0)
  1522. {
  1523. pointList = lineList[lineList.Count - 1];
  1524. lineList.RemoveAt(lineList.Count - 1);
  1525. pointList.RemoveAt(pointList.Count - 1);
  1526. }
  1527. }
  1528. if (isAddKong)
  1529. {
  1530. int cou =kongPointList.Count;
  1531. if (cou > 1)
  1532. {
  1533. if (cou == 2)
  1534. {
  1535. kongPointList.Clear();
  1536. }
  1537. else
  1538. {
  1539. kongPointList.RemoveAt(cou - 1);
  1540. }
  1541. }
  1542. else if (kongLineList.Count > 0)
  1543. {
  1544. kongPointList = kongLineList[kongLineList.Count - 1];
  1545. kongLineList.RemoveAt(kongLineList.Count - 1);
  1546. kongPointList.RemoveAt(kongPointList.Count - 1);
  1547. }
  1548. }
  1549. if (isDelKong)
  1550. {
  1551. int cou = delPointList.Count;
  1552. if (cou > 0)
  1553. {
  1554. delPointList.RemoveAt(cou - 1);
  1555. }
  1556. List<List<int>> CircleDate = new List<List<int>>();
  1557. if (DrwDone)
  1558. {
  1559. CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
  1560. }
  1561. else
  1562. {
  1563. binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
  1564. }
  1565. bindGrid(CircleDate);
  1566. //if (cou > 1)
  1567. //{
  1568. // if (cou == 2)
  1569. // {
  1570. // delPointList.Clear();
  1571. // }
  1572. // else
  1573. // {
  1574. // delPointList.RemoveAt(cou - 1);
  1575. // }
  1576. //}
  1577. //else if (delLineList.Count > 0)
  1578. //{
  1579. // delPointList = delLineList[delLineList.Count - 1];
  1580. // delLineList.RemoveAt(delLineList.Count - 1);
  1581. // delPointList.RemoveAt(delPointList.Count - 1);
  1582. //}
  1583. }
  1584. this.documentWorkspace.Refresh();
  1585. }
  1586. else if ( e.KeyCode == Keys.Delete)
  1587. {
  1588. if (selected > -1)
  1589. {
  1590. DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.Areyousuretodmeasurementline.text") + "?", PdnResources.GetString("Menu.alert.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  1591. if (dr == DialogResult.OK)
  1592. {
  1593. this.pointList1.Remove(this.pointList1[selected]);
  1594. this.rectangleFList.Remove(this.rectangleFList[selected]);
  1595. selected = -1;
  1596. lineCount--;
  1597. this.documentWorkspace.Refresh();
  1598. }
  1599. }
  1600. }
  1601. }
  1602. #endregion
  1603. #region 辅助测量
  1604. /// <summary>
  1605. /// 添加测量线
  1606. /// </summary>
  1607. /// <param name="sender"></param>
  1608. /// <param name="e"></param>
  1609. private void btnAddCeliang_Click(object sender, EventArgs e)
  1610. {
  1611. operationK = 0;
  1612. selected = -1;
  1613. this.documentWorkspace.Refresh();
  1614. }
  1615. /// <summary>
  1616. /// 选择测量线
  1617. /// </summary>
  1618. /// <param name="sender"></param>
  1619. /// <param name="e"></param>
  1620. private void btnSelCeliang_Click(object sender, EventArgs e)
  1621. {
  1622. operationK = 1;
  1623. }
  1624. /// <summary>
  1625. /// 删除测量线
  1626. /// </summary>
  1627. /// <param name="sender"></param>
  1628. /// <param name="e"></param>
  1629. private void btnDelCeliang_Click(object sender, EventArgs e)
  1630. {
  1631. if (selected > -1)
  1632. {
  1633. DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.Areyousuretodmeasurementline.text") + "?", PdnResources.GetString("Menu.alert.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  1634. if (dr == DialogResult.OK)
  1635. {
  1636. this.pointList1.Remove(this.pointList1[selected]);
  1637. this.rectangleFList.Remove(this.rectangleFList[selected]);
  1638. selected = -1;
  1639. lineCount--;
  1640. this.documentWorkspace.Refresh();
  1641. }
  1642. }
  1643. else
  1644. MessageBox.Show("请先选择测量线");
  1645. }
  1646. #endregion
  1647. /// <summary>
  1648. /// 导出孔隙信息
  1649. /// </summary>
  1650. /// <param name="sender"></param>
  1651. /// <param name="e"></param>
  1652. private void button14_Click(object sender, EventArgs e)
  1653. {
  1654. SaveFileDialog exe = new SaveFileDialog();
  1655. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  1656. exe.FilterIndex = 0;
  1657. exe.RestoreDirectory = true;
  1658. //exe.CreatePrompt = true;
  1659. exe.Title = "Export Excel File";
  1660. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1661. exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") +"孔隙信息";
  1662. DialogResult dr = exe.ShowDialog();
  1663. if (dr != DialogResult.OK)
  1664. {
  1665. return;
  1666. }
  1667. DataTable dtb = new DataTable();
  1668. foreach (DataGridViewColumn c in this.dataGridView4.Columns)
  1669. {
  1670. dtb.Columns.Add(c.HeaderText);
  1671. }
  1672. for (int r = 0; r < this.dataGridView4.Rows.Count; r++)
  1673. {
  1674. DataRow dataRow = dtb.NewRow();
  1675. for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count; c++)
  1676. {
  1677. dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
  1678. }
  1679. dtb.Rows.Add(dataRow);
  1680. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1681. }
  1682. List<DataTable> list = new List<DataTable>();
  1683. list.Add(dtb);
  1684. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  1685. }
  1686. /// <summary>
  1687. /// 不可点击空白区域
  1688. /// </summary>
  1689. /// <param name="sender"></param>
  1690. /// <param name="e"></param>
  1691. private void listView1_MouseUp(object sender, MouseEventArgs e)
  1692. {
  1693. if (e.Button == MouseButtons.Left)
  1694. {
  1695. if (listView1.SelectedItems.Count > 0)
  1696. {
  1697. }
  1698. else if (listView1.SelectedItems.Count <= 0)//点击空白区
  1699. {
  1700. if (this.listView1.FocusedItem != null)
  1701. {
  1702. ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);
  1703. if (item == null)
  1704. {
  1705. this.listView1.FocusedItem.Selected = true;
  1706. }
  1707. }
  1708. }
  1709. }
  1710. }
  1711. /// <summary>
  1712. /// 生成孔隙率
  1713. /// </summary>
  1714. /// <param name="sender"></param>
  1715. /// <param name="e"></param>
  1716. private void button13_Click(object sender, EventArgs e)
  1717. {
  1718. //string path = string.Empty;
  1719. //System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
  1720. //fbd.Description = "请选择文件夹";
  1721. //String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
  1722. //if (!string.IsNullOrEmpty(str))
  1723. //{
  1724. // fbd.SelectedPath = str;
  1725. //}
  1726. //if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1727. //{
  1728. // path = fbd.SelectedPath;
  1729. // PrintReport(path);
  1730. // Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  1731. // cfa.AppSettings.Settings["FolderSelectedPath"].Value = path;
  1732. // cfa.Save();
  1733. // System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  1734. //}
  1735. string name = this.listView1.FocusedItem.Name.Split('.')[0] + "-";
  1736. SaveFileDialog exe = new SaveFileDialog();
  1737. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  1738. exe.FilterIndex = 0;
  1739. exe.RestoreDirectory = true;
  1740. //exe.CreatePrompt = true;
  1741. exe.Title = "Export Excel File";
  1742. // exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1743. exe.FileName = name;
  1744. //String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
  1745. DialogResult dr = exe.ShowDialog();
  1746. if (dr != DialogResult.OK)
  1747. {
  1748. return;
  1749. }
  1750. DataTable dtb = new DataTable();
  1751. foreach (DataGridViewColumn c in this.dataGridView1.Columns)
  1752. {
  1753. dtb.Columns.Add(c.HeaderText);
  1754. }
  1755. for (int r = 0; r < this.dataGridView1.Rows.Count; r++)
  1756. {
  1757. DataRow dataRow = dtb.NewRow();
  1758. for (int c = 0; c < this.dataGridView1.Rows[r].Cells.Count; c++)
  1759. {
  1760. dataRow[this.dataGridView1.Columns[c].HeaderText] = (string)this.dataGridView1.Rows[r].Cells[c].Value.ToString();
  1761. }
  1762. dtb.Rows.Add(dataRow);
  1763. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1764. }
  1765. int index = exe.FileName.LastIndexOf('\\');
  1766. List<DataTable> list = new List<DataTable>();
  1767. list.Add(dtb);
  1768. DataTable dtb1 = new DataTable();
  1769. for (int r = 0; r < this.dataGridView4.Columns.Count-1; r++)
  1770. {
  1771. DataGridViewColumn c = dataGridView4.Columns[r];
  1772. dtb1.Columns.Add(c.HeaderText);
  1773. }
  1774. for (int r = 0; r < this.dataGridView4.Rows.Count-1; r++)
  1775. {
  1776. DataRow dataRow = dtb1.NewRow();
  1777. for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count-1; c++)
  1778. {
  1779. dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
  1780. }
  1781. dtb1.Rows.Add(dataRow);
  1782. //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  1783. }
  1784. list.Add(dtb1);
  1785. //保存处理后的图片
  1786. double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
  1787. List<Bitmap> tempBit = new List<Bitmap>();
  1788. Bitmap originalBit = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  1789. originalBit.Tag = pantographRatio;
  1790. tempBit.Add(originalBit);
  1791. Bitmap processedBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
  1792. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  1793. Graphics graphics = Graphics.FromImage(newBit);
  1794. graphics.DrawImage(processedBit, new PointF(0, 0));
  1795. Draw(graphics);
  1796. newBit.Tag = pantographRatio;
  1797. tempBit.Add(newBit);
  1798. string GR_Path = exe.FileName.Remove(index) + "\\" + name + ".jpg";
  1799. newBit.Save(GR_Path);
  1800. List<string> pics = new List<string>() { GR_Path + ",10,150," + newBit.Width / 2 + "," + newBit.Height / 2 };
  1801. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true, null, null, pics);
  1802. }
  1803. /// <summary>
  1804. /// 导出报告
  1805. /// </summary>
  1806. private void PrintReport(string savePath)
  1807. {
  1808. List<List<string>> analysisContent = new List<List<string>>();
  1809. for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
  1810. {
  1811. List<string> content1 = new List<string>();
  1812. if (i == 0)
  1813. {
  1814. content1.Add(this.dataGridView1.Columns[0].HeaderText);
  1815. content1.Add(this.dataGridView1.Columns[1].HeaderText);
  1816. content1.Add(this.dataGridView1.Columns[2].HeaderText);
  1817. analysisContent.Add(content1);
  1818. }
  1819. content1 = new List<string>();
  1820. content1.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
  1821. content1.Add(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
  1822. content1.Add(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
  1823. analysisContent.Add(content1);
  1824. }
  1825. List<string> content2 = new List<string>();
  1826. analysisContent.Add(content2);
  1827. for (int i = 0; i < this.dataGridView4.Rows.Count; i++)
  1828. {
  1829. List<string> content1 = new List<string>();
  1830. if (i == 0)
  1831. {
  1832. content1.Add(this.dataGridView4.Columns[0].HeaderText);
  1833. content1.Add(this.dataGridView4.Columns[1].HeaderText);
  1834. content1.Add(this.dataGridView4.Columns[2].HeaderText);
  1835. content1.Add(this.dataGridView4.Columns[3].HeaderText);
  1836. content1.Add(this.dataGridView4.Columns[4].HeaderText);
  1837. content1.Add(this.dataGridView4.Columns[5].HeaderText);
  1838. content1.Add(this.dataGridView4.Columns[6].HeaderText);
  1839. analysisContent.Add(content1);
  1840. }
  1841. content1 = new List<string>();
  1842. content1.Add(this.dataGridView4.Rows[i].Cells[0].Value.ToString());
  1843. content1.Add(this.dataGridView4.Rows[i].Cells[1].Value.ToString());
  1844. content1.Add(this.dataGridView4.Rows[i].Cells[2].Value.ToString());
  1845. content1.Add(this.dataGridView4.Rows[i].Cells[3].Value.ToString());
  1846. content1.Add(this.dataGridView4.Rows[i].Cells[4].Value.ToString());
  1847. content1.Add(this.dataGridView4.Rows[i].Cells[5].Value.ToString());
  1848. content1.Add(this.dataGridView4.Rows[i].Cells[6].Value.ToString());
  1849. analysisContent.Add(content1);
  1850. }
  1851. //保存处理后的图片
  1852. double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
  1853. List<Bitmap> tempBit = new List<Bitmap>();
  1854. Bitmap originalBit = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  1855. originalBit.Tag = pantographRatio;
  1856. tempBit.Add(originalBit);
  1857. Bitmap processedBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
  1858. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  1859. Graphics graphics = Graphics.FromImage(newBit);
  1860. graphics.DrawImage(processedBit, new PointF(0, 0));
  1861. Draw(graphics);
  1862. newBit.Tag = pantographRatio;
  1863. tempBit.Add(newBit);
  1864. List<Bitmap> bitList = new List<Bitmap>();
  1865. bitList.Add(tempBit[0]);
  1866. bitList.Add(tempBit[1]);
  1867. Dictionary<string, string> tagInfos = new Dictionary<string, string>();
  1868. // this.analyzeSettingModel.analyzeClassify
  1869. AnalyzeSettingModel model = new AnalyzeSettingModel();
  1870. string filePath = Application.StartupPath + "\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";//获取文档路径
  1871. string filePath2 = "E:\\图形处理\\Metisvision20210204\\RBDC\\Metisvision\\bin\\Debug\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";
  1872. model.modulePath = filePath;
  1873. model.savePath = savePath;
  1874. //model.itemNumber = this.textBox2.Text;
  1875. //model.ratingDate = this.dateTimePicker1.Value;
  1876. //model.inspectionPerson = this.textBox4.Text;
  1877. model.contact = "";
  1878. //model.inspectionDepartment = this.textBox5.Text;
  1879. //model.inspectionDate = this.dateTimePicker2.Value;
  1880. string name = this.listView1.FocusedItem.Name.Split('.')[0];
  1881. model.analyzeClassify = name;
  1882. this.analyzeSettingModel = new AnalyzeSettingModel();
  1883. //this.analyzeSettingModel.modelItems = new List<AnalyzeSavedModel.ModelItem>();
  1884. //this.analyzeSettingModel = new AnalyzeSavedModel.ModelItem();
  1885. //this.analyzeSettingModel.languageName = this.languageName;
  1886. this.analyzeSettingModel = model;
  1887. //this.analyzeSettingModel.modelItems.Add(analyzeModel);
  1888. this.appWorkspace.CreateAnalysisReport(this.analyzeSettingModel, analysisContent, bitList, tagInfos);
  1889. }
  1890. /// 导出图片
  1891. /// </summary>
  1892. /// <param name="sender"></param>
  1893. /// <param name="e"></param>
  1894. private void btnImageOut_Click(object sender, EventArgs e)
  1895. {
  1896. SaveFileDialog exe = new SaveFileDialog();
  1897. exe.Filter = "Image Files (*.jpg)|*.jpg";
  1898. exe.FilterIndex = 0;
  1899. exe.RestoreDirectory = true;
  1900. //exe.CreatePrompt = true;
  1901. exe.Title = "Image Files";
  1902. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  1903. exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "孔隙率";
  1904. DialogResult dr = exe.ShowDialog();
  1905. if (dr != DialogResult.OK)
  1906. {
  1907. return;
  1908. }
  1909. Mat mat = binaryClass.MargMat();
  1910. if (Cv2.ImWrite(exe.FileName, mat))
  1911. {
  1912. MessageBox.Show("保存成功!");
  1913. }
  1914. //List<DataTable> list = new List<DataTable>();
  1915. //list.Add(dtb);
  1916. //this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  1917. }
  1918. /// <summary>
  1919. /// 准备用来处理单个颗粒的选中事件
  1920. /// </summary>
  1921. /// <param name="sender"></param>
  1922. /// <param name="e"></param>
  1923. private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
  1924. {
  1925. //当点击表头部的列时,e.RowIndex==-1
  1926. if (e.RowIndex > -1)
  1927. {
  1928. int index= Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[6].Value);
  1929. int x = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[0].Value);
  1930. int y = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[1].Value);
  1931. PointF select = new PointF() { X = x, Y = y };
  1932. binaryClass.DrwLunKuoSel(index);
  1933. // binaryClass.DrwLunKuoLie1Selected(select);
  1934. this.documentWorkspace.Refresh();
  1935. }
  1936. }
  1937. #region 内部类
  1938. internal class LocalListViewItem
  1939. {
  1940. public DocumentWorkspace Value { get; }
  1941. public string Display { get; }
  1942. public LocalListViewItem(DocumentWorkspace Value, string Display)
  1943. {
  1944. this.Value = Value;
  1945. this.Display = Display;
  1946. }
  1947. }
  1948. internal class DocumentItem
  1949. {
  1950. public Bitmap bitmap;
  1951. public GraphicsList graphicsList;
  1952. public List<PhaseModel> phaseModels;
  1953. //public AnalysisResult analysisResult;
  1954. public DocumentItem(Bitmap bitmap, GraphicsList graphicsList, List<PhaseModel> phaseModels)
  1955. {
  1956. this.bitmap = bitmap;
  1957. this.graphicsList = graphicsList;
  1958. this.phaseModels = phaseModels;
  1959. }
  1960. }
  1961. #endregion
  1962. #endregion
  1963. }
  1964. }