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.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PaintDotNet.DedicatedAnalysis.Battery
{
internal partial class Membranes : PdnBaseForm
{
#region 属性
//二值化集成1
BinaryClassForB binaryClass;
private int menuId;
///
/// 选中图片的bitmap
///
private Bitmap bitmap;
///
/// 存在视场标记
///
private bool existViewFlag = false;
private int changeCount;
///
/// 主空间
///
private AppWorkspace appWorkspace;
///
/// 图像面板
///
private DocumentWorkspaceWindow documentWorkspace;
///
/// 公共控件
///
private GeneralAnalysisCommonControlButtons commonControlButtons;
///
/// 图片测量记录
///
private List pictureDataTables = new List();
///
/// 网格Xml
///
private GrainSizePointCountingMethodModel grainSizePointCountingMethodModel;
///
/// 数据变化
///
private bool saveSuccess = false;
private BinaryControlSmaller bc;
#endregion 属性
#region 数据属性
///
/// 折线点的集合
///
public List pointArray = new List();
///
/// 存储所有轮廓线的点集合
///
private List pointList = new List();
///
/// 存储所有轮廓线集合
///
private List> lineList = new List>();
///
/// 存储所有孔隙线的点集合
///
private List kongPointList = new List();
///
/// 存储所有孔隙线集合
///
private List> kongLineList = new List>();
///
/// 存储所有删除孔隙线的点集合
///
private List delPointList = new List();
///
/// 存储所有删除孔隙线集合
///
private List> delLineList = new List>();
//轮廓修改状态
bool isLunKuo = false;
//添加孔隙状态
bool isAddKong = false;
//删除孔隙状态
bool isDelKong = false;
//绘制轮廓
bool isDrwKuo = false;
//绘制轮廓结束
bool DrwDone = false;
#endregion
///
///
///
///
public Membranes(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);
//二值化集成2
bc.OriginCheckedChangedAction += new EventHandler(this.bcOriginCheckedChanged);//初始化原图勾选改变事件
binaryClass.imageType = 5;
binaryClass.createDocumentItemsSmaller(new string[] { "bp1" }, this.bc, this.appWorkspace, this.documentWorkspace, this.listView1);//初始化相的工作结构
binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
InitCommonButtonEvent();
InitGridStyle();
InitPicList();
// SetAnalyzeModelFromXml(getModelName());
}
private void ShowImgEvent(object sender, EventArgs e)
{
if (this.listView1.Items.Count == 0)
{
return;
}
listView1.Focus();
this.listView1.Items[0].Focused = true;
this.listView1.Items[0].Selected = true;
}
#region 二值化相关方法
private void InclusionsStandardDialog_FormClosing(object sender, FormClosingEventArgs e)
{
binaryClass.saveParams();
}
private void ShownChoiseItemAndInitData(object sender, EventArgs e)
{
binaryClass.RefreshHistogramControl1Values();
}
///
/// 添加参数改变的监听
///
///
///
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;
}
///
/// 显示原图/原图+二值图
///
///
///
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
///
/// 初始化画面数据
///
private void InitGridStyle()
{
grainSizePointCountingMethodModel = XmlSerializeHelper.DESerializer(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)
{
}
}
}
///
/// 初始化图片列表数据
///
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 > 1)//###20186
{
e.SortResult = (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) > 0) ? 1 : (Convert.ToDouble(e.CellValue1) - Convert.ToDouble(e.CellValue2) < 0) ? -1 : 0;
}
//否则,按字符串比较
else
{
e.SortResult = System.String.Compare(Convert.ToString(e.CellValue1), Convert.ToString(e.CellValue2));
}
e.Handled = true;//不能省掉,不然没效果
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
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++;
}
///
/// 切换图片
///
///
///
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedIndexChanged();
}
List list = new List();
///
/// 切换图片
///
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;
isDrwKuo = false;
DrwDone = false;
this.documentWorkspace.Refresh();
this.dataGridView1.Rows.Clear();
this.dataGridView4.Rows.Clear();
Init();
this.documentWorkspace.GridRectangleEnabled = false;
//getimage();
//getbian(bitmap);
binaryClass.CilCount = Convert.ToInt32(txt_RightLineWeight.Text);
//二值化集成4
binaryClass.listView1_SelectedIndexChanged(this.bitmap);
binaryClass.bcDefault();
// btnCircleDrawing_Click(null, null);
this.documentWorkspace.Refresh();
}
}
#region 原始事件
private void Init()
{
this.commonControlButtons.Visible = true;
this.dataGridView1.Rows.Clear();
this.dataGridView4.Rows.Clear();
this.documentWorkspace.phaseModels.Clear();
}
///
/// 画布绘制
///
///
///
private void Panel_Paint(object sender, PaintEventArgs e)
{
if (this.documentWorkspace.CompositionSurface != null)
{
//
// 以下是计算绘制图片的位置和大小并绘制图片
//
Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
//if (pointArray.Count > 1)
//{
// e.Graphics.DrawLines(new Pen(Color.FromArgb(0, 0, 0), 2), pointArray.ToArray());
//}
//
// 以下是绘制网格、标注、测量、视场等开始
//
e.Graphics.TranslateTransform(x, y);
e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
Draw(e.Graphics);
//e.Graphics.DrawLines(new Pen(this.panel1.BackColor, Convert.ToInt32(this.txt_LeftLineWeight.Text)), list.ToArray());
e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
e.Graphics.TranslateTransform(-x, -y);
}
if (bc.isedit)
{
bc.isedit = false;
List> CircleDate = bc.data;
bindGrid(CircleDate);
}
}
///
/// 绘制
///
private void Draw(Graphics graphics)
{
//轮廓手动干预线
if (lineList.Count > 0)
{
for (int i = 0; i < lineList.Count; i++)
{
graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), lineList[i].ToArray());
}
}
if (pointList.Count > 1)
{
graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 255), 2), pointList.ToArray());
}
//孔隙手动干预线
Color color = bc.BinaryBackColor;
if (kongLineList.Count > 0)
{
for (int i = 0; i < kongLineList.Count; i++)
{
graphics.DrawLines(new Pen(color, 2), kongLineList[i].ToArray());
}
}
if (kongPointList.Count > 1)
{
graphics.DrawLines(new Pen(color, 2), kongPointList.ToArray());
}
//删除孔隙
//if (delLineList.Count > 0)
//{
// for (int i = 0; i < delLineList.Count; i++)
// {
// graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 0), 2), delLineList[i].ToArray());
// }
//}
//if (delPointList.Count > 1)
//{
// graphics.DrawLines(new Pen(Color.FromArgb(255, 0, 0), 2), delPointList.ToArray());
//}
}
///
/// 画同心圆
///
///
///
private void btnCircleDrawing_Click(object sender, EventArgs e)
{
if (this.listView1.Items.Count == 0)
{
return;
}
if (rb_RightCircle.Checked)
{
List> CircleDate = binaryClass.getCircleDate(Convert.ToInt32(txt_RightLineWeight.Text));
bindGrid(CircleDate);
}
else
{
List> CircleDate = binaryClass.getCircleDate(0);
bindGrid(CircleDate);
}
}
private void bindGrid(List> dataList)
{
if (dataList == null)
{
return;
}
if (dataList.Count < 1)
{
return;
}
//标尺 微米/像素
double d = binaryClass.GetRuler();
// double d = this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRuler(MeasurementUnit.Micron);
//图像自动识别标尺x微米/像素
//double q= binaryClass.GetRuler();
//孔隙率信息列表
dataGridView1.Rows.Clear();
Mat mat = this.documentWorkspace.PhaseModels[0].mat;
double maxArea = mat.Rows* (mat.Cols-70);
double totalArea = 0;
//孔隙信息列表
dataGridView4.Rows.Clear();
for (int i = 1; i < dataList.Count - 1; i++)
{
if (dataList[i].Count < 6)
{ continue; }
int newPo = dataGridView4.Rows.Add();
//序号
dataGridView4.Rows[newPo].Cells[0].Value = (newPo + 1).ToString();
//X
dataGridView4.Rows[newPo].Cells[1].Value = dataList[i][0];
//Y
dataGridView4.Rows[newPo].Cells[2].Value = dataList[i][1];
//长
dataGridView4.Rows[newPo].Cells[3].Value = Math.Round(Convert.ToDouble(dataList[i][2]) * d, 4).ToString();
//宽
dataGridView4.Rows[newPo].Cells[4].Value = Math.Round(Convert.ToDouble(dataList[i][3]) * d, 4).ToString();
//面积
dataGridView4.Rows[newPo].Cells[5].Value = Math.Round(Convert.ToDouble(dataList[i][4]) * d * d, 4).ToString();
//标识
dataGridView4.Rows[newPo].Cells[6].Value = dataList[i][5];
totalArea = totalArea + Math.Round(Convert.ToDouble(dataList[i][4]) * d * d, 4);
}
int newrow = dataGridView1.Rows.Add();
dataGridView1.Rows[newrow].Cells[0].Value = Math.Round(maxArea * d * d, 4).ToString();
dataGridView1.Rows[newrow].Cells[1].Value = Math.Round(totalArea, 4).ToString();
dataGridView1.Rows[newrow].Cells[2].Value = Math.Round((100*totalArea)/ (maxArea * d * d), 4).ToString() + "%";
}
#region 手动干预
///
/// 修改轮廓
///
///
///
private void btnLunkuo_Click(object sender, EventArgs e)
{
isLunKuo = true;
isAddKong = false;
isDelKong = false;
isDrwKuo = false;
//DrwDone = false;
}
///
/// 添加孔隙
///
///
///
private void btnAddKong_Click(object sender, EventArgs e)
{
isLunKuo = false;
isAddKong = true;
isDelKong = false;
isDrwKuo = false;
//DrwDone = false;
}
///
/// 删除孔隙
///
///
///
private void btnDelKong_Click(object sender, EventArgs e)
{
isLunKuo = false;
isAddKong = false;
isDelKong = true;
isDrwKuo = false;
//DrwDone = false;
}
///
/// 绘制轮廓
///
///
///
private void btnDrwKuo_Click(object sender, EventArgs e)
{
isLunKuo = false;
isAddKong = false;
isDelKong = false;
isDrwKuo = true;
DrwDone = true;
}
///
/// 重新统计
///
///
///
private void btnReDrw_Click(object sender, EventArgs e)
{
if (pointList.Count > 1)
{
List pointFs = new List(pointList.ToArray());
lineList.Add(pointFs);
pointList.Clear();
}
if (kongPointList.Count > 1)
{
List pointFs = new List(kongPointList.ToArray());
kongLineList.Add(pointFs);
kongPointList.Clear();
}
if (delPointList.Count > 1)
{
}
List> CircleDate = new List>();
if (DrwDone)
{
CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
}
else
{
CircleDate = binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
}
bindGrid(CircleDate);
isLunKuo = false;
isAddKong = false;
isDelKong = false;
isDrwKuo = false;
// DrwDone = false;
}
///
/// 移动
///
///
///
private void onMouseMove(object sender, MouseEventArgs e)
{
//if (e.Button == MouseButtons.Left)
//{
if (isLunKuo)
{
int cou = pointList.Count;
if (cou > 0)
{ // 换算后的点
PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
pointList[cou - 1] = movePoint;
}
}
if (isAddKong)
{
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;
//}
}
if (isDrwKuo)
{
int cou = pointList.Count;
if (cou > 0)
{ // 换算后的点
PointF movePoint = this.documentWorkspace.GetScalePoint(e.Location);
pointList[cou - 1] = movePoint;
}
}
this.documentWorkspace.Refresh();
// }
}
///
/// 鼠标抬起
///
///
///
private void OnMouseUp(object sender, MouseEventArgs e)
{
}
///
/// 鼠标按下
///
///
///
private void OnMouseDown(object sender, MouseEventArgs e)
{
PointF downPoint = this.documentWorkspace.GetScalePoint(e.Location);
if (e.Button == MouseButtons.Left)
{
if (isLunKuo)
{
if (pointList.Count == 0)
{
pointList.Add(downPoint);
pointList.Add(downPoint);
}
else
{
pointList.Add(downPoint);
}
}
if (isAddKong)
{
if (kongPointList.Count == 0)
{
kongPointList.Add(downPoint);
kongPointList.Add(downPoint);
}
else
{
kongPointList.Add(downPoint);
}
}
if (isDelKong)
{
//if (delPointList.Count == 0)
//{
// delPointList.Add(downPoint);
// delPointList.Add(downPoint);
//}
//else
//{
// delPointList.Add(downPoint);
//}
delPointList.Add(downPoint);
List> CircleDate = new List>();
if (DrwDone)
{
CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
}
else
{
binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
}
bindGrid(CircleDate);
}
if (isDrwKuo)
{
if (pointList.Count == 0)
{
pointList.Add(downPoint);
pointList.Add(downPoint);
}
else
{
pointList.Add(downPoint);
}
}
}
if (e.Button == MouseButtons.Right)
{
if (isLunKuo)
{
if (pointList.Count > 2)
{
pointList.RemoveAt(pointList.Count - 1);
List pointFs = new List(pointList.ToArray());
lineList.Add(pointFs);
}
pointList.Clear();
}
if (isAddKong)
{
if (kongPointList.Count > 2)
{
kongPointList.RemoveAt(kongPointList.Count - 1);
List pointFs = new List(kongPointList.ToArray());
kongLineList.Add(pointFs);
}
kongPointList.Clear();
}
if (isDelKong)
{
//if (delPointList.Count > 2)
//{
// delPointList.RemoveAt(delPointList.Count - 1);
// List pointFs = new List(delPointList.ToArray());
// delLineList.Add(pointFs);
//}
//delPointList.Clear();
}
if (isDrwKuo)
{
if (pointList.Count > 2)
{
pointList.RemoveAt(pointList.Count - 1);
List pointFs = new List(pointList.ToArray());
lineList.Add(pointFs);
}
pointList.Clear();
}
isLunKuo = false;
isAddKong = false;
isDelKong = false;
isDrwKuo = false;
}
}
///
/// Ctrl+z
///
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Z)
{
if (isLunKuo)
{
int cou = pointList.Count;
if (cou > 1)
{
if (cou == 2)
{
pointList.Clear();
}
else
{
pointList.RemoveAt(cou - 1);
}
}
else if (lineList.Count > 0)
{
pointList = lineList[lineList.Count - 1];
lineList.RemoveAt(lineList.Count - 1);
pointList.RemoveAt(pointList.Count - 1);
}
}
if (isAddKong)
{
int cou = kongPointList.Count;
if (cou > 1)
{
if (cou == 2)
{
kongPointList.Clear();
}
else
{
kongPointList.RemoveAt(cou - 1);
}
}
else if (kongLineList.Count > 0)
{
kongPointList = kongLineList[kongLineList.Count - 1];
kongLineList.RemoveAt(kongLineList.Count - 1);
kongPointList.RemoveAt(kongPointList.Count - 1);
}
}
if (isDelKong)
{
int cou = delPointList.Count;
if (cou > 0)
{
delPointList.RemoveAt(cou - 1);
}
List> CircleDate = new List>();
if (DrwDone)
{
CircleDate = binaryClass.DrwLunKuoNew(lineList, kongLineList, delPointList);
}
else
{
binaryClass.DrwLunKuo(lineList, kongLineList, delPointList);
}
bindGrid(CircleDate);
//if (cou > 1)
//{
// if (cou == 2)
// {
// delPointList.Clear();
// }
// else
// {
// delPointList.RemoveAt(cou - 1);
// }
//}
//else if (delLineList.Count > 0)
//{
// delPointList = delLineList[delLineList.Count - 1];
// delLineList.RemoveAt(delLineList.Count - 1);
// delPointList.RemoveAt(delPointList.Count - 1);
//}
}
this.documentWorkspace.Refresh();
}
}
#endregion
///
/// 导出孔隙信息
///
///
///
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 list = new List();
list.Add(dtb);
this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
}
///
/// 不可点击空白区域
///
///
///
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;
}
}
}
}
}
///
/// 生成孔隙率
///
///
///
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 list = new List();
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 tempBit = new List();
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);
}
///
/// 导出报告
///
private void PrintReport(string savePath)
{
List> analysisContent = new List>();
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
List content1 = new List();
if (i == 0)
{
content1.Add(this.dataGridView1.Columns[0].HeaderText);
content1.Add(this.dataGridView1.Columns[1].HeaderText);
content1.Add(this.dataGridView1.Columns[2].HeaderText);
analysisContent.Add(content1);
}
content1 = new List();
content1.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
content1.Add(this.dataGridView1.Rows[i].Cells[1].Value.ToString());
content1.Add(this.dataGridView1.Rows[i].Cells[2].Value.ToString());
analysisContent.Add(content1);
}
List content2 = new List();
analysisContent.Add(content2);
for (int i = 0; i < this.dataGridView4.Rows.Count; i++)
{
List content1 = new List();
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);
content1.Add(this.dataGridView4.Columns[6].HeaderText);
analysisContent.Add(content1);
}
content1 = new List();
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());
content1.Add(this.dataGridView4.Rows[i].Cells[6].Value.ToString());
analysisContent.Add(content1);
}
//保存处理后的图片
double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数
List tempBit = new List();
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 bitList = new List();
bitList.Add(tempBit[0]);
bitList.Add(tempBit[1]);
Dictionary tagInfos = new Dictionary();
// 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();
//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);
}
/// 导出图片
///
///
///
private void btnImageOut_Click(object sender, EventArgs e)
{
SaveFileDialog exe = new SaveFileDialog();
exe.Filter = "Image Files (*.jpg)|*.jpg";
exe.FilterIndex = 0;
exe.RestoreDirectory = true;
//exe.CreatePrompt = true;
exe.Title = "Image Files";
exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
exe.FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "孔隙率";
DialogResult dr = exe.ShowDialog();
if (dr != DialogResult.OK)
{
return;
}
Mat mat = binaryClass.MargMat();
if (Cv2.ImWrite(exe.FileName, mat))
{
MessageBox.Show("保存成功!");
}
//List list = new List();
//list.Add(dtb);
//this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
}
///
/// 准备用来处理单个颗粒的选中事件
///
///
///
private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
{
//当点击表头部的列时,e.RowIndex==-1
if (e.RowIndex > -1)
{
int index = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[6].Value);
int x = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[0].Value);
int y = Convert.ToInt32(dataGridView4.Rows[e.RowIndex].Cells[1].Value);
PointF select = new PointF() { X = x, Y = y };
binaryClass.MembranesSel(index);
// binaryClass.DrwLunKuoLie1Selected(select);
this.documentWorkspace.Refresh();
}
}
#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 phaseModels;
//public AnalysisResult analysisResult;
public DocumentItem(Bitmap bitmap, GraphicsList graphicsList, List phaseModels)
{
this.bitmap = bitmap;
this.graphicsList = graphicsList;
this.phaseModels = phaseModels;
}
}
#endregion
#endregion
}
}