12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778 |
- using OpenCvSharp;
- using PaintDotNet.Annotation;
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.CommTool;
- using PaintDotNet.Base.Functionodel;
- using PaintDotNet.Base.SettingModel;
- using PaintDotNet.CustomControl;
- using PaintDotNet.DbOpreate.DbBll;
- using PaintDotNet.DbOpreate.DbModel;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace PaintDotNet.DedicatedAnalysis.Battery
- {
- internal partial class BatteryPrimaryDialog : PdnBaseForm
- {
- #region 属性
- //二值化集成1
- BinaryClassForB binaryClass;
- private int menuId;
- /// <summary>
- /// 选中图片的bitmap
- /// </summary>
- private Bitmap bitmap;
- /// <summary>
- /// 存在视场标记
- /// </summary>
- private bool existViewFlag = false;
- private int changeCount;
- /// <summary>
- /// 主空间
- /// </summary>
- private AppWorkspace appWorkspace;
- /// <summary>
- /// 图像面板
- /// </summary>
- private DocumentWorkspaceWindow documentWorkspace;
- /// <summary>
- /// 公共控件
- /// </summary>
- private GeneralAnalysisCommonControlButtons commonControlButtons;
- /// <summary>
- /// 图片测量记录
- /// </summary>
- private List<DataTable> pictureDataTables = new List<DataTable>();
- /// <summary>
- /// 网格Xml
- /// </summary>
- private GrainSizePointCountingMethodModel grainSizePointCountingMethodModel;
- /// <summary>
- /// 数据变化
- /// </summary>
- private bool saveSuccess = false;
- private BinaryControlSmaller bc;
- #endregion 属性
- #region 数据属性
- /// <summary>
- /// 折线点的集合
- /// </summary>
- public List<System.Drawing.Point> pointArray = new List<System.Drawing.Point>();
- /// <summary>
- /// 存储所有轮廓线的点集合
- /// </summary>
- private List<PointF> pointList = new List<PointF>();
- /// <summary>
- /// 存储所有轮廓线集合
- /// </summary>
- private List<List<PointF>> lineList = new List<List<PointF>>();
- /// <summary>
- /// 存储所有增加颗粒的点集合
- /// </summary>
- private List<PointF> kongPointList = new List<PointF>();
- /// <summary>
- /// 存储所有孔隙线集合
- /// </summary>
- private List<List<PointF>> kongLineList = new List<List<PointF>>();
- /// <summary>
- /// 存储所有删除孔隙线的点集合
- /// </summary>
- private List<PointF> delPointList = new List<PointF>();
- /// <summary>
- /// 存储所有删除孔隙线集合
- /// </summary>
- private List<List<PointF>> delLineList = new List<List<PointF>>();
- //轮廓修改状态
- bool isLunKuo = false;
- //添加孔隙状态
- bool isAddKong = false;
- //删除孔隙状态
- bool isDelKong = false;
- /// <summary>
- /// 0:添加 1:选择 2:删除
- /// </summary>
- private int operationK = -1;
- /// <summary>
- /// 被选择的线
- /// </summary>
- private int selected = -1;
- /// <summary>
- /// 起止点
- /// </summary>
- private System.Drawing.PointF startPoint, endPoint;
- /// <summary>
- /// 存储所有线的点集合
- /// </summary>
- private List<PointF[]> pointList1 = new List<PointF[]>();
- /// <summary>
- /// 存储当前线的点集合
- /// </summary>
- private PointF[] nowLine;
- /// <summary>
- /// 辅助计算偏移量-移动线两端的点
- /// </summary>
- private PointF lastPoint = new PointF(0, 0);
- /// <summary>
- /// 辅助计算偏移量-移动线中间的点
- /// </summary>
- private PointF tempPoint = new PointF(0, 0);
- /// <summary>
- /// 判断图形是否可移动
- /// </summary>
- private bool canMove = false;
- /// <summary>
- /// 测量点
- /// </summary>
- public List<RectangleF> rectangleFList = new List<RectangleF>();
- /// <summary>
- /// 移动线上的哪一个点
- /// </summary>
- private int movePointIndex = -1;
- /// <summary>
- /// 线的总数
- /// </summary>
- private int lineCount = 0;
- /// <summary>
- /// 单位标尺
- /// </summary>
- private double unitLength = 1;
- #endregion
- System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
- /// <summary>
- ///
- /// </summary>
- /// <param name="appWorkspace"></param>
- public BatteryPrimaryDialog(AppWorkspace appWorkspace, int menuId)
- {
- this.KeyPreview = true;
- this.KeyDown += OnKeyDown;
- this.menuId = menuId;
- binaryClass = new BinaryClassForB(menuId);
- InitializeComponent();
- InitializeLanguageText();
- ColumnHeader header0 = new ColumnHeader();
- header0.Text = "";
- header0.Width = 180;
- this.appWorkspace = appWorkspace;
- this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
- this.documentWorkspace.Dock = DockStyle.Fill;
- this.documentWorkspace.HookMouseEvents();
- this.documentWorkspace.AuxiliaryLineEnabled = false;
- this.documentWorkspace.Visible = false;
- this.documentWorkspace.panel.Paint += Panel_Paint;
- this.documentWorkspace.panel.MouseDown += OnMouseDown;
- this.documentWorkspace.panel.MouseUp += OnMouseUp;
- this.documentWorkspace.panel.Paint += Panel_Paint;
- this.documentWorkspace.panel.MouseMove += onMouseMove;
- this.documentWorkspace.activeTool = DrawToolType.Pointer;
- this.groupBox7.Controls.Add(this.documentWorkspace);
- this.commonControlButtons = new GeneralAnalysisCommonControlButtons();
- this.commonControlButtons.Dock = DockStyle.Top;
- this.commonControlButtons.Height = 30;
- this.commonControlButtons.HideZoomToWindowAndActualSize();
- this.commonControlButtons.trackLabel.Text = "单击开始画线,右键停止。";
- this.commonControlButtons.trackLabel.Visible = true;
- this.commonControlButtons.trackLabel.Width = 300;
- this.commonControlButtons.Visible = false;
- this.groupBox7.Controls.Add(commonControlButtons);
- //获取
- //Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out ruleValue);
- //double ruleValue = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
- ////double physical_area_length_Micron = getCurrentWorkspace().GetRuler(MeasurementUnit.Micron);
- chart1.Size = new System.Drawing.Size(1150, 180);
- chart1.Location = new System.Drawing.Point(5, 15);
- //二值化集成2
- bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);//初始化原图勾选改变事件
- binaryClass.imageType = 6;
- binaryClass.createDocumentItemsSmaller(new string[] { "bp1" }, this.bc, this.appWorkspace, this.documentWorkspace, this.listView1);//初始化相的工作结构
- //binaryClass.BinaryImplFinishAction += new EventHandler(this.bClassBinaryImplFinishAction);//二值化后续处理事件(参数改变时,重新处理图像)
- binaryClass.parentPrimary = this;
- this.ckChanged.CheckedChanged += new EventHandler(this.CheckedChanged);
- InitCommonButtonEvent();
- InitGridStyle();
- InitPicList();
- // SetAnalyzeModelFromXml(getModelName());
- }
- private void ShowImgEvent(object sender, EventArgs e)
- {
- if (this.listView1.Items.Count == 0)
- {
- return;
- }
- listView1.Focus();
- this.listView1.Items[0].Focused = true;
- this.listView1.Items[0].Selected = true;
- }
- #region 二值化相关方法
- private void InclusionsStandardDialog_FormClosing(object sender, FormClosingEventArgs e)
- {
- //binaryClass.saveParams();
- }
- private void ShownChoiseItemAndInitData(object sender, EventArgs e)
- {
- binaryClass.RefreshHistogramControl1Values();
- }
- /// <summary>
- /// 添加参数改变的监听
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void GrainSizePointCountingMethodDialog_Load(object sender, EventArgs e)
- {
- this.dataGridView1.SortCompare += DataGridView4_SortCompare;
- this.binaryClass.loadParams();
- }
- private bool bcBinaryChecked()
- {
- return bc != null && bc.BinaryChecked;
- }
- private bool bcOriginChecked()
- {
- return bc != null && bc.OriginChecked;
- }
- /// <summary>
- /// 显示原图/原图+二值图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void bcOriginCheckedChanged(object sender, EventArgs e)
- {
- if (!bcOriginChecked())
- {
- if (bcBinaryChecked())
- this.documentWorkspace.PhaseModels[0].choise = true;
- }
- else
- this.documentWorkspace.PhaseModels[0].choise = false;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 显示原图/原图+二值图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CheckedChanged(object sender, EventArgs e)
- {
- if (!ckChanged.Checked)
- {
- this.documentWorkspace.PhaseModels[0].choise = true;
- }
- else
- this.documentWorkspace.PhaseModels[0].choise = false;
- this.documentWorkspace.Refresh();
- }
- #endregion
- /// <summary>
- /// 初始化画面数据
- /// </summary>
- private void InitGridStyle()
- {
- grainSizePointCountingMethodModel = XmlSerializeHelper.DESerializer<GrainSizePointCountingMethodModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\GrainSizePointCountingMethodModel.xml", FileMode.Open));
- if (grainSizePointCountingMethodModel.GridStyleList != null)
- {
- ArrayList arrayList = new ArrayList();
- foreach (var GridStyle in grainSizePointCountingMethodModel.GridStyleList)
- {
- arrayList.Add(GridStyle.gridName);
- }
- if (grainSizePointCountingMethodModel.GridStyleList.Count == 0)
- {
- }
- }
- }
- /// <summary>
- /// 初始化图片列表数据
- /// </summary>
- public void InitPicList()
- {
- //初始化图片列表
- for (int i = 0; i < this.appWorkspace.DocumentWorkspaces.Length; i++)
- {
- this.imageList1.Images.Add("img" + i, this.appWorkspace.DocumentWorkspaces[i].BinarizationThumbnail);
- this.listView1.Items.Add("", i);
- this.listView1.Items[i].ImageIndex = i;
- this.listView1.Items[i].Text = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
- this.listView1.Items[i].Name = this.appWorkspace.DocumentWorkspaces[i].GetFriendlyName();
- }
- this.Shown += ShowImgEvent;
- }
- #region 初始化
- private void InitializeLanguageText()
- {
- this.bc = new PaintDotNet.CustomControl.BinaryControlSmaller();
- //
- // bc
- //
- this.bc.BinaryBackColor = System.Drawing.Color.Red;
- this.bc.BinaryChecked = false;
- this.bc.BinaryStyle = 1;
- this.bc.Location = new System.Drawing.Point(152, 7);
- this.bc.Name = "bc";
- this.bc.OriginChecked = false;
- this.bc.scope1End = 170D;
- this.bc.scope1Start = 80D;
- this.bc.scope2End = 0D;
- this.bc.scope2Start = 0D;
- this.bc.scope3End = 0D;
- this.bc.scope3Start = 0D;
- this.bc.Size = new System.Drawing.Size(360, 247);
- this.bc.TabIndex = 3;
- // this.Controls.Add(this.bc);
- // this.Controls.SetChildIndex(this.bc, 0);
- //this.groupBox9.Controls.Add(chart1);
- this.groupBox8.Controls.Add(chart1);
- chart1.Visible = false;
- //this.label10.Visible = true;
- //this.label10.Text = "【" + tn.Name + "】图";
- chart1.Titles.Add("单晶尺寸分布图");
- }
- #endregion
- #region 公共按钮
- private void InitCommonButtonEvent()
- {
- this.commonControlButtons.zoomInButton.Click += new EventHandler(zoomInButton_Click);
- this.commonControlButtons.zoomOutButton.Click += new EventHandler(zoomOutButton_Click);
- this.commonControlButtons.zoomToWindowButton.Click += new EventHandler(zoomToWindowButton_Click);
- this.commonControlButtons.actualSizeButton.Click += new EventHandler(actualSizeButton_Click);
- this.commonControlButtons.pointerButton.Click += new EventHandler(pointerButton_Click);
- this.commonControlButtons.mobileModeButton.Click += new EventHandler(mobileModeButton_Click);
- }
- private void zoomInButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomIn();
- }
- private void zoomOutButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomOut();
- }
- private void zoomToWindowButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
- }
- private void actualSizeButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
- this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
- }
- private void pointerButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.Pointer;
- }
- private void mobileModeButton_Click(object sender, EventArgs e)
- {
- this.documentWorkspace.ActiveTool = Annotation.Enum.DrawToolType.MoveMode;
- }
- #endregion
- private void listView1_MouseDown(object sender, MouseEventArgs e)
- {
- this.changeCount = 0;
- }
- private void DataGridView4_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
- {
- // 如果是数据的列,则按浮点数处理
- if (e.Column.Index > 1)//###20186
- {
- e.SortResult = (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) > 0) ? 1 : (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) < 0) ? -1 : 0;
- }
- //否则,按字符串比较
- else
- {
- e.SortResult = System.String.Compare(Convert.ToString(e.CellValue1), Convert.ToString(e.CellValue2));
- }
- e.Handled = true;//不能省掉,不然没效果
- }
- private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
- {
-
- if (unitLength == 1)
- unitLength = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
- this.changeCount++;
- }
- /// <summary>
- /// 切换图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void listView1_SelectedIndexChanged(object sender, EventArgs e)
- {
- SelectedIndexChanged();
- }
- List<PointF> list = new List<PointF>();
- /// <summary>
- /// 切换图片
- /// </summary>
- private void SelectedIndexChanged()
- {
- if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Selected)
- {
- existViewFlag = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GraphicsList.IsExsitView();
- this.bitmap = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
- lineList.Clear();
- pointList.Clear();
- kongLineList.Clear();
- kongPointList.Clear();
- delLineList.Clear();
- delPointList.Clear();
- isLunKuo = false;
- isAddKong = false;
- isDelKong = false;
- this.documentWorkspace.Refresh();
- this.dataGridView1.Rows.Clear();
- this.dataGridView2.Rows.Clear();
- Init();
- this.documentWorkspace.GridRectangleEnabled = false;
- //getimage();
- //getbian(bitmap);
- double minpoint = Convert.ToDouble(numericUpDown11.Value);
- if (!ckMin.Checked)
- minpoint = 0.01;
- binaryClass.pointsize = Convert.ToDouble(minpoint);
- int border = Convert.ToInt32(numericMax.Value);
- if (!ckMax.Checked)
- border = 0;
- binaryClass.border = border;
- double HW = Convert.ToDouble(numericHW.Value);
- if (!ckHW.Checked)
- HW = 100;
- binaryClass.HW = HW;
- double AreaRatio = Convert.ToDouble(numericArea.Value);
- if (!ckArea.Checked)
- AreaRatio = 0.01;
- binaryClass.AreaRatio = AreaRatio;
- //二值化集成4
- binaryClass.listView1_SelectedIndexChanged(this.bitmap);
- // binaryClass.bcDefault();
- this.documentWorkspace.Refresh();
- }
- }
- #region 原始事件
- private void Init()
- {
- this.commonControlButtons.Visible = true;
- this.dataGridView1.Rows.Clear();
- this.documentWorkspace.phaseModels.Clear();
- }
- /// <summary>
- /// 画布绘制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Panel_Paint(object sender, PaintEventArgs e)
- {
- if (this.documentWorkspace.CompositionSurface != null)
- {
- //
- // 以下是计算绘制图片的位置和大小并绘制图片
- //
- Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
- int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
- int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
- int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
- int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
- //if (pointArray.Count > 1)
- //{
- // e.Graphics.DrawLines(new Pen(Color.FromArgb(0, 0, 0), 2), pointArray.ToArray());
- //}
- //
- // 以下是绘制网格、标注、测量、视场等开始
- //
- e.Graphics.TranslateTransform(x, y);
- e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
- Draw(e.Graphics);
- //e.Graphics.DrawLines(new Pen(this.panel1.BackColor, Convert.ToInt32(this.txt_LeftLineWeight.Text)), list.ToArray());
- e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
- e.Graphics.TranslateTransform(-x, -y);
- }
- if (bc.isedit)
- {
- bc.isedit = false;
- List<List<int>> CircleDate = bc.data;
- bindGrid(CircleDate);
- }
- }
- // <summary>
- /// 存储字号与颜色字典
- /// </summary>
- public Dictionary<string, Color> colourFont = new Dictionary<string, Color>() { { "20", Color.White } };
- /// <summary>
- /// 绘制
- /// </summary>
- private void Draw(Graphics graphics)
- {
- //轮廓手动干预线
- if (lineList.Count > 0)
- {
- for (int i = 0; i < lineList.Count; i++)
- {
- graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), lineList[i].ToArray());
- }
- }
- if (pointList.Count > 1)
- {
- graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), pointList.ToArray());
- }
- //添加颗粒
- Color color = bc.BinaryBackColor;
- if (kongLineList.Count > 0)
- {
- for (int i = 0; i < kongLineList.Count; i++)
- {
- if (kongLineList[i].Count > 1)
- {
- graphics.DrawLines(new Pen(color, 2), kongLineList[i].ToArray());
- }
- }
- }
- graphics.SmoothingMode = SmoothingMode.AntiAlias;
- Pen standardPen = new Pen(Color.FromArgb(0, 255, 0), 4);//基准线画笔
- Pen surfacePen = new Pen(Color.FromArgb(255, 0, 0), 4);//钢样表面画笔
- Pen completePen = new Pen(Color.FromArgb(0, 0, 255), 4);//完全脱碳层画笔
- Pen validPen = new Pen(Color.FromArgb(255, 255, 255), 4);//有效脱碳层画笔
- Pen partPen = new Pen(Color.FromArgb(0, 0, 0), 2);//部分脱碳层画笔
- Pen borderPen = new Pen(Color.Black);//填充点外圈
- SolidBrush insideBrush = new SolidBrush(Color.White);//填充点内圈
- Pen rectPen = new Pen(Color.Black);
- rectPen.DashStyle = DashStyle.Custom;
- float[] dashArray = { 2.0f, 3.0f };
- rectPen.DashPattern = dashArray;
- if (pointList1.Count > 0)
- {
- foreach (PointF[] pointFs in pointList1)
- {
- PointF[] pf = pointFs;
- if (pointFs.Count() > 1 && pointFs[0] != pointFs[1])
- {
- float lineLength = 12;
- double angle = Math.Round(BasicCalculationHelper.AngleText(pointFs[0], pointFs[1], new PointF(pointFs[0].X + lineLength, pointFs[0].Y)), 10);
- //判断第二个点相对于第一个点的象限
- int x2 = (int)(pointFs[1].X - pointFs[0].X);
- if (x2 == 0)
- x2 = 1;
- int y2 = (int)(pointFs[1].Y - pointFs[0].Y);
- if (y2 == 0)
- y2 = 1;
- int i2 = 0;
- if (x2 > 0 && y2 > 0) //第4象限
- {
- i2 = 4;
- }
- else if (x2 > 0 && y2 < 0) //第1象限
- {
- i2 = 1;
- }
- else if (x2 < 0 && y2 < 0) //第2象限
- {
- i2 = 2;
- }
- else if (x2 < 0 && y2 > 0) //第3象限
- {
- i2 = 3;
- }
- double sAngle1;
- double eAngle1;
- double sAngle2;
- double eAngle2;
- if (i2 == 1 || i2 == 2)
- {
- sAngle1 = 360 - angle;
- eAngle1 = 180 - angle;
- sAngle2 = 270 - angle;
- eAngle2 = 90 - angle;
- }
- else
- {
- sAngle1 = angle;
- eAngle1 = 180 + angle;
- sAngle2 = 270 + angle;
- eAngle2 = 90 + angle;
- }
- graphics.DrawLine(standardPen, pointFs[0], pointFs[1]);
- if (!double.IsNaN(sAngle1) && !double.IsNaN(eAngle1) && !double.IsNaN(sAngle2) && !double.IsNaN(eAngle2))
- {
- PointF assistPoint1 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[0].X + lineLength), pointFs[0].Y), pointFs[0], sAngle2);
- PointF assistPoint2 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[0].X + lineLength), pointFs[0].Y), pointFs[0], eAngle2);
- PointF assistPoint3 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[1].X + lineLength), pointFs[1].Y), pointFs[1], sAngle2);
- PointF assistPoint4 = BasicCalculationHelper.GetAnglePoint(new PointF((pointFs[1].X + lineLength), pointFs[1].Y), pointFs[1], eAngle2);
- graphics.DrawLine(surfacePen, assistPoint1, assistPoint2);
- graphics.DrawLine(partPen, assistPoint3, assistPoint4);
- //PointF[] pf = pointFs;
- if (i2 == 1 || i2 == 2)
- {
- graphics.DrawString(Math.Round((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength), 2).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);
- }
- else
- {
- graphics.DrawString(Math.Round((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength), 2).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);
- }
- }
- }
- }
- if (selected > -1)
- {
- int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1));
- int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1));
- graphics.DrawRectangle(borderPen, new Rectangle((int)(pointList1[selected][0].X - tempLess), (int)(pointList1[selected][0].Y - tempLess), tempPlus, tempPlus));
- graphics.FillRectangle(insideBrush, new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus));
- graphics.DrawRectangle(borderPen, new Rectangle((int)(pointList1[selected][1].X - tempLess), (int)(pointList1[selected][1].Y - tempLess), tempPlus, tempPlus));
- graphics.FillRectangle(insideBrush, new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus));
- graphics.DrawRectangle(rectPen, rectangleFList[selected].X, rectangleFList[selected].Y, rectangleFList[selected].Width, rectangleFList[selected].Height);
- }
- }
- standardPen.Dispose();
- surfacePen.Dispose();
- completePen.Dispose();
- validPen.Dispose();
- partPen.Dispose();
- borderPen.Dispose();
- insideBrush.Dispose();
- rectPen.Dispose();
- }
- //最长直径
- double MaxM = 0;
- double MinM = 0;
- double MeanM = 0;
- private void bindGrid(List<List<int>> dataList)
- {
- if (dataList.Count < 1)
- {
- return;
- }
- chart1.Visible = true;
- List<int> CircleDate = dataList[0];
- //标尺 微米/像素
- double d = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
- dataGridView1.Rows.Clear();
- dataGridView2.Rows.Clear();
- chart1.Series.Clear();
- chart1.ChartAreas.Clear();
- 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) };
- List<double> diameterPI = new List<double>() { };
- List<double> diameter = new List<double>() { };
- List<double> diameterMax = new List<double>() { };
- for (int i = 0; i < dataList.Count; i++)
- {
- int newPo = dataGridView1.Rows.Add();
- //序号
- dataGridView1.Rows[newPo].Cells[0].Value = (newPo + 1).ToString();
- //XY
- dataGridView1.Rows[newPo].Cells[1].Value = "X:" + dataList[i][0].ToString() + ",Y:" + dataList[i][1].ToString();
- //直径
- double w = Convert.ToDouble(dataList[i][2]);
- double h = Convert.ToDouble(dataList[i][3]);
- double dd = (w + h) * 0.5 * d;
- double area = Convert.ToDouble(dataList[i][4]) * d * d;
- double r = Math.Sqrt(area / Math.PI) * 2;
- diameterPI.Add(r);
- diameter.Add(dd);
- double maxD = w > h ? w : h;
- double minD = w < h ? w : h;
- diameterMax.Add(maxD * d);
- dataGridView1.Rows[newPo].Cells[2].Value = Math.Round(dd, 2).ToString();
- dataGridView1.Rows[newPo].Cells[3].Value = dataList[i][5];
- dataGridView1.Rows[newPo].Cells[4].Value = Math.Round(maxD * d, 2).ToString();
- dataGridView1.Rows[newPo].Cells[5].Value = Math.Round(minD * d, 2).ToString();
- dataGridView1.Rows[newPo].Cells[6].Value = Math.Round(dataList[i][9] * d, 2).ToString();
- dataGridView1.Rows[newPo].Cells[7].Value = Math.Round(dataList[i][4] * d * d, 2).ToString();
- }
- diameterMax.Sort();
- MinM = diameterMax[0];
- MaxM = diameterMax[diameterMax.Count - 1];
- foreach (var item in diameterMax)
- {
- MeanM += Math.Round(item, 1);
- }
- MeanM = MeanM / diameterMax.Count;
- DataTable dt = new DataTable();
- dt = CreatTable(diameter, diameterPI);
- CreatChart(dt);
- diameter.Sort();
- double total = 0;
- foreach (var item in diameter)
- {
- total += item;
- }
- total = total / diameter.Count;
- int newRow = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow].Cells[0].Value = (1).ToString();
- dataGridView2.Rows[newRow].Cells[1].Value = "平均值";
- dataGridView2.Rows[newRow].Cells[2].Value = Math.Round(total, 2).ToString();
- int newRow1 = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow1].Cells[0].Value = (2).ToString();
- dataGridView2.Rows[newRow1].Cells[1].Value = "最大值";
- dataGridView2.Rows[newRow1].Cells[2].Value = Math.Round(diameter[diameter.Count - 1], 2).ToString();
- int newRow2 = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow2].Cells[0].Value = (3).ToString();
- dataGridView2.Rows[newRow2].Cells[1].Value = "最小值";
- dataGridView2.Rows[newRow2].Cells[2].Value = Math.Round(diameter[0], 2).ToString();
- int count = diameter.Count;
- int d10 = Convert.ToInt32(count * 0.1);
- int d50 = Convert.ToInt32(count * 0.5);
- int d90 = Convert.ToInt32(count * 0.9);
- int newRow3 = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow3].Cells[0].Value = (4).ToString();
- dataGridView2.Rows[newRow3].Cells[1].Value = "D10";
- dataGridView2.Rows[newRow3].Cells[2].Value = Math.Round(diameter[d10], 2).ToString();
- int newRow4 = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow4].Cells[0].Value = (5).ToString();
- dataGridView2.Rows[newRow4].Cells[1].Value = "D50";
- dataGridView2.Rows[newRow4].Cells[2].Value = Math.Round(diameter[d50], 2).ToString();
- int newRow5 = dataGridView2.Rows.Add();
- dataGridView2.Rows[newRow5].Cells[0].Value = (5).ToString();
- dataGridView2.Rows[newRow5].Cells[1].Value = "D90";
- dataGridView2.Rows[newRow5].Cells[2].Value = Math.Round(diameter[d90], 2).ToString();
- }
- /// <summary>
- /// 准备用来处理单个颗粒的选中事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- //当点击表头部的列时,e.RowIndex==-1
- if (e.RowIndex > -1)
- {
- string selectrow = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
- int x = Convert.ToInt32(selectrow);
- binaryClass.DrwCrystalSelected(x);
- this.documentWorkspace.Refresh();
- }
- }
- //外接矩形
- double MaxP = 0;
- double Minp = 0;
- double MeanP = 0;
- //拟圆直径
- double MaxP2 = 0;
- double Minp2 = 0;
- double MeanP2 = 0;
- private DataTable CreatTable(List<double> diameter, List<double> diameterPI)
- {
- diameter.Sort();
- double max = diameter[diameter.Count - 1];
- double total = max * 2;
- double min = 0;
- Minp = diameter[0];
- MaxP = diameter[diameter.Count - 1];
- diameterPI.Sort();
- Minp2 = diameterPI[0];
- MaxP2 = diameterPI[diameterPI.Count - 1];
- foreach (var item in diameter)
- {
- MeanP += Math.Round(item, 2);
- }
- foreach (var item in diameterPI)
- {
- MeanP2 += Math.Round(item, 2);
- }
- MeanP = MeanP / diameter.Count;
- MeanP2 = MeanP2 / diameterPI.Count;
- //int eval =Convert.ToInt32( (max - min) / 20);
- double eval =Math.Round( max / 15,1);
- List<double[]> vs = new List<double[]>();
- double rightmax = 0;
- for (int i = 0; eval * i < max; i++)
- {
- vs.Add(new double[] { eval * i, eval * (i + 1) });
- rightmax = eval * (i + 1);
- }
- DataTable dt = new DataTable();
- dt.Columns.Add("diameter");
- dt.Columns.Add("sum");
- dt.Columns.Add("sum1");
- for (int i = 0; i < vs.Count; i++)
- {
- DataRow dr;
- dr = dt.NewRow();
- double low = eval * i;
- double hight = eval * (i + 1);
- dr["diameter"] = string.Format("{0}", hight);
- var emp = diameter.FindAll(e => e > low && e <= hight);
- dr["sum"] = emp.Count;
- var emp1 = diameterPI.FindAll(e => e > low && e <= hight);
- dr["sum1"] = emp1.Count;
- dt.Rows.Add(dr);
- }
- return dt;
- }
- private void CreatChart(DataTable dt)
- {
- if (dt == null || dt.Rows.Count == 0)
- {
- return;
- }
- chart1.Series.Clear();
- chart1.ChartAreas.Clear();
- chart1.Legends.Clear();
- //定义一个chart
- System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
- //定义一个绘图区域
- System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
- System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
- //定义一个绘图区域
- System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
- chartArea1.Name = "ChartArea1";
- chart1.Series.Add(series1);
- //chart1.Series.Add(series2);
- chart1.ChartAreas.Add(chartArea1);
- chart1.Legends.Add(legend1);
- series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
- chart1.Series[0].LegendText = "";
- chart1.Series[0].Label = "#VALY";
- chart1.Series[0].LabelBackColor = Color.Transparent;
- chart1.Series[0].ToolTip = "#VALY";
- chart1.Series[0]["PointWidth"] = "0.5";
- chart1.Series[0].XValueMember = "diameter";
- chart1.Series[0].YValueMembers = "sum";
- chart1.Series[0].LegendText = "卡规直径";
- //chart1.Series[1].LegendText = "";
- //chart1.Series[1].Label = "#VALY";
- //chart1.Series[1].LabelBackColor = Color.Transparent;
- //chart1.Series[1].ToolTip = "#VALY";
- //chart1.Series[1]["PointWidth"] = "0.5";
- //chart1.Series[1].XValueMember = "diameter";
- //chart1.Series[1].YValueMembers = "sum1";
- //chart1.Series[1].LegendText = "拟圆直径";
- chart1.ChartAreas[0].AxisX.Interval = 1;
- chart1.ChartAreas[0].AxisX.Title = "尺寸(μm)";
- chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false; //X轴上网格
- chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false; //y轴上网格
- series1.Color = Color.DarkSeaGreen;
- series2.Color = Color.PaleTurquoise;
- chart1.DataSource = dt;
- chart1.DataBind();
- }
- #region 手动干预
- /// <summary>
- /// 修改轮廓
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnLunkuo_Click(object sender, EventArgs e)
- {
- isLunKuo = true;
- isAddKong = false;
- isDelKong = false;
- operationK = -1;
- }
- /// <summary>
- /// 添加孔隙
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnAddKong_Click(object sender, EventArgs e)
- {
- isLunKuo = false;
- isAddKong = true;
- isDelKong = false;
- operationK = -1;
- }
- /// <summary>
- /// 删除孔隙
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDelKong_Click(object sender, EventArgs e)
- {
- isLunKuo = false;
- isAddKong = false;
- isDelKong = true;
- operationK = -1;
- }
- /// <summary>
- /// 重新统计
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnReDrw_Click(object sender, EventArgs e)
- {
- double minpoint = Convert.ToDouble(numericUpDown11.Value);
- if (!ckMin.Checked)
- minpoint = 0.01;
- binaryClass.pointsize = minpoint;
- int border = Convert.ToInt32(numericMax.Value);
- if (!ckMax.Checked)
- border = 0;
- binaryClass.border = border;
- double HW = Convert.ToDouble(numericHW.Value);
- if (!ckHW.Checked)
- HW = 100;
- binaryClass.HW = HW;
- double AreaRatio = Convert.ToDouble(numericArea.Value);
- if (!ckArea.Checked)
- AreaRatio = 0.01;
- binaryClass.AreaRatio = AreaRatio;
- if (lineList.Count > 0)
- {
- List<OpenCvSharp.Point> contour = new List<OpenCvSharp.Point>();
- for (int i = 0; i < lineList.Count; i++)
- {
- if (lineList[i].Count > 0)
- {
- for (int j = 0; j < lineList[i].Count; j++)
- {
- contour.Add(new OpenCvSharp.Point(lineList[i][j].X, lineList[i][j].Y));
- }
- }
- }
- if (contour.Count > 0)
- binaryClass.Contour = contour;
- }
- else
- {
- binaryClass.Contour = null;
- }
- List<List<int>> CircleDate = binaryClass.ForgetPrimary(minpoint);
- bindGrid(CircleDate);
- }
- /// <summary>
- /// 计算外接矩形
- /// </summary>
- private RectangleF CalcOtherRectangle(PointF p1, PointF p2)
- {
- RectangleF rectangleF = new RectangleF();
- if (p1.X > p2.X)
- rectangleF.X = p2.X;
- else
- rectangleF.X = p1.X;
- if (p1.Y > p2.Y)
- rectangleF.Y = p2.Y;
- else
- rectangleF.Y = p1.Y;
- rectangleF.Width = Math.Abs(p2.X - p1.X);
- rectangleF.Height = Math.Abs(p2.Y - p1.Y);
- int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- return new RectangleF(rectangleF.X - tempLess, rectangleF.Y - tempLess, rectangleF.Width + tempPlus, rectangleF.Height + tempPlus);
- }
- /// <summary>
- /// 移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseMove(object sender, MouseEventArgs e)
- {
- //if (e.Button == MouseButtons.Left)
- //{
- if (isLunKuo)
- {
- int cou = pointList.Count;
- if (cou > 0)
- { // 换算后的点
- PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- pointList[cou - 1] = movePoint;
- }
- }
- if (isAddKong && e.Button == MouseButtons.Left)
- {
- if (kongLineList.Count > 0)
- { // 换算后的点
- PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- //kongPointList[kongPointList.Count - 1] = movePoint;
- kongLineList[kongLineList.Count - 1].Add(movePoint);
- }
- }
- if (isDelKong)
- {
- //if (delPointList.Count > 0)
- //{ // 换算后的点
- // PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- // delPointList[delPointList.Count - 1] = movePoint;
- //}
- }
- if (operationK >= 0 && e.Button == MouseButtons.Left)
- {
- PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- if (operationK == 0)
- {
- this.endPoint = movePoint;
- pointList1[lineCount][1] = endPoint;
- }
- else if (operationK == 1 && selected > -1 && canMove)
- {
- float dx = movePoint.X - lastPoint.X;
- float dy = movePoint.Y - lastPoint.Y;
- float dxTemp = movePoint.X - tempPoint.X;
- float dyTemp = movePoint.Y - tempPoint.Y;
- lastPoint.X = movePoint.X;
- lastPoint.Y = movePoint.Y;
- if (movePointIndex == -1)
- {
- RectangleF rectangleF = new RectangleF();
- rectangleF = rectangleFList[selected];
- if (rectangleF.Contains(movePoint))
- this.documentWorkspace.panel.Cursor = Cursors.SizeAll;
- rectangleF.X += dx;
- rectangleF.Y += dy;
- rectangleFList[selected] = rectangleF;
- for (int i = 0; i < pointList1[selected].Count(); i++)
- {
- if (pointList1[selected][i] == PointF.Empty)
- continue;
- pointList1[selected][i].X += dx;
- pointList1[selected][i].Y += dy;
- }
- }
- else if (movePointIndex == 0)
- {
- pointList1[selected][0].X += dx;
- pointList1[selected][0].Y += dy;
- rectangleFList[selected] = CalcOtherRectangle(pointList1[selected][0], pointList1[selected][1]);
- }
- else if (movePointIndex == 1)
- {
- pointList1[selected][1].X += dx;
- pointList1[selected][1].Y += dy;
- rectangleFList[selected] = CalcOtherRectangle(pointList1[selected][0], pointList1[selected][1]);
- }
- }
- this.documentWorkspace.Refresh();
- }
- this.documentWorkspace.Refresh();
- // }
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnMouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (operationK == 0)
- {
- if (startPoint != null && endPoint != null && startPoint != endPoint)
- {
- RectangleF rectangleF = CalcOtherRectangle(pointList1[lineCount][0], pointList1[lineCount][1]);
- rectangleFList.Add(rectangleF);
- lineCount++;
- // AddContentToDataGridView();
- }
- else
- {
- if (pointList.Count > 0)
- pointList.RemoveAt(pointList.Count - 1);
- }
- this.documentWorkspace.Refresh();
- }
- else if (operationK == 1)
- {
- canMove = false;
- movePointIndex = -1;
- // RefreshDataGridView1();
- }
- else if (isAddKong)
- {
- if (kongLineList[kongLineList.Count - 1].Count > 3)
- {
- isAddKong = false;
- List<List<int>> CircleDate = binaryClass.DeletePrimary(delPointList, kongLineList);
- bindGrid(CircleDate);
- }
- else
- {
- kongLineList.RemoveAt(kongLineList.Count - 1);
- }
- }
- }
- if (operationK >= 0 && e.Button == MouseButtons.Left)
- {
- PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
- if (operationK == 0)
- {
- startPoint = endPoint = downPoint;
- pointList1[lineCount - 1][1] = endPoint;
- }
- else if (operationK == 1)
- {
- lastPoint.X = downPoint.X;
- lastPoint.Y = downPoint.Y;
- tempPoint.X = downPoint.X;
- tempPoint.Y = downPoint.Y;
- for (int i = rectangleFList.Count - 1; i >= 0; i--)
- {
- if (rectangleFList[i].Contains(downPoint))
- {
- selected = i;
- canMove = true;
- }
- }
- if (selected > -1 && pointList1 != null && pointList1.Count > 0)
- {
- int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- if (new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
- movePointIndex = 0;
- else if (new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
- movePointIndex = 1;
- else
- movePointIndex = -1;
- this.documentWorkspace.Refresh();
- }
- if (canMove)
- return;
- selected = -1;
- }
- }
- }
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- private void OnMouseDown(object sender, MouseEventArgs e)
- {
- PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
- if (operationK >= 0 && e.Button == MouseButtons.Left)
- {
- if (operationK == 0)
- {
- startPoint = endPoint = downPoint;
- nowLine = new PointF[4];
- nowLine[0] = startPoint;
- nowLine[1] = endPoint;
- pointList1.Add(nowLine);
- }
- else if (operationK == 1)
- {
- lastPoint.X = downPoint.X;
- lastPoint.Y = downPoint.Y;
- tempPoint.X = downPoint.X;
- tempPoint.Y = downPoint.Y;
- for (int i = rectangleFList.Count - 1; i >= 0; i--)
- {
- if (rectangleFList[i].Contains(downPoint))
- {
- selected = i;
- canMove = true;
- }
- }
- if (selected > -1 && pointList1 != null && pointList1.Count > 0)
- {
- int tempLess = (int)(InvariantData.rectless * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- int tempPlus = (int)(InvariantData.rectplus * (this.documentWorkspace.ScaleFactor.Ratio < 1 ? 1 / this.documentWorkspace.ScaleFactor.Ratio : 1)) * 5;
- if (new RectangleF(pointList1[selected][0].X - tempLess, pointList1[selected][0].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
- movePointIndex = 0;
- else if (new RectangleF(pointList1[selected][1].X - tempLess, pointList1[selected][1].Y - tempLess, tempPlus, tempPlus).Contains(downPoint))
- movePointIndex = 1;
- else
- movePointIndex = -1;
- this.documentWorkspace.Refresh();
- }
- if (canMove)
- return;
- selected = -1;
- }
- }
- else if (e.Button == MouseButtons.Left)
- {
- if (isLunKuo)
- {
- if (pointList.Count == 0)
- {
- pointList.Add(downPoint);
- pointList.Add(downPoint);
- }
- else
- {
- pointList.Add(downPoint);
- }
- }
- if (isAddKong)
- {
- List<PointF> pl = new List<PointF>();
- pl.Add(downPoint);
- kongLineList.Add(pl);
- //if (kongPointList.Count == 0)
- //{
- // kongPointList.Add(downPoint);
- // kongPointList.Add(downPoint);
- //}
- //else
- //{
- // kongPointList.Add(downPoint);
- //}
- }
- if (isDelKong)
- {
- delPointList.Add(downPoint);
- List<List<int>> CircleDate = binaryClass.DeletePrimary(delPointList, kongLineList);
- bindGrid(CircleDate);
- }
- }
- if (e.Button == MouseButtons.Right)
- {
- if (isLunKuo)
- {
- if (pointList.Count > 2)
- {
- pointList.RemoveAt(pointList.Count - 1);
- List<PointF> pointFs = new List<PointF>(pointList.ToArray());
- lineList.Add(pointFs);
- }
- pointList.Clear();
- }
- if (isAddKong)
- {
- if (kongPointList.Count > 2)
- {
- kongPointList.RemoveAt(kongPointList.Count - 1);
- List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
- kongLineList.Add(pointFs);
- List<List<int>> CircleDate = binaryClass.DeletePrimary(delPointList, kongLineList);
- bindGrid(CircleDate);
- }
- kongPointList.Clear();
- }
- if (isDelKong)
- {
- //if (delPointList.Count > 2)
- //{
- // delPointList.RemoveAt(delPointList.Count - 1);
- // List<PointF> pointFs = new List<PointF>(delPointList.ToArray());
- // delLineList.Add(pointFs);
- //}
- //delPointList.Clear();
- }
- isLunKuo = false;
- isAddKong = false;
- isDelKong = false;
- }
- }
- /// <summary>
- /// Ctrl+z
- /// </summary>
- private void OnKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.Z)
- {
- if (isLunKuo)
- {
- int cou = pointList.Count;
- if (cou > 1)
- {
- if (cou == 2)
- {
- pointList.Clear();
- }
- else
- {
- pointList.RemoveAt(cou - 1);
- }
- }
- else if (lineList.Count > 0)
- {
- pointList = lineList[lineList.Count - 1];
- lineList.RemoveAt(lineList.Count - 1);
- pointList.RemoveAt(pointList.Count - 1);
- }
- }
- if (isAddKong)
- {
- if (kongLineList.Count > 0)
- {
- kongLineList.RemoveAt(kongLineList.Count - 1);
- List<List<int>> CircleDate = binaryClass.DeletePrimary(delPointList, kongLineList);
- bindGrid(CircleDate);
- }
- }
- if (isDelKong)
- {
- int cou = delPointList.Count;
- if (cou > 0)
- {
- delPointList.RemoveAt(cou - 1);
- List<List<int>> CircleDate = binaryClass.DeletePrimary(delPointList, kongLineList);
- bindGrid(CircleDate);
- }
- }
- this.documentWorkspace.Refresh();
- }
-
- }
- #endregion
- /// <summary>
- /// 导出孔隙信息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button14_Click(object sender, EventArgs e)
- {
- SaveFileDialog exe = new SaveFileDialog();
- exe.Filter = "Image Files (*.jpg)|*.jpg";
- exe.FilterIndex = 0;
- exe.RestoreDirectory = true;
- //exe.CreatePrompt = true;
- exe.Title = "Image Files";
- exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "单晶";
- DialogResult dr = exe.ShowDialog();
- if (dr != DialogResult.OK)
- {
- return;
- }
- string GR_Path = exe.FileName + ".jpg";
- chart1.SaveImage(GR_Path, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Jpeg);
- }
- /// <summary>
- /// 不可点击空白区域
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void listView1_MouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (listView1.SelectedItems.Count > 0)
- {
- }
- else if (listView1.SelectedItems.Count <= 0)//点击空白区
- {
- if (this.listView1.FocusedItem != null)
- {
- ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);
- if (item == null)
- {
- this.listView1.FocusedItem.Selected = true;
- }
- }
- }
- }
- }
- /// <summary>
- /// 生成孔隙率
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button13_Click(object sender, EventArgs e)
- {
- string name = this.listView1.FocusedItem.Name.Split('.')[0] + "-";
- SaveFileDialog exe = new SaveFileDialog();
- exe.Filter = "Execl files (*.xlsx)|*.xlsx";
- exe.FilterIndex = 0;
- exe.RestoreDirectory = true;
- //exe.CreatePrompt = true;
- exe.Title = "Export Excel File";
- // exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- exe.FileName = name;
- String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
- DialogResult dr = exe.ShowDialog();
- if (dr != DialogResult.OK)
- {
- return;
- }
- DataTable dtb = new DataTable();
- dtb.Columns.Add("序号");
- dtb.Columns.Add("位置");
- dtb.Columns.Add("卡规直径/微米");
- dtb.Columns.Add("长径/微米");
- dtb.Columns.Add("短径/微米");
- dtb.Columns.Add("周长/微米");
- dtb.Columns.Add("面积/平方微米");
- for (int r = 0; r < this.dataGridView1.Rows.Count; r++)
- {
- DataRow dataRow = dtb.NewRow();
- dataRow[0] = dataGridView1.Rows[r].Cells[0].Value.ToString();
- dataRow[1] = dataGridView1.Rows[r].Cells[1].Value.ToString();
- dataRow[2] = dataGridView1.Rows[r].Cells[2].Value.ToString();
- dataRow[3] = dataGridView1.Rows[r].Cells[4].Value.ToString();
- dataRow[4] = dataGridView1.Rows[r].Cells[5].Value.ToString();
- dataRow[5] = dataGridView1.Rows[r].Cells[6].Value.ToString();
- dataRow[6] = dataGridView1.Rows[r].Cells[7].Value.ToString();
- dtb.Rows.Add(dataRow);
- }
- DataTable dtb1 = new DataTable();
- dtb1.Columns.Add("序号");
- dtb1.Columns.Add("名称");
- dtb1.Columns.Add("值/微米");
- for (int r = 0; r < this.dataGridView2.Rows.Count; r++)
- {
- DataRow dataRow = dtb1.NewRow();
- dataRow[0] = dataGridView2.Rows[r].Cells[0].Value.ToString();
- dataRow[1] = dataGridView2.Rows[r].Cells[1].Value.ToString();
- dataRow[2] = dataGridView2.Rows[r].Cells[2].Value.ToString();
- dtb1.Rows.Add(dataRow);
- }
- //DataRow dataRow2 = dtb1.NewRow();
- //dataRow2[0] = "1";
- //dataRow2[1] = "卡规直径";
- //dataRow2[2] = Math.Round(MaxP, 2).ToString();
- //dataRow2[3] = Math.Round(Minp, 2).ToString();
- //dataRow2[4] = Math.Round(MeanP, 2).ToString();
- //dtb1.Rows.Add(dataRow2);
- //DataRow dataRow3 = dtb1.NewRow();
- //dataRow3[0] = "2";
- //dataRow3[1] = "长径";
- //dataRow3[2] = Math.Round(MaxP2, 2).ToString();
- //dataRow3[3] = Math.Round(Minp2, 2).ToString();
- //dataRow3[4] = Math.Round(MeanP2, 2).ToString();
- ////dtb1.Rows.Add(dataRow3);
- //DataRow dataRow4 = dtb1.NewRow();
- //dataRow4[0] = "3";
- //dataRow4[1] = "短径";
- //dataRow4[2] = Math.Round(MaxM, 2).ToString();
- //dataRow4[3] = Math.Round(MinM, 2).ToString();
- //dataRow4[4] = Math.Round(MeanM, 2).ToString();
- //dtb1.Rows.Add(dataRow4);
- int index = exe.FileName.LastIndexOf('\\');
- string GR_Path = exe.FileName.Remove(index) + "\\" + name + ".jpg";
- chart1.SaveImage(GR_Path, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Jpeg);
- //保存处理后的图片
- double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
- List<Bitmap> tempBit = new List<Bitmap>();
- Bitmap originalBit = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
- originalBit.Tag = pantographRatio;
- tempBit.Add(originalBit);
- Bitmap processedBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(this.documentWorkspace.PhaseModels[0].mat);
- Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
- Graphics graphics = Graphics.FromImage(newBit);
- graphics.DrawImage(processedBit, new PointF(0, 0));
- Draw(graphics);
- newBit.Tag = pantographRatio;
- tempBit.Add(newBit);
- string GR_Path1 = exe.FileName.Remove(index) + "\\" + name + "1.jpg";
- newBit.Save(GR_Path1);
- List<DataTable> list = new List<DataTable>();
- list.Add(dtb1);
- list.Add(dtb);
- List<string> pics = new List<string>() { GR_Path+",400,200,"+ chart1.Width+","+ chart1.Height, GR_Path1 + ",400,420," + newBit.Width + "," + newBit.Height };
- this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true, null,null,pics);
- }
- #region 辅助测量
- /// <summary>
- /// 添加测量线
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnAddCeliang_Click(object sender, EventArgs e)
- {
- operationK = 0;
- selected = -1;
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 选择测量线
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnSelCeliang_Click(object sender, EventArgs e)
- {
- operationK = 1;
- }
- /// <summary>
- /// 删除测量线
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDelCeliang_Click(object sender, EventArgs e)
- {
- if (selected > -1)
- {
- DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.Areyousuretodmeasurementline.text") + "?", PdnResources.GetString("Menu.alert.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (dr == DialogResult.OK)
- {
- this.pointList1.Remove(this.pointList1[selected]);
- this.rectangleFList.Remove(this.rectangleFList[selected]);
- selected = -1;
- lineCount--;
- this.documentWorkspace.Refresh();
- }
- }
- else
- MessageBox.Show("请先选择测量线");
- }
- /// <summary>
- /// 添加测量线按钮失去焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnAddCeliang_LostFocus(object sender, EventArgs e)
- {
- operationK = -1;
- selected = -1;
- this.documentWorkspace.Refresh();
- }
- #endregion
- #region 内部类
- internal class LocalListViewItem
- {
- public DocumentWorkspace Value { get; }
- public string Display { get; }
- public LocalListViewItem(DocumentWorkspace Value, string Display)
- {
- this.Value = Value;
- this.Display = Display;
- }
- }
- internal class DocumentItem
- {
- public Bitmap bitmap;
- public GraphicsList graphicsList;
- public List<PhaseModel> phaseModels;
- //public AnalysisResult analysisResult;
- public DocumentItem(Bitmap bitmap, GraphicsList graphicsList, List<PhaseModel> phaseModels)
- {
- this.bitmap = bitmap;
- this.graphicsList = graphicsList;
- this.phaseModels = phaseModels;
- }
- }
- #endregion
- #endregion
- }
- }
|