1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876 |
- 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 BatteryCrackBallDialog : 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;
- /// <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;
- #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;
- private BinaryControlSmaller bc;
- /// <summary>
- /// 单位标尺
- /// </summary>
- private double unitLength = 1;
- #endregion
- /// <summary>
- ///
- /// </summary>
- /// <param name="appWorkspace"></param>
- public BatteryCrackBallDialog(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);
- binaryClass.imageType = 4;
- //二值化集成2
- bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);//初始化原图勾选改变事件
- binaryClass.createDocumentItemsSmaller(new string[] { "bp1"/*PdnResources.GetString("Menu.BinaryAction.BinaryExtraction.Text")*//*, PdnResources.GetString("Menu.Grainboundarytoreproduce.text")*/ }
- , this.bc, this.appWorkspace, this.documentWorkspace, this.listView1);//初始化相的工作结构
- //binaryClass.BinaryImplFinishAction += new EventHandler(this.bClassBinaryImplFinishAction);//二值化后续处理事件(参数改变时,重新处理图像)
- InitCommonButtonEvent();
- InitGridStyle();
- InitPicList();
- // SetAnalyzeModelFromXml(getModelName());
- //Action a = getball;
- //a.BeginInvoke(null, null);
- }
- 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)
- {
- for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
- {
- this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
- }
- this.dataGridView4.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();
- }
- #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 = 0D;
- this.bc.scope1Start = 0D;
- 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);
- }
- #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 != 3)//###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)
- {
- bool change = false;
- if (this.changeCount == 0)
- {
- if (this.dataGridView1.Rows.Count > 0)
- {
- if (this.saveSuccess)
- {
- DialogResult dt = MessageBox.Show(PdnResources.GetString("Menu.Whethertosavetherrentpicture.text") + "?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- if (dt == DialogResult.Yes)
- {
- string imgName = this.appWorkspace.DocumentWorkspaces[e.ItemIndex].GetFriendlyName();
- string tag = this.imageList1.Images.Keys[e.ItemIndex];
- if (this.dataGridView1.Rows.Count == 0)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Pleaseaddtype.text") + "!");
- return;
- }
- }
- }
- }
- }
- else
- {
- if (change)
- SelectedIndexChanged();
- }
- 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.dataGridView4.Rows.Clear();
- Init();
- this.documentWorkspace.GridRectangleEnabled = false;
- //getimage();
- //getbian(bitmap);
-
- binaryClass.parent = this;
- //二值化集成4
- binaryClass.listView1_SelectedIndexChanged(this.bitmap);
- binaryClass.bcDefault();
- this.documentWorkspace.Refresh();
- }
- }
- #region 原始事件
- private void Init()
- {
- this.commonControlButtons.Visible = true;
- this.dataGridView1.Rows.Clear();
- this.dataGridView4.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;
- //
- // 以下是绘制网格、标注、测量、视场等开始
- //
- 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.Black } };
- /// <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());
- //}
- 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((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength).ToString(), new Font("宋体", float.Parse(colourFont.Keys.First()), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))), new SolidBrush(colourFont.Values.First()), assistPoint4.X, assistPoint4.Y - 10);
- }
- else
- {
- graphics.DrawString((BasicCalculationHelper.GetDistance(pf[1], pf[0], 4) * unitLength).ToString(), new Font("宋体", float.Parse(colourFont.Keys.First()), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))), new SolidBrush(colourFont.Values.First()), assistPoint3.X, assistPoint3.Y - 10);
- }
- }
- }
- }
- 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();
- }
- /// <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();
- }
- private void bindGrid(List<List<int>> dataList)
- {
- if (dataList.Count < 1)
- {
- return;
- }
- int areaAll = 0;
- int areaIsK = 0;
- int areaNot = 0;
- int numIsk = 0;
- int numNot = 0;
- double d = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
- //孔隙信息列表
- dataGridView4.Rows.Clear();
- foreach (var item in dataList)
- {
- string str = "普通球";
- if (item[5] == 1)
- {
- str = "开裂球";
- areaIsK += item[4];
- numIsk++;
- }
- else
- {
- areaNot += item[4];
- numNot++;
- }
- int newPo = dataGridView4.Rows.Add();
- double crl = (double)item[6];
- crl = crl *0.001;
- dataGridView4.Rows[newPo].Cells[0].Value = (newPo+1).ToString();
- dataGridView4.Rows[newPo].Cells[1].Value = item[0];
- dataGridView4.Rows[newPo].Cells[2].Value = item[1];
- dataGridView4.Rows[newPo].Cells[3].Value = str;
- if (item[3] < 0)
- {
- dataGridView4.Rows[newPo].Cells[4].Value = -1;
- dataGridView4.Rows[newPo].Cells[5].Value = -1;
- }
- else
- {
- dataGridView4.Rows[newPo].Cells[4].Value = Math.Round(item[3] * d, 2);
- dataGridView4.Rows[newPo].Cells[5].Value = Math.Round(item[4] * d, 2);
- }
-
- dataGridView4.Rows[newPo].Cells[6].Value = crl;
- }
- dataGridView1.Rows.Clear();
- int newrow = dataGridView1.Rows.Add();
- dataGridView1.Rows[newrow].Cells[0].Value = "开裂球";
- //dataGridView1.Rows[newrow].Cells[1].Value = areaIsK * d * d;
- dataGridView1.Rows[newrow].Cells[1].Value = numIsk;
- dataGridView1.Rows[newrow].DefaultCellStyle.ForeColor = Color.Red;
- newrow = dataGridView1.Rows.Add();
- dataGridView1.Rows[newrow].Cells[0].Value = "普通球";
- //dataGridView1.Rows[newrow].Cells[1].Value = areaNot * d * d;
- dataGridView1.Rows[newrow].Cells[1].Value = numNot;
- dataGridView1.Rows[newrow].DefaultCellStyle.ForeColor = Color.Blue;
- newrow = dataGridView1.Rows.Add();
- areaAll = areaIsK + areaNot;
- dataGridView1.Rows[newrow].Cells[0].Value = "总体";
- // dataGridView1.Rows[newrow].Cells[1].Value = areaAll * d * d;
- dataGridView1.Rows[newrow].Cells[1].Value = numIsk + numNot;
- newrow = dataGridView1.Rows.Add();
- dataGridView1.Rows[newrow].Cells[0].Value = "开裂占比";
- dataGridView1.Rows[newrow].Cells[1].Value = (numIsk * 100 / (numIsk + numNot)).ToString() + "%";
- // dataGridView1.Rows[newrow].Cells[2].Value = "";
- }
- #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;
- }
- /// <summary>
- /// 添加孔隙
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnAddKong_Click(object sender, EventArgs e)
- {
- isLunKuo = false;
- isAddKong = true;
- isDelKong = false;
- }
- /// <summary>
- /// 删除孔隙
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDelKong_Click(object sender, EventArgs e)
- {
- isLunKuo = false;
- isAddKong = false;
- isDelKong = true;
- }
- /// <summary>
- /// 重新统计
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnReDrw_Click(object sender, EventArgs e)
- {
- isLunKuo = false;
- isAddKong = false;
- isDelKong = false;
- if (pointList.Count > 1)
- {
- List<PointF> pointFs = new List<PointF>(pointList.ToArray());
- lineList.Add(pointFs);
- pointList.Clear();
- }
- if (kongPointList.Count > 1)
- {
- //List<PointF> pointFs = new List<PointF>(kongPointList.ToArray());
- //kongLineList.Add(pointFs);
- //kongPointList.Clear();
- }
- if (delPointList.Count > 1)
- {
- //List<PointF> pointFs = new List<PointF>(delPointList.ToArray());
- //delLineList.Add(pointFs);
- //delPointList.Clear();
- }
- List<List<int>> CircleDate = binaryClass.DrwLunKuoLie(lineList, kongPointList, delPointList);
- bindGrid(CircleDate);
- }
- /// <summary>
- /// 移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseMove(object sender, MouseEventArgs e)
- {
- this.documentWorkspace.panel.Cursor = Cursors.Default;
- 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();
- }
- if (isLunKuo)
- {
- int cou = pointList.Count;
- if (cou > 0)
- { // 换算后的点
- PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- pointList[cou - 1] = movePoint;
- }
- }
- if (isAddKong)
- {
- //if (kongPointList.Count > 0)
- //{ // 换算后的点
- // PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- // kongPointList[kongPointList.Count - 1] = movePoint;
- //}
- }
- if (isDelKong)
- {
- //if (delPointList.Count > 0)
- //{ // 换算后的点
- // PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
- // delPointList[delPointList.Count - 1] = movePoint;
- //}
- }
- this.documentWorkspace.Refresh();
- // this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 格式化坐标点
- /// </summary>
- /// <param name="coordinateA">参考点1</param>
- /// <param name="coordinateB">参考点2</param>
- /// <param name="basicCoordinate">需比对的点</param>
- /// <returns></returns>
- private float FormatCoordinate(float coordinateA, float coordinateB, float basicCoordinate)
- {
- float coordinate = basicCoordinate;
- float[] pointArr = new float[] { coordinateA, coordinateB };
- if (coordinate > pointArr.Max() - 3)
- coordinate = pointArr.Max() - 3;
- if (coordinate < pointArr.Min() + 3)
- coordinate = pointArr.Min() + 3;
- return coordinate;
- }
- /// <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 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 (pointList1.Count > 0)
- {
- pointList1.RemoveAt(pointList1.Count - 1);
- this.documentWorkspace.Refresh();
- return;
- }
- }
- this.documentWorkspace.Refresh();
- }
- else if (operationK == 1)
- {
- canMove = false;
- movePointIndex = -1;
- // RefreshDataGridView1();
- }
- }
- if (operationK >= 0 && e.Button == MouseButtons.Left)
- {
- PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
- if (operationK == 0)
- {
- pointList1[lineCount - 1][1] = downPoint;
- }
- 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)//开裂球
- {
- pointList.Add(downPoint);
- }
- if (isAddKong)//忽略球
- {
- kongPointList.Add(downPoint);
- }
- if (isDelKong)//普通球
- {
- delPointList.Add(downPoint);
- }
- List<List<int>> CircleDate = binaryClass.DeleteBall(kongPointList, pointList, delPointList);
- 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 (isDelKong)
- {
- }
- 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 > 0)
- {
- pointList.RemoveAt(cou - 1);
- List<List<int>> CircleDate = binaryClass.DeleteBall(kongPointList, pointList, delPointList);
- bindGrid(CircleDate);
- }
- }
- if (isAddKong)
- {
- int cou = kongPointList.Count;
- if (cou > 0)
- {
- kongPointList.RemoveAt(cou - 1);
- List<List<int>> CircleDate = binaryClass.DeleteBall(kongPointList, pointList, delPointList);
- bindGrid(CircleDate);
- }
-
- }
- if (isDelKong)
- {
- int cou = delPointList.Count;
- if (cou > 0)
- {
- delPointList.RemoveAt(cou - 1);
- List<List<int>> CircleDate = binaryClass.DeleteBall(kongPointList, pointList, delPointList);
- bindGrid(CircleDate);
- }
- }
-
- this.documentWorkspace.Refresh();
- }
- }
- #endregion
- /// <summary>
- /// 准备用来处理单个颗粒的选中事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- //当点击表头部的列时,e.RowIndex==-1
- if (e.RowIndex > -1)
- {
-
- int x =Convert.ToInt32( dataGridView4.Rows[e.RowIndex].Cells[0].Value);
- binaryClass.DrwLunKuoLie1Selected(x);
- this.documentWorkspace.Refresh();
-
- }
- }
- /// <summary>
- /// 导出孔隙信息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button14_Click(object sender, EventArgs e)
- {
- 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 = DateTime.Now.ToString("yyyyMMddhhmmss") + "孔隙信息";
- DialogResult dr = exe.ShowDialog();
- if (dr != DialogResult.OK)
- {
- return;
- }
- DataTable dtb = new DataTable();
- foreach (DataGridViewColumn c in this.dataGridView4.Columns)
- {
- dtb.Columns.Add(c.HeaderText);
- }
- for (int r = 0; r < this.dataGridView4.Rows.Count; r++)
- {
- DataRow dataRow = dtb.NewRow();
- for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count; c++)
- {
- dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
- }
- dtb.Rows.Add(dataRow);
- //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
- }
- List<DataTable> list = new List<DataTable>();
- list.Add(dtb);
- this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
- }
- /// <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 path = string.Empty;
- //System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
- //fbd.Description = "请选择文件夹";
- //String str = System.Configuration.ConfigurationManager.AppSettings["FolderSelectedPath"];
- //if (!string.IsNullOrEmpty(str))
- //{
- // fbd.SelectedPath = str;
- //}
- //if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- //{
- // path = fbd.SelectedPath;
- // PrintReport(path);
- // Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- // cfa.AppSettings.Settings["FolderSelectedPath"].Value = path;
- // cfa.Save();
- // System.Configuration.ConfigurationManager.RefreshSection("appSettings");
- //}
- 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();
- foreach (DataGridViewColumn c in this.dataGridView1.Columns)
- {
- dtb.Columns.Add(c.HeaderText);
- }
- for (int r = 0; r < this.dataGridView1.Rows.Count; r++)
- {
- DataRow dataRow = dtb.NewRow();
- for (int c = 0; c < this.dataGridView1.Rows[r].Cells.Count; c++)
- {
- dataRow[this.dataGridView1.Columns[c].HeaderText] = (string)this.dataGridView1.Rows[r].Cells[c].Value.ToString();
- }
- dtb.Rows.Add(dataRow);
- //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
- }
- int index = exe.FileName.LastIndexOf('\\');
- List<DataTable> list = new List<DataTable>();
- list.Add(dtb);
- DataTable dtb1 = new DataTable();
- foreach (DataGridViewColumn c in this.dataGridView4.Columns)
- {
- dtb1.Columns.Add(c.HeaderText);
- }
- for (int r = 0; r < this.dataGridView4.Rows.Count; r++)
- {
- DataRow dataRow = dtb1.NewRow();
- for (int c = 0; c < this.dataGridView4.Rows[r].Cells.Count; c++)
- {
- dataRow[this.dataGridView4.Columns[c].HeaderText] = (string)this.dataGridView4.Rows[r].Cells[c].Value.ToString();
- }
- dtb1.Rows.Add(dataRow);
- //this.dataGridView4.Rows[r].Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
- }
- list.Add(dtb1);
- //保存处理后的图片
- 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_Path = exe.FileName.Remove(index) + "\\" + name + ".jpg";
- newBit.Save(GR_Path);
- this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
- }
- /// <summary>
- /// 导出报告
- /// </summary>
- private void PrintReport(string savePath)
- {
- List<List<string>> analysisContent = new List<List<string>>();
- for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
- {
- List<string> content1 = new List<string>();
- if (i == 0)
- {
- content1.Add(this.dataGridView1.Columns[0].HeaderText);
- content1.Add(this.dataGridView1.Columns[1].HeaderText);
- analysisContent.Add(content1);
- }
- content1 = new List<string>();
- content1.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
- content1.Add(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
- analysisContent.Add(content1);
- }
- List<string> content2 = new List<string>();
- analysisContent.Add(content2);
- for (int i = 0; i < this.dataGridView4.Rows.Count; i++)
- {
- List<string> content1 = new List<string>();
- if (i == 0)
- {
- content1.Add(this.dataGridView4.Columns[0].HeaderText);
- content1.Add(this.dataGridView4.Columns[1].HeaderText);
- content1.Add(this.dataGridView4.Columns[2].HeaderText);
- content1.Add(this.dataGridView4.Columns[3].HeaderText);
- content1.Add(this.dataGridView4.Columns[4].HeaderText);
- content1.Add(this.dataGridView4.Columns[5].HeaderText);
- analysisContent.Add(content1);
- }
- content1 = new List<string>();
- content1.Add(this.dataGridView4.Rows[i].Cells[0].Value.ToString());
- content1.Add(this.dataGridView4.Rows[i].Cells[1].Value.ToString());
- content1.Add(this.dataGridView4.Rows[i].Cells[2].Value.ToString());
- content1.Add(this.dataGridView4.Rows[i].Cells[3].Value.ToString());
- content1.Add(this.dataGridView4.Rows[i].Cells[4].Value.ToString());
- content1.Add(this.dataGridView4.Rows[i].Cells[5].Value.ToString());
- analysisContent.Add(content1);
- }
- //保存处理后的图片
- 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);
- List<Bitmap> bitList = new List<Bitmap>();
- bitList.Add(tempBit[0]);
- bitList.Add(tempBit[1]);
- Dictionary<string, string> tagInfos = new Dictionary<string, string>();
- // this.analyzeSettingModel.analyzeClassify
- AnalyzeSettingModel model = new AnalyzeSettingModel();
- string filePath = Application.StartupPath + "\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";//获取文档路径
- string filePath2 = "E:\\图形处理\\Metisvision20210204\\RBDC\\Metisvision\\bin\\Debug\\ModuleManage\\Dedicated\\MicrostructureBand_ASTME1268\\截面孔隙.docx";
- model.modulePath = filePath;
- model.savePath = savePath;
- //model.itemNumber = this.textBox2.Text;
- //model.ratingDate = this.dateTimePicker1.Value;
- //model.inspectionPerson = this.textBox4.Text;
- model.contact = "";
- //model.inspectionDepartment = this.textBox5.Text;
- //model.inspectionDate = this.dateTimePicker2.Value;
- string name = this.listView1.FocusedItem.Name.Split('.')[0];
- model.analyzeClassify = name;
- this.analyzeSettingModel = new AnalyzeSettingModel();
- //this.analyzeSettingModel.modelItems = new List<AnalyzeSavedModel.ModelItem>();
- //this.analyzeSettingModel = new AnalyzeSavedModel.ModelItem();
- //this.analyzeSettingModel.languageName = this.languageName;
- this.analyzeSettingModel = model;
- //this.analyzeSettingModel.modelItems.Add(analyzeModel);
- this.appWorkspace.CreateAnalysisReport(this.analyzeSettingModel, analysisContent, bitList, tagInfos);
- }
- #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
- #region 测试方法
- //小球识别
- private void saveimaget(int y)
- {
- DirectoryInfo derectory = new DirectoryInfo(@"E:\0\");
- int uu = 0;
- foreach (var file in derectory.GetFiles())
- {
- uu++;
- Mat colon = new Mat();
- Mat gray = new Mat();
- Mat binary = new Mat();
- Mat shifted = new Mat();
- Mat g_srcImage = Cv2.ImRead(file.FullName);
- colon = g_srcImage.Clone();
- Cv2.GaussianBlur(g_srcImage, shifted, new OpenCvSharp.Size(15, 15), 0);
- Cv2.CvtColor(shifted, gray, ColorConversionCodes.BGR2GRAY);
- //二值化
- double otsu = Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Otsu/*.Otsu*/);
- // distance transform
- Mat dist = new Mat();
- Cv2.DistanceTransform(binary, dist, DistanceTypes.L2, DistanceMaskSize.Mask3);
- //归一化数据
- Cv2.Normalize(dist, dist, 0, 1, NormTypes.MinMax);
- // binary
- Cv2.Threshold(dist, dist, 0.4, 1, ThresholdTypes.Binary);
- // markers
- Mat dist_m = new Mat();
- dist.ConvertTo(dist_m, MatType.CV_8UC1);
- OpenCvSharp.Point[][] contours = new OpenCvSharp.Point[][] { };
- HierarchyIndex[] hierarchyIndex = new HierarchyIndex[] { };
- Cv2.FindContours(dist_m, out contours, out hierarchyIndex, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double maxarea = 0;
- int maxindex = 0;
- // create markers
- Mat markers = Mat.Zeros(g_srcImage.Size(), MatType.CV_32SC1);
- for (int t = 0; t < contours.Length; t++)
- {
- if (Cv2.ContourArea(contours[t]) > maxarea)
- {
- maxarea = Cv2.ContourArea(contours[t]);
- maxindex = t;
- }
- }
- Cv2.DrawContours(markers, contours, maxindex, new Scalar(1), -1);
- // Cv2.ImShow("分割后", markers);
- Cv2.Circle(markers, new OpenCvSharp.Point(5, 5), 30, new Scalar(255), -1);
- //形体学操作 去掉干扰
- InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(g_srcImage, g_srcImage, MorphTypes.Erode, kernel);
- Cv2.Watershed(g_srcImage, markers);
- // 颜色填充与最终显示
- int index = 0;
- Dictionary<int, List<OpenCvSharp.Point>> particlePixles = new Dictionary<int, List<OpenCvSharp.Point>>();
- for (int row = 0; row < markers.Rows; row++)
- {
- for (int col = 0; col < markers.Cols; col++)
- {
- index = markers.At<int>(row, col);
- if (index > 0 && index <= contours.Length)
- {
- if (particlePixles.ContainsKey(index))
- {
- particlePixles[index].Add(new OpenCvSharp.Point(col, row));
- }
- else
- {
- List<OpenCvSharp.Point> Pixles = new List<OpenCvSharp.Point>();
- Pixles.Add(new OpenCvSharp.Point(col, row));
- particlePixles.Add(index, Pixles);
- }
- }
- }
- }
- foreach (var item in particlePixles)
- {
- Rect rect = Cv2.BoundingRect(item.Value);
- List<OpenCvSharp.Point> pixel = item.Value;
- Mat forwrit = g_srcImage[rect];
- for (int i = 0; i < g_srcImage.Cols; i++)
- {
- for (int j = 0; j < g_srcImage.Rows; j++)
- {
- OpenCvSharp.Point pp = new OpenCvSharp.Point() { X = i, Y = j };
- if (pixel.Contains(pp))
- {
- continue;
- }
- colon.Set<Vec3b>(j, i, new Vec3b(255, 0, 0));
- }
- }
- }
- Cv2.ImWrite("E:/ballcut/" + file.Name, colon);
- }
- }
- //小球识别
- private void getball()
- {
- DirectoryInfo derectory = new DirectoryInfo(@"E:\JPEGImages\");
- int uu = 0;
- foreach (var file in derectory.GetFiles())
- {
- uu++;
- Mat colon = new Mat();
- Mat gray = new Mat();
- Mat binary = new Mat();
- Mat shifted = new Mat();
- Mat g_srcImage = Cv2.ImRead(file.FullName);
- colon = g_srcImage.Clone();
- Cv2.GaussianBlur(g_srcImage, shifted, new OpenCvSharp.Size(15, 15), 0);
- Cv2.CvtColor(shifted, gray, ColorConversionCodes.BGR2GRAY);
- //二值化
- double otsu = Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Otsu/*.Otsu*/);
- // distance transform
- Mat dist = new Mat();
- Cv2.DistanceTransform(binary, dist, DistanceTypes.L2, DistanceMaskSize.Mask3);
- //归一化数据
- Cv2.Normalize(dist, dist, 0, 1, NormTypes.MinMax);
- // binary
- Cv2.Threshold(dist, dist, 0.4, 1, ThresholdTypes.Binary);
- // markers
- Mat dist_m = new Mat();
- dist.ConvertTo(dist_m, MatType.CV_8UC1);
- OpenCvSharp.Point[][] contours = new OpenCvSharp.Point[][] { };
- HierarchyIndex[] hierarchyIndex = new HierarchyIndex[] { };
- Cv2.FindContours(dist_m, out contours, out hierarchyIndex, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- // create markers
- Mat markers = Mat.Zeros(g_srcImage.Size(), MatType.CV_32SC1);
- for (int t = 0; t < contours.Length; t++)
- {
- Cv2.DrawContours(markers, contours, t, new Scalar(t + 1), -1);
- }
- // Cv2.ImShow("分割后", markers);
- Cv2.Circle(markers, new OpenCvSharp.Point(5, 5), 30, new Scalar(255), -1);
- //形体学操作 去掉干扰
- InputArray kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(g_srcImage, g_srcImage, MorphTypes.Erode, kernel);
- Cv2.Watershed(g_srcImage, markers);
- // 颜色填充与最终显示
- int index = 0;
- Dictionary<int, List<OpenCvSharp.Point>> particlePixles = new Dictionary<int, List<OpenCvSharp.Point>>();
- for (int row = 0; row < markers.Rows; row++)
- {
- for (int col = 0; col < markers.Cols; col++)
- {
- index = markers.At<int>(row, col);
- if (index > 0 && index <= contours.Length)
- {
- if (particlePixles.ContainsKey(index))
- {
- particlePixles[index].Add(new OpenCvSharp.Point(col, row));
- }
- else
- {
- List<OpenCvSharp.Point> Pixles = new List<OpenCvSharp.Point>();
- Pixles.Add(new OpenCvSharp.Point(col, row));
- particlePixles.Add(index, Pixles);
- }
- }
- }
- }
- int k = 0;
- foreach (var item in particlePixles)
- {
- if (item.Value.Count > 99999)
- {
- k = item.Key;
- break;
- }
- }
- particlePixles.Remove(k);
- int index1 = 0;
- foreach (var item in particlePixles)
- {
- Rect rect = Cv2.BoundingRect(item.Value);
- List<OpenCvSharp.Point> pixel = item.Value;
- Mat forwrit = colon[rect];
-
- Mat small = forwrit.Clone();
- for (int i = 0; i < small.Cols; i++)
- {
- for (int j = 0; j < small.Rows; j++)
- {
- OpenCvSharp.Point pp = new OpenCvSharp.Point() { X = i + rect.X, Y = j + rect.Y };
- if (pixel.Contains(pp))
- {
- continue;
- }
- small.Set<Vec3b>(j, i, new Vec3b(255, 0, 0));
- }
- }
- Cv2.ImWrite("E:/0/" +file.Name + index1.ToString() + ".jpg", small);
- index1++;
-
- }
- }
- }
- #endregion
- #endregion
- }
- }
|