using OTSCommon.DBOperate.Model;
using OTSIncAReportGraph.Class;
using OTSMeasureApp._0_OTSModel.OTSDataType;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OTSIncAReportGraph
{
///
/// Particle对象上操作状态的枚举
///
public enum PaintState
{
PAINT = 0,
NOPAINT = 1,
CONCISEPAINT=2,// display as a "+"
}
public enum DisplayState
{
DISPLAY = 0,
NODISPLAY = 1,
}
///
/// Particle对象上操作xray的枚举状态,显示xray数据,不显示
///
public enum ParticleShowXray
{
DISPLAYXRAY = 0,
NODISPLAY = 1,
}
///
/// Segment对象绘制的枚举,以点绘制,还是以线绘制
///
public enum SegmentShowMode
{
DRAWPARTICLEIMAGE = 0,
DRAWCOLORIMAGE = 1,
}
///
/// 颗粒类
///
public class DisplayParticle : ICloneable
{
private const float m_zoom_displayThreshold = 0.1f;
public Particle objParticleData;
private Guid m_id;
private RectangleF m_Globalrect;
private PointF m_OTSPointF;
private RectangleF m_smallRect;
private bool m_isSelected;
private bool m_isDeleted;
private bool m_isMouseOver;
private PaintState m_paintState = PaintState.PAINT;
private ParticleShowXray m_operator_showxray = ParticleShowXray.NODISPLAY;//选定显示XRAY,不显示XRAY,默认应为(不显示XRAY)
private bool m_isdragging;
private PointF m_dragingpoint;
private Color m_color;
private DisplayState m_displayState;
private SegmentShowMode show_mode = SegmentShowMode.DRAWPARTICLEIMAGE;//绘线,绘点,默认绘点,意思为默认显示BSE原图像
private Bitmap m_BSEimage;
private Bitmap m_Colorimage;
private string m_sort_type = "从大到小";
private float m_f_size = 0;
private string m_str_lj = "颗粒粒级";
private string m_str_klzl = "颗粒种类";
private string m_str_klfl = "颗粒分类";
private float m_CurrentZoomNum = 1;
public COTSRect OTSRect=new COTSRect();
public int TypeId
{
get
{
return objParticleData.TypeId;
}
}
//TypeName
public string TypeName
{
get
{
return objParticleData.TypeName;
}
}
//XRayId
public int XRayId
{
get
{
return objParticleData.XrayId;
}
}
public int SEMPosX
{
get
{
return objParticleData.SEMPosX;
}
}
public int SEMPosY
{
get
{
return objParticleData.SEMPosY;
}
}
public DisplayParticle()
{
m_id = System.Guid.NewGuid();
}
public DisplayParticle(Particle part)
{
m_id = System.Guid.NewGuid();
objParticleData = part;
this.Color = DrawFunction.GetColorBySTDTypeIDForBSEAndSorImage(part.TypeColor, part.TypeId);
}
public DisplayParticle(Particle particle,PointF FieldLeftTop,Bitmap originalFieldImage/*,Bitmap fieldParticleImage*/)
{
m_id = System.Guid.NewGuid();
objParticleData = particle;
this.Color = DrawFunction.GetColorBySTDTypeIDForBSEAndSorImage(particle.TypeColor, particle.TypeId);
List list_seg;
list_seg = particle.SegmentList;
this.m_BSEimage = new Bitmap(particle.RectWidth+1, particle.RectHeight+1);
this.m_Colorimage = new Bitmap(particle.RectWidth+1, particle.RectHeight+1);
//再循环取出里面所有的segment
foreach (Segment seg in list_seg)
{
#region
//合成图像完成,开始抠取像素-----------------------------------------------------------------
for (int m = 0; m < seg.Length; m++)
{
int lsjs_x = seg.Start+ m;
int lsjs_y = seg.Height;
var pixelColor = originalFieldImage.GetPixel(lsjs_x, lsjs_y);
lsjs_x = seg.Start - particle.RectLeft + m;
lsjs_y = seg.Height - particle.RectTop;
m_BSEimage.SetPixel(lsjs_x, lsjs_y, pixelColor);//ls_list_colors[m]
m_Colorimage.SetPixel(lsjs_x, lsjs_y, m_color);
}
#endregion //------------------------------------------------------------------------------
}
SetPaintState(PaintState.PAINT);
m_Globalrect.X = particle.RectLeft + FieldLeftTop.X;
m_Globalrect.Y = particle.RectTop + FieldLeftTop.Y;
m_Globalrect.Width = particle.RectWidth;
m_Globalrect.Height = particle.RectHeight;
}
public bool WhetherInRange(Point WhetherPoint)
{
var rect = GetShowRect();
if(rect.Contains(WhetherPoint))
{
m_smallRect = GetSmallRectangleFromRect();
bool b_inrange;
b_inrange=m_smallRect.Contains(WhetherPoint);
return b_inrange;
}
else
{
return false;
}
}
public DisplayParticle( DisplayParticle in_particle)
{
objParticleData = in_particle.objParticleData;
m_id = in_particle.m_id;
m_paintState = in_particle.m_paintState;
m_operator_showxray = in_particle.m_operator_showxray;
m_Globalrect = in_particle.m_Globalrect;
this.m_BSEimage = in_particle.m_BSEimage;
this.m_Colorimage = in_particle.m_Colorimage;
}
///
/// 设置显示的方式,可以用,绘线显示查看标准库颜色的,也可以用绘点,查看BSE原图颗粒图色的
///
public SegmentShowMode ShowMode
{
get { return show_mode; }
set { show_mode = value; }
}
///
/// 克隆方法
///
///
public object Clone()
{
return new DisplayParticle( this);
}
public PointF GetCenterPoint()
{
return new PointF(m_Globalrect.X + m_Globalrect.Width / 2, m_Globalrect.Y + m_Globalrect.Height / 2);
}
///
/// ID
///
public Guid guid
{
get { return m_id; }
set { m_id = value; }
}
///
/// 颗粒的外边框大小
///
public RectangleF GetShowRect()
{
return m_Globalrect;
}
public void SetShowRect(RectangleF rectangleF)
{
m_Globalrect= rectangleF;
}
///
/// OTSPointF
///
public PointF OTSPointF
{
get { return m_OTSPointF; }
set { m_OTSPointF = value; }
}
public bool IsSelect
{
get { return m_isSelected; }
set { m_isSelected = value; }
}
///
/// 该颗粒是否被设置成,选中状态
///
public PaintState GetPaintState()
{
if (GetDisplayState() == DisplayState.NODISPLAY)
{
m_paintState = PaintState.NOPAINT;
}
return m_paintState;
}
///
/// 该颗粒是否被设置成,选中状态
///
public void SetPaintState(PaintState value)
{
if (value == PaintState.PAINT)
{
if (m_CurrentZoomNum >= m_zoom_displayThreshold)
{
m_paintState = PaintState.PAINT;
}
else
{
m_paintState = PaintState.CONCISEPAINT;
}
}
else
{
m_paintState = value;
}
}
///
/// 是否对该颗粒选定显示X-Ray能谱图
///
public ParticleShowXray Operator_ShowXRay
{
get { return m_operator_showxray; }
set { m_operator_showxray = value; }
}
///
/// 是否在被拖动
///
public bool IsDragging
{
get { return m_isdragging; }
set { m_isdragging = value; }
}
///
/// 被拖动到的位置坐标
///
public PointF DraggingPoint
{
get { return m_dragingpoint; }
set {
m_dragingpoint = value;
}
}
///
/// 线的颜色
///
public Color Color
{
get { return m_color; }
set { m_color = value; }
}
///
/// 里面包含的多个线的集合
///
public List GetDSegments()
{ return objParticleData.SegmentList; }
public void Zoom(float zoomDelta,PointF refPoint)
{
//var rec = m_rect;
m_Globalrect.Width = (float)(m_Globalrect.Width + Convert.ToDouble(m_Globalrect.Width / m_CurrentZoomNum * zoomDelta));
m_Globalrect.Height = (float)(m_Globalrect.Height + Convert.ToDouble(m_Globalrect.Height / m_CurrentZoomNum * zoomDelta));
//锚点缩放补差值计算,得出差值
float xShiftOld = m_Globalrect.X - refPoint.X;
float yShiftOld = m_Globalrect.Y - refPoint.Y;
float xShift = m_Globalrect.X - refPoint.X + xShiftOld / m_CurrentZoomNum * zoomDelta;
float yShift = m_Globalrect.Y - refPoint.Y + yShiftOld / m_CurrentZoomNum * zoomDelta;
//对背景矩形与所有的segment进行修补差值
m_Globalrect.X = refPoint.X + xShift;
m_Globalrect.Y = refPoint.Y + yShift;
m_CurrentZoomNum += zoomDelta;
if (m_CurrentZoomNum < 0.7)
{
m_isMouseOver = false;
}
//设置缩放到多少倍时进行显示
if (m_CurrentZoomNum >= m_zoom_displayThreshold)
{
m_paintState = PaintState.PAINT;
}
else
{
m_paintState = PaintState.CONCISEPAINT;
}
}
public void DraggingMove(PointF mousePoint)
{
m_Globalrect.X = m_Globalrect.X + mousePoint.X - this.DraggingPoint.X; //获取到原先点与移动点的增减量,+原先的x坐标,就是新的坐标
m_Globalrect.Y = m_Globalrect.Y + mousePoint.Y - this.DraggingPoint.Y;
this.DraggingPoint = mousePoint;
}
public void Move(SizeF xyShift)
{
m_Globalrect.X =m_Globalrect.X - xyShift.Width; //获取到原先点与移动点的增减量,+原先的x坐标,就是新的坐标
m_Globalrect.Y = m_Globalrect.Y - xyShift.Height;
}
///
/// 设置排序的类型
///
public string SortType
{
get { return m_sort_type; }
set { m_sort_type = value; }
}
///
/// 设置该多边形的尺寸大小
///
public float FSize
{
get { return m_f_size; }
set { m_f_size = value; }
}
///
/// 设置粒级
///
public string ParticleLJ
{
get { return m_str_lj; }
set { m_str_lj = value; }
}
///
/// 设置种类
///
public string ParticleZL
{
get { return m_str_klzl; }
set { m_str_klzl = value; }
}
///
/// 设置分类
///
public string ParticleFL
{
get { return m_str_klfl; }
set { m_str_klfl = value; }
}
///
/// 获取或设置该Particle对应底层的FieldID值
///
public int FieldId
{
get { return objParticleData.FieldId; }
}
///
/// 获取或设置该Particle对应底层的ParticleID值
///
public int ParticleId
{
get { return objParticleData.ParticleId; }
}
public bool IsDeleted { get => m_isDeleted; set => m_isDeleted = value; }
public bool IsMouseOver { get => m_isMouseOver; set => m_isMouseOver = value; }
public Image GetImage()
{
return m_BSEimage;
}
public void SetImage(Image value)
{
m_BSEimage = (Bitmap)value;
}
public DisplayState GetDisplayState()
{
if (m_isDeleted)
{
m_displayState = DisplayState.NODISPLAY;
}
return m_displayState;
}
public void SetDisplayState(DisplayState value)
{
m_displayState = value;
if (m_displayState == DisplayState.DISPLAY)
{
m_paintState = PaintState.PAINT;
}
}
///
/// 绘制函数
///
///
public void Paint(Graphics g)
{
//Graphics g = e.Graphics;
//绘制鼠标移动到颗粒上时的边框,需要判断当前鼠标在颗粒上,及颗粒的操作为正常显示
if (m_isMouseOver == true)
{
//如果有鼠标在该矩形上,那么进行描边
ControlPaint.DrawBorder(g,
Rectangle.Round(this.GetShowRect()),
Color.Lime,
1,
ButtonBorderStyle.Solid,
Color.Lime,
1,
ButtonBorderStyle.Solid,
Color.Lime,
1,
ButtonBorderStyle.Solid,
Color.Lime,
1,
ButtonBorderStyle.Solid);
}
if (GetPaintState() == PaintState.PAINT)
{
if (this.ShowMode == SegmentShowMode.DRAWPARTICLEIMAGE)
{
g.DrawImage(m_BSEimage, m_Globalrect);
}
else
{
g.DrawImage(m_Colorimage, m_Globalrect);
}
}
if (GetPaintState() == PaintState.CONCISEPAINT)
{
if (m_isSelected)
{
g.DrawString("+", new Font("黑体", 12), new SolidBrush(Color.Red), new PointF(GetCenterPoint().X, GetCenterPoint().Y));
}
else
{
g.DrawString("+", new Font("黑体", 6), new SolidBrush(Color.DarkSlateBlue), new PointF(GetCenterPoint().X, GetCenterPoint().Y));
}
}
if (m_isSelected)
{
//如果説该矩形被选择上了的话,那么也显示边框
ControlPaint.DrawBorder(g,
Rectangle.Round(this.GetShowRect()),
Color.Blue,
1,
ButtonBorderStyle.Solid,
Color.Blue,
1,
ButtonBorderStyle.Solid,
Color.Blue,
1,
ButtonBorderStyle.Solid,
Color.Blue,
1,
ButtonBorderStyle.Solid);
}
if (ParticleShowXray.DISPLAYXRAY == m_operator_showxray && PaintState.NOPAINT != GetPaintState())
{
//当鼠标在该颗粒上进行点击,则对颗粒状态更改为选定状态,用来显示X-ray能谱表
ControlPaint.DrawBorder(g,
Rectangle.Round(this.GetShowRect()),
Color.DeepSkyBlue,
1,
ButtonBorderStyle.Solid,
Color.DeepSkyBlue,
1,
ButtonBorderStyle.Solid,
Color.DeepSkyBlue,
1,
ButtonBorderStyle.Solid,
Color.DeepSkyBlue,
1,
ButtonBorderStyle.Solid);
}
}
///
/// 从已经确定的外边框来计算出里面的+号小框位置
///
///
private RectangleF GetSmallRectangleFromRect()
{
RectangleF rect = new RectangleF();
//用外边框的坐标,除2获得中心点,然后再分别+,- 4
float x = 0, y = 0;
x = m_Globalrect.X + (m_Globalrect.Width / 2);
y = m_Globalrect.Y + (m_Globalrect.Height / 2);
var smallwidth = m_Globalrect.Width * 1 / 5;
var smallheight = m_Globalrect.Height * 1 / 5;
rect.X = x - smallwidth/2;
rect.Y = y - smallheight / 2;
rect.Width = smallwidth;
rect.Height = smallheight;
return rect;
}
///
/// 根据该多边形所有包含的线长度,计算出,该多边形的面积大小
///
///
public float GetAreaFormSegments()
{
float f_size_sum = 0;
foreach (var ls_ds in this.objParticleData.SegmentList)
{
f_size_sum = f_size_sum + ls_ds.Length;
}
return f_size_sum;
}
}
}