123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- using OpenCvSharp;
- using PaintDotNet.Adjust;
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.CommTool;
- using PaintDotNet.CustomControl;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PaintDotNet.DedicatedAnalysis.SpheroidizedStructure.GBT38770
- {
- internal class SpheroidizationGradingEditDialog : Form
- {
- #region 控件
- private GroupBox groupBox1;
- private GroupBox groupBox2;
- private GroupBox groupBox3;
- private Button button2;
- private Button button1;
- private ComboBox comboBox1;
- private Label label2;
- private Label label1;
- private Button button5;
- private Button button4;
- private Button button3;
- private GroupBox groupBox4;
- private Label label3;
- #endregion
- /// <summary>
- /// 图像面板
- /// </summary>
- private DocumentWorkspaceWindow documentWorkspace;
- /// <summary>
- /// 公共控件
- /// </summary>
- private CommonControlButtons commonControlButtons;
- /// <summary>
- /// 图片
- /// </summary>
- private Mat imageMat;
- /// <summary>
- /// 球状长宽比
- /// </summary>
- private decimal globulAraspectRatio;
- /// <summary>
- /// 片状长宽比
- /// </summary>
- private decimal flakAspectRatio;
- /// <summary>
- /// 直径
- /// </summary>
- private double length;
- /// <summary>
- /// 圆心
- /// </summary>
- private PointF center;
- /// <summary>
- /// 点集合
- /// </summary>
- List<RectangleF> points = new List<RectangleF>();
- /// <summary>
- /// 停止绘制
- /// </summary>
- private bool stop;
- /// <summary>
- /// 操作区分 0:折线分割 1:多边形添加 2:圆形添加
- /// </summary>
- private int operation = -1;
- /// <summary>
- /// 是否绘制
- /// </summary>
- private bool canDraw = true;
- /// <summary>
- /// 确定
- /// </summary>
- private bool determine = false;
- Mat mat = new Mat();
- Mat mat1 = new Mat();
- public SpheroidizationGradingEditDialog(DocumentWorkspaceWindow documentWorkspace, Mat imageMat, decimal globulAraspectRatio, decimal flakAspectRatio)
- {
- InitializeComponent();
- InitializeLanguageText();
- this.globulAraspectRatio = globulAraspectRatio;
- this.flakAspectRatio = flakAspectRatio;
- this.comboBox1.SelectedIndex = 0;
- this.imageMat = imageMat;
- this.documentWorkspace = documentWorkspace;
- this.documentWorkspace.PhaseModels[0].mat.CopyTo(this.mat);
- this.documentWorkspace.PhaseModels[0].mat.CopyTo(this.mat1);
- this.groupBox4.Controls.Add(documentWorkspace);
- Document document = Document.FromImageMat(imageMat.Clone());//待测试效率
- this.documentWorkspace.Document = document;
- this.documentWorkspace.Visible = true;
- this.commonControlButtons = new CommonControlButtons();
- this.commonControlButtons.Dock = DockStyle.Top;
- this.commonControlButtons.Height = 30;
- this.commonControlButtons.HideZoomToWindowAndActualSize();
- this.groupBox4.Controls.Add(commonControlButtons);
- this.documentWorkspace.panel.MouseDown += OnMouseDown;
- this.documentWorkspace.panel.Paint += Panel_Paint;
- this.documentWorkspace.panel.MouseMove += onMouseMove;
- this.documentWorkspace.panel.MouseUp += onMouseUp;
- InitCommonButtonEvent();
- }
- #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
- #region 初始化
- private void InitializeLanguageText()
- {
- this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
- this.button2.Text = PdnResources.GetString("Menu.File.Close.Text");
- this.button1.Text = PdnResources.GetString("Form.OkButton.Text");
- this.groupBox2.Text = PdnResources.GetString("Menu.Imagement.Measurementlist.Attributes.text");
- this.comboBox1.Items.AddRange(new object[] {
- PdnResources.GetString("Menu.Globularcarbide.Text"),
- PdnResources.GetString("Menu.Dedicatedanalysis.blackmetal.lamellarpearlite.text")});
- this.label2.Text = PdnResources.GetString("Menu.Aspectio.Text") + ":";
- this.label1.Text = PdnResources.GetString("Menu.Type.text") + ":";
- this.groupBox3.Text = PdnResources.GetString("Menu.BinaryAction.Text");
- this.button5.Text = PdnResources.GetString("Menu.Thecircularadded.Text");
- this.button4.Text = PdnResources.GetString("Menu.polygonadd.text");
- this.button3.Text = PdnResources.GetString("Menu.Linesegmentation.Text");
- this.groupBox4.Text = PdnResources.GetString("Menu.organizationalproperties.Text");
- this.Text = PdnResources.GetString("Menu.anizationhierarchicalediting.Text");
- }
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.button2 = new System.Windows.Forms.Button();
- this.button1 = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.label3 = new System.Windows.Forms.Label();
- this.comboBox1 = new System.Windows.Forms.ComboBox();
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.button5 = new System.Windows.Forms.Button();
- this.button4 = new System.Windows.Forms.Button();
- this.button3 = new System.Windows.Forms.Button();
- this.groupBox4 = new System.Windows.Forms.GroupBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.button2);
- this.groupBox1.Controls.Add(this.button1);
- this.groupBox1.Location = new System.Drawing.Point(12, 12);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(862, 60);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(751, 19);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(96, 27);
- this.button2.TabIndex = 1;
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(649, 20);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(96, 27);
- this.button1.TabIndex = 0;
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.label3);
- this.groupBox2.Controls.Add(this.comboBox1);
- this.groupBox2.Controls.Add(this.label2);
- this.groupBox2.Controls.Add(this.label1);
- this.groupBox2.Location = new System.Drawing.Point(12, 78);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(214, 86);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(72, 60);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(23, 12);
- this.label3.TabIndex = 2;
- this.label3.Text = "< 0";
- //
- // comboBox1
- //
- this.comboBox1.FormattingEnabled = true;
- this.comboBox1.Location = new System.Drawing.Point(56, 25);
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.Size = new System.Drawing.Size(110, 20);
- this.comboBox1.TabIndex = 0;
- this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(13, 60);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(53, 12);
- this.label2.TabIndex = 1;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(13, 29);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(41, 12);
- this.label1.TabIndex = 0;
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.button5);
- this.groupBox3.Controls.Add(this.button4);
- this.groupBox3.Controls.Add(this.button3);
- this.groupBox3.Location = new System.Drawing.Point(12, 170);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(214, 94);
- this.groupBox3.TabIndex = 1;
- this.groupBox3.TabStop = false;
- //
- // button5
- //
- this.button5.Location = new System.Drawing.Point(6, 53);
- this.button5.Name = "button5";
- this.button5.Size = new System.Drawing.Size(96, 27);
- this.button5.TabIndex = 3;
- this.button5.UseVisualStyleBackColor = true;
- this.button5.Click += new System.EventHandler(this.button5_Click);
- //
- // button4
- //
- this.button4.Location = new System.Drawing.Point(112, 20);
- this.button4.Name = "button4";
- this.button4.Size = new System.Drawing.Size(96, 27);
- this.button4.TabIndex = 2;
- this.button4.UseVisualStyleBackColor = true;
- this.button4.Click += new System.EventHandler(this.button4_Click);
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(6, 20);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(96, 27);
- this.button3.TabIndex = 1;
- this.button3.UseVisualStyleBackColor = true;
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // groupBox4
- //
- this.groupBox4.Location = new System.Drawing.Point(232, 78);
- this.groupBox4.Name = "groupBox4";
- this.groupBox4.Size = new System.Drawing.Size(642, 521);
- this.groupBox4.TabIndex = 1;
- this.groupBox4.TabStop = false;
- //
- // SpheroidizationGradingEditDialog
- //
- this.ClientSize = new System.Drawing.Size(886, 611);
- this.Controls.Add(this.groupBox4);
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.Name = "SpheroidizationGradingEditDialog";
- this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SpheroidizationGradingEditDialog_FormClosing);
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox2.PerformLayout();
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- this.operation = -1;
- this.points.Clear();
- SpheroidizationGradingDialog spheroidizationGradingDialog = (SpheroidizationGradingDialog)this.Owner;
- this.documentWorkspace.PhaseModels[0].mat = this.mat1;
- spheroidizationGradingDialog.EditPageClose(this.documentWorkspace);
- this.Close();
- }
- /// <summary>
- /// 切换
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (PdnResources.GetString("Menu.Globularcarbide.Text").Equals(this.comboBox1.SelectedItem))
- this.label3.Text = "< " + this.globulAraspectRatio.ToString();
- if (PdnResources.GetString("Menu.Dedicatedanalysis.blackmetal.lamellarpearlite.text").Equals(this.comboBox1.SelectedItem))
- this.label3.Text = "> " + this.flakAspectRatio.ToString();
- }
- /// <summary>
- /// 折线分割
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- this.points.Clear();
- this.canDraw = true;
- this.operation = 0;
- this.stop = true;
- }
- /// <summary>
- /// 多边形添加
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button4_Click(object sender, EventArgs e)
- {
- this.points.Clear();
- this.canDraw = true;
- this.operation = 1;
- this.stop = true;
- }
- /// <summary>
- /// 圆形添加
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button5_Click(object sender, EventArgs e)
- {
- this.points.Clear();
- this.canDraw = true;
- this.operation = 2;
- this.stop = true;
- }
- /// <summary>
- /// 画布绘制
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Panel_Paint(object sender, PaintEventArgs e)
- {
- if (this.documentWorkspace.CompositionSurface != null)
- {
- //
- // 以下是计算绘制图片的位置和大小并绘制图片
- //
- System.Drawing.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.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
- e.Graphics.TranslateTransform(-x, -y);
- }
- }
- /// <summary>
- /// 绘制
- /// </summary>
- private void Draw(Graphics graphics)
- {
- if(this.canDraw)
- {
- Pen linePen = new Pen(Color.Black, 3);
- linePen.DashStyle = DashStyle.DashDotDot;
- // 抗锯齿
- graphics.SmoothingMode = SmoothingMode.AntiAlias;
- if (this.operation == 0)
- {
- List<PointF> points = new List<PointF>();
- foreach (var point in this.points)
- {
- points.Add(new PointF(point.X + 1, point.Y + 1));
- }
- if (points.Count > 1)
- graphics.DrawLines(linePen, points.ToArray());
- }
- else if (this.operation == 1)
- {
- List<PointF> points = new List<PointF>();
- foreach (var point in this.points)
- {
- points.Add(new PointF(point.X + 1, point.Y + 1));
- }
- if (points.Count > 0)
- graphics.DrawPolygon(linePen, points.ToArray());
- }
- else if (this.operation == 2)
- {
- if (this.points.Count == 2)
- {
- // 判断位置
- PointF pointF = new PointF(this.points[0].X + 1, this.points[0].Y + 1);
- PointF pointF1 = new PointF(this.points[1].X + 1, this.points[1].Y + 1);
- // 直径
- this.length = BasicCalculationHelper.GetDistance(pointF, pointF1, 10);
- double x = (pointF.X + pointF1.X) / 2;
- double y = (pointF.Y + pointF1.Y) / 2;
- // 圆心
- this.center = new PointF((float)x, (float)y);
- if (pointF.X != pointF1.X || pointF.Y != pointF1.Y)
- graphics.DrawEllipse(linePen, center.X - (float)length / 2, center.Y - (float)length / 2, (float)length, (float)length);
- }
- }
- }
- }
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- private void OnMouseDown(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if(e.Button == MouseButtons.Left)
- {
- if((this.operation == 0 || this.operation == 1) && this.stop)
- {
- RectangleF pointF = new RectangleF(point1.X - 1, point1.Y - 1, 2, 2);
- points.Add(pointF);
- pointF = new RectangleF(point1.X, point1.Y, 2, 2);
- points.Add(pointF);
- }
- else if(this.operation == 2 && this.stop)
- {
- RectangleF pointF = new RectangleF(point1.X - 1, point1.Y - 1, 2, 2);
- points.Add(pointF);
- pointF = new RectangleF(point1.X, point1.Y, 2, 2);
- points.Add(pointF);
- }
- }
- else
- {
- if(this.operation == 1)
- {
- List<PointF> points = new List<PointF>();
- foreach (var point in this.points)
- {
- points.Add(new PointF(point.X + 1, point.Y + 1));
- }
- if (points.Count > 2)
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.PolygonAdd
- (this.mat, this.documentWorkspace.PhaseModels[0].color, points);
- }
- else if(this.operation == 0)
- {
- List<PointF> points = new List<PointF>();
- foreach (var point in this.points)
- {
- points.Add(new PointF(point.X + 1, point.Y + 1));
- }
- if (points.Count > 1)
- {
- for (int i = 0; i < points.Count; i++)
- {
- if(i > 0)
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.LineSplit
- (this.mat, points[i - 1], points[i], 3);
- }
- }
- }
- this.stop = false;
- this.canDraw = false;
- }
- this.documentWorkspace.Refresh();
- }
- /// <summary>
- /// 移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseMove(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if (this.operation == 2 && this.stop)
- {
- if (this.points.Count == 2)
- {
- if (points.Count > 0)
- {
- RectangleF pointF = new RectangleF(point1.X - 1, point1.Y - 1, 2, 2);
- points[1] = pointF;
- this.documentWorkspace.Refresh();
- }
- }
- }
- else if ((this.operation == 0 || this.operation == 1) && this.stop)
- {
- if (points.Count > 0)
- {
- RectangleF pointF = new RectangleF(point1.X - 1, point1.Y - 1, 2, 2);
- points[this.points.Count - 1] = pointF;
- this.documentWorkspace.Refresh();
- }
- }
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void onMouseUp(object sender, MouseEventArgs e)
- {
- // 换算后的点
- PointF point1 = documentWorkspace.GetScalePoint(e.Location);
- if(this.operation == 2 && this.stop)
- {
- RectangleF pointF = new RectangleF(point1.X - 1, point1.Y - 1, 2, 2);
- points.Add(pointF);
- this.operation = -1;
- this.canDraw = false;
- this.documentWorkspace.PhaseModels[0].mat = PreActionIntent.EllipseAdd
- (this.mat, this.documentWorkspace.PhaseModels[0].color, new RectangleF
- ((float)this.center.X - (float)this.length / 2, (float)this.center.Y - (float)this.length / 2, (float)this.length, (float)this.length));
- this.documentWorkspace.Refresh();
- }
- }
- /// <summary>
- /// 确定按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.SpheroidizationGradingEditDialog_FormClosing);
- this.operation = -1;
- this.points.Clear();
- SpheroidizationGradingDialog spheroidizationGradingDialog = (SpheroidizationGradingDialog)this.Owner;
- this.documentWorkspace.PhaseModels[0].mat = this.mat;
- spheroidizationGradingDialog.EditPageClose(this.documentWorkspace);
- this.Close();
- }
- private void SpheroidizationGradingEditDialog_FormClosing(object sender, FormClosingEventArgs e)
- {
- this.operation = -1;
- this.points.Clear();
- SpheroidizationGradingDialog spheroidizationGradingDialog = (SpheroidizationGradingDialog)this.Owner;
- this.documentWorkspace.PhaseModels[0].mat = this.mat1;
- spheroidizationGradingDialog.EditPageClose(this.documentWorkspace);
- }
- }
- }
|