using PaintDotNet.Base.CommTool; using PaintDotNet.Base.SettingModel; using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.DedicatedAnalysis.GrainSize { class GuideSizeArLawClass { GrainSizeArbitrationLawModel grainSizeArbitrationLawModel; ///// ///// 单位 ///// //public double unitLength; /// /// 网格交点 /// public List rectangleFs = new List(); /// /// 旋转角度 /// public int rotateAngleV = 0; /// /// 图形位置 /// public PointF loctation; /// /// 图形位移 /// public RectangleF rectangleFLine = new RectangleF(); /// /// 网格绘制区域 /// private float minX; private float maxX; private float minY; private float maxY; /// /// 水平数量 /// public int hNumber; /// /// 垂直数量 /// public int vNumber; /// /// 网格线宽 /// public int gridWidth; /// /// 网格间距 /// public float gridSpacing; public float gridSpacing1; /// /// 网格颜色 /// public int gridColor; /// /// 全图 true:全图 false:非全图 /// public bool wholePicture; /// /// 余白 /// public int blank; /// /// 绘制网格 /// /// public void DrawGridViews(Graphics graphics, int CSHeight, int CSWidth) { Pen linePen = new Pen(Color.FromArgb(this.gridColor), this.gridWidth); //float panX = this.loctation.X - this.documentWorkspace.CompositionSurface.Width / 2; //float panY = this.loctation.Y - this.documentWorkspace.CompositionSurface.Height / 2; float hGridSpacingDraw; float vGridSpacingDraw; float blankDraw; if (this.wholePicture) { hGridSpacingDraw = (float)(CSHeight - 2 * this.blank) / (this.hNumber - 1); vGridSpacingDraw = (float)(CSWidth - 2 * this.blank) / (this.vNumber - 1); blankDraw = this.blank; } else { blankDraw = 0; hGridSpacingDraw = this.gridSpacing; vGridSpacingDraw = this.gridSpacing; } if (this.hNumber > 0) { if (this.hNumber % 2 == 0) { float Spacing = hGridSpacingDraw / 2; for (int i = 0; i < (float)this.hNumber / 2; i++) { if (this.wholePicture) { if (CSHeight > 2 * this.blank && CSWidth > 2 * this.blank) { if ((float)CSHeight / 2 - Spacing >= blankDraw) { //PointF pointF1 = rotatedAndPanPoint((float)(Convert.ToDecimal(x) - Convert.ToDecimal((float)this.gridWidth / 2) - 3) // , (float)(Convert.ToDecimal(y) - Convert.ToDecimal((float)this.gridWidth / 2) - 3)); graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2 - Spacing, CSHeight, CSWidth)); } if ((float)CSHeight / 2 + Spacing <= CSHeight - blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2 + Spacing, CSHeight, CSWidth)); } } } else { if ((float)CSHeight / 2 - Spacing >= 0) { graphics.DrawLine(linePen, rotatedAndPanPoint(minX, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth) , rotatedAndPanPoint(maxX, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth)); } if ((float)CSHeight / 2 + Spacing <= CSHeight) { graphics.DrawLine(linePen, rotatedAndPanPoint(minX, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth) , rotatedAndPanPoint(maxX, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth)); } } Spacing += hGridSpacingDraw; } } else { if ((float)CSWidth > 2 * blankDraw) { if (this.hNumber == 1) graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2, CSHeight, CSWidth)); else { float Spacing = hGridSpacingDraw; for (int i = 0; i < (this.hNumber - 1) / 2; i++) { if (this.wholePicture) { if (CSHeight > 2 * this.blank && CSWidth > 2 * this.blank) { if (i == 0) graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2, CSHeight, CSWidth)); if ((float)CSHeight / 2 - Spacing >= blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2 - Spacing, CSHeight, CSWidth)); } if ((float)CSHeight / 2 + Spacing <= CSHeight - blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint(blankDraw, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth) , rotatedAndPanPoint((float)(CSWidth - blankDraw), (float)CSHeight / 2 + Spacing, CSHeight, CSWidth)); } } } else { if (i == 0) graphics.DrawLine(linePen, rotatedAndPanPoint(minX, (float)CSHeight / 2, CSHeight, CSWidth) , rotatedAndPanPoint(maxX, (float)CSHeight / 2, CSHeight, CSWidth)); if ((float)CSHeight / 2 - Spacing >= 0) { graphics.DrawLine(linePen, rotatedAndPanPoint(minX, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth) , rotatedAndPanPoint(maxX, (float)CSHeight / 2 - Spacing, CSHeight, CSWidth)); } if ((float)CSHeight / 2 + Spacing <= CSHeight) { graphics.DrawLine(linePen, rotatedAndPanPoint(minX, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth) , rotatedAndPanPoint(maxX, (float)CSHeight / 2 + Spacing, CSHeight, CSWidth)); } } Spacing += hGridSpacingDraw; } } } } } if (this.vNumber > 0) { if (this.vNumber % 2 == 0) { float Spacing = vGridSpacingDraw / 2; for (int i = 0; i < (float)this.vNumber / 2; i++) { if (this.wholePicture) { if (CSHeight > 2 * this.blank && CSWidth > 2 * this.blank) { if ((float)CSWidth / 2 - Spacing >= 0 && (float)CSWidth / 2 - Spacing >= blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 - Spacing, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 - Spacing, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); } if ((float)CSWidth / 2 + Spacing <= CSWidth && (float)CSWidth / 2 + Spacing <= CSWidth - blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 + Spacing, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 + Spacing, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); } } } else { if ((float)CSWidth / 2 - Spacing >= 0) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 - Spacing, minY, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 - Spacing, maxY, CSHeight, CSWidth)); } if ((float)CSWidth / 2 + Spacing <= CSWidth) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 + Spacing, minY, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 + Spacing, maxY, CSHeight, CSWidth)); } } Spacing += vGridSpacingDraw; } } else { if (CSHeight > 2 * blankDraw) { if (this.vNumber == 1) graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); else { float Spacing = vGridSpacingDraw; for (int i = 0; i < (this.vNumber - 1) / 2; i++) { if (this.wholePicture) { if (CSHeight > 2 * this.blank && CSWidth > 2 * this.blank) { if (i == 0) graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); if ((float)CSWidth / 2 + Spacing <= CSWidth && CSWidth / 2 + Spacing <= CSWidth - blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 + Spacing, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 + Spacing, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); } if ((float)CSWidth / 2 - Spacing >= 0 && (float)CSWidth / 2 - Spacing >= blankDraw) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 - Spacing, blankDraw, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 - Spacing, (float)(CSHeight - blankDraw), CSHeight, CSWidth)); } } } else { if (i == 0) graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2, minY, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2, maxY, CSHeight, CSWidth)); if ((float)CSWidth / 2 + Spacing <= CSWidth) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 + Spacing, minY, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 + Spacing, maxY, CSHeight, CSWidth)); } if ((float)CSWidth / 2 - Spacing >= 0) { graphics.DrawLine(linePen, rotatedAndPanPoint((float)CSWidth / 2 - Spacing, minY, CSHeight, CSWidth) , rotatedAndPanPoint((float)CSWidth / 2 - Spacing, maxY, CSHeight, CSWidth)); } } Spacing += vGridSpacingDraw; } } } } } } /// /// 获取辅助线下拉集合 /// /// public ArrayList GetComboBox1DataSource() { ArrayList arrayList1 = new ArrayList(); // 垂直辅助线 if (grainSizeArbitrationLawModel.GridStyleList != null) { foreach (var vGuideStyle in grainSizeArbitrationLawModel.GridStyleList) { arrayList1.Add(vGuideStyle.gridName); } } arrayList1.Insert(0, PdnResources.GetString("Menu.Pleaseselecttheauxiliarylineused.text")); return arrayList1; } public void InitLawModel() { grainSizeArbitrationLawModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\GrainSizeArbitrationLawModel.xml", FileMode.Open)); } //public void InitUnitLength(Dictionary MeasureInfo) //{ // MeasureInfo.TryGetValue(MeasurementUnit.Micron, out unitLength); double unitLength // //double physical_area_length_Micron = getCurrentWorkspace().GetRuler(MeasurementUnit.Micron); //} /// /// 更新辅助线及其样式 /// public void UpdateGuideStyle(object comboBox1SelectedItem, double physical_Micron = 1.0) { if (grainSizeArbitrationLawModel.GridStyleList != null) { foreach (var GridStyle in grainSizeArbitrationLawModel.GridStyleList) { if (GridStyle.gridName.Equals(comboBox1SelectedItem)) { this.hNumber = GridStyle.hNumber + 1; this.vNumber = GridStyle.vNumber + 1; this.gridWidth = GridStyle.gridWidth; this.gridSpacing = (float)(GridStyle.gridSpacing / physical_Micron); this.gridSpacing1 = (float)(GridStyle.gridSpacing); this.gridColor = GridStyle.gridColor; this.wholePicture = GridStyle.wholePicture; this.blank = GridStyle.blank; } } } } /// /// 初始化矩形 /// public void InitRectangleF(PointF pointF, int CompositionSurfaceHeight, int CompositionSurfaceWidth) { // 全图 if (this.wholePicture) { rectangleFLine = new RectangleF(pointF.X - CompositionSurfaceWidth / 2 + this.blank, pointF.Y - CompositionSurfaceHeight / 2 + this.blank , CompositionSurfaceWidth - 2 * this.blank, CompositionSurfaceHeight - 2 * this.blank); } else { rectangleFLine = new RectangleF(pointF.X - (this.vNumber - 1) * this.gridSpacing / 2, pointF.Y - (this.hNumber - 1) * this.gridSpacing / 2 , (this.vNumber - 1) * this.gridSpacing, (this.hNumber - 1) * this.gridSpacing); } } /// /// 刷新网格矩形 /// public void RefreshGridIntersections(int CompositionSurfaceHeight, int CompositionSurfaceWidth) { this.rectangleFs.Clear(); ////float rectangleFLineX = this.rectangleFLine.X + this.lineWidth / 2 + spacing; //float panX = this.loctation.X - this.documentWorkspace.CompositionSurface.Width / 2; //float panY = this.loctation.Y - this.documentWorkspace.CompositionSurface.Height / 2; float hGridSpacingDraw; float vGridSpacingDraw; if (this.wholePicture) { hGridSpacingDraw = (float)(CompositionSurfaceHeight - 2 * this.blank) / (this.hNumber - 1); vGridSpacingDraw = (float)(CompositionSurfaceWidth - 2 * this.blank) / (this.vNumber - 1); } else { hGridSpacingDraw = this.gridSpacing; vGridSpacingDraw = this.gridSpacing; } ArrayList xs = new ArrayList(); ArrayList ys = new ArrayList(); if (this.hNumber > 0) { if (this.hNumber % 2 == 0) { float Spacing = hGridSpacingDraw / 2; for (int i = 0; i < this.hNumber / 2; i++) { if ((float)CompositionSurfaceHeight / 2 - Spacing >= 0 && (float)CompositionSurfaceHeight / 2 + Spacing <= CompositionSurfaceHeight) { ys.Add((float)CompositionSurfaceHeight / 2 - Spacing); ys.Add((float)CompositionSurfaceHeight / 2 + Spacing); } Spacing += hGridSpacingDraw; } } else { if (this.hNumber == 1) ys.Add((float)CompositionSurfaceHeight / 2); else { float Spacing = hGridSpacingDraw; for (int i = 0; i < (this.hNumber - 1) / 2; i++) { if (i == 0) ys.Add((float)CompositionSurfaceHeight / 2); if ((float)CompositionSurfaceHeight / 2 - Spacing >= 0 && (float)CompositionSurfaceHeight / 2 + Spacing <= CompositionSurfaceHeight) { ys.Add((float)CompositionSurfaceHeight / 2 - Spacing); ys.Add((float)CompositionSurfaceHeight / 2 + Spacing); } Spacing += hGridSpacingDraw; } } } } if (this.vNumber > 0) { if (this.vNumber % 2 == 0) { float Spacing = vGridSpacingDraw / 2; for (int i = 0; i < this.vNumber / 2; i++) { if ((float)CompositionSurfaceWidth / 2 - Spacing >= 0 && (float)CompositionSurfaceWidth / 2 + Spacing <= CompositionSurfaceWidth) { xs.Add((float)CompositionSurfaceWidth / 2 - Spacing); xs.Add((float)CompositionSurfaceWidth / 2 + Spacing); } Spacing += vGridSpacingDraw; } } else { if (this.vNumber == 1) xs.Add((float)CompositionSurfaceWidth / 2); else { float Spacing = vGridSpacingDraw; for (int i = 0; i < (this.vNumber - 1) / 2; i++) { if (i == 0) xs.Add((float)CompositionSurfaceWidth / 2); if ((float)CompositionSurfaceWidth / 2 + Spacing <= CompositionSurfaceWidth && (float)CompositionSurfaceWidth / 2 - Spacing >= 0) { xs.Add((float)CompositionSurfaceWidth / 2 - Spacing); xs.Add((float)CompositionSurfaceWidth / 2 + Spacing); } Spacing += vGridSpacingDraw; } } } } if (xs.Count > 1) { this.minX = (float)xs[xs.Count - 2]; this.maxX = (float)xs[xs.Count - 1]; } if (ys.Count > 1) { this.minY = (float)ys[ys.Count - 2]; this.maxY = (float)ys[ys.Count - 1]; } foreach (var x in xs) { foreach (var y in ys) { PointF pointF1 = rotatedAndPanPoint/*计算旋转后的网点位置,同时需要加中心偏移*/((float)(Convert.ToDecimal(x) - 0/*Convert.ToDecimal((float)this.gridWidth / 2)*/) , (float)(Convert.ToDecimal(y) - 0/*Convert.ToDecimal((float)this.gridWidth / 2)*/), CompositionSurfaceHeight, CompositionSurfaceWidth); this.rectangleFs.Add(new RectangleF(pointF1.X - 6 - this.gridWidth / 2, pointF1.Y - 6 - this.gridWidth / 2, this.gridWidth + 6, this.gridWidth + 6)); } } if (this.wholePicture) { int count = this.rectangleFs.Count; RectangleF rectangleF = new RectangleF(this.blank, this.blank, CompositionSurfaceWidth + 1 - 2 * this.blank , CompositionSurfaceHeight + 1 - 2 * this.blank); for (int i = count - 1; i >= 0; i--) { if (!rectangleF.Contains(new PointF(this.rectangleFs[i].X + 3 + (float)this.gridWidth / 2, this.rectangleFs[i].Y + 3 + (float)this.gridWidth / 2))) { this.rectangleFs.RemoveAt(i); } } } } private PointF rotatedPoint(float point_X, float point_Y, int CompositionSurfaceHeight, int CompositionSurfaceWidth) { double rotatedVPai = this.rotateAngleV * Math.PI / 180.0; double disX = point_X - CompositionSurfaceWidth / 2; double disY = point_Y - CompositionSurfaceHeight / 2; double outX = disX * Math.Cos(rotatedVPai) - disY * Math.Sin(rotatedVPai); double outY = disX * Math.Sin(rotatedVPai) + disY * Math.Cos(rotatedVPai); return new PointF((float)outX, (float)outY); } private PointF rotatedAndPanPoint(float point_X, float point_Y, int CompositionSurfaceHeight, int CompositionSurfaceWidth) { PointF point1 = rotatedPoint(point_X, point_Y, CompositionSurfaceHeight, CompositionSurfaceWidth); float panX = this.loctation.X; float panY = this.loctation.Y; return new PointF(point1.X + panX, point1.Y + panY); } } }