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
///
/// 图像面板
///
private DocumentWorkspaceWindow documentWorkspace;
///
/// 公共控件
///
private CommonControlButtons commonControlButtons;
///
/// 图片
///
private Bitmap bitmap;
///
/// 球状长宽比
///
private decimal globulAraspectRatio;
///
/// 片状长宽比
///
private decimal flakAspectRatio;
///
/// 直径
///
private double length;
///
/// 圆心
///
private PointF center;
///
/// 点集合
///
List points = new List();
///
/// 停止绘制
///
private bool stop;
///
/// 操作区分 0:折线分割 1:多边形添加 2:圆形添加
///
private int operation = -1;
///
/// 是否绘制
///
private bool canDraw = true;
///
/// 确定
///
private bool determine = false;
Mat mat = new Mat();
Mat mat1 = new Mat();
public SpheroidizationGradingEditDialog(DocumentWorkspaceWindow documentWorkspace, Bitmap bitmap, decimal globulAraspectRatio, decimal flakAspectRatio)
{
InitializeComponent();
InitializeLanguageText();
this.globulAraspectRatio = globulAraspectRatio;
this.flakAspectRatio = flakAspectRatio;
this.comboBox1.SelectedIndex = 0;
this.bitmap = bitmap;
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.FromImage(bitmap);
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
///
/// 关闭
///
///
///
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();
}
///
/// 切换
///
///
///
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();
}
///
/// 折线分割
///
///
///
private void button3_Click(object sender, EventArgs e)
{
this.points.Clear();
this.canDraw = true;
this.operation = 0;
this.stop = true;
}
///
/// 多边形添加
///
///
///
private void button4_Click(object sender, EventArgs e)
{
this.points.Clear();
this.canDraw = true;
this.operation = 1;
this.stop = true;
}
///
/// 圆形添加
///
///
///
private void button5_Click(object sender, EventArgs e)
{
this.points.Clear();
this.canDraw = true;
this.operation = 2;
this.stop = true;
}
///
/// 画布绘制
///
///
///
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);
}
}
///
/// 绘制
///
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 points = new List();
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 points = new List();
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);
}
}
}
}
///
/// 鼠标按下
///
///
///
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 points = new List();
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 points = new List();
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();
}
///
/// 移动
///
///
///
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();
}
}
}
///
/// 鼠标抬起
///
///
///
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();
}
}
///
/// 确定按钮
///
///
///
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);
}
}
}