using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using app.Configuration.Models;
using app.Configuration.Services;
using app.Lib;
using app.Lib.Graph;
using app.Measure.Events;
using app.Measure.Models;
using app.Measure.ViewModels;
using app.Measure.Actions;
using Bedrock.Events;
using Microsoft.Practices.Unity;
using System.Drawing.Drawing2D;
using app.Main;
using app.Main.Graph;
using app.KeyBoards.Events;
using app.Main.ViewModels;
namespace app.Measure.Tools
{
//轨迹/区域测量工具
public class _2DTrace : PropertyChangedBase,IToolAction
{
private readonly IEventAggregator _eventAggregator;
private readonly IConfigService _configService;
private readonly MeasureData _measureData; //存放绘图工具的ID
private List<Point> _points; //轨迹/区域的坐标集
private List<List<Point>> _tempPoints;
private readonly MeasureTool _tool; //绘图工具的配置信息
private readonly Measurement _measurement; //该测量环境的XML配置
private readonly Font _textFont; //文本格式对象
private readonly Brush _textBrush; //绘制文本的对象
private ICoordinate _coordinate; //坐标系对象
private readonly List<MeasureResultItem> _resultItems; //存放测量值的列表
private readonly CalculationFomula _calculationFomula; // 计算公式
private readonly MeasureToolType _measuretooltype;
private readonly SystemStateModel _systemStateModel;
private readonly string[] _promptString = { "Place the first point of the closed trace", "Trace the area boundary" };
//读取工具XML的配置,初始化绘图工具
public _2DTrace(IUnityContainer unityContainer, IEventAggregator eventAggregator)
{
Logger.Log("_2DTrace Start Initial", Level.Info);
var unityContainer1 = unityContainer;
_eventAggregator = eventAggregator;
_configService = unityContainer1.Resolve<IConfigService>();
_measureData = unityContainer1.Resolve<MeasureData>();
_systemStateModel = unityContainer1.Resolve<SystemStateModel>();
_coordinate = MeasureAction.GetCoordinate();
_resultItems = new List<MeasureResultItem>
{
new MeasureResultItem("TCir", "mm", ),
new MeasureResultItem("TArea", "mm2", )
};
_calculationFomula = new CalculationFomula();
_points = new List<Point>();
_tempPoints = new List<List<Point>>();
if (_measureData.IsGenericTool == false)
{
_measurement = _configService.GetMeasurementId(_measureData.MeasurementId);
_tool = _configService.GetMeasureToolId(_measureData.ToolId);
}
else
{
_measurement = _configService.GetMeasurementId(_measureData.GenericToolItem[_measureData.GenericToolIndex].MeasurementId);
_tool = _configService.GetMeasureToolId(_measureData.GenericToolItem[_measureData.GenericToolIndex].ToolId);
}
_measuretooltype = _tool.MeasureToolType;
_textFont = new Font("Consolas", 10.5F, FontStyle.Bold, GraphicsUnit.Point, );
_textBrush = new SolidBrush(Common.IntToColor(_tool.ColorOfLine));
_eventAggregator.GetEvent<PubSubEvent<string>>().Subscribe(EventHandle, true);
if (_measureData.GetToolPositionDataItem == null)
{
var toolDataList = new List<ToolPositionDataItem>();
_measureData.GetToolPositionDataItem = toolDataList;
}
if (_measureData.GetMeasureResultData == null)
{
var toolDataList = new List<MeasureResultData>();
_measureData.GetMeasureResultData = toolDataList;
}
_pen = new Pen(Color.Green, _tool.WidthOfLine) {DashStyle = DashStyle.Dash};
Logger.Log("_2DTrace End Initial", Level.Info);
}
private ToolPositionDataItem _toolPositionDataItem;
private List<Point> _fillEllipse;
private List<DrawCurvePoint> _drawCurve;
private readonly Pen _pen;//旋钮旋转重绘的颜色
private bool _isRotatebuttonRepaintFirstTime = true;//旋钮旋转重绘,该情况下是否第一次调用Draw函数
//绘图
public void Draw(Graphics graphics, ICoordinate coordinate)
{
if (coordinate != null)
{
_coordinate = coordinate;
}
else
{
return;
}
if (false == _measureData.IsRotatingButtonRePaint)
{
ToolDraw.DrawFromToolData(graphics, coordinate, _measureData.GetToolPositionDataItem, _currentsender);
switch (_keynum%)
{
case :
if (_isInViewRegion)
{
MeasureAction.Cursortype = CursorType.CursorAreaPicture;
}
break;
case :
if (_isInViewRegion == false)
{
UserControl temp = _measureData.MouseEventSender as UserControl;
if (null != temp)
Cursor.Clip = temp.RectangleToScreen(Rectangle.Round(_coordinate.DisplayRegion));
}
else
{
MeasureAction.Cursortype = CursorType.CursorPicture;
}
break;
default:
MeasureAction.Cursortype = MeasureAction.AutoNextMeasure == false ? CursorType.CursorEmptyPicture : CursorType.CursorAreaPicture;
break;
}
}
else
{
if (_isInViewRegion == false)
{
UserControl temp = _measureData.MouseEventSender as UserControl;
if (null != temp)
Cursor.Clip = temp.RectangleToScreen(Rectangle.Round(_coordinate.DisplayRegion));
}
MeasureAction.Cursortype = CursorType.CursorPicture;
_isRotatebuttonRepaintFirstTime = _measureData.IsFirsttimeRotatePaint;
_tempPoints.Clear();
ToolDraw.RotateRepaintFromToolData(graphics, coordinate, _measureData.GetToolPositionDataItem,
_measureData.RotatingButtonSelectedPointNum, _measureData.RotatingButtonSelectedIndex, ref _tempPoints,
_mousePoint, _pen, _isRotatebuttonRepaintFirstTime, _isInViewRegion, _measureData.MouseEventSender);
_measureData.IsFirsttimeRotatePaint = false;
_points = _tempPoints[];
}
}
//计算结果
private void CaculateResult()
{
if (_coordinate == null)
{
return;
}
var pointds = new List<PointD>();
pointds.Clear();
for (var i = ; i < _points.Count - ; i++)
{
var p1 = _coordinate.ViewToWorld(_points[i]);
pointds.Add(p1);
}
//trace的周长计算
_resultItems[].Result = _calculationFomula.TraceCirculation(pointds);
//trace的面积
_resultItems[].Result = _calculationFomula.TraceArea(pointds);
}
/// <summary>
/// 功能键、旋钮处理
/// </summary>
/// <param name="eventArgs"></param>
public void KeyBoardEventHandle(KeyBoardEventArgs eventArgs)
{
}
private MousePointEvent _mousePointEvent;
private bool _isInViewRegion;
private double _distance;
private object _currentsender;
//获取鼠标坐标
private Point _mousePoint;
private PointD _mousePointInView;
public void MouseEventHandle(object sender, MouseEventArgs eventArgs)
{
_currentsender = sender;
_measureData.MouseEventSender = sender;
_mousePoint = eventArgs.Location;
if (_coordinate != null)
{
_mousePointInView = _coordinate.DisplayToView(_mousePoint);
_isInViewRegion = _coordinate.ViewRegionD.Contains(_mousePointInView);
_distance = _calculationFomula.Distance(_coordinate.ViewToWorld(_mousePoint),
_coordinate.ViewToWorld(_coordinate.CenterPointD));
}
if (_keynum % == )
{
if (_isInViewRegion)
{
if (_points.Count > )
{
double angle = _calculationFomula.AngleOfPoint(_mousePoint, _points[_points.Count - ],
_points[_points.Count - ]);
double distance = _calculationFomula.Distance(_mousePoint, _points[_points.Count - ]);
if (distance > && angle >= )
{
_points.Add(_mousePoint);
//ToolDraw.SetCursorPositon(_mousePoint, sender);
}
else if (distance > && angle < )
{
_points.Remove(_points[_points.Count - ]);
//ToolDraw.SetCursorPositon(_points[_points.Count - 1], sender);
}
}
else
{
_points.Add(_mousePoint);
}
if (false == _measureData.IsRotatingButtonRePaint)
{
AddDataItem(_points);
}
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
}
if (_isInViewRegion)
{
switch (_keynum%)
{
case :
if (_systemStateModel.HintText != _promptString[])
_systemStateModel.HintText = _promptString[];
break;
case :
if (_systemStateModel.HintText != _promptString[])
_systemStateModel.HintText = _promptString[];
break;
default:
if (_systemStateModel.HintText != "")
_systemStateModel.HintText = "";
break;
}
}
else
{
MeasureAction.Cursortype = CursorType.Max;
if (_systemStateModel.HintText != "")
_systemStateModel.HintText = "";
}
_mousePointEvent = new MousePointEvent(_distance, _isInViewRegion) {MeasureResultItem = {Name = "Area"}};
NotifyPropertyChanged(OperateState.MeasureRepaint);
_eventAggregator.GetEvent<PubSubEvent<MousePointEvent>>().Publish(_mousePointEvent);
if (eventArgs.Button == MouseButtons.Left && eventArgs.Clicks == && _isInViewRegion)
{
if (_measureData.IsRotatingButtonRePaint)
{
Cursor.Clip = Rectangle.Empty;
CaculateResult();
var temp = new MeasureResultData {Name = _measuretooltype.ToString()};
foreach (var t in _resultItems)
{
temp.MeasureResultItem.Add(t.Clone() as MeasureResultItem);
}
_measureData.GetMeasureResultData.Add(temp);
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[].Clone()));
}
else
{
MouseClick();
}
_measureData.IsRotatingButtonRePaint = false;
}
}
private void AddDataItem(List<Point> points)
{
if (_fillEllipse.Count > )
{
_fillEllipse.Remove(_fillEllipse[]);
}
_fillEllipse.Add(points[]);
DrawCurvePoint point1 = new DrawCurvePoint();
foreach (Point point in points)
{
point1.CurvePoint.Add(point);
}
if (_drawCurve.Count > )
{
_drawCurve.Remove(_drawCurve[]);
}
_drawCurve.Add(point1);
if (_measureData.GetToolPositionDataItem.Count > _measureData.MaxMeasureTimes)
{
_measureData.GetToolPositionDataItem.Remove(_measureData.GetToolPositionDataItem[]);
}
}
//响应按键,收集坐标,commit后结果值在结果窗口中显示出来
int _keynum; //记录按键次数
public void MouseClick()
{
//if (keynum != 0 && keynum % 3 == 2 && _measurement.AutoNext == false)
if (_keynum != && _keynum % == && MeasureAction.AutoNextMeasure == false)
{
return;
}
_keynum++;
switch (_keynum % )
{
case :
_points.Clear();
_fillEllipse = new List<Point>();
_drawCurve = new List<DrawCurvePoint>();
_toolPositionDataItem = new ToolPositionDataItem(_drawCurve, _fillEllipse)
{
ColorOfLine = _tool.ColorOfLine,
ColorOfPlot = _tool.ColorOfPlot,
WidthOfLine = _tool.WidthOfLine,
WidthOfPlot = _tool.WidthOfPlot,
Dashstype = _tool.Dashstype,
TextBrush = _textBrush,
TextFont = _textFont,
MeasureToolType = _measuretooltype,
ModeType = _measureData.ModelType,
PointNum = ,
ItemNum = _resultItems.Count,
ViewName = (_measureData.MouseEventSender as Control) == null ? "" : ((Control) _measureData.MouseEventSender).Name
};
_measureData.GetToolPositionDataItem.Add(_toolPositionDataItem);
if (_isInViewRegion)
{
_points.Add(_mousePoint);
if (false == _measureData.IsRotatingButtonRePaint)
{
AddDataItem(_points);
}
}
break;
case :
if (_drawCurve.Count != )
_drawCurve[].IsFinished = true;
if (_measurement.AutoCaculate == false)
{
return;
}
CaculateResult();
var temp = new MeasureResultData {Name = _measuretooltype.ToString()};
foreach (var t in _resultItems)
{
temp.MeasureResultItem.Add(t.Clone() as MeasureResultItem);
}
_measureData.GetMeasureResultData.Add(temp);
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[].Clone()));
_toolPositionDataItem.IsFinished = true;
break;
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
}
bool _isCalc;
private void EventHandle(string e) //重绘,显示结果等事件处理
{
switch (e)
{
case EventNames.MeasureToolpaint:
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolRepaint:
_points.Clear();
_keynum = ;
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
case EventNames.MeasureToolCalcResult:
var measuretooltype = _configService.GetMeasureToolId(_measureData.ToolId).MeasureToolType;
if (_measurement.AutoCaculate == false && _isCalc == false && _keynum % == && measuretooltype == _measuretooltype)
{
_isCalc = true;
CaculateResult();
for (var i = _resultItems.Count - ; i >= ; i--)
{
_eventAggregator.GetEvent<MeasureToolEvent>()
.Publish(new MeasureEventArgs(MessureEventType.MessureToolCommit, _resultItems[i].Clone()));
}
}
break;
case EventNames.MeasureToolNextPaint:
if (_measurement.AutoNext == false)
{
_keynum = ;
_isCalc = false;
}
NotifyPropertyChanged(OperateState.MeasureRepaint);
break;
}
}
}
}