using Newtonsoft.Json;
|
using PBS.WinFrmUI.Hydro;
|
using System.Data;
|
using System.Numerics;
|
using System.Text;
|
|
namespace HydroUI
|
{
|
public partial class MapViewer : XtraUserControl
|
{
|
|
#region View
|
#region 一、全局
|
|
#region 初始化
|
private bool _showToolBar = true;
|
[DisplayName("显示工具栏")]
|
public bool showToolBar
|
{
|
get
|
{
|
return _showToolBar;
|
}
|
set
|
{
|
_showToolBar = value;
|
if (this.panel1 != null) this.panel1.Visible = value;
|
}
|
}
|
|
|
private bool _showStatusBar = true;
|
[DisplayName("显示状态栏")]
|
public bool ShowStatusBar
|
{
|
get
|
{
|
return _showStatusBar;// this.panel1==null?true:Visible;
|
}
|
set
|
{
|
_showStatusBar = value;
|
if (this.statusStrip1 != null) this.statusStrip1.Visible = value;
|
}
|
}
|
|
private bool _showPropertyForm = true;
|
[DisplayName("显示属性栏")]
|
public bool ShowPropertyForm
|
{
|
get
|
{
|
return _showPropertyForm;// this.panel1==null?true:Visible;
|
}
|
set
|
{
|
_showPropertyForm = value;
|
if (this.propertyForm != null) this.propertyForm.Visible = value;
|
}
|
}
|
|
|
public MapViewer()
|
{
|
|
this.showToolBar = false;
|
InitializeComponent();
|
|
MapCenter = PointF.Empty;
|
zoom = 1.0f;
|
|
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
|
BackColor = Color.Transparent;
|
|
}
|
|
private void MapViewer_Load(object sender, EventArgs e)
|
{
|
GlobalObject.PropertyForm = this.propertyForm;
|
this.panel1.Visible = _showToolBar;
|
this.statusStrip1.Visible = _showStatusBar;
|
|
|
cb_Node_Colour.Items.Clear();
|
cb_Link_Colour.Items.Clear();
|
//repositoryItemComboBox1.Items.Add(ColourType.无);
|
cb_Link_Colour.Items.Add(ColourType.无);
|
int i = 0;
|
//遍历ColourType
|
|
// 遍历枚举类型
|
foreach (ColourType colour in Enum.GetValues(typeof(ColourType)))
|
{
|
if (i <= Colour.NodeTypeCount)
|
cb_Node_Colour.Items.Add(colour);
|
else
|
cb_Link_Colour.Items.Add(colour);
|
|
i++;
|
}
|
|
|
cb_Node_Colour.SelectedIndex = 0;
|
|
cb_Link_Colour.SelectedIndex = 0;
|
|
map.Init(TC, new DrawDelegate[] { Draw, Draw, DrawBackGroud, DrawH }, new MouseDelegate[] { mapMouseDown, mapMouseMove, mapMouseUp, mapMouseWheel });
|
|
|
TContainer TCCube = new TContainer();
|
TCCube.template = new Template();
|
TCCube.template.network = new MapViewNetWork();
|
TCCube.template.network.Areas = new List<AreaViewModel>();
|
Cube cb = new Cube();
|
foreach (var item in cb.FacesVertices)
|
{
|
AreaViewModel area = new AreaViewModel();
|
area.ID = item.Key.ToString();
|
area.InnerNodes = new List<NodeViewModel>();
|
area.InnerNodes = new List<NodeViewModel>();
|
|
//6个面设置不一样的颜色
|
switch (item.Key)
|
{
|
case 1:
|
area.color = Color.Red;
|
area.Name = "右";
|
break;
|
case 2:
|
area.color = Color.SkyBlue;
|
area.Name = "左";
|
break;
|
case 3:
|
area.color = Color.Green;
|
area.Name = "后";
|
break;
|
case 4:
|
area.color = Color.DarkBlue;
|
area.Name = "前";
|
break;
|
case 5:
|
area.color = Color.Purple;
|
area.Name = "上";
|
break;
|
case 6:
|
area.color = Color.Orange;
|
area.Name = "下";
|
break;
|
default:
|
area.color = Color.White;
|
break;
|
}
|
|
foreach (var p in item.Value)
|
{
|
NodeViewModel node = new NodeViewModel();
|
node.X = p.X;
|
node.Y = p.Y;
|
node.Elev = p.Z;
|
area.InnerNodes.Add(node);
|
}
|
TCCube.template.network.Areas.Add(area);
|
}
|
CubeView.Init(TCCube, new DrawDelegate[] { Draw, Draw, DrawBackGroud, DrawH }, new MouseDelegate[] { CubeViewMouseDown, CubeViewMouseMove, CubeViewMouseUp, mapMouseWheel });
|
|
}
|
|
#endregion
|
|
|
#region 对外开放,全局控制方法
|
|
public bool LoadData(bool isDelCache = false)
|
{
|
|
if (_Template == null) return false;
|
if (isDelCache || _Template.network == null)
|
{
|
if (!_Template.loadInpFile())
|
{
|
//MessageBox.Show("读取地图失败");
|
_Template.network = new MapViewNetWork();
|
//return false;
|
}
|
}
|
|
SuspendLayout();
|
if (_ViewModel == null)
|
{
|
if (!_IsEditMode)
|
{
|
if (param == null)
|
param = new dict<string, dynamic>();
|
_Template.network.LoadRepeaters(_Template.MaxLevel, param, _ViewModel, !_IsEditMode);
|
|
}
|
else
|
{
|
if (param == null)
|
param = new dict<string, dynamic>();
|
_Template.network.LoadRepeaters(_Template.MaxLevel, param, null, !_IsEditMode);
|
}
|
}
|
else
|
{
|
if (_IsEditMode)
|
{
|
if (param == null)
|
param = new dict<string, dynamic>();
|
_Template.network.LoadRepeaters(_Template.MaxLevel, param, _ViewModel, !_IsEditMode);
|
|
}
|
else
|
{
|
if (param == null)
|
param = new dict<string, dynamic>();
|
_Template.network.LoadRepeaters(_Template.MaxLevel, param, null, !_IsEditMode);
|
}
|
|
}
|
if (GlobalObject.PropertyForm != null)
|
{
|
GlobalObject.PropertyForm.SetEnabled(_IsEditMode);
|
GlobalObject.PropertyForm.SetNet(_Template.network);
|
}
|
|
|
SetStartEndPoint(_Template.Node1, _Template.Node2);
|
|
ResumeLayout(false);
|
return true;
|
}
|
|
|
Dictionary<PBS.eModelTemplateType, bool> _ViewModel = null;
|
public void Clear()
|
{
|
_Template = null;
|
MapCenter = PointF.Empty;
|
zoom = 1.0f;
|
Rotation = 0;
|
RotationF = 90;
|
SetMapInvalidate();
|
}
|
public void SetData(Template template, dict<string, dynamic> param = null, Dictionary<PBS.eModelTemplateType, bool> viewMode = null)
|
{
|
this.param = param;
|
bool reLoad = _Template == template;
|
this._Template = template;
|
this._ViewModel = viewMode;
|
|
|
|
if (!LoadData()) return;
|
if (!reLoad)
|
{
|
if (_Template.view == null)
|
InitCenter();
|
else
|
{
|
this.mapOption = _Template.view.Copy();
|
SetMapInvalidate();
|
}
|
}
|
|
刷新楼层ToolStripMenuItem_Click(1, new EventArgs());
|
label_center.Text = $"center:({MapCenter.X.ToString("0.00")} ,{MapCenter.Y.ToString("0.00")})";
|
label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
|
toolStripStatusLabel_rotation.Text = $"Rotation:({Rotation.ToString("0")},{RotationF.ToString("0")})";
|
|
}
|
|
private void InitCenter()
|
{
|
//MapCenter
|
var p = PointF.Empty;
|
float x0 = 99999999999f, y0 = 99999999999f, x1 = -99999999999f, y1 = -99999999999f;
|
|
foreach (NodeViewModel junction in _Nodes)
|
{
|
p.X += (float)junction.X;
|
p.Y += (float)junction.Y;
|
if (x0 > junction.X) x0 = junction.X;
|
if (y0 > junction.Y) y0 = junction.Y;
|
if (x1 < junction.X) x1 = junction.X;
|
if (y1 < junction.Y) y1 = junction.Y;
|
}
|
|
float x_span = x1 - x0, y_span = y1 - y0;
|
|
zoom = Math.Min(this.map.Width / x_span, this.map.Height / y_span);
|
zoom = Math.Max(zoom, MinZoom);
|
zoom = Math.Min(zoom, MaxZoom);
|
|
Rotation = 0;
|
RotationF = 90;
|
|
|
if (_Nodes.Count > 0)
|
{
|
p.X /= _Nodes.Count;
|
p.Y /= _Nodes.Count;
|
}
|
MapCenter = p;
|
}
|
|
public void SetMapInvalidate()
|
{
|
|
_needPaintAll = true;
|
PMin_Show = ScreenToMap(new PointF(0, this.map.Height));
|
PMax_Show = ScreenToMap(new PointF(this.map.Width, 0));
|
_timerDraw = true;
|
}
|
|
public void SetStartEndPoint(String node1, string node2)
|
{
|
_StartPoint = node1;
|
_EndPoint = node2;
|
SetMapInvalidate();
|
}
|
public void Set3DView(bool is3Dview, double 俯视角度)
|
|
{
|
this.RotationF = 俯视角度;
|
this.is3Dview = is3Dview;
|
SetMapInvalidate();
|
}
|
|
|
|
public void SetRotation(double d)
|
{
|
this.Rotation = d;
|
SetMapInvalidate();
|
}
|
|
#endregion
|
|
|
|
|
#region 重绘函数
|
|
bool _needPaintAll
|
{
|
get
|
{
|
return __needpaintall;
|
}
|
set
|
{
|
__needpaintall = value;
|
}
|
}
|
|
|
PointF[] getCurclePoints(int num)
|
{
|
PointF[] points = new PointF[num + 1];
|
float angle = 0;
|
for (int i = 0; i < points.Length; i++)
|
{
|
float x = (float)Math.Cos(angle);
|
float y = (float)Math.Sin(angle);
|
points[i] = new PointF(x, y);
|
angle += 2 * (float)Math.PI / num;
|
}
|
points[num] = points[0];
|
return points;
|
}
|
|
|
|
|
|
|
private void timer_draw_Tick(object sender, EventArgs e)
|
{
|
_mouseHoverCheckFlag = true;
|
if (_timerDraw && map.Status == DrawingStatus.Ready)
|
{
|
this.map.Invalidate();
|
this.CubeView.Invalidate();
|
_timerDraw = false;
|
}
|
|
}
|
|
protected override void OnResize(EventArgs e)
|
{
|
base.OnResize(e);
|
// 当控件尺寸改变时,触发重绘
|
this.SetMapInvalidate();
|
}
|
|
#endregion
|
|
|
#region 鼠标事件
|
|
void mapMouseDown(MouseEventArgs e)
|
{
|
base.OnMouseDown(e);
|
|
_ClickStartPos = new PointF(e.X, e.Y);
|
DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
|
mousePosition = _MousePosition = ScreenToMap(new PointF(e.X, e.Y));
|
if (e.Button == MouseButtons.Left && _isInsertingObject)
|
{
|
|
}
|
else
|
if (e.Button == MouseButtons.Middle || e.Button == MouseButtons.XButton2)
|
{
|
|
if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)//按下框选放大
|
{
|
DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
|
_isDragging = true;
|
}
|
|
else
|
{
|
|
_lastCursor = this.Cursor;
|
this.Cursor = Cursors.SizeAll;
|
MapCenter0 = MapCenter;
|
mapOption0 = mapOption.Copy();
|
_isPanning = true;
|
}
|
}
|
else if (e.Button == MouseButtons.Left && _mouseState == MouseState.无)
|
{
|
|
|
if (ModifierKeys == Keys.Shift)
|
{
|
var point = ScreenToMap(new PointF(e.X, e.Y));
|
if (!_isDrawingPolygon)
|
{
|
// 开始绘制多边形
|
polygonPoints.Clear();
|
|
_isDrawingPolygon = true;
|
}
|
|
polygonPoints.Add(point);
|
mousePosition = _MousePosition = ScreenToMap(new PointF(e.X, e.Y));
|
SetMapInvalidate();
|
return;
|
}
|
if (_isDrawingPolygon)
|
{
|
return;
|
}
|
|
//移动
|
if (_IsEditMode && selectedObjs.Count >= 1) //&& selectedObjs[0].isNode())
|
{
|
List<NodeViewModel> nodes = selectedNodes;
|
foreach (NodeViewModel node in nodes)
|
{
|
|
|
PointF mapPos = WorldPointToMapPoint(node);
|
PointF currentPoint = MapToScreen(mapPos);
|
if (Get_dist(new PointF(e.X, e.Y), currentPoint) < 15f)
|
{
|
|
|
_NewNet.Nodes.AddRange(selectedNodes);
|
_NewNet.Links.AddRange(selectedLinks);
|
_isMovingObject = true;
|
_OperaNode = node;
|
_undoOldValue = node.Position3D;
|
break;
|
}
|
}
|
}
|
|
|
if (!_isMovingObject)//拖拽选择
|
{
|
DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
|
_isDragging = true;
|
}
|
|
}
|
|
else if (e.Button == MouseButtons.Left && _mouseState == MouseState.设置底图范围)
|
{
|
_mouseState = MouseState.无;
|
DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
|
BackGroudPicLeftPos = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y)), _Template.BackGroundElev);
|
_isSettingBackGroundPictur = true;
|
}
|
else if (e.Button == MouseButtons.Right)
|
{
|
RotaStartPos = new PointF(e.X, e.Y);
|
Rotation0 = Rotation;
|
俯视角度_start = RotationF;
|
_lastCursor = this.Cursor;
|
|
Cursor = Cursors.Hand;
|
mapOption0 = mapOption.Copy();
|
_isRotating = true;
|
}
|
|
}
|
private bool IsPointInPolygon(PointF point, List<PointF> polygon)
|
{
|
int count = polygon.Count;
|
bool inside = false;
|
|
PointF p1, p2;
|
for (int i = 0, j = count - 1; i < count; j = i++)
|
{
|
p1 = polygon[i];
|
p2 = polygon[j];
|
|
if (((p1.Y > point.Y) != (p2.Y > point.Y)) &&
|
(point.X < (p2.X - p1.X) * (point.Y - p1.Y) / (p2.Y - p1.Y) + p1.X))
|
{
|
inside = !inside;
|
}
|
}
|
|
return inside;
|
}
|
|
public void InsertNet(Template temp)
|
{
|
_isInsertingObject = true;
|
_newTemplate = temp;
|
_undoOldValue = new PointF3D(0, 0, 0);
|
_OperaNode = temp.network.Nodes.Find(node => node.ID == temp.Node1);
|
}
|
|
|
PointF _MousePosition = new PointF(0, 0);
|
void mapMouseMove(MouseEventArgs e)
|
{
|
bool flag = false;
|
_MousePosition = ScreenToMap(new PointF(e.X, e.Y));
|
if (_isMovingObject)
|
{
|
PointF pointF = MapPointToWorldPoint(_MousePosition, _OperaNode.Elev);
|
PointF3D pointF3D = (PointF3D)_undoOldValue;
|
if (!float.IsInfinity(pointF.X) && !float.IsInfinity(pointF.Y))
|
{
|
_newTemplate.OffSet = new PointF3D(pointF.X - pointF3D.X, pointF.Y - pointF3D.Y, 0f);
|
}
|
|
GlobalObject.PropertyForm.propertyGridControl1.Refresh();
|
flag = true;
|
}
|
else if (_isPainting)
|
{
|
flag = true;
|
}
|
else if (_isPanning)
|
{
|
PointF rotateVector = GetRotateVector(new PointF(e.X, e.Y), new PointF(_lastMouseX, _lastMouseY));
|
MapCenter = new PointF(MapCenter.X - rotateVector.X / Zoom.X, MapCenter.Y - rotateVector.Y / Zoom.Y);
|
label_center.Text = "center:(" + MapCenter.X.ToString("0.000") + " ," + MapCenter.Y.ToString("0.000") + ")";
|
flag = true;
|
}
|
else if (_isDragging || _isDrawingPolygon || _isSettingBackGroundPictur)
|
{
|
mousePosition = _MousePosition;
|
flag = true;
|
}
|
else if (_isRotating)
|
{
|
mousePosition = _MousePosition;
|
bool flag2 = RotaStartPos.Y >= (float)(map.Height / 2);
|
if (Control.ModifierKeys != Keys.Alt)
|
{
|
Rotation = Rotation0 + (double)(((float)e.X - RotaStartPos.X) * 180f) * 2.5 / (double)(float)map.Width * (double)(flag2 ? 1 : 1);
|
}
|
|
if (Control.ModifierKeys != Keys.Shift)
|
{
|
RotationF = 俯视角度_start + (double)(((float)e.Y - RotaStartPos.Y) * 180f) * 2.5 / (double)(float)map.Height;
|
}
|
|
if (RotationF > 90.0)
|
{
|
RotationF = 90.0;
|
}
|
|
if (RotationF < 0.0)
|
{
|
RotationF = 0.0;
|
}
|
|
flag = true;
|
}
|
else if (_isInsertingObject)
|
{
|
PointF pointF2 = MapPointToWorldPoint(_MousePosition, _OperaNode.Elev);
|
PointF3D pointF3D2 = (PointF3D)_undoOldValue;
|
if (!float.IsInfinity(pointF2.X) && !float.IsInfinity(pointF2.Y))
|
{
|
_newTemplate.OffSet = new PointF3D(pointF2.X - pointF3D2.X, pointF2.Y - pointF3D2.Y, 0f);
|
}
|
|
GlobalObject.PropertyForm.propertyGridControl1.Refresh();
|
flag = true;
|
}
|
|
if (_mouseHoverCheckFlag)
|
{
|
_mouseHoverCheckFlag = false;
|
PointF clickedPoint = new PointF(e.X, e.Y);
|
IBaseViewModel obj_by_ScreenPoint = GetObj_by_ScreenPoint(clickedPoint);
|
if ((hoveredObjs.Count <= 0 || hoveredObjs[0] != obj_by_ScreenPoint) && (hoveredObjs.Count != 0 || obj_by_ScreenPoint != null))
|
{
|
flag = true;
|
hoveredObjs.ForEach(delegate (IBaseViewModel o)
|
{
|
o.Hovered = false;
|
});
|
hoveredObjs.Clear();
|
if (obj_by_ScreenPoint != null)
|
{
|
obj_by_ScreenPoint.Hovered = true;
|
hoveredObjs.Add(obj_by_ScreenPoint);
|
}
|
}
|
}
|
|
if (flag)
|
{
|
SetMapInvalidate();
|
}
|
|
label_mouse.Text = "X:" + e.X.ToString("0") + " Y:" + e.Y.ToString("0") + " [Map]X:" + _MousePosition.X.ToString("0.00") + " Y:" + _MousePosition.Y.ToString("0.00");
|
_lastMouseX = e.X;
|
_lastMouseY = e.Y;
|
}
|
|
bool RectangContain(RectangleF r, PointF p)
|
{
|
var x = p.X; var y = p.Y;
|
|
|
if (r.X <= x && x < r.X + r.Width && r.Y <= y)
|
{
|
return y < r.Y + r.Height;
|
}
|
|
return false;
|
}
|
|
/// <summary>
|
/// 用于绘制管线
|
/// </summary>
|
NodeViewModel _select_junction1 = null;
|
NodeViewModel _select_junction2 = null;
|
DateTime _lastMouseUp = DateTime.Now;
|
int doubleClick_Delay = 500;//毫秒
|
bool recordView = false;
|
void mapMouseUp(MouseEventArgs e)
|
{
|
//BookMark :鼠标抬起事件
|
base.OnMouseUp(e);
|
bool isMouseMoved = Get_dist(_ClickStartPos, new PointF(e.X, e.Y)) > 10;
|
bool isdoubleClick = (DateTime.Now - _lastMouseUp).TotalMilliseconds <= doubleClick_Delay;
|
_lastMouseUp = DateTime.Now;
|
if /*框选放大*/(ModifierKeys == Keys.Control && (e.Button == MouseButtons.Middle || e.Button == MouseButtons.XButton2))
|
{
|
_isDragging = false;
|
|
this.Cursor = _lastCursor;
|
|
mapOption0 = mapOption;
|
var _lastMousePosition = DragStartPos;
|
|
List<IBaseViewModel> objs = new List<IBaseViewModel>();
|
// 绘制矩形
|
var start = new Point((int)Math.Min(mousePosition.X, _lastMousePosition.X), (int)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new Size((int)Math.Abs(_lastMousePosition.X - mousePosition.X), (int)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new Rectangle(start, size);
|
|
var new_center = MapPointToWorldPoint(new PointF(start.X + size.Width / 2, start.Y + size.Height / 2));
|
|
this.MapCenter = new_center;
|
this.zoom = Math.Max(1.0f * this.map.Width / size.Width, 1.0f * this.map.Height / size.Height);
|
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
|
label_center.Text = $"center:({MapCenter.X.ToString("0.000")} ,{MapCenter.Y.ToString("0.000")})";
|
label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
|
SetMapInvalidate();
|
return;
|
}
|
if /*放大镜*/(ModifierKeys == Keys.Shift && (e.Button == MouseButtons.Middle || e.Button == MouseButtons.XButton2))
|
{
|
_isDragging = false;
|
|
this.Cursor = _lastCursor;
|
|
|
var _lastMousePosition = DragStartPos;
|
|
List<IBaseViewModel> objs = new List<IBaseViewModel>();
|
// 绘制矩形
|
var start = new Point((int)Math.Min(mousePosition.X, _lastMousePosition.X), (int)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new Size((int)Math.Abs(_lastMousePosition.X - mousePosition.X), (int)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new Rectangle(start, size);
|
for (int i = 0; i < _Nodes.Count; i++)
|
{
|
var node = _Nodes[i] as NodeViewModel;
|
PointF p = WorldPointToMapPoint(node);
|
if (rectangle0.Contains((int)p.X, (int)p.Y))
|
{
|
objs.Add(node);
|
}
|
}
|
|
for (int i = 0; i < _Links.Count; i++)
|
{
|
var link = _Links[i] as LinkViewModel;
|
if (rectangle0.Contains(new Point((int)link.Position.X, (int)link.Position.Y)))
|
{
|
objs.Add(link);
|
}
|
}
|
Form_Magnifier fr = new Form_Magnifier(objs);
|
|
fr.ShowDialog();
|
return;
|
}
|
if /*平移视角*/(ModifierKeys == Keys.None && (e.Button == MouseButtons.Middle || e.Button == MouseButtons.XButton2))
|
{
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
this.Cursor = _lastCursor;
|
_isPanning = false;
|
return;
|
}
|
if /*设置背景*/(e.Button == MouseButtons.Left && _isSettingBackGroundPictur)
|
{
|
_Template.BackGroundPoint1 = BackGroudPicLeftPos;
|
_Template.BackGroundPoint2 = MapPointToWorldPoint(mousePosition, _Template.BackGroundElev);
|
|
_isSettingBackGroundPictur = false;
|
mapOption.isShowPic = true;
|
this.Cursor = _lastCursor;
|
SetMapInvalidate();
|
return;
|
}
|
if /*插入结构*/(e.Button == MouseButtons.Left && _isInsertingObject)
|
{
|
var net = _newTemplate.network;//clone
|
var net0 = _newTemplate.network.DeepCopy<MapViewNetWork>();
|
List<NodeViewModel> nodes = _Nodes.Select(node => (NodeViewModel)node).ToList();
|
float minDist = 100f;
|
NodeViewModel minNode = null;
|
foreach (NodeViewModel node in nodes)
|
{
|
PointF mapPos = WorldPointToMapPoint(node);
|
PointF currentPoint = MapToScreen(mapPos);
|
float currentDist = 0;
|
if (node != _OperaNode && (currentDist = Get_dist(new PointF(e.X, e.Y), currentPoint)) < 15f)
|
{
|
if (currentDist < minDist)
|
currentDist = minDist;
|
minNode = node;
|
break;
|
}
|
}
|
float dx, dy, dz;
|
|
if (ModifierKeys != Keys.Alt && minNode != null)
|
{
|
|
var p1 = (PointF3D)_undoOldValue;
|
var p2 = minNode.Position3D;
|
dx = p2.X - p1.X;
|
dy = p2.Y - p1.Y;
|
dz = p2.Z - p1.Z;
|
|
}
|
else
|
{
|
_NewNet.Clear();
|
_OperaNode = null;
|
_isInsertingObject = false;
|
this.SetMapInvalidate();
|
return;
|
}
|
|
|
net.Nodes.ForEach(n => { ((NodeViewModel)n).Position3D = new PointF3D(n.X + dx, n.Y + dy, n.Elev + dz); });
|
|
var list = _Network.Add(net);
|
|
var j = _Network.AddPipe(minNode, _OperaNode);
|
list.Add(j);
|
_OperaNode = null;
|
_NewNet.Clear();
|
|
_Network.BuildRelation();
|
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
|
list.ForEach(m => m.Selected = true);
|
selectedObjs.AddRange(list);
|
|
MapObjectExtensions.AddCommand(_Network, "Add", null, list);
|
|
|
_isInsertingObject = false;
|
|
|
|
_newTemplate.network = net0;
|
_isInsertingObject = true;
|
_undoOldValue = new PointF3D(0, 0, 0);
|
_OperaNode = _newTemplate.network.Nodes.Find(node => node.ID == _newTemplate.Node1) as NodeViewModel;
|
|
SetMapInvalidate();
|
return;
|
}
|
if /*多边形选择*/(_isDrawingPolygon && e.Button == MouseButtons.Left && ModifierKeys == Keys.None)
|
{
|
_isDrawingPolygon = false;
|
if (polygonPoints.Count >= 3)
|
{
|
_Nodes.ForEach(n0 =>
|
{
|
var n = (NodeViewModel)n0;
|
if (IsPointInPolygon(WorldPointToMapPoint(n), polygonPoints))
|
{
|
selectedObjs.Add(n);
|
n.Selected = true;
|
}
|
});
|
_Links.ForEach(n0 =>
|
{
|
var n = (LinkViewModel)n0;
|
if (IsPointInPolygon(WorldPointToMapPoint(n.Position, n.Elev), polygonPoints))
|
{
|
selectedObjs.Add(n);
|
n.Selected = true;
|
}
|
});
|
|
}
|
SetMapInvalidate();
|
// 结束绘制多边形
|
return;
|
}
|
if /*移动对象*/(_isMovingObject && isMouseMoved && e.Button == MouseButtons.Left)
|
{
|
List<NodeViewModel> nodes = _Nodes.Select(n => (NodeViewModel)n).ToList();
|
float minDist = 100f;
|
NodeViewModel minNode = null;
|
foreach (NodeViewModel node in nodes)
|
{
|
|
|
PointF mapPos = WorldPointToMapPoint(node);
|
PointF currentPoint = MapToScreen(mapPos);
|
float currentDist = 0;
|
if (node != _OperaNode && (currentDist = Get_dist(new PointF(e.X, e.Y), currentPoint)) < 15f)
|
{
|
if (currentDist < minDist) currentDist = minDist;
|
minNode = node;
|
break;
|
}
|
}
|
|
|
if (ModifierKeys != Keys.Alt && minNode != null)
|
{
|
_isMovingObject = false;
|
var p1 = (PointF3D)_undoOldValue;
|
var p2 = minNode.Position3D;
|
var dd = _newTemplate.OffSet;
|
var dx = p2.X - p1.X;
|
var dy = p2.Y - p1.Y;
|
var dz = p2.Z - p1.Z;
|
selectedNodes.ForEach(n => { n.Position3D = new PointF3D(n.X + dx, n.Y + dy, n.Elev + dz); });
|
List<PointF3D> newPositons = selectedNodes.Select(n => n.Position3D).ToList();
|
List<PointF3D> oldPositons = newPositons.Select(n => new PointF3D(n.X - dx, n.Y - dy, n.Z - dz)).ToList();
|
MapObjectExtensions.AddCommand(selectedNodes, "Position3D", oldPositons, newPositons);
|
var l = new List<IBaseViewModel>();
|
l.Add(_Network.AddPipe(_OperaNode, minNode));
|
if (l.Count > 0) MapObjectExtensions.AddCommand(_Network, "Add", null, l);
|
|
_OperaNode = null;
|
_NewNet.Clear();
|
}
|
else
|
{
|
_isMovingObject = false;
|
var p1 = (PointF3D)_undoOldValue;
|
var p2 = _OperaNode.Position3D;
|
var dd = _newTemplate.OffSet;
|
var dx = dd.X; //p2.X - p1.X;
|
var dy = dd.Y;// p2.Y - p1.Y;
|
var dz = dd.Z;// p2.Z - p1.Z;
|
selectedNodes.ForEach(n => { n.Position3D = new PointF3D(n.X + dx, n.Y + dy, n.Elev + dz); });
|
List<PointF3D> newPositons = selectedNodes.Select(n => n.Position3D).ToList();
|
List<PointF3D> oldPositons = newPositons.Select(n => new PointF3D(n.X - dx, n.Y - dy, n.Z - dz)).ToList();
|
MapObjectExtensions.AddCommand(selectedNodes, "Position3D", oldPositons, newPositons);
|
_OperaNode = null;
|
_NewNet.Clear();
|
}
|
this.SetMapInvalidate();
|
return;
|
}
|
if /*取消移动对象*/(_isMovingObject && !isMouseMoved && e.Button == MouseButtons.Left)
|
{
|
_isMovingObject = false;
|
var p1 = (PointF3D)_undoOldValue;
|
var p2 = _OperaNode.Position3D;
|
var dd = _newTemplate.OffSet;
|
var dx = dd.X;
|
var dy = dd.Y;
|
var dz = dd.Z;
|
_OperaNode = null;
|
_NewNet.Clear();
|
return;
|
}
|
if /*特殊功能*/(_mouseState != MouseState.无 && e.Button == MouseButtons.Left)
|
{
|
PointF p;
|
float z;
|
NodeViewModel n;
|
|
switch (_mouseState)
|
{
|
case MouseState.新增节点:
|
getPointAndHeight(e, out p, out z);
|
n = _Network.AddJunction(p, z);
|
MapObjectExtensions.AddCommand(_Network, "Add", null, new List<IBaseViewModel>() { n });
|
break;
|
case MouseState.新建水表:
|
getPointAndHeight(e, out p, out z);
|
n = _Network.AddMeter(p);
|
MapObjectExtensions.AddCommand(_Network, "Add", null, new List<IBaseViewModel>() { n });
|
break;
|
case MouseState.新建水库:
|
getPointAndHeight(e, out p, out z);
|
n = _Network.AddReservoir(p);
|
MapObjectExtensions.AddCommand(_Network, "Add", null, new List<IBaseViewModel>() { n });
|
break;
|
case MouseState.新建水池:
|
getPointAndHeight(e, out p, out z);
|
n = _Network.AddTank(p);
|
MapObjectExtensions.AddCommand(_Network, "Add", null, new List<IBaseViewModel>() { n });
|
break;
|
case MouseState.新增管线:
|
case MouseState.新增立管:
|
case MouseState.新建水泵:
|
case MouseState.新建阀门:
|
case MouseState.新建重复器:
|
if (_select_junction1 == null)
|
{
|
if (_mouseState == MouseState.新增立管)
|
{
|
var m = ScreenToVMap(new PointF(e.X, e.Y));
|
z = m.Y;
|
p = new PointF(m.X, 0);
|
Set_junction1(e);
|
}
|
else
|
{
|
getPointAndHeight(e, out p, out z);
|
Set_junction1(e);
|
}
|
|
}
|
else
|
{
|
if (_mouseState == MouseState.新增立管)
|
{
|
//需要把鼠标位置转换为立管的位置,获取鼠标位置的高程
|
|
var wPos = GetZZWorldPoint(_select_junction1.Position3D, _MousePosition, new Vector3(0, 0, 1));
|
|
p = new PointF(wPos.X, wPos.Y);
|
var l = AddLink(new PointF(e.X, e.Y), isdoubleClick, p, wPos.Z);
|
if (l.Count > 0) MapObjectExtensions.AddCommand(_Network, "Add", null, l);
|
}
|
else
|
{
|
if (mapOption.IsOrtho)
|
{
|
var wPos = GetZZWorldPoint(_select_junction1.Position3D, _MousePosition, new Vector3(1, 1, 0));
|
p = new PointF(wPos.X, wPos.Y);
|
var l = AddLink(new PointF(e.X, e.Y), isdoubleClick, p, wPos.Z);
|
if (l.Count > 0) MapObjectExtensions.AddCommand(_Network, "Add", null, l);
|
}
|
else
|
{
|
getPointAndHeight(e, _select_junction1, out p, out z);
|
var l = AddLink(new PointF(e.X, e.Y), isdoubleClick, p, z);
|
if (l.Count > 0) MapObjectExtensions.AddCommand(_Network, "Add", null, l);
|
}
|
|
|
}
|
|
}
|
break;
|
}
|
_Network.BuildRelation();
|
|
SetMapInvalidate();
|
}
|
if /*锁定点选*/(GlobalObject.LockSelect && !isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left)
|
{
|
_isDragging = false;
|
|
// 遍历所有点,找出最近的点
|
PointF clickedPoint = new PointF(e.X, e.Y);
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
bool isJunction = obj is NodeViewModel;
|
|
if (obj != null)
|
{
|
GlobalObject.PropertyForm.SetObjs(new List<IBaseViewModel>() { obj });
|
if (isJunction) _OperaNode = (NodeViewModel)obj;
|
mousePosition = new PointF(0, 0);
|
SetMapInvalidate();
|
|
}
|
else
|
{
|
GlobalObject.PropertyForm.SetObjs(new List<IBaseViewModel>() { });
|
_OperaNode = null;
|
mousePosition = new PointF(0, 0);
|
SetMapInvalidate();
|
}
|
return;
|
}
|
if /*锁定框选*/(GlobalObject.LockSelect && isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left)
|
{
|
_isDragging = false;
|
Cursor = Cursors.Default;
|
|
var _lastMousePosition = DragStartPos;
|
|
// 绘制矩形
|
var start = new PointF((float)Math.Min(mousePosition.X, _lastMousePosition.X), (float)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new SizeF((float)Math.Abs(_lastMousePosition.X - mousePosition.X), (float)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new RectangleF(start, size);
|
var selectedObjs = new List<IBaseViewModel>();
|
for (int i = 0; i < _Nodes.Count; i++)
|
{
|
|
var node = _Nodes[i] as NodeViewModel;
|
if (!node.Visible) continue;
|
PointF p = WorldPointToMapPoint(node);
|
|
if (RectangContain(rectangle0, p))
|
{
|
selectedObjs.Add(node);
|
|
}
|
}
|
|
for (int i = 0; i < _Links.Count; i++)
|
{
|
var link = _Links[i] as LinkViewModel;
|
if (!link.Visible) continue;
|
PointF p = WorldPointToMapPoint(link.Position, link.Elev);
|
if (RectangContain(rectangle0, p))
|
{
|
selectedObjs.Add(link);
|
}
|
}
|
var findNode = selectedObjs.FindAll(o => o is NodeViewModel);
|
if (findNode.Count > 0) _OperaNode = findNode[0] as NodeViewModel;
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
return;
|
}
|
if /*叠加框选*/(isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.Control)
|
{
|
|
_isDragging = false;
|
Cursor = Cursors.Default;
|
|
var _lastMousePosition = DragStartPos;
|
|
// 绘制矩形
|
var start = new PointF((float)Math.Min(mousePosition.X, _lastMousePosition.X), (float)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new SizeF((float)Math.Abs(_lastMousePosition.X - mousePosition.X), (float)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new RectangleF(start, size);
|
for (int i = 0; i < _Nodes.Count; i++)
|
{
|
|
var node = _Nodes[i] as NodeViewModel;
|
if (!node.Visible) continue;
|
PointF p = WorldPointToMapPoint(node);
|
|
if (RectangContain(rectangle0, p))
|
{
|
if (selectedObjs.Contains(node))
|
{
|
node.Selected = false;
|
selectedObjs.Remove(node);
|
}
|
else
|
{
|
node.Selected = true;
|
selectedObjs.Add(node);
|
}
|
|
}
|
}
|
|
for (int i = 0; i < _Links.Count; i++)
|
{
|
var link = _Links[i] as LinkViewModel;
|
if (!link.Visible) continue;
|
PointF p = WorldPointToMapPoint(link.Position, link.Elev);
|
if (RectangContain(rectangle0, p))
|
{
|
if (selectedObjs.Contains(link))
|
{
|
link.Selected = false;
|
selectedObjs.Remove(link);
|
}
|
else
|
{
|
link.Selected = true;
|
selectedObjs.Add(link);
|
}
|
}
|
}
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
return;
|
}
|
if /*框选*/(isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.None)
|
{
|
_isDragging = false;
|
Cursor = Cursors.Default;
|
|
var _lastMousePosition = DragStartPos;
|
|
// 绘制矩形
|
var start = new PointF((float)Math.Min(mousePosition.X, _lastMousePosition.X), (float)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new SizeF((float)Math.Abs(_lastMousePosition.X - mousePosition.X), (float)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new RectangleF(start, size);
|
selectedObjs.ForEach(obj => obj.Selected = false);
|
selectedObjs.Clear();
|
for (int i = 0; i < _Nodes.Count; i++)
|
{
|
var node = _Nodes[i] as NodeViewModel;
|
if (!node.Visible) continue;
|
PointF p = WorldPointToMapPoint(node);
|
|
if (RectangContain(rectangle0, p))
|
{
|
node.Selected = true;
|
selectedObjs.Add(node);
|
}
|
}
|
|
for (int i = 0; i < _Links.Count; i++)
|
{
|
var link = _Links[i] as LinkViewModel;
|
|
if (!link.Visible) continue;
|
PointF p = WorldPointToMapPoint(link.Position, link.Elev);
|
if (RectangContain(rectangle0, p))
|
{
|
link.Selected = true;
|
selectedObjs.Add(link);
|
}
|
}
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
return;
|
}
|
|
if /*点选*/(!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.None)
|
{
|
_isDragging = false;
|
|
// 遍历所有点,找出最近的点
|
PointF clickedPoint = new PointF(e.X, e.Y); //ScreenToMap(new PointF(e.X, e.Y));
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
bool isJunction = obj is NodeViewModel;
|
|
if (obj != null)
|
{
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
obj.Selected = true;
|
selectedObjs.Add(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
_OperaNode = null;
|
|
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
|
|
}
|
else
|
{
|
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
|
_OperaNode = null;
|
|
SetMapInvalidate();
|
}
|
return;
|
|
|
}
|
|
if (/*叠加点选*/!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.Control)
|
{
|
_isDragging = false;
|
|
// 遍历所有点,找出最近的点
|
PointF clickedPoint = new PointF(e.X, e.Y); //ScreenToMap(new PointF(e.X, e.Y));
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
bool isJunction = obj is NodeViewModel;
|
if (obj != null)
|
{
|
if (selectedObjs.Contains(obj))
|
{
|
obj.Selected = false;
|
selectedObjs.Remove(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
}
|
else
|
{
|
obj.Selected = true;
|
selectedObjs.Add(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
}
|
}
|
return;
|
}
|
|
if (e.Button == MouseButtons.Right)
|
{
|
if (_isRotating)
|
{
|
_isRotating = false;
|
this.Cursor = _lastCursor;
|
if (!isMouseMoved)
|
{
|
右键_Menu.Show(this, e.Location);
|
|
转换为ToolStripMenuItem.Enabled = selectedNodes.Count > 0; //(selectedObjs.Count == 1);
|
删除ToolStripMenuItem.Enabled = selectedObjs.Count > 0;
|
删除ToolStripMenuItem1.Enabled = selectedObjs.Count > 0;
|
|
}
|
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
mousePosition = new PointF(0, 0);
|
}
|
}
|
}
|
private void getPointAndHeight(MouseEventArgs e, out PointF p, out float z)
|
{
|
z = 0;
|
if (RotationF != 0)
|
{
|
p = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), z));
|
|
}
|
else
|
{
|
var m = ScreenToVMap(new PointF(e.X, e.Y));
|
z = m.Y;
|
p = new PointF(m.X, 0);
|
}
|
}
|
private void getPointAndHeight(MouseEventArgs e, NodeViewModel j, out PointF p, out float z)
|
{
|
z = j.Elev;
|
if (RotationF != 0)
|
{
|
p = MapPointToWorldPoint(ScreenToMap(new PointF(e.X, e.Y), j.Elev), j.Elev);
|
|
}
|
else
|
{
|
var m = ScreenToVMap(new PointF(e.X, e.Y));
|
z = m.Y;
|
p = new PointF(j.X, j.Y);
|
}
|
}
|
IBaseViewModel GetObj_by_ScreenPoint(PointF clickedPoint, float DistLimit = 15f)
|
{
|
float minDist = float.MaxValue;
|
|
int minIndex = -1;
|
bool isJunction = true;
|
IBaseViewModel obj = null;
|
|
for (int i = 0; i < _Nodes.Count; i++)
|
{
|
var node = _Nodes[i] as NodeViewModel;
|
if (!node.Visible) continue;
|
PointF mapPos = WorldPointToMapPoint(node);
|
PointF currentPoint = MapToScreen(mapPos);
|
float dist = Get_dist(clickedPoint, currentPoint);
|
if (dist < minDist && dist <= DistLimit)
|
{
|
minDist = dist;
|
minIndex = i;
|
obj = node;
|
}
|
}
|
for (int i = 0; i < _Links.Count; i++)
|
{
|
var link = _Links[i] as LinkViewModel;
|
if (!link.Visible) continue;
|
PointF mapPos1 = WorldPointToMapPoint(link.StartNode.Position, link.StartNode.Elev);
|
PointF currentPoint1 = MapToScreen(mapPos1);
|
PointF mapPos2 = WorldPointToMapPoint(link.EndNode.Position, link.EndNode.Elev);
|
PointF currentPoint2 = MapToScreen(mapPos2);
|
float dist = Get_dist(clickedPoint, currentPoint1, currentPoint2, DistLimit);
|
|
if (dist < minDist && dist <= DistLimit)
|
{
|
minDist = dist;
|
minIndex = i;
|
isJunction = false;
|
obj = link;
|
}
|
}
|
return obj;
|
}
|
|
|
/// <summary>
|
/// 鼠标滚轮事件
|
/// </summary>
|
/// <param name="e"></param>
|
void mapMouseWheel(MouseEventArgs e)
|
{
|
base.OnMouseWheel(e);
|
mapOption0 = mapOption.Copy();
|
float oldZoom = zoom;
|
zoom *= (float)Math.Pow(2, e.Delta / 120.0 / 10.0);
|
zoom = Math.Max(MinZoom, Math.Min(MaxZoom, zoom));
|
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
SetMapInvalidate();
|
}
|
|
private int _lastMouseX;
|
private int _lastMouseY;
|
#endregion
|
|
|
#region CubeView鼠标事件
|
void CubeViewMouseDown(MouseEventArgs e)
|
{
|
base.OnMouseDown(e);
|
|
_ClickStartPos = new PointF(e.X, e.Y);
|
DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
|
mousePosition = _MousePosition = ScreenToMap(new PointF(e.X, e.Y));
|
if (e.Button == MouseButtons.Left && _isInsertingObject)
|
{
|
|
}
|
else if (e.Button == MouseButtons.Left && _mouseState == MouseState.无)
|
{
|
|
}
|
|
else if (e.Button == MouseButtons.Left && _mouseState == MouseState.设置底图范围)
|
{
|
|
}
|
else if (e.Button == MouseButtons.Right)
|
{
|
RotaStartPos = new PointF(e.X, e.Y);
|
Rotation0 = Rotation;
|
俯视角度_start = RotationF;
|
_lastCursor = this.Cursor;
|
|
Cursor = Cursors.Hand;
|
mapOption0 = mapOption.Copy();
|
_isRotating = true;
|
}
|
|
}
|
void CubeViewMouseMove(MouseEventArgs e)
|
{
|
//base.OnMouseMove(e);
|
bool needInvalidate = false;
|
_MousePosition = ScreenToMap(new PointF(e.X, e.Y));
|
|
|
if (_isRotating)
|
{
|
mousePosition = _MousePosition;
|
bool is下半屏幕 = RotaStartPos.Y >= this.map.Height / 2;
|
if (ModifierKeys != Keys.Alt) Rotation = Rotation0 + ((float)e.X - (float)RotaStartPos.X) * 180 * 2.5 / (float)this.map.Width * (is下半屏幕 ? 1 : 1);
|
if (ModifierKeys != Keys.Shift) RotationF = 俯视角度_start + ((float)e.Y - (float)RotaStartPos.Y) * 180 * 2.5 / (float)this.map.Height;
|
if (RotationF > 90) RotationF = 90;
|
if (RotationF < 0) RotationF = 0;
|
needInvalidate = true;
|
|
}
|
|
|
/*判断是否触碰到对象*/
|
if (_mouseHoverCheckFlag)
|
{
|
_mouseHoverCheckFlag = false;
|
// 遍历所有对象,找出范围内的对象
|
PointF clickedPoint = new PointF(e.X, e.Y); //ScreenToMap(new PointF(e.X, e.Y));
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
if (hoveredObjs.Count > 0 && hoveredObjs[0] == obj || hoveredObjs.Count == 0 && obj == null)
|
{
|
//needInvalidate = false;
|
}
|
else
|
{
|
needInvalidate = true;
|
hoveredObjs.ForEach(o => o.Hovered = false);
|
hoveredObjs.Clear();
|
if (obj != null)
|
{
|
obj.Hovered = true;
|
hoveredObjs.Add(obj);
|
}
|
|
|
}
|
|
|
}
|
|
|
|
if (needInvalidate) this.SetMapInvalidate();
|
label_mouse.Text = $"X:{e.X.ToString("0")} Y:{e.Y.ToString("0")} [Map]X:{_MousePosition.X.ToString("0.00")} Y:{_MousePosition.Y.ToString("0.00")}";
|
_lastMouseX = e.X;
|
_lastMouseY = e.Y;
|
}
|
|
void CubeViewMouseUp(MouseEventArgs e)
|
{
|
//BookMark :鼠标抬起事件
|
base.OnMouseUp(e);
|
bool isMouseMoved = Get_dist(_ClickStartPos, new PointF(e.X, e.Y)) > 10;
|
bool isdoubleClick = (DateTime.Now - _lastMouseUp).TotalMilliseconds <= doubleClick_Delay;
|
_lastMouseUp = DateTime.Now;
|
|
if /*点选*/(!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.None)
|
{
|
_isDragging = false;
|
|
// 遍历所有点,找出最近的点
|
PointF clickedPoint = new PointF(e.X, e.Y); //ScreenToMap(new PointF(e.X, e.Y));
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
bool isJunction = obj is NodeViewModel;
|
|
if (obj != null)
|
{
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
obj.Selected = true;
|
selectedObjs.Add(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
_OperaNode = null;
|
|
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
|
|
}
|
else
|
{
|
//GlobalObject.LockSelect
|
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
|
_OperaNode = null;
|
|
SetMapInvalidate();
|
}
|
return;
|
|
|
}
|
|
if (/*叠加点选*/!isMouseMoved && _mouseState == MouseState.无 && e.Button == MouseButtons.Left && ModifierKeys == Keys.Control)
|
{
|
_isDragging = false;
|
|
// 遍历所有点,找出最近的点
|
PointF clickedPoint = new PointF(e.X, e.Y); //ScreenToMap(new PointF(e.X, e.Y));
|
var obj = GetObj_by_ScreenPoint(clickedPoint);
|
bool isJunction = obj is NodeViewModel;
|
if (obj != null)
|
{
|
if (selectedObjs.Contains(obj))
|
{
|
obj.Selected = false;
|
selectedObjs.Remove(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
}
|
else
|
{
|
obj.Selected = true;
|
selectedObjs.Add(obj);
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
mousePosition = new PointF(0, 0);
|
}
|
}
|
return;
|
}
|
|
if (e.Button == MouseButtons.Right)
|
{
|
if (_isRotating)
|
{
|
_isRotating = false;
|
this.Cursor = _lastCursor;
|
|
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
mousePosition = new PointF(0, 0);
|
}
|
}
|
}
|
#endregion
|
|
|
#endregion 一、全局
|
|
#region 二、工具栏
|
|
|
#region 视角工具
|
|
private void label_center_Click(object sender, EventArgs e)
|
{
|
GlobalObject.PropertyForm.SetObj(MapCenter);
|
}
|
|
double 俯视角度_bak = 45;
|
private void tool视角_ButtonClick(object sender, EventArgs e)
|
{
|
mapOption0 = mapOption.Copy();
|
if (RotationF == 90)
|
{
|
RotationF = 俯视角度_bak;
|
}
|
else
|
{
|
俯视角度_bak = RotationF;
|
RotationF = 90;
|
|
}
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
SetMapInvalidate();
|
}
|
|
private void tool设置俯视角度_Click(object sender, EventArgs e)
|
{
|
mapOption0 = mapOption.Copy();
|
double jiaodu = 45;
|
var tool = sender as ToolStripItem;
|
jiaodu = Convert.ToDouble(tool.Text);
|
RotationF = jiaodu;
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
SetMapInvalidate();
|
|
}
|
|
public void 重置视角ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
mapOption0 = mapOption.Copy();
|
|
RotationF = 90;
|
Rotation = 0;
|
InitCenter();
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
SetMapInvalidate();
|
}
|
private void 正视图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
mapOption0 = mapOption.Copy();
|
RotationF = 0;
|
if (recordView) MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
|
SetMapInvalidate();
|
}
|
private void 俯视图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
mapOption0 = mapOption.Copy();
|
RotationF = 90;
|
MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
|
SetMapInvalidate();
|
}
|
private void 默认视角ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
|
mapOption0 = mapOption.Copy();
|
|
InitCenter();
|
RotationF = 45;
|
Rotation = -45;
|
MapObjectExtensions.AddCommand(mapOption, "Map", mapOption0, mapOption);
|
|
SetMapInvalidate();
|
}
|
private void 设为隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
selectedObjs.ForEach(v =>
|
{
|
if (v.Visible)
|
{
|
MapObjectExtensions.AddCommand(v, "Visible", v.Visible, false);
|
v.Visible = false;
|
}
|
|
});
|
|
this.SetMapInvalidate();
|
}
|
private void 设置长度ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
if (selectedObjs.Count == 1 && selectedObjs[0] is LinkViewModel l)
|
{
|
var dlg = new InputDlg();
|
dlg.SetBindingData("输入口径");
|
dlg.ReloadDataEvent += (length) =>
|
{
|
|
|
var count1 = _Links.FindAll(ll => ll.Node1 == l.StartNode.ID || ll.Node2 == l.StartNode.ID).Count;
|
var count2 = _Links.FindAll(ll => ll.Node1 == l.EndNode.ID || ll.Node2 == l.EndNode.ID).Count;
|
if (count1 > 1 && count2 <= 1)
|
{
|
MovePointbyLength(l.StartNode, l.EndNode, (float)length);
|
}
|
else if (count2 > 1 && count1 <= 1)
|
{
|
MovePointbyLength(l.EndNode, l.StartNode, (float)length);
|
}
|
else
|
{
|
MovePointbyLength(l.StartNode, l.EndNode, (float)length);
|
}
|
|
SetMapInvalidate();
|
|
|
};
|
|
}
|
}
|
void MovePointbyLength(NodeViewModel node1, NodeViewModel node2, float Length)
|
{
|
|
float distance = Vector3.Distance(new Vector3(node1.X, node1.Y, node1.Elev), new Vector3(node2.X, node2.Y, node2.Elev));
|
|
if (distance > 0)
|
{ // 如果 A 和 B 不是同一个点
|
// 计算要移动多少距离
|
float moveDistance = Length - distance;
|
|
// 计算向量 AB(从 A 指向 B)
|
Vector3 AB = new Vector3(node2.X - node1.X, node2.Y - node1.Y, node2.Elev - node1.Elev);
|
|
// 计算 AB 的单位向量
|
Vector3 unitAB = Vector3.Normalize(AB);
|
|
// 计算需要移动的距离的向量(与 AB 方向相同)
|
Vector3 moveVector = unitAB * moveDistance;
|
|
// 更新点 B 的坐标
|
node2.X += moveVector.X;
|
node2.Y += moveVector.Y;
|
node2.Elev += moveVector.Z;
|
|
}
|
}
|
private void 全部显示ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Nodes.Select(n => (NodeViewModel)n).ToList().ForEach(v =>
|
{
|
if (!v.Visible)
|
{
|
MapObjectExtensions.AddCommand(v, "Visible", v.Visible, true);
|
v.Visible = true;
|
}
|
});
|
_Links.Select(n => (LinkViewModel)n).ToList().ForEach(v0 =>
|
{
|
var v = (LinkViewModel)v0;
|
if (!v.Visible)
|
{
|
MapObjectExtensions.AddCommand(v, "Visible", v.Visible, true);
|
v.Visible = true;
|
}
|
});
|
this.SetMapInvalidate();
|
}
|
#endregion
|
|
|
#region 文件工具
|
private void tool打开_ButtonClick(object sender, EventArgs e)
|
{
|
|
// 创建打开文件对话框
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "INP Files (*.inp)|*.inp|All Files (*.*)|*.*";
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
_Template = new Template();
|
_Template.network = new MapViewNetWork();
|
|
|
//_Template.network.use_old = false;
|
// 获取选中文件的文件路径
|
string filePath = openFileDialog.FileName;
|
{
|
// 读取文件内容
|
_Network.BuildFromInp(filePath);
|
_Template.filePath = filePath;
|
//_filePath = filePath;
|
SetData(_Template);
|
}
|
}
|
}
|
|
private void 旧版打开ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
// 创建打开文件对话框
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "INP Files (*.inp)|*.inp|All Files (*.*)|*.*";
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
_Template = new Template();
|
_Template.network = new MapViewNetWork();
|
//_Template.network.use_old = true;
|
// 获取选中文件的文件路径
|
string filePath = openFileDialog.FileName;
|
{
|
// 读取文件内容
|
_Network.BuildFromInp(filePath);
|
_Template.filePath = filePath;
|
//_filePath = filePath;
|
SetData(_Template);
|
}
|
}
|
}
|
|
private void 打开文件位置ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_filePath == null) return;
|
FileInfo fi = new FileInfo(_filePath);
|
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
|
//System.Diagnostics.Process.Start("explorer.exe", fi.Directory.FullName);
|
}
|
private void EPA中打开ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_filePath == null) return;
|
|
|
System.Diagnostics.Process.Start(@"epanetH\Epanet2wH.exe", _filePath);
|
}
|
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (!_IsEditMode)
|
{
|
TipFormHelper.ShowWarn("浏览模式无法保存,请切换编辑模式");
|
return;
|
}
|
else
|
_Network.BuildToInp(_filePath);
|
}
|
|
private void toolStripButton_save_Click(object sender, EventArgs e)
|
{
|
|
|
|
|
|
}
|
|
private void 另存ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
}
|
|
private void 导出形状ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
}
|
#endregion
|
|
|
#region 绘图工具
|
|
|
|
public void toolStripButton_新建节点_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新增节点;
|
Cursor = Cursors.Cross;
|
}
|
|
public void toolStripButton_普通_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.无;
|
Cursor = Cursors.Default;
|
if (_isPainting)
|
{
|
_select_junction1 = null;
|
_isPainting = false;
|
SetMapInvalidate();
|
}
|
else if (_isDragging)
|
{
|
_isDragging = false;
|
SetMapInvalidate();
|
}
|
else if (_isPanning)
|
{
|
_isPanning = false;
|
SetMapInvalidate();
|
}
|
else if (_isRotating)
|
{
|
Rotation = Rotation0;
|
_isRotating = false;
|
SetMapInvalidate();
|
}
|
else if (_isMovingObject)
|
{
|
_NewNet.Clear();
|
_isMovingObject = false;
|
SetMapInvalidate();
|
}
|
else if (_mouseState != MouseState.无)
|
{
|
_mouseState = MouseState.无;
|
Cursor = Cursors.Default;
|
}
|
else if (_isInsertingObject)
|
{
|
_NewNet.Clear();
|
_isInsertingObject = false;
|
_OperaNode = null;
|
SetMapInvalidate();
|
}
|
else
|
{
|
_Nodes.ForEach(o => ((NodeViewModel)o).Selected = false);
|
_Links.ForEach(o => ((LinkViewModel)o).Selected = false);
|
selectedObjs.Clear();
|
SetMapInvalidate();
|
|
|
}
|
}
|
|
public void toolStripButton_新建管线_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新增管线;
|
Cursor = Cursors.Cross;
|
}
|
public void toolStripButton_新建立管_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新增立管;
|
Cursor = Cursors.Cross;
|
}
|
|
|
private List<IBaseViewModel> AddLink(PointF e, bool isdoubleClick, PointF p, float z)
|
{
|
List<IBaseViewModel> l = new List<IBaseViewModel>();
|
var node = GetObj_by_ScreenPoint(e);
|
if (node != null || isdoubleClick)
|
{
|
if (node != null && node is NodeViewModel j)
|
{
|
_select_junction2 = j;
|
}
|
else if (isdoubleClick)
|
{
|
_select_junction2 = _Network.AddJunction(p, z);
|
l.Add(_select_junction2);
|
}
|
|
if (_mouseState == MouseState.新增管线 || _mouseState == MouseState.新增立管)
|
l.Add(_Network.AddPipe(_select_junction1, _select_junction2));
|
else if (_mouseState == MouseState.新建阀门)
|
l.Add(_Network.AddValve(_select_junction1, _select_junction2));
|
else if (_mouseState == MouseState.新建重复器)
|
l.Add(_Network.AddRepeater(_select_junction1, _select_junction2));
|
else if (_mouseState == MouseState.新建水泵)
|
l.Add(_Network.AddPump(_select_junction1, _select_junction2));
|
|
_select_junction1 = null;
|
_select_junction2 = null;
|
_isPainting = false;
|
|
|
}
|
return l;
|
}
|
|
private void Set_junction1(MouseEventArgs e)
|
{
|
var node = GetObj_by_ScreenPoint(new PointF(e.X, e.Y));
|
|
if (node != null && node is NodeViewModel j)
|
{
|
_select_junction1 = j;
|
_isPainting = true;
|
}
|
}
|
|
public void toolStripButton_添加水表_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建水表;
|
Cursor = Cursors.Cross;
|
}
|
|
public void toolStripButton_添加阀门_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建阀门;
|
Cursor = Cursors.Cross;
|
}
|
public void toolStripButton_添加水泵_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建水泵;
|
Cursor = Cursors.Cross;
|
}
|
public void toolStripButton_添加水库_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建水库;
|
Cursor = Cursors.Cross;
|
}
|
|
public void toolStripButton_添加水池_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建水池;
|
Cursor = Cursors.Cross;
|
}
|
public void toolStripButton_重复器_Click(object sender, EventArgs e)
|
{
|
_mouseState = MouseState.新建重复器;
|
Cursor = Cursors.Cross;
|
}
|
|
bool Buzylock = false;
|
public void MapViewer_KeyDown(object sender, KeyEventArgs e)
|
{
|
|
if (e.KeyCode == Keys.Escape)
|
{
|
Cursor = _lastCursor;
|
if (_isPainting)
|
{
|
_select_junction1 = null;
|
_isPainting = false;
|
SetMapInvalidate();
|
}
|
else if (_isDragging)
|
{
|
_isDragging = false;
|
SetMapInvalidate();
|
}
|
else if (_isPanning)
|
{
|
_isPanning = false;
|
SetMapInvalidate();
|
}
|
else if (_isRotating)
|
{
|
Rotation = Rotation0;
|
_isRotating = false;
|
SetMapInvalidate();
|
}
|
else if (_isMovingObject)
|
{
|
_NewNet.Clear();
|
_isMovingObject = false;
|
SetMapInvalidate();
|
}
|
else if (_mouseState != MouseState.无)
|
{
|
_mouseState = MouseState.无;
|
Cursor = Cursors.Default;
|
}
|
else if (_isInsertingObject)
|
{
|
_NewNet.Clear();
|
_isInsertingObject = false;
|
_OperaNode = null;
|
SetMapInvalidate();
|
}
|
else
|
{
|
_Nodes.ForEach(o => ((NodeViewModel)o).Selected = false);
|
_Links.ForEach(o => ((LinkViewModel)o).Selected = false);
|
selectedObjs.Clear();
|
SetMapInvalidate();
|
|
|
}
|
|
|
}
|
if (_IsEditMode && e.KeyCode == Keys.Delete)
|
{
|
DeleteChoosedObj();
|
}
|
if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
|
{
|
selectedObjs.Clear();
|
_Nodes.ForEach(o => { ((NodeViewModel)o).Selected = true; selectedObjs.Add((NodeViewModel)o); });
|
_Links.ForEach(o => { ((LinkViewModel)o).Selected = true; selectedObjs.Add((LinkViewModel)o); });
|
SetMapInvalidate();
|
}
|
|
if (_IsEditMode && e.KeyCode == Keys.C && e.Modifiers == Keys.Control)
|
{
|
复制ToolStripMenuItem_Click(1, new EventArgs());
|
}
|
if (_IsEditMode && e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
|
{
|
_ClickStartPos = Cursor.Position;
|
粘贴ToolStripMenuItem1_Click(1, new EventArgs());
|
}
|
if (_IsEditMode && e.KeyCode == Keys.D1 && e.Modifiers == Keys.Control)
|
{
|
南北对齐ToolStripMenuItem_Click(1, new EventArgs());
|
|
}
|
if (_IsEditMode && e.KeyCode == Keys.Oemtilde && e.Modifiers == Keys.Control)
|
{
|
东西对齐ToolStripMenuItem_Click(1, new EventArgs());
|
}
|
if (_IsEditMode && e.KeyCode == Keys.D2 && e.Modifiers == Keys.Control)
|
{
|
竖直对齐ToolStripMenuItem_Click(1, new EventArgs());
|
}
|
if (_IsEditMode && e.KeyCode == Keys.D3 && e.Modifiers == Keys.Control)
|
{
|
自动对齐ToolStripMenuItem_Click(1, new EventArgs());
|
}
|
}
|
|
|
|
private void MapViewer_KeyPress(object sender, KeyPressEventArgs e)
|
{
|
if (_IsEditMode && ctrlPressed && e.KeyChar == 'Z' - 64)
|
{
|
// 执行相应的操作
|
buttonUndo_Click(sender, e);
|
ctrlPressed = false;
|
|
// 防止默认操作
|
e.Handled = true;
|
}
|
if (_IsEditMode && ctrlPressed && e.KeyChar == 'Y' - 64)
|
{
|
// 执行相应的操作
|
buttonRedo_Click(sender, e);
|
ctrlPressed = false;
|
|
// 防止默认操作
|
e.Handled = true;
|
}
|
if (ctrlPressed && e.KeyChar == 'R' - 64)
|
{
|
// 执行相应的操作
|
tool视角_ButtonClick(sender, e);
|
ctrlPressed = false;
|
|
// 防止默认操作
|
e.Handled = true;
|
}
|
}
|
private void MapViewer_PreKeyPress(object sender, PreviewKeyDownEventArgs e)
|
{
|
if (_IsEditMode && e.Control && e.KeyCode == Keys.Z)
|
{
|
ctrlPressed = true;
|
// 防止默认操作
|
e.IsInputKey = true;
|
|
}
|
if (_IsEditMode && e.Control && e.KeyCode == Keys.Y)
|
{
|
ctrlPressed = true;
|
// 防止默认操作
|
e.IsInputKey = true;
|
|
}
|
if (e.Control && e.KeyCode == Keys.R)
|
{
|
ctrlPressed = true;
|
// 防止默认操作
|
e.IsInputKey = true;
|
|
}
|
}
|
public void setCenter(IBaseViewModel obj)
|
{
|
PointF position;
|
if (obj is LinkViewModel link)
|
position = link.Position;
|
else
|
position = obj.Position;
|
PointF currentPos = MapToScreen(WorldPointToMapPoint(position, obj.Elev));
|
PointF centerScreen = new PointF(this.map.Width / 2, this.map.Height / 2);
|
var vector = GetRotateVector(centerScreen, currentPos);
|
MapCenter = new PointF(
|
MapCenter.X - vector.X / Zoom.X,
|
MapCenter.Y - vector.Y / Zoom.Y);
|
}
|
|
bool ctrlPressed = false;
|
#endregion
|
|
|
#region 右键菜单
|
|
private void 转换ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var nodes = selectedNodes;
|
foreach (var obj in nodes)
|
{
|
if (obj == null) return;
|
var toolItem = sender as ToolStripItem;
|
NodeViewModel junc = null;
|
int i = 0;
|
string ID;
|
switch (toolItem.Text)
|
{
|
case "水表":
|
junc = _Network.AddMeter(obj.Position);
|
i = 0;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
while (_Nodes.Find(p0 => p0.ID == ID) != null)
|
{
|
i++;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
}
|
junc.ID = ID;
|
break;
|
case "基本节点":
|
|
junc = _Network.AddJunction(obj.Position, obj.Elev);
|
i = 0;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
while (_Nodes.Find(p0 => p0.ID == ID) != null)
|
{
|
i++;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
}
|
junc.ID = ID;
|
break;
|
case "水库":
|
junc = _Network.AddReservoir(obj.Position);
|
i = 0;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
while (_Nodes.Find(p0 => p0.ID == ID) != null)
|
{
|
i++;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
}
|
junc.ID = ID;
|
break;
|
case "水池":
|
junc = _Network.AddTank(obj.Position);
|
i = 0;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
while (_Nodes.Find(p0 => p0.ID == ID) != null)
|
{
|
i++;
|
ID = $"{Default.GetPreString(junc)}{i}";
|
}
|
junc.ID = ID;
|
break;
|
}
|
//junc.ID = obj.ID;
|
|
junc.Level = obj.Level;
|
junc.Elev = obj.Elev;
|
junc.Demand = obj.Demand;
|
|
|
junc.Selected = true;
|
foreach (var p in _Links)
|
{
|
if (p.StartNode == obj)
|
{
|
p.StartNode = junc;
|
|
}
|
else if (p.EndNode == obj)
|
{
|
p.EndNode = junc;
|
}
|
}
|
selectedObjs.Add(junc);
|
selectedObjs.Remove(obj);
|
//MapObjectExtensions.AddCommand(obj, "Add", null, new List<MapObject>() { n });
|
_Nodes.Remove(obj);
|
}
|
|
|
SetMapInvalidate();
|
}
|
|
public void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
DeleteChoosedObj();
|
}
|
|
private void DeleteChoosedObj()
|
{
|
var list = _Network.Remove(selectedObjs);
|
MapObjectExtensions.AddCommand(_Network, "Remove", null, list);
|
selectedObjs.Clear();
|
SetMapInvalidate();
|
}
|
#endregion
|
|
|
#region 编辑模式/浏览模式切换工具
|
|
|
private void toolStripComboBox_expandRepeater_ButtonClick(object sender, EventArgs e)
|
{
|
_IsEditMode = !_IsEditMode;
|
}
|
|
private void 浏览模式ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var obj = sender as ToolStripItem;
|
_IsEditMode = obj.Text == "编辑模式";
|
}
|
|
#endregion
|
|
#region 分析工具
|
|
private void 水平旋转ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var objs = GlobalObject.PropertyForm.selectionSet.selectedObjects;
|
var selectobjs = objs.FindAll(o => o is NodeViewModel); //GlobalObject.PropertyForm.listBox1.SelectedItems;
|
if (selectobjs.Count <= 0) return;
|
if (!(selectobjs[0] as IBaseViewModel).isNode())
|
{
|
MessageBox.Show("围绕选中节点进行平面旋转,请在属性列表中选中一个节点类对象");
|
return;
|
}
|
|
NodeViewModel origin = selectobjs[0] as NodeViewModel;
|
var nodes = objs.FindAll(o => o.isNode());
|
|
float jiaodu = float.Parse(toolStripTextBox_水平旋转角度.Text.ToString());
|
var NewPositions = GetRotatedPoints(nodes.Select(n => n.Position).ToList(), origin.Position, jiaodu);
|
var OldPositions = nodes.Select(n => n.Position).ToList();
|
for (int i = 0; i < nodes.Count; i++)
|
{
|
nodes[i].Position = NewPositions[i];
|
}
|
|
MapObjectExtensions.AddCommand(nodes, "Position", OldPositions, NewPositions);
|
|
SetMapInvalidate();
|
|
}
|
|
public List<PointF> GetRotatedPoints(List<PointF> points, PointF origin, float angle)
|
{
|
// 将角度转换为弧度
|
float radians = angle * (float)Math.PI / 180.0f;
|
|
// 计算正余弦值
|
float cos = (float)Math.Cos(radians);
|
float sin = (float)Math.Sin(radians);
|
|
// 定义结果集合,并遍历输入点集合进行旋转处理
|
List<PointF> result = new List<PointF>(points.Count);
|
foreach (PointF point in points)
|
{
|
// 将点相对于旋转中心点平移,使其成为以原点为中心的坐标系
|
float translatedX = point.X - origin.X;
|
float translatedY = point.Y - origin.Y;
|
|
// 应用旋转变换
|
float rotatedX = translatedX * cos - translatedY * sin;
|
float rotatedY = translatedX * sin + translatedY * cos;
|
|
// 将点相对于原坐标系平移回去
|
float finalX = rotatedX + origin.X;
|
float finalY = rotatedY + origin.Y;
|
|
// 添加到结果集合中
|
result.Add(new PointF(finalX, finalY));
|
}
|
|
return result;
|
}
|
|
|
public List<PointF3D> GetRotatedPoints(List<PointF3D> points, Vector3 line, float angle)
|
{
|
// 将角度转换为弧度
|
float radians = (float)(angle * Math.PI / 180.0f);
|
|
// 根据旋转直线的方向矢量和旋转角度计算旋转矩阵
|
float cos = (float)Math.Cos(radians);
|
float sin = (float)Math.Sin(radians);
|
float x = line.X;
|
float y = line.Y;
|
float z = line.Z;
|
float[,] rotationMatrix = new float[,]
|
{
|
{cos + x * x * (1 - cos), x * y * (1 - cos) - z * sin, x * z * (1 - cos) + y * sin},
|
{y * x * (1 - cos) + z * sin, cos + y * y * (1 - cos), y * z * (1 - cos) - x * sin},
|
{z * x * (1 - cos) - y * sin, z * y * (1 - cos) + x * sin, cos + z * z * (1 - cos)}
|
};
|
|
// 定义结果集合,并遍历输入点集合进行旋转处理
|
List<PointF3D> result = new List<PointF3D>(points.Count);
|
foreach (PointF3D point in points)
|
{
|
// 将点按照旋转中心线平移,使其成为以原点为中心的坐标系
|
Vector3 translatedPoint = new Vector3(point.X - line.X, point.Y - line.Y, point.Z - line.Z);
|
|
// 应用旋转变换
|
Vector3 rotatedPointVector = new Vector3(
|
translatedPoint.X * rotationMatrix[0, 0] + translatedPoint.Y * rotationMatrix[0, 1] + translatedPoint.Z * rotationMatrix[0, 2],
|
translatedPoint.X * rotationMatrix[1, 0] + translatedPoint.Y * rotationMatrix[1, 1] + translatedPoint.Z * rotationMatrix[1, 2],
|
translatedPoint.X * rotationMatrix[2, 0] + translatedPoint.Y * rotationMatrix[2, 1] + translatedPoint.Z * rotationMatrix[2, 2]);
|
|
// 将点相对于原坐标系平移回去
|
float finalX = rotatedPointVector.X + line.X;
|
float finalY = rotatedPointVector.Y + line.Y;
|
float finalZ = rotatedPointVector.Z + line.Z;
|
|
// 将旋转后的点添加到结果集合中
|
result.Add(new PointF3D(finalX, finalY, finalZ));
|
}
|
|
return result;
|
}
|
|
private void 缩放ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
var objs = GlobalObject.PropertyForm.selectionSet.selectedObjects;
|
var list = objs.FindAll(o => o is NodeViewModel);
|
if (list.Count <= 0) return;
|
if (list.Count >= 1 && !list[0].isNode())
|
{
|
MessageBox.Show("围绕选中节点进行三维缩放,请在属性列表中选中一个[节点类]缩放中心对象");
|
return;
|
}
|
|
NodeViewModel origin = list[0] as NodeViewModel;
|
var nodes = objs.FindAll(o => o.isNode()).Select(o => o as NodeViewModel).ToList();
|
|
ToolStripMenuItem item = sender as ToolStripMenuItem;
|
|
float jiaodu;
|
switch (item.Text)
|
{
|
case "缩小2倍":
|
jiaodu = 0.5f;
|
break;
|
case "放大2倍":
|
jiaodu = 2f;
|
|
break;
|
default:
|
jiaodu = float.Parse(toolStripTextBox_缩放比例.Text.ToString());
|
|
break;
|
}
|
var OldPositions = nodes.Select(n => n.Position3D).ToList();
|
var NewPositions = ScalePoints(nodes.Select(n => n.Position3D).ToList(), origin.Position3D, jiaodu);
|
for (int i = 0; i < nodes.Count; i++)
|
{
|
nodes[i].X = NewPositions[i].X;
|
nodes[i].Y = NewPositions[i].Y;
|
nodes[i].Elev = NewPositions[i].Z;
|
}
|
|
MapObjectExtensions.AddCommand(nodes, "Position3D", OldPositions, NewPositions);
|
|
SetMapInvalidate();
|
}
|
|
public static List<PointF3D> ScalePoints(List<PointF3D> points, PointF3D centerPoint, float scale)
|
{
|
//定义结果集合,并遍历输入点集合进行缩放处理
|
List<PointF3D> result = new List<PointF3D>(points.Count);
|
foreach (PointF3D point in points)
|
{
|
//将点相对于缩放中心点平移,使其成为以原点为中心的坐标系
|
Vector3 translatedPoint = new Vector3(point.X - centerPoint.X, point.Y - centerPoint.Y, point.Z - centerPoint.Z);
|
|
//应用缩放变换
|
Vector3 scaledPointVector = new Vector3(
|
translatedPoint.X * scale + centerPoint.X,
|
translatedPoint.Y * scale + centerPoint.Y,
|
translatedPoint.Z * scale + centerPoint.Z);
|
|
//将缩放后的点添加到结果集合中
|
result.Add(new PointF3D(scaledPointVector.X, scaledPointVector.Y, scaledPointVector.Z));
|
}
|
|
return result;
|
}
|
|
|
public void btn_拓扑检查_Click(object sender, EventArgs e)
|
{
|
Dictionary<string, List<string>> result = _Template.network.CheckValidate();
|
if (result.Count > 0)
|
{
|
ListBox listBox1 = new ListBox();
|
listBox1.Dock = DockStyle.Fill;
|
listBox1.Width = 200;
|
listBox1.Height = 400;
|
listBox1.Items.AddRange(result.Keys.ToArray());
|
listBox1.SelectedIndexChanged += (s, e) =>
|
{
|
//listBox2.Items.Clear();
|
if (listBox1.SelectedIndex >= 0)
|
{
|
var arr = result[listBox1.SelectedItem.ToString()].ToHashSet();
|
|
List<IBaseViewModel> Objs = new List<IBaseViewModel>();
|
Objs.AddRange(_Nodes.Select(n => (NodeViewModel)n));
|
Objs.AddRange(_Links.Select(l => (LinkViewModel)l));
|
var nodes = _Nodes.FindAll(o => arr.Contains(o.ID));
|
var links = _Links.FindAll(o => arr.Contains(o.ID));
|
//listBox2.Items.AddRange(result[listBox1.SelectedItem.ToString()].ToArray());
|
selectedObjs.ForEach(obj => obj.Selected = false);
|
selectedObjs.Clear();
|
nodes.ForEach(obj =>
|
{
|
obj.Selected = true;
|
selectedObjs.Add((NodeViewModel)obj);
|
});
|
links.ForEach(obj =>
|
{
|
obj.Selected = true;
|
selectedObjs.Add((LinkViewModel)obj);
|
});
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
SetMapInvalidate();
|
}
|
};
|
|
|
Form ResultForm = new Form();
|
ResultForm.Text = "拓扑检查结果";
|
ResultForm.Width = 300;
|
ResultForm.Height = 400;
|
ResultForm.StartPosition = FormStartPosition.CenterScreen;
|
ResultForm.Controls.Add(listBox1);
|
ResultForm.MinimizeBox = false;
|
ResultForm.MaximizeBox = false;
|
ResultForm.Show();
|
|
}
|
else
|
{
|
MessageBox.Show("拓扑检查通过");
|
}
|
|
}
|
|
|
|
public void 关阀搜索ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedObjs.Count > 0)
|
{
|
var objs = selectedObjs.ToList();
|
var selectsValve = objs.FindAll(o => o is ValveViewModel);
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
TraversePipeNetwork(objs, null, false);
|
selectsValve.ForEach(o => o.Selected = false);
|
this.SetMapInvalidate();
|
}
|
|
}
|
|
/// <summary>
|
/// 关阀分析(考虑水源的情况)
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void 关阀搜索考虑水源ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedObjs.Count > 0)//&& selectedObjs[0] is Link l
|
{
|
|
var objs = selectedObjs.ToList();
|
var selectsValve = objs.FindAll(o => o is ValveViewModel);
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
TraversePipeNetwork(objs, null);
|
selectsValve.ForEach(o => o.Selected = false);
|
this.SetMapInvalidate();
|
|
}
|
}
|
//Queue<Link> queue2 = null;
|
List<LinkViewModel> LinksToFindSource = null;
|
Dictionary<LinkViewModel, List<IBaseViewModel>> Sets = null;
|
Dictionary<LinkViewModel, bool> Sets_hasSource = null;
|
private void TraversePipeNetwork(List<IBaseViewModel> startObjs, HashSet<NodeViewModel> visitedNodes = null, bool consider = true)
|
{
|
|
LinksToFindSource = new List<LinkViewModel>();
|
|
if (visitedNodes == null)
|
visitedNodes = new HashSet<NodeViewModel>();
|
|
startObjs.ForEach(o =>
|
{
|
if (o is LinkViewModel l)
|
{
|
TraversePipeNetwork(l, visitedNodes);
|
}
|
else if (o is NodeViewModel n)
|
{
|
n.Links.Select(oo => oo as LinkViewModel).ToList().ForEach(link => TraversePipeNetwork(link, visitedNodes));
|
|
}
|
});
|
if (!consider) return;
|
Sets = new Dictionary<LinkViewModel, List<IBaseViewModel>>();
|
Sets_hasSource = new Dictionary<LinkViewModel, bool>();
|
LinksToFindSource.ForEach(l =>
|
{
|
TraversePipeNetwork_Set(l, visitedNodes);
|
});
|
|
foreach (var kp in Sets)
|
{
|
if (!Sets_hasSource[kp.Key])
|
{
|
kp.Value.ForEach(o =>
|
{
|
if (!(o is ValveViewModel))
|
{
|
o.Selected = true;
|
selectedObjs.Add(o);
|
}
|
|
});
|
}
|
}
|
if (GlobalObject.PropertyForm != null)
|
GlobalObject.PropertyForm.SetObjs(selectedObjs);
|
|
}
|
private void TraversePipeNetwork(LinkViewModel startLink, HashSet<NodeViewModel> visitedNodes = null)
|
{
|
|
Queue<LinkViewModel> queue = new Queue<LinkViewModel>();
|
|
|
|
queue.Enqueue(startLink);
|
if (visitedNodes == null)
|
visitedNodes = new HashSet<NodeViewModel>();
|
|
while (queue.Count > 0)
|
{
|
LinkViewModel currentLink = queue.Dequeue();
|
|
foreach (var node in new NodeViewModel[] { currentLink.StartNode, currentLink.EndNode })
|
{
|
if (!visitedNodes.Contains(node) && node != null)
|
{
|
visitedNodes.Add(node);
|
node.Selected = true;
|
selectedObjs.Add(node);
|
|
//Console.WriteLine("Visiting Node: " + node.ID);
|
foreach (var link in node.ViewLinks)
|
{
|
if (!visitedNodes.Contains(link.StartNode) || !visitedNodes.Contains(link.EndNode))
|
{
|
link.Selected = true;
|
selectedObjs.Add(link);
|
|
|
if (!(link is ValveViewModel))
|
{
|
queue.Enqueue(link);
|
|
}
|
else
|
{
|
LinksToFindSource.Add(link);
|
}
|
|
|
}
|
}
|
}
|
}
|
|
}
|
|
|
|
}
|
private void TraversePipeNetwork_Set(LinkViewModel startLink, HashSet<NodeViewModel> visitedNodes = null)
|
{
|
|
Queue<LinkViewModel> queue = new Queue<LinkViewModel>();
|
|
|
|
|
queue.Enqueue(startLink);
|
if (visitedNodes == null)
|
visitedNodes = new HashSet<NodeViewModel>();
|
//visitedNodes.Add(startLink.StartNode);
|
//visitedNodes.Add(startLink.EndNode);
|
Sets.Add(startLink, new List<IBaseViewModel>());
|
Sets_hasSource.Add(startLink, false);
|
while (queue.Count > 0)
|
{
|
LinkViewModel currentLink = queue.Dequeue();
|
//Console.WriteLine("Traversing Link: " + currentLink.ID);
|
|
foreach (var node in new NodeViewModel[] { currentLink.StartNode, currentLink.EndNode })
|
{
|
if (node == null) continue;
|
if (!visitedNodes.Contains(node))
|
{
|
Sets[startLink].Add(node);
|
visitedNodes.Add(node);
|
if (node is TankViewModel || node is ReservoirViewModel)
|
{
|
Sets_hasSource[startLink] = true;
|
}
|
|
//Console.WriteLine("Visiting Node: " + node.ID);
|
foreach (var link in node.ViewLinks)
|
{
|
if (!visitedNodes.Contains(link.StartNode) || !visitedNodes.Contains(link.EndNode))
|
{
|
Sets[startLink].Add(link);
|
queue.Enqueue(link);
|
}
|
}
|
}
|
else
|
{
|
foreach (var kp in Sets)
|
{
|
if (kp.Key != startLink && kp.Value.Contains(node))
|
{
|
kp.Value.AddRange(Sets[startLink]);
|
Sets_hasSource[kp.Key] = Sets_hasSource[startLink] || Sets_hasSource[kp.Key];
|
Sets.Remove(startLink);
|
Sets_hasSource.Remove(startLink);
|
startLink = kp.Key;
|
break;
|
}
|
}
|
}
|
}
|
|
}
|
|
|
|
}
|
private bool FindSouce(LinkViewModel startLink, HashSet<LinkViewModel> visitedLinks, HashSet<LinkViewModel> visitedLinks2, Dictionary<IBaseViewModel, bool> hasSource = null)
|
{
|
|
|
foreach (var node in new NodeViewModel[] { startLink.StartNode, startLink.EndNode })
|
{
|
|
if (!hasSource.ContainsKey(node))
|
{
|
|
hasSource[node] = FindSouce(node, visitedLinks, visitedLinks2, hasSource);
|
}
|
|
if (hasSource[node] == true) return true;
|
|
}
|
return false;
|
|
}
|
private bool FindSouce(NodeViewModel startNode, HashSet<LinkViewModel> visitedLinks, HashSet<LinkViewModel> visitedLinks2, Dictionary<IBaseViewModel, bool> hasSource = null)
|
{
|
|
foreach (var link in startNode.ViewLinks)
|
{
|
if (hasSource.ContainsKey(link) && hasSource[link] == true) return true;
|
if (visitedLinks.Contains(link) && startNode == link.StartNode) continue;
|
if (visitedLinks2.Contains(link) && startNode == link.EndNode) continue;
|
if (startNode == link.StartNode)
|
visitedLinks.Add(link);
|
else
|
visitedLinks2.Add(link);
|
if (!hasSource.ContainsKey(link))
|
{
|
hasSource[link] = FindSouce(link, visitedLinks, visitedLinks2, hasSource);
|
}
|
|
if (hasSource[link] == true) return true;
|
|
|
}
|
return false;
|
|
|
}
|
|
private void TraversePipeNetworkALL(LinkViewModel startLink, HashSet<NodeViewModel> visitedNodes = null, int direction = 0)
|
{
|
|
Queue<LinkViewModel> queue = new Queue<LinkViewModel>();
|
|
|
|
queue.Enqueue(startLink);
|
if (visitedNodes == null)
|
visitedNodes = new HashSet<NodeViewModel>();
|
|
while (queue.Count > 0)
|
{
|
LinkViewModel currentLink = queue.Dequeue();
|
|
foreach (var node in new NodeViewModel[] { currentLink.StartNode, currentLink.EndNode })
|
{
|
if (direction == 1 && currentLink.EN_FLOW >= 0 && node == currentLink.StartNode) continue;
|
if (direction == -1 && currentLink.EN_FLOW <= 0 && node == currentLink.EndNode) continue;
|
if (node != null && !visitedNodes.Contains(node))
|
{
|
visitedNodes.Add(node);
|
node.Selected = true;
|
selectedObjs.Add(node);
|
|
foreach (var link in node.ViewLinks)
|
{
|
if (!visitedNodes.Contains(link.StartNode) || !visitedNodes.Contains(link.EndNode))
|
{
|
link.Selected = true;
|
selectedObjs.Add(link);
|
queue.Enqueue(link);
|
|
|
|
|
}
|
}
|
}
|
}
|
|
}
|
|
|
|
}
|
#endregion
|
|
|
#region 显示选项
|
|
private void 显示节点ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_ShowJunction = true;
|
SetMapInvalidate();
|
}
|
|
private void 隐藏节点ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_ShowJunction = false;
|
SetMapInvalidate();
|
}
|
|
private void 大ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var item = sender as ToolStripItem;
|
if (item.Text == "大")
|
{
|
junction_multiply = 1f;
|
}
|
else if (item.Text == "中")
|
{
|
junction_multiply = 0.6667f;
|
}
|
else
|
{
|
junction_multiply = 0.4f;
|
}
|
SetMapInvalidate();
|
}
|
|
private void 显示阀门ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_ShowValve = true;
|
SetMapInvalidate();
|
|
}
|
|
private void 隐藏阀门ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_ShowValve = false;
|
SetMapInvalidate();
|
|
|
}
|
|
private void 大ToolStripMenuItem1_Click(object sender, EventArgs e)
|
{
|
var item = sender as ToolStripItem;
|
if (item.Text == "大")
|
{
|
Link_multiply = 1f;
|
}
|
else if (item.Text == "中")
|
{
|
Link_multiply = 0.6667f;
|
}
|
else
|
{
|
Link_multiply = 0.4f;
|
}
|
SetMapInvalidate();
|
}
|
|
|
#endregion
|
|
|
#endregion 二、工具栏
|
|
#region 方法
|
private void 标高推测ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
int num = UpdateNodesEle();
|
if (num > 0)
|
{
|
MessageBox.Show($"更新成功,更新节点数量:{num}个");
|
}
|
else
|
{
|
MessageBox.Show($"没有需要更新的节点");
|
|
}
|
}
|
public int UpdateNodesEle()
|
{
|
int num = 0;
|
foreach (NodeViewModel node in _Nodes)
|
{
|
if (node.Elev == 0 && node is JunctionViewModel)
|
{
|
HashSet<NodeViewModel> nodeSet = new HashSet<NodeViewModel>();
|
nodeSet.Add(node);
|
List<NodeViewModel> nonZeroNeighbors = new List<NodeViewModel>();
|
FindNonZeroNeighbors(node, nonZeroNeighbors, nodeSet);
|
if (nonZeroNeighbors.Count != 0)
|
{
|
float sum = 0;
|
float weightSum = 0;
|
|
foreach (NodeViewModel neighbor in nonZeroNeighbors)
|
{
|
float dist = Get_dist(node.Position, neighbor.Position);
|
if (dist == 0)
|
{
|
dist = 0.0000000001f;
|
}
|
float weight = 1 / dist;// CalculateWeight(node, neighbor);
|
sum += neighbor.Elev * weight;
|
weightSum += weight;
|
}
|
|
float average = sum / weightSum;
|
node.Elev = average;
|
num++;
|
}
|
}
|
}
|
return num;
|
}
|
|
private void FindNonZeroNeighbors(NodeViewModel node, List<NodeViewModel> result, HashSet<NodeViewModel> origin)
|
{
|
foreach (LinkViewModel link in node.ViewLinks)
|
{
|
NodeViewModel neighbor = null;
|
if (link.StartNode == node)
|
{
|
neighbor = link.EndNode;
|
|
}
|
else
|
{
|
neighbor = link.StartNode;
|
}
|
|
if (neighbor.Elev != 0)
|
{
|
result.Add(neighbor);
|
}
|
else if (!origin.Contains(neighbor))
|
{
|
origin.Add(neighbor);
|
FindNonZeroNeighbors(neighbor, result, origin);
|
}
|
}
|
}
|
private void 标高导出ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
DataTable dt = new DataTable();
|
dt.Columns.Add("ID");
|
dt.Columns.Add("Elev", typeof(float));
|
foreach (NodeViewModel node in _Nodes)
|
{
|
if (node is JunctionViewModel junction)
|
{
|
var dr = dt.NewRow();
|
dr.ItemArray = new object[] { junction.ID, junction.Elev };
|
dt.Rows.Add(dr);
|
}
|
}
|
dtToSql(dt);
|
|
}
|
void dtToCsv(DataTable dt)
|
{
|
// 创建 SaveFileDialog 对象
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "CSV Files (*.csv)|*.csv";
|
saveFileDialog.FileName = "output.csv";
|
|
// 如果用户选择了保存位置和文件名,则执行保存操作
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
// 获取用户选择的保存路径和文件名
|
string filePath = saveFileDialog.FileName;
|
|
// 假设 dt 是包含数据的 DataTable 对象
|
|
// 构建 CSV 字符串
|
StringBuilder csvContent = new StringBuilder();
|
|
// 写入表头
|
foreach (DataColumn column in dt.Columns)
|
{
|
csvContent.Append(column.ColumnName);
|
csvContent.Append(",");
|
}
|
csvContent.AppendLine();
|
|
// 写入数据行
|
foreach (DataRow row in dt.Rows)
|
{
|
for (int i = 0; i < dt.Columns.Count; i++)
|
{
|
csvContent.Append(row[i]);
|
csvContent.Append(",");
|
}
|
csvContent.AppendLine();
|
}
|
|
// 将 CSV 内容写入文件
|
File.WriteAllText(filePath, csvContent.ToString());
|
|
// 显示保存成功消息框
|
MessageBox.Show("文件保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
}
|
}
|
void dtToSql(DataTable dt)
|
{
|
// 创建 SaveFileDialog 对象
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "Txt Files (*.txt)|*.txt";
|
|
|
// 如果用户选择了保存位置和文件名,则执行保存操作
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
// 获取用户选择的保存路径和文件名
|
string filePath = saveFileDialog.FileName;
|
|
// 假设 dt 是包含数据的 DataTable 对象
|
string tableString = @"
|
IF OBJECT_ID('Elev', 'U') IS NOT NULL
|
BEGIN
|
-- 清空表
|
TRUNCATE TABLE Elev;
|
END
|
ELSE
|
BEGIN
|
-- 创建表
|
CREATE TABLE Elev (
|
ID NVARCHAR(50),
|
Elev REAL
|
);
|
END
|
|
|
";
|
// 构建 CSV 字符串
|
StringBuilder csvContent = new StringBuilder();
|
|
csvContent.AppendLine(tableString);
|
|
|
|
//// 写入表头
|
//foreach (DataColumn column in dt.Columns)
|
//{
|
|
// csvContent.Append(column.ColumnName);
|
// csvContent.Append(",");
|
//}
|
//csvContent.AppendLine();
|
|
int i = 0;
|
int j = 0;
|
while (i < dt.Rows.Count)
|
{
|
|
if (j == 0) csvContent.AppendLine("INSERT INTO Elev (ID, Elev) VALUES");
|
// 写入数据行
|
|
DataRow row = dt.Rows[i];
|
csvContent.Append($"('{row[0]}',{row[1]})");
|
csvContent.Append(i == dt.Rows.Count - 1 || j == 999 ? ";" : ",");
|
csvContent.AppendLine();
|
i++;
|
j++;
|
if (j == 1000) j = 0;
|
|
|
|
}
|
|
|
// 将 CSV 内容写入文件
|
File.WriteAllText(filePath, csvContent.ToString());
|
|
// 显示保存成功消息框
|
MessageBox.Show("文件保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
}
|
}
|
|
private void 导入jsonToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var ofd = new OpenFileDialog();
|
ofd.Filter = "json文件|*.json";
|
var result = ofd.ShowDialog();
|
if (result == DialogResult.OK)
|
{
|
_Template = new Template(new Guid().ToString(), "新建", "复制", PBS.eModelTemplateType.Custom);
|
string json = File.ReadAllText(ofd.FileName);
|
_Template.network = JsonConvert.DeserializeObject<MapViewNetWork>(json);
|
_Template.network.BuildRelation();
|
LoadData();
|
}
|
}
|
public void buttonUndo_Click(object sender, EventArgs e)
|
{
|
SetMapInvalidate();
|
GlobalObject.PropertyForm.propertyGridControl1.Refresh();
|
MapObjectExtensions.Undo();
|
}
|
public void buttonRedo_Click(object sender, EventArgs e)
|
{
|
SetMapInvalidate();
|
GlobalObject.PropertyForm.propertyGridControl1.Refresh();
|
MapObjectExtensions.Redo();
|
}
|
public void toolStripButton_save_ButtonClick(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
|
if (!_IsEditMode)
|
{
|
TipFormHelper.ShowWarn("浏览模式无法保存,请切换编辑模式");
|
return;
|
}
|
else
|
{
|
bool isReplace = false;
|
_Network.BuildToInp(_filePath, null, null, false);
|
}
|
}
|
|
/// <summary>
|
/// 批量一键生成楼层模型
|
/// </summary>
|
public void BatchInsertNet(Template temp)
|
{
|
var nodes = _Nodes.Where(c => c.Elev > 0).Select(node => (NodeViewModel)node).ToList();
|
_undoOldValue = new PointF3D(0, 0, 0);
|
_OperaNode = temp.network.Nodes.Find(node => node.ID == temp.Node1) as NodeViewModel;
|
//foreach (var item in nodes)
|
if (nodes.Any())
|
{
|
for (var i = 0; i < nodes.Count(); i++)
|
{
|
var net = temp.network.DeepCopy();
|
|
float dx, dy, dz;
|
var p1 = (PointF3D)_undoOldValue;
|
var p2 = nodes[i].Position3D;
|
var dd = temp.OffSet;
|
dx = p2.X - p1.X;
|
dy = p2.Y - p1.Y;
|
dz = p2.Z - p1.Z;
|
|
net.Nodes.ForEach(n => { ((NodeViewModel)n).Position3D = new PointF3D(n.X + dx, n.Y + dy, n.Elev + dz); });
|
|
var list = _Network.Add(net);
|
|
//var j = _Network.AddPipe(nodes[i], _OperaNode);
|
//j.Length = 0.0001f;
|
//list.Add(j);
|
_NewNet.Clear();
|
|
_Network.BuildRelation();
|
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
|
//list.ForEach(m => m.Selected = true);
|
//selectedObjs.AddRange(list);
|
|
MapObjectExtensions.AddCommand(_Network, "Add", null, list);
|
}
|
|
_OperaNode = null;
|
|
//_OperaNode = null;
|
//_Network.Nodes.AddRange(net.Nodes);
|
//_Network.Links.AddRange(net.Links);
|
|
SetMapInvalidate();
|
}
|
}
|
|
private void 以当前视角另存ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
|
StringBuilder sb = new StringBuilder();
|
sb.AppendLine("[COORDINATES]");
|
sb.AppendLine(";Node X-Coord Y-Coord");
|
_Nodes.ForEach(n =>
|
{
|
var p = WorldPointToMapPoint((NodeViewModel)n);
|
sb.AppendLine($"{n.ID} {p.X} {p.Y}");
|
|
});
|
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "Inp Files (*.inp)|*.inp";
|
saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory() + $@"\template\";
|
if (_filePath != null) saveFileDialog.FileName = _filePath;
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = saveFileDialog.FileName;
|
// 使用 filePath 变量来保存文件
|
//temp.路径 = filePath;
|
_Network.BuildToInp(filePath, sb.ToString(), _filePath);
|
//_filePath = filePath;
|
|
|
}
|
}
|
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
bool isReplace = false;
|
//isReplace = !message.show("模板选择", "使用模板新增/替换当前文件", MessageBoxButtons.YesNo);
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
saveFileDialog.Filter = "Inp Files (*.inp)|*.inp";
|
saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory() + $@"\template\";
|
if (_filePath != null) saveFileDialog.FileName = _filePath;
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = saveFileDialog.FileName;
|
// 使用 filePath 变量来保存文件
|
//temp.路径 = filePath;
|
|
|
_Network.BuildToInp(filePath, null, _filePath, isReplace);
|
//_filePath = filePath;
|
|
|
}
|
}
|
private void 连通性检查ToolStripMenuItem1_Click(object sender, EventArgs e)
|
{
|
if (selectedObjs.Count > 0)//&& selectedObjs[0] is Link l
|
{
|
|
var objs = selectedObjs.FindAll(o => o is LinkViewModel).Select(o => o as LinkViewModel).ToList();
|
if (objs.Count == 0) selectedObjs.FindAll(o => o is NodeViewModel).Select(o => o as NodeViewModel).ToList().ForEach(o => objs.AddRange(o.Links.Select(l => l as LinkViewModel).ToList()));
|
//objs去掉重复的元素
|
objs = objs.Distinct().ToList();
|
var visitedNodes = new HashSet<NodeViewModel>();
|
objs.ForEach(o => TraversePipeNetworkALL(o, visitedNodes));
|
|
|
this.SetMapInvalidate();
|
|
}
|
}
|
private void 下游连通性ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedObjs.Count > 0)//&& selectedObjs[0] is Link l
|
{
|
|
var objs = selectedObjs.FindAll(o => o is LinkViewModel).Select(o => o as LinkViewModel).ToList();
|
if (objs.Count == 0) selectedObjs.FindAll(o => o is NodeViewModel).Select(o => o as NodeViewModel).ToList().ForEach(o =>
|
{
|
objs.AddRange(o.Links.Select(oo => oo as LinkViewModel).ToList().FindAll(oo => oo.StartNode == o ? oo.EN_FLOW > 0 : oo.EN_FLOW < 0));
|
});
|
//objs去掉重复的元素
|
objs = objs.Distinct().ToList();
|
var visitedNodes = new HashSet<NodeViewModel>();
|
objs.ForEach(o => TraversePipeNetworkALL(o, visitedNodes, 1));
|
this.SetMapInvalidate();
|
|
}
|
}
|
public void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (hoveredObjs.Count <= 0 || !(hoveredObjs[0] is NodeViewModel))
|
{
|
MessageBox.Show("请将鼠标悬停在一个节点对象上,作为基准点");
|
return;
|
}
|
if (selectedObjs.Count <= 0 || selectedNodes.Count <= 0)
|
{
|
MessageBox.Show("请选择要复制的对象");
|
return;
|
}
|
|
_OperaNode = hoveredObjs[0] as NodeViewModel;
|
|
MapViewNetWork net = new MapViewNetWork();
|
net.StartPoint = _OperaNode;
|
net.Nodes.AddRange(selectedObjs.FindAll(o => o is NodeViewModel).Select(o => o as NodeViewModel));
|
net.Links.AddRange(selectedObjs.FindAll(o => o is LinkViewModel).Select(o => o as LinkViewModel));
|
|
string json = net.WriteToJson();
|
try
|
{
|
Clipboard.SetText(json);
|
this.SetMapInvalidate();
|
}
|
catch
|
{
|
//提醒
|
|
}
|
|
|
}
|
public void 粘贴ToolStripMenuItem1_Click(object sender, EventArgs e)
|
{
|
{
|
|
var net = new MapViewNetWork();
|
string json = Clipboard.GetText();
|
net.ReadFromJson(json);
|
if (net.StartPoint == null) return;
|
string SID = net.StartPoint.ID;
|
net.StartPoint = net.Nodes.Find(n => n.ID == SID);
|
PointF3D minPoint = net.StartPoint.Position3D;// new PointF(net.StartPoint.X, net.StartPoint.Y);
|
Point controlLocation = this.PointToScreen(new Point(0, 0));
|
int offsetX = Cursor.Position.X - controlLocation.X;
|
int offsetY = Cursor.Position.Y - controlLocation.Y;
|
var position = new Point(offsetX, offsetY);
|
|
net.BuildRelation();
|
//var basePoint = MapPointToWorldPoint(ScreenToMap(position, net.StartPoint.Elev), net.StartPoint.Elev);
|
var mPos = _MousePosition;
|
if (hoveredObjs.Count > 0 && hoveredObjs[0] is NodeViewModel minNode)
|
{
|
|
var basePoint = minNode.Position3D;
|
net.Nodes.ForEach(obj =>
|
{
|
obj.X = obj.X + basePoint.X - minPoint.X;
|
obj.Y = obj.Y + basePoint.Y - minPoint.Y;
|
obj.Elev = obj.Elev + basePoint.Z - minPoint.Z;
|
});
|
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
selectedObjs.AddRange(net.Nodes.Select(n => (NodeViewModel)n));
|
selectedObjs.AddRange(net.Links.ViewLinks);
|
var list = _Network.Add(net, null, false, minNode);
|
net.BuildRelation();
|
//list.Add(_Network.AddPipe(Snode, minNode));
|
MapObjectExtensions.AddCommand(_Network, "Add", null, list);
|
SetMapInvalidate();
|
}
|
else
|
{
|
var basePoint = MapPointToWorldPoint(_MousePosition, net.StartPoint.Elev);
|
net.Nodes.ForEach(obj =>
|
{
|
obj.X = obj.X + basePoint.X - minPoint.X;
|
obj.Y = obj.Y + basePoint.Y - minPoint.Y;
|
});
|
selectedObjs.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
selectedObjs.AddRange(net.Nodes.Select(n => (NodeViewModel)n));
|
selectedObjs.AddRange(net.Links.ViewLinks);
|
var list = _Network.Add(net);
|
net.BuildRelation();
|
MapObjectExtensions.AddCommand(_Network, "Add", null, list);
|
SetMapInvalidate();
|
}
|
|
|
}
|
}
|
private void 增量保存ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
|
if (!_IsEditMode)
|
{
|
TipFormHelper.ShowWarn("浏览模式无法保存,请切换编辑模式");
|
return;
|
}
|
else
|
{
|
bool isReplace = true;
|
//isReplace = !message.show("模板选择", "使用模板新增/替换当前文件", MessageBoxButtons.YesNo);
|
_Network.BuildToInp(_filePath, null, null, false);
|
|
|
}
|
}
|
private void 设为关闭ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
selectedObjs.ForEach(obj => { if (obj is LinkViewModel link) link.Status = StatusType.CLOSED; });
|
SetMapInvalidate();
|
}
|
private void 显示全部楼层ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//this.mapOption.ShowFloor = int.MinValue;
|
_Network.MapObjects.ForEach(o => o.Visible = true);
|
this.SetMapInvalidate();
|
}
|
private void 保存楼层视角ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//if (_Template?.Floors!=null)
|
//{
|
// var fl = _Template.Floors.Find(f => f.FloorIndex == this.mapOption.ShowFloor);
|
// if (fl!=null)fl.MapView = this.mapOption.Copy();
|
//}
|
}
|
public void 南北对齐ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedNodes.Count < 1) return;
|
List<float> list = new List<float>();
|
List<float> list1 = new List<float>();
|
selectedNodes.ForEach(n => { list.Add(n.X); n.X = selectedNodes[0].X; list1.Add(n.X); });
|
MapObjectExtensions.AddCommand(selectedNodes, "X", list, list1);
|
this.SetMapInvalidate();
|
}
|
public void 东西对齐ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedNodes.Count < 1) return;
|
List<float> list = new List<float>();
|
List<float> list1 = new List<float>();
|
selectedNodes.ForEach(n => { list.Add(n.Y); n.Y = selectedNodes[0].Y; list1.Add(n.Y); });
|
MapObjectExtensions.AddCommand(selectedNodes, "Y", list, list1);
|
this.SetMapInvalidate();
|
}
|
public void 竖直对齐ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedNodes.Count < 1) return;
|
List<PointF> list = new List<PointF>();
|
List<PointF> list1 = new List<PointF>();
|
selectedNodes.ForEach(n => { list.Add(n.Position); n.Position = selectedNodes[0].Position; list1.Add(n.Position); });
|
MapObjectExtensions.AddCommand(selectedNodes, "Position", list, list1);
|
this.SetMapInvalidate();
|
}
|
public void 自动对齐ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedNodes.Count < 3) return;
|
List<PointF3D> OldPosition3Ds = selectedNodes.Select(n => n.Position3D).ToList();
|
List<PointF3D> NewPosition3Ds = ProjectPointsToLine(OldPosition3Ds);
|
for (int i = 0; i < selectedNodes.Count; i++)
|
{
|
selectedNodes[i].Position3D = NewPosition3Ds[i];
|
}
|
|
MapObjectExtensions.AddCommand(selectedNodes, "Position3D", OldPosition3Ds, NewPosition3Ds);
|
this.SetMapInvalidate();
|
}
|
List<PointF3D> ProjectPointsToLine(List<PointF3D> points)
|
{
|
// 使用最小二乘法构造直线,并将点投影到直线上
|
|
// 计算均值
|
double sumX = 0;
|
double sumY = 0;
|
double sumZ = 0;
|
|
foreach (PointF3D point in points)
|
{
|
sumX += point.X;
|
sumY += point.Y;
|
sumZ += point.Z;
|
}
|
|
double meanX = sumX / points.Count;
|
double meanY = sumY / points.Count;
|
double meanZ = sumZ / points.Count;
|
|
// 计算最小二乘法拟合直线的参数
|
|
double sumXY = 0;
|
double sumX2 = 0;
|
|
foreach (PointF3D point in points)
|
{
|
double devX = point.X - meanX;
|
double devY = point.Y - meanY;
|
double devZ = point.Z - meanZ;
|
|
sumXY += devX * devY;
|
sumX2 += devX * devX;
|
}
|
|
double slope = sumXY / sumX2;
|
double interceptY = meanY - slope * meanX;
|
double interceptZ = meanZ - slope * meanX;
|
|
// 计算点投影到直线上的坐标
|
|
List<PointF3D> modelTemplateedPoints = new List<PointF3D>();
|
|
foreach (PointF3D point in points)
|
{
|
double modelTemplateedY = slope * point.X + interceptY;
|
double modelTemplateedZ = slope * point.X + interceptZ;
|
|
modelTemplateedPoints.Add(new PointF3D(point.X, (float)modelTemplateedY, (float)modelTemplateedZ));
|
}
|
|
return modelTemplateedPoints;
|
}
|
|
public void 添加底图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.png;*.gif)|*.bmp;*.jpg;*.jpeg;*.png;*.gif";
|
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
var backgroundImagePath = openFileDialog.FileName;
|
HelperC.ClearFileReadOnly(_Template.BackGroundImg_FullPath);
|
HelperC.Copy(backgroundImagePath, _Template.BackGroundImg_FullPath, true);
|
设置底图ToolStripMenuItem_Click(1, new EventArgs());
|
}
|
}
|
bool _isSettingBackGroundPictur = false;
|
|
public void 设置底图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
string inputValue = "0";// Prompt.ShowDialog("请输入底图标高", "");
|
float BackGroundElev = 0;
|
if (float.TryParse(inputValue, out float result))
|
{
|
BackGroundElev = result;
|
}
|
else
|
{
|
BackGroundElev = 0;
|
}
|
_Template.BackGroundElev = BackGroundElev;
|
_mouseState = MouseState.设置底图范围;
|
this._lastCursor = this.Cursor;
|
this.Cursor = Cursors.Cross;
|
}
|
|
public void 显示隐藏底图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
mapOption.isShowPic = !mapOption.isShowPic;
|
SetMapInvalidate();
|
}
|
|
private dict<string, dynamic> param = null;
|
|
public void 清除底图ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
mapOption.isShowPic = false;
|
SetMapInvalidate();
|
try
|
{
|
File.Delete(_Template.BackGroundImg_FullPath);
|
}
|
catch
|
{
|
}
|
}
|
|
private void label_zoom_Click(object sender, EventArgs e)
|
{
|
TipFormHelper.ShowError("请输入比例,待补充");
|
//string inputValue = Prompt.ShowDialog("请输入比例", "");
|
//if (float.TryParse(inputValue, out float result))
|
//{
|
// zoom = result;
|
//}
|
}
|
private void label_file_DoubleClick(object sender, EventArgs e)
|
{
|
if (_filePath == null) return;
|
FileInfo fi = new FileInfo(_filePath);
|
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
|
}
|
private void label_file_Click(object sender, EventArgs e)
|
{
|
if (_filePath == null) return;
|
FileInfo fi = new FileInfo(_filePath);
|
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_filePath}\"");
|
}
|
private void 设为立管点ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (selectedNodes.Count == 1)
|
{
|
_Template.Node2 = _EndPoint = selectedNodes[0].ID;
|
}
|
|
}
|
private void 显示水流ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Template.network.MapObjects.ForEach(o => o.Selected = false);
|
selectedObjs.Clear();
|
_Template.network.Links.ForEach(link =>
|
{
|
if (link.EN_FLOW != 0)
|
{
|
selectedObjs.Add(link);
|
link.Selected = true;
|
NodeViewModel node = link.StartNode;
|
node.Selected = true;
|
if (!selectedObjs.Contains(node)) selectedObjs.Add(node);
|
node = link.EndNode;
|
node.Selected = true;
|
if (!selectedObjs.Contains(node)) selectedObjs.Add(node);
|
|
}
|
|
});
|
this.SetMapInvalidate();
|
|
}
|
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
selectedObjs.Clear();
|
_Template.network.MapObjects.ForEach(o =>
|
{
|
if (o.Visible)
|
{
|
o.Selected = true;
|
selectedObjs.Add(o);
|
}
|
|
});
|
this.SetMapInvalidate();
|
|
}
|
private void 反选ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
_Template.network.MapObjects.ForEach(o =>
|
{
|
if (o.Visible)
|
{
|
if (o.Selected)
|
{
|
o.Selected = false;
|
selectedObjs.Remove(o);
|
}
|
else
|
{
|
o.Selected = true;
|
selectedObjs.Add(o);
|
}
|
|
}
|
|
});
|
this.SetMapInvalidate();
|
}
|
private void 显示状态ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Template.mapOption._ShowStatus = true;
|
this.SetMapInvalidate();
|
}
|
private void 隐藏状态ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Template.mapOption._ShowStatus = false;
|
this.SetMapInvalidate();
|
|
}
|
private void 显示流向ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Template.mapOption._ShowFlowDirection = true;
|
this.SetMapInvalidate();
|
|
}
|
private void 隐藏流向ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Template.mapOption._ShowFlowDirection = false;
|
this.SetMapInvalidate();
|
|
|
}
|
private void 方向修复ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Network.Links.ForEach(l =>
|
{
|
//根据l.EN_FLOW重新设置l.STARTNODE和l.ENDNODE
|
if (l.EN_FLOW < 0)
|
{
|
var node = l.StartNode;
|
l.StartNode = l.EndNode;
|
l.EndNode = node;
|
}
|
});
|
}
|
private void 刷新楼层ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
ToolStripMenuItem_Floor.DropDownItems.Clear();
|
var mapobjects = _Network.MapObjects;
|
TagList tags = new TagList();
|
foreach (var item in mapobjects)
|
{
|
if (item.Tags != null)
|
tags.AddRange(item.Tags);
|
}
|
var t = tags.Distinct().ToList();
|
tags.Clear();
|
tags.AddRange(t);
|
for (int i = -10; i < 1000; i++)
|
{
|
string tagstring = i.ToString() + "楼";
|
if (tags.Contains(tagstring))
|
{
|
var item = ToolStripMenuItem_Floor.DropDownItems.Add(i.ToString() + "楼");
|
item.Click += (oo, ee) =>
|
{
|
|
|
mapobjects.ForEach(o =>
|
{
|
if (o.Tags.Contains(tagstring))
|
{
|
o.Visible = true;
|
}
|
else
|
{
|
o.Visible = false;
|
}
|
});
|
this.SetMapInvalidate();
|
|
};
|
}
|
}
|
this.SetMapInvalidate();
|
}
|
|
private void 显示所有隐藏内容ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
_Network.MapObjects.ForEach(o => o.Visible = true);
|
this.SetMapInvalidate();
|
}
|
|
private void toolStripButton_ClearMinor_Click(object sender, EventArgs e)
|
{
|
_Network.ClearMinorLoss();
|
}
|
|
bool _isShowDirection = false;
|
private bool __needpaintall = false;
|
|
|
private void cb_Link_Colour_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
var type = cb_Link_Colour.SelectedItem == null ? ColourType.管线流量 : (ColourType)cb_Link_Colour.SelectedItem;
|
var doubles = _Template.network.getMinMax(type);
|
var colour = new Colour(type, null, type.ToString());
|
List<ColourItem> colours = Colour.GetColourItems((float)doubles[0], (float)doubles[1], 5, Color.Gray, Color.Red);
|
colour.Items = colours;
|
LinkColour = colour;
|
this.SetMapInvalidate();
|
|
|
|
}
|
private void cb_Node_Colour_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
if (_Template == null) return;
|
var type = cb_Node_Colour.SelectedItem == null ? ColourType.节点自由压力 : (ColourType)cb_Node_Colour.SelectedItem;
|
var doubles = _Template.network.getMinMax(type);
|
var colour = new Colour(type, null, type.ToString());
|
List<ColourItem> colours = Colour.GetColourItems((float)doubles[0], (float)doubles[1], 5, Color.Gray, Color.Red);
|
colour.Items = colours;
|
NodeColour = colour;
|
this.SetMapInvalidate();
|
|
|
|
}
|
|
private void label_ZZ_Click(object sender, EventArgs e)
|
{
|
//正交模式的全局变量开关
|
_isOrtho = !_isOrtho;
|
}
|
|
|
|
|
#endregion
|
|
#endregion
|
|
#region Model
|
#region 核心属性
|
TContainer TC = new TContainer();
|
|
/// <summary>
|
/// 地图选项
|
/// </summary>
|
public MapDimensions mapOption
|
{
|
get { return TC.mapOption; }
|
set { TC.mapOption = value; }
|
}
|
/// <summary>
|
/// 地图选项_起始操作时
|
/// </summary>
|
private MapDimensions mapOption0 = new MapDimensions();
|
/// <summary>
|
/// 临时管网层
|
/// </summary>
|
[Browsable(false)]
|
public Template _newTemplate
|
{
|
get { return TC.newTemplate; }
|
set { TC.newTemplate = value; }
|
}
|
|
|
[Browsable(false)]
|
public Template _Template
|
{
|
get { return TC.template; }
|
set
|
{
|
TC.template = value;
|
|
label_file.Text = TC.template?.filePath;
|
}
|
}
|
|
#endregion
|
#region 交互属性
|
/// <summary>
|
/// 悬停对象
|
/// </summary>
|
private List<IBaseViewModel> hoveredObjs = new List<IBaseViewModel>();
|
/// <summary>
|
/// 选中对象
|
/// </summary>
|
public List<IBaseViewModel> selectedObjs = new List<IBaseViewModel>();
|
|
|
private List<NodeViewModel> selectedNodes => selectedObjs.FindAll(o => o is NodeViewModel).Select(o => (NodeViewModel)o).ToList();
|
private List<LinkViewModel> selectedLinks => selectedObjs.FindAll(o => o is LinkViewModel).Select(o => (LinkViewModel)o).ToList();
|
|
MouseState _mouseState = MouseState.无;
|
private NodeViewModel _OperaNode = null;
|
public PointF mouseXY = new PointF(0, 0);
|
|
PointF DragStartPos;
|
PointF _ClickStartPos;
|
PointF RotaStartPos;
|
PointF BackGroudPicLeftPos;
|
bool _isPanning;
|
/// <summary>
|
/// 拖拽选择
|
/// </summary>
|
bool _isDragging;
|
bool _isRotating;
|
bool _isPainting;
|
|
|
PointF mousePosition;
|
// control+鼠标中间按下缩放
|
bool _isInsertingObject = false;
|
bool _isMovingObject = false;
|
bool _isPastingObject = false;
|
Cursor _lastCursor;
|
object _undoOldValue = null;
|
private List<PointF> polygonPoints = new List<PointF>();
|
|
private bool _isDrawingPolygon;
|
|
#endregion
|
#region 新增管网(辅助)
|
|
MapViewNetWork _NewNet
|
{
|
get
|
{
|
if (_newTemplate == null) _newTemplate = new Template();
|
if (_newTemplate.network == null) _newTemplate.network = new MapViewNetWork();
|
return _newTemplate.network;
|
}
|
}
|
#endregion
|
#region 显示选项(辅助)
|
|
|
private string _StartPoint = null;
|
private string _EndPoint = null;
|
//private bool __isEditMode = true;
|
[Browsable(false)]
|
public bool _IsEditMode
|
{
|
get { return this.mapOption?.isEditMode ?? true; }
|
set
|
{
|
toolStripComboBox_浏览模式.Text = value ? "编辑模式" : "浏览模式";
|
|
if (this.mapOption != null) this.mapOption.isEditMode = value;
|
转换为ToolStripMenuItem.Visible = _IsEditMode;
|
toolStripSeparator9.Visible = _IsEditMode;
|
删除ToolStripMenuItem.Visible = _IsEditMode;
|
删除ToolStripMenuItem1.Visible = _IsEditMode;
|
|
复制ToolStripMenuItem.Visible = _IsEditMode;
|
复制ToolStripMenuItem1.Visible = _IsEditMode;
|
粘贴ToolStripMenuItem.Visible = _IsEditMode;
|
粘贴ToolStripMenuItem1.Visible = _IsEditMode;
|
设置长度ToolStripMenuItem.Visible = _IsEditMode;
|
设为关闭ToolStripMenuItem.Visible = _IsEditMode;
|
设为立管点ToolStripMenuItem.Visible = _IsEditMode;
|
对齐ToolStripMenuItem.Visible = _IsEditMode;
|
对齐ToolStripMenuItem1.Visible = _IsEditMode;
|
|
toolStripButton_新建节点.Enabled = _IsEditMode;
|
toolStripButton_新建管线.Enabled = _IsEditMode;
|
toolStripButton_新建立管.Enabled = _IsEditMode;
|
toolStripButton_添加水库.Enabled = _IsEditMode;
|
toolStripButton_添加水池.Enabled = _IsEditMode;
|
toolStripButton_添加水表.Enabled = _IsEditMode;
|
toolStripButton_添加阀门.Enabled = _IsEditMode;
|
toolStripButton_重复器.Enabled = _IsEditMode;
|
|
|
|
}
|
}
|
|
|
[Browsable(false)]
|
public float Link_multiply
|
{
|
get
|
{
|
if (_Template == null || _Template.mapOption == null) return 1.0f;
|
return _Template.mapOption.Link_multiply;
|
}
|
set
|
{
|
if (_Template == null || _Template.mapOption == null) return; _Template.mapOption.Link_multiply = value;
|
}
|
}
|
[Browsable(false)]
|
public float junction_multiply
|
{
|
get
|
{
|
if (_Template == null || _Template.mapOption == null) return 1.0f;
|
return _Template.mapOption.junction_multiply;
|
}
|
set
|
{
|
if (_Template == null || _Template.mapOption == null) return; _Template.mapOption.junction_multiply = value;
|
}
|
}
|
[DisplayName("显示阀门")]
|
|
public bool _ShowValve
|
{
|
get
|
{
|
if (_Template == null || _Template.mapOption == null) return true;
|
return _Template.mapOption._ShowValve;
|
}
|
set
|
{
|
if (_Template == null || _Template.mapOption == null) return; _Template.mapOption._ShowValve = value;
|
}
|
}
|
[DisplayName("显示节点")]
|
public bool _ShowJunction
|
{
|
get
|
{
|
if (_Template == null || _Template.mapOption == null) return true;
|
return _Template.mapOption._ShowJunction;
|
}
|
set
|
{
|
|
if (_Template == null || _Template.mapOption == null) return; _Template.mapOption._ShowJunction = value;
|
}
|
}
|
string _filePath
|
{
|
get
|
{
|
if (string.IsNullOrEmpty(_Template?.filePath))
|
return null;
|
string path = _Template.filePath.TrimStart('\\');
|
return
|
Path.Combine(Directory.GetCurrentDirectory(), path);
|
}
|
//set
|
//{
|
|
// _Template.路径 = value;
|
//}
|
}
|
private MapViewNetWork _Network
|
{
|
get
|
{
|
return _Template?.network;
|
}
|
}
|
#endregion
|
#region 管网属性(辅助)
|
[Browsable(false)]
|
public List<NodeCalcModel> _Nodes
|
{
|
get { return _Network?.Nodes ?? new List<NodeCalcModel>(); }
|
|
}
|
[Browsable(false)]
|
public List<LinkCalcModel> _Links
|
{
|
get { return _Network?.Links ?? new List<LinkCalcModel>(); }
|
|
}
|
|
#endregion
|
#region 视角设置(辅助)
|
|
|
private const float MinZoom = 0.1f;
|
private const float MaxZoom = 1000.0f;
|
|
[DisplayName("缩放系数")]
|
public float zoom
|
{
|
get
|
{
|
return mapOption.zoom;
|
}
|
set
|
{
|
label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
|
mapOption.zoom = value;
|
}
|
}
|
[DisplayName("旋转角度")]
|
public double Rotation
|
{
|
get
|
{
|
return mapOption.rotation;
|
}
|
set
|
{
|
toolStripStatusLabel_rotation.Text = $"Rotation:({Rotation.ToString("0")},{RotationF.ToString("0")})";
|
//将旋转角度转换为-180~180
|
value = value % 360;
|
if (value > 180)
|
value -= 360;
|
else if (value < -180)
|
value += 360;
|
mapOption.rotation = value;
|
}
|
}
|
|
private double Rotation0 = 0;
|
[Browsable(false)]
|
public PointF MapCenter
|
{
|
get
|
{
|
return mapOption.Center;
|
}
|
set
|
{
|
label_center.Text = $"center:({MapCenter.X.ToString("0.00")} ,{MapCenter.Y.ToString("0.00")})";
|
mapOption.Center = value;
|
}
|
}
|
private PointF MapCenter0;
|
private bool is3Dview = false;
|
double 俯视角度_start = 90;
|
|
|
|
public bool Lock2DView
|
{
|
get
|
{
|
return mapOption.Lock2DView;
|
}
|
set
|
{
|
mapOption.Lock2DView = value;
|
}
|
}
|
|
|
/// <summary>
|
/// 俯视线与底面的夹角,投影用sin
|
/// </summary>
|
[DisplayName("俯视角度")]
|
public double RotationF
|
{
|
get
|
{
|
return mapOption.rotationF;
|
}
|
set
|
{
|
mapOption.rotationF = value;
|
}
|
}
|
[Browsable(false)]
|
public double 俯视弧度
|
{
|
get
|
{
|
return RotationF / 180 * Math.PI;
|
}
|
}
|
|
[Browsable(false)]
|
public PointF Zoom
|
{
|
get
|
{
|
return new PointF(zoom, -zoom);
|
}
|
}
|
|
|
private PointF Z(float z)
|
{
|
return new PointF(0, 0);
|
}
|
#endregion
|
#region 颜色分级(辅助)
|
public Colour NodeColour
|
{
|
set
|
{
|
var type = value.Type;
|
value.isChoosed = true;
|
_Template?.Colours?.RemoveAll(cl => cl.Type == type);
|
_Template.Colours.Add(value);
|
mapOption.ColourNode = type;
|
}
|
private get
|
{
|
return _Template?.Colours?.FirstOrDefault(cl => cl.isChoosed && cl.Type == mapOption.ColourNode);
|
}
|
}
|
public Colour LinkColour
|
{
|
set
|
{
|
var type = value.Type;
|
value.isChoosed = true;
|
_Template?.Colours?.RemoveAll(cl => cl.Type == type);
|
_Template.Colours.Add(value);
|
mapOption.ColourLink = type;
|
}
|
private get
|
{
|
return _Template?.Colours?.FirstOrDefault(cl => cl.isChoosed && cl.Type == mapOption.ColourLink);
|
}
|
}
|
|
//private bool __isOrtho = true;
|
|
#endregion
|
#region 正交模式
|
private bool _isOrtho
|
{
|
get
|
{
|
return mapOption.IsOrtho;
|
}
|
set
|
{
|
mapOption.IsOrtho = value;
|
if (mapOption.IsOrtho)
|
{
|
label_ZZ.Text = "正交模式:开";
|
}
|
else
|
{
|
label_ZZ.Text = "正交模式:关";
|
}
|
}
|
}
|
#endregion
|
|
|
#endregion
|
|
#region Draw
|
|
#region 事件
|
//按帧数判断是否重绘,减少计算量(每帧最多重绘一次)
|
bool _timerDraw = false;
|
//按帧数判断鼠标悬停对象,减少计算量(每帧最多判断一次)
|
bool _mouseHoverCheckFlag = false;
|
#endregion
|
|
void DrawBackGroud(Graphics bufferG, Template template)
|
{
|
if (template == null) return;
|
var _Nodes = template.network.Nodes.ViewNodes;
|
var _Links = template.network.Links.ViewLinks;
|
|
var Cpoints = getCurclePoints(64).ToList();
|
|
var r = 1.73f / zoom;
|
var rt = r;
|
|
r = r * Link_multiply;
|
List<PointF> diametersZoom = new List<PointF>() { new PointF(0, 0.08f), new PointF(150, 0.03f), new PointF(300, 0.001f), new PointF(800, 0.0001f) };
|
|
Pen penN = new Pen(Color.FromArgb(0, 0, 255), 1 * r);
|
|
|
//背景图绘制
|
if (this.mapOption.isShowPic && template != null && File.Exists(template.BackGroundImg_FullPath))
|
{
|
List<PointF> p = new List<PointF>();
|
|
if (!this.mapOption.isAutoBackgroundImage)
|
{
|
var Cps = new List<PointF>
|
{
|
template.BackGroundPoint1,
|
new PointF(template.BackGroundPoint2.X,template.BackGroundPoint1.Y),
|
new PointF(template.BackGroundPoint1.X,template.BackGroundPoint2.Y),
|
//template.BackGroundPoint2,
|
|
|
};
|
Cps.ForEach(cp => p.Add(WorldPointToMapPoint(cp, template.BackGroundElev, template.OffSet)));
|
}
|
else
|
{
|
|
|
var p1 = new PointF(template.BackGroundImgX, template.BackGroundImgY);
|
var p2 = new PointF(template.BackGroundImgX + template.BackGroundImgWidth, template.BackGroundImgY + template.BackGroundImgHeight);
|
|
var f = template.BackGroundImgRotaAngle / 180 * Math.PI;
|
var djx = Math.Sqrt((Math.Pow(template.BackGroundImgWidth, 2) + Math.Pow(template.BackGroundImgHeight, 2)));
|
var p3 = new PointF(p1.X + (float)(Math.Cos(f) * template.BackGroundImgWidth), p1.Y + (float)(Math.Sin(f) * template.BackGroundImgWidth));
|
var p4 = new PointF(p1.X - (float)(Math.Sin(f) * template.BackGroundImgHeight), p1.Y + (float)(Math.Cos(f) * template.BackGroundImgHeight));
|
p3.Y = p4.Y;
|
var Cps = new List<PointF>
|
{
|
p4,
|
p3,p1
|
};
|
template.BackGroundPoint1 = p4;
|
template.BackGroundPoint2 = new PointF(p3.X, p1.Y);
|
|
|
|
Cps.ForEach(cp => p.Add(WorldPointToMapPoint(cp, template.BackGroundElev, template.OffSet)));
|
}
|
|
try
|
{
|
var img = System.Drawing.Image.FromFile(template.BackGroundImg_FullPath);
|
if (img != null)
|
{
|
bufferG.FillPolygon(penN.Brush, p.ToArray());
|
bufferG.DrawImage(img, p.ToArray());
|
}
|
}
|
catch
|
{
|
|
}
|
|
}
|
}
|
|
void Draw(Graphics bufferG, Template template)
|
{
|
if (template == null) return;
|
var _Nodes = template.network.Nodes.ViewNodes;
|
var _Links = template.network.Links.ViewLinks;
|
var _Areas = template.network.Areas;
|
var Cpoints = getCurclePoints(64).ToList();
|
|
var r = 1.73f / zoom;
|
var rt = r;
|
|
float minElve = float.MinValue;
|
float maxElve = float.MaxValue;
|
r = r * Link_multiply;
|
List<PointF> diametersZoom = new List<PointF>() { new PointF(0, 0.08f), new PointF(150, 0.03f), new PointF(300, 0.001f), new PointF(800, 0.0001f) };
|
|
Pen penN = new Pen(Color.FromArgb(0, 0, 255), 1 * r);
|
|
Pen penChoosed = new Pen(Color.Purple, 5 * r);
|
Pen pen_valveChoosed = new Pen(Color.Red, 5 * r);
|
|
Pen penClosed = new Pen(Color.OrangeRed, 2 * r);
|
Pen penHovered = new Pen(Color.DeepSkyBlue, 5 * r);
|
|
|
//绘制面
|
using (Pen pen0 = new Pen(Color.FromArgb(0, 0, 255), 2 * r))
|
{
|
foreach (var area in _Areas)
|
{
|
|
if (!area.Visible) continue;
|
if (!IsFaceVisibleToCamera(area, GetCameraPosition())) continue;
|
if (area.Elev < minElve || area.Elev >= maxElve) continue;
|
|
var p = new List<PointF>();
|
foreach (var node in area.InnerNodes)
|
{
|
p.Add(CubeWorldPointToMapPoint(node, template.OffSet));
|
}
|
if (p.Count < 3) continue;
|
pen0.Color = penClosed.Color = area.color;
|
Pen pen = pen0;
|
if (area.Hovered) pen = penHovered;
|
bufferG.FillPolygon(pen.Brush, p.ToArray());
|
bufferG.DrawPolygon(pen, p.ToArray());
|
//显示area的名称
|
var c = new PointF(p.Average(p0 => p0.X), p.Average(p0 => p0.Y));
|
var brush = new SolidBrush(Color.White);
|
|
|
var gs = bufferG.Save();
|
// 应用矩阵变换以抵消之前的翻转效果
|
bufferG.ScaleTransform(1 / Zoom.X, 1 / Zoom.Y);
|
|
Font font = new Font(FontFamily.GenericSansSerif, 10 * 10 * zoom);
|
|
SizeF textSize = bufferG.MeasureString(area.Name, font);
|
var center = new PointF(c.X * Zoom.X, c.Y * Zoom.Y);
|
float textLeft = center.X - textSize.Width / 2;
|
float textTop = center.Y - textSize.Height / 2;
|
PointF pd = new PointF(textLeft, textTop);
|
|
bufferG.DrawString(area.Name, font, brush, pd);
|
// 恢复之前保存的绘图状态
|
bufferG.Restore(gs);
|
}
|
}
|
|
|
// 绘制线
|
HashSet<long> dict_flow_direction = new HashSet<long>();
|
using (Pen pen0 = new Pen(Color.FromArgb(0, 0, 255), 2 * r))
|
{
|
|
foreach (var link in _Links)
|
{
|
if (!link.Visible) continue;
|
if (link.Elev < minElve || link.Elev >= maxElve) continue;
|
//if (_isMovingObject && (link.StartNode == _OperaNode || link.EndNode == _OperaNode)) continue;
|
var p1 = WorldPointToMapPoint(link.StartNode, template.OffSet);
|
var p2 = WorldPointToMapPoint(link.EndNode, template.OffSet);
|
if (!isVisible(p1) && !isVisible(p2)) continue;
|
if (LinkColour != null)
|
{
|
pen0.Color = penClosed.Color = GraphHelper.getLinkColor(LinkColour, link);
|
}
|
|
Pen pen = pen0;
|
#if DEBUG
|
#else
|
if (_Template != null && _Template.mapOption._ShowStatus && link.Status == Hydro.Core.ObjectEnum.StatusType.CLOSED) pen = penClosed;
|
#endif
|
|
if (link.Hovered) pen = penHovered;
|
float zoomAtMin = 0;
|
for (int i = 0; i < diametersZoom.Count; i++)
|
{
|
PointF point = diametersZoom[i];
|
if (link.Diameter >= point.X) continue;
|
zoomAtMin = diametersZoom[i - 1].Y;
|
break;
|
}
|
if (zoomAtMin >= zoom) continue;
|
if (link is ValveViewModel)
|
{
|
if (link.Selected || _ShowValve)
|
{
|
|
var c = new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
|
bufferG.DrawLines(link.Selected ? penChoosed : pen, new PointF[] { p1, p2 });
|
var valveShapeHeight = link.Selected ? 10 : 5;
|
PointF[] points = new PointF[] {
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight * r, c.Y + valveShapeHeight * r,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight * r, c.Y - valveShapeHeight * r,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + valveShapeHeight * r, c.Y + valveShapeHeight * r,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + valveShapeHeight * r, c.Y - valveShapeHeight * r,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight * r, c.Y + valveShapeHeight * r,c,p1,p2),
|
|
};
|
|
bufferG.FillPolygon(link.Selected ? pen_valveChoosed.Brush : pen.Brush, points);
|
|
}
|
|
|
}
|
else if (link is PumpViewModel)
|
{
|
if (link.Selected || _ShowValve)
|
{
|
var c = new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
|
|
|
bufferG.DrawLines(link.Selected ? penChoosed : pen, new PointF[] { p1, p2 });
|
// 绘制圆形部分(水泵的泵体)
|
float radius = 5 * r;
|
float diameter = radius * 2;
|
#region 圆形拆分
|
|
List<PointF> p = new List<PointF>();
|
Cpoints.ForEach(cp => p.Add(GraphHelper.getRotatePoint(c.X + cp.X * radius - radius, c.Y + cp.Y * radius, c, p1, p2)));
|
|
#endregion
|
|
|
|
bufferG.FillPolygon(link.Selected ? pen_valveChoosed.Brush : pen.Brush, p.ToArray());
|
|
|
// 绘制连接线
|
|
|
var valveShapeHeight = link.Selected ? radius * 2 : radius;
|
PointF[] points = new PointF[] {
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight , c.Y ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + valveShapeHeight , c.Y ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
|
};
|
|
bufferG.FillPolygon(link.Selected ? pen_valveChoosed.Brush : pen.Brush, points);
|
}
|
|
|
}
|
else if (link is RepeaterViewModel re)
|
{
|
if (re.Status == RepeatStatus.收起 || _IsEditMode)
|
{
|
|
bufferG.DrawLines(link.Selected ? penChoosed : pen, new PointF[] { p1, p2 });
|
|
|
var listNode = GraphHelper.Get等分Nodes(p1, p2, Math.Max(re.RepeatTimes, 1));
|
|
for (int i = 0; i < listNode.Count; i++)
|
{
|
|
var c = listNode[i];
|
RectangleF[] rects = new RectangleF[] {
|
new RectangleF(c.X-10*r,c.Y-8*r,20*r,16*r),
|
};
|
bufferG.FillRectangles(new SolidBrush(Color.White), rects);
|
|
|
// 保存当前绘图状态
|
var gs = bufferG.Save();
|
// 应用矩阵变换以抵消之前的翻转效果
|
bufferG.ScaleTransform(1 / Zoom.X, 1 / Zoom.Y);
|
int index = re.GetIndex(i);
|
string indexString = index == 0 ? "" : index.ToString();
|
Font font = new Font(FontFamily.GenericSansSerif, 10);
|
string text = $"{indexString}{re.NetworkShowName}";
|
SizeF textSize = bufferG.MeasureString(text, font);
|
var center = new PointF(c.X * Zoom.X, c.Y * Zoom.Y);
|
float textLeft = center.X - textSize.Width / 2;
|
float textTop = center.Y - textSize.Height / 2;
|
PointF p = new PointF(textLeft, textTop);
|
|
bufferG.DrawString(text, font, link.Selected ? penChoosed.Brush : pen.Brush, p);
|
// 恢复之前保存的绘图状态
|
bufferG.Restore(gs);
|
|
if (textSize.Width / Zoom.X > rects[0].Width)
|
{
|
rects[0] = new RectangleF(c.X - textSize.Width / 2 / Zoom.X - 1 * r, c.Y - 8 * r, textSize.Width / Zoom.X + 2 * r, 16 * r);
|
}
|
try
|
{
|
bufferG.DrawRectangles(penN, rects);
|
|
}
|
catch { }
|
|
}
|
}
|
|
}
|
else
|
{
|
|
if (link.StartNode == null || link.EndNode == null) continue;
|
try
|
{
|
bufferG.DrawLines(link.Selected ? penChoosed : pen, new PointF[] { p1, p2 });
|
}
|
catch (Exception ex)
|
{
|
|
}
|
if (_Template.mapOption._ShowFlowDirection)
|
{
|
var c = new PointF((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
|
var ps = MapToScreen(c);
|
//将ps转换为ulong,精度为20,并加入到dict_flow_direction中
|
var ps_20 = GraphHelper.GetUlongByPoint(ps, 5);
|
if (!dict_flow_direction.Contains(ps_20))
|
{
|
dict_flow_direction.Add(ps_20);
|
bufferG.DrawLines(link.Selected ? penChoosed : pen, new PointF[] { p1, p2 });
|
// 绘制圆形部分(水泵的泵体)
|
float radius = 5 * r;
|
float diameter = radius * 2;
|
#region 圆形拆分
|
float activeD = 1;
|
if (link.EN_FLOW < 0) activeD = -1;
|
List<PointF> p = new List<PointF>();
|
Cpoints.ForEach(cp => p.Add(GraphHelper.getRotatePoint(c.X - activeD * cp.X * radius + activeD * radius, c.Y + cp.Y * radius, c, p1, p2)));
|
|
#endregion
|
|
|
bufferG.FillPolygon(link.Selected ? pen_valveChoosed.Brush : pen.Brush, p.ToArray());
|
|
var valveShapeHeight = link.Selected ? radius * 2 : radius;
|
PointF[] points = new PointF[] {
|
GraphHelper.getRotatePoint(c.X -activeD* valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X , c.Y ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - activeD*valveShapeHeight , c.Y - valveShapeHeight,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + activeD*valveShapeHeight , c.Y - valveShapeHeight,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X + activeD*valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
GraphHelper.getRotatePoint(c.X - activeD*valveShapeHeight , c.Y + valveShapeHeight ,c,p1,p2),
|
};
|
|
bufferG.FillPolygon(link.Selected ? pen_valveChoosed.Brush : pen.Brush, points);
|
}
|
|
|
|
}
|
}
|
|
}
|
}
|
|
|
r = rt;
|
|
|
HashSet<long> dict_point = new HashSet<long>();
|
//绘制点
|
penChoosed = new Pen(Color.Green, 1f * r);
|
Brush brushChoosed = penChoosed.Brush;
|
SolidBrush whiteBrush = new SolidBrush(Color.White);
|
|
using (Pen pen0 = new Pen(Color.FromArgb(255, 0, 0), 1 * r))
|
{
|
|
foreach (NodeViewModel node in _Nodes)
|
{
|
if (!node.Visible) continue;
|
if (node.Elev < minElve || node.Elev >= maxElve) continue;
|
Pen pen = pen0;
|
Brush brush = pen.Brush;
|
float pr = (float)(r * 0.5);
|
pr = pr * junction_multiply;
|
PointF p = WorldPointToMapPoint(node, template.OffSet);
|
if (!isVisible(p)) continue;
|
var ps_20 = GraphHelper.GetUlongByPoint(p, 0.1f);
|
if (dict_point.Contains(ps_20))
|
continue;
|
dict_point.Add(ps_20);
|
if (NodeColour != null)
|
{
|
pen.Color = penChoosed.Color = GraphHelper.getNodeColor(NodeColour, node);
|
brush = pen.Brush;
|
brushChoosed = penChoosed.Brush;
|
|
|
}
|
if (node.Hovered)
|
{
|
pen = penHovered;
|
brush = pen.Brush;
|
pr = pr * 2;
|
}
|
|
var rectangle = new RectangleF((float)p.X - 5 * pr, (float)p.Y - 5 * pr, 10 * pr, 10 * pr);
|
float zoomAtMin = 0;
|
for (int i = 0; i < diametersZoom.Count; i++)
|
{
|
PointF point = diametersZoom[i];
|
if (node.MaxDiameter >= point.X) continue;
|
zoomAtMin = diametersZoom[i - 1].Y;
|
break;
|
}
|
if (zoomAtMin >= zoom) continue;
|
|
if (node == _OperaNode)
|
{
|
var whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.FillEllipse(whiteBrush, whiteRect);
|
|
whiteRect = new RectangleF(rectangle.X + 2 * pr, rectangle.Y + 2 * pr, rectangle.Width - 4 * pr, rectangle.Height - 4 * pr);
|
bufferG.DrawEllipse(node.Selected ? penChoosed : pen, whiteRect);
|
|
|
whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.DrawEllipse(node.Selected ? penChoosed : pen, whiteRect);
|
|
|
}
|
else if (node.ID == _EndPoint)
|
{
|
|
var whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.FillEllipse(whiteBrush, whiteRect);
|
|
whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.DrawEllipse(node.Selected ? penChoosed : pen, whiteRect);
|
|
|
var p1 = new PointF(rectangle.X + 2 * pr, rectangle.Y + 2 * pr);
|
var p2 = new PointF(p1.X + 6 * pr, p1.Y + 6 * pr);
|
bufferG.DrawLine(node.Selected ? penChoosed : pen, p1, p2);
|
p1 = new PointF(rectangle.X + 2 * pr, rectangle.Y + 8 * pr);
|
p2 = new PointF(p1.X + 6 * pr, p1.Y - 6 * pr);
|
bufferG.DrawLine(node.Selected ? penChoosed : pen, p1, p2);
|
}
|
else if (node is TankViewModel)
|
{
|
pr *= 2;
|
rectangle = new RectangleF((float)p.X - 5 * pr, (float)p.Y - 5 * pr, 10 * pr, 10 * pr);
|
RectangleF r0 = new RectangleF(rectangle.X, rectangle.Y + 5 * pr, 10 * pr, 5 * pr);
|
RectangleF r1 = new RectangleF(rectangle.X + 2 * pr, rectangle.Y, 6 * pr, 5 * pr);
|
bufferG.FillRectangle(node.Selected ? brushChoosed : brush, r0);
|
bufferG.FillRectangle(node.Selected ? brushChoosed : brush, r1);
|
|
}
|
else if (node is ReservoirViewModel)
|
{
|
pr *= 2;
|
rectangle = new RectangleF((float)p.X - 5 * pr, (float)p.Y - 5 * pr, 10 * pr, 10 * pr);
|
|
RectangleF r0 = new RectangleF(rectangle.X, rectangle.Y + 2 * pr, rectangle.Width, 6 * pr);
|
RectangleF r1 = new RectangleF(rectangle.X, rectangle.Y + 8 * pr, 1 * pr, 2 * pr);
|
RectangleF r2 = new RectangleF(rectangle.X + 9 * pr, rectangle.Y + 8 * pr, 1 * pr, 2 * pr);
|
bufferG.FillRectangle(node.Selected ? brushChoosed : brush, r0);
|
bufferG.FillRectangle(node.Selected ? brushChoosed : brush, r1);
|
bufferG.FillRectangle(node.Selected ? brushChoosed : brush, r2);
|
}
|
else if (node is MeterViewModel)
|
{
|
bufferG.FillEllipse(node.Selected ? brushChoosed : brush, rectangle);
|
var whiteRect = new RectangleF(rectangle.X + 1 * pr, rectangle.Y + 1 * pr, rectangle.Width - 2 * pr, rectangle.Height - 2 * pr);
|
bufferG.FillEllipse(whiteBrush, whiteRect);
|
|
var p1 = new PointF(rectangle.X + 5 * pr, rectangle.Y);
|
var p2 = new PointF(rectangle.X + 5 * pr, rectangle.Y + 10 * pr);
|
bufferG.DrawLine(node.Selected ? penChoosed : pen, p1, p2);
|
}
|
else
|
{
|
rectangle = new RectangleF((float)p.X - 3 * pr, (float)p.Y - 3 * pr, 6 * pr, 6 * pr);
|
if (node.Selected || _ShowJunction)
|
bufferG.FillEllipse(node.Selected ? brushChoosed : brush, rectangle);
|
}
|
|
}
|
}
|
|
using (Pen pen = new Pen(Color.FromArgb(255, 0, 0), 1 * r))
|
{
|
Brush brush = pen.Brush;
|
//获取_Nodes中自由水压最小的节点
|
var node = _Nodes.Where(n => n is JunctionViewModel || n is MeterViewModel && n.EN_PRESSURE != float.NaN).OrderBy(n => n.EN_PRESSURE).FirstOrDefault();
|
//判断node.EN_PRESSURE不是float.NaN
|
|
if (node != null && !float.IsNaN(node.EN_PRESSURE))
|
{
|
|
float pr = (float)(r * 0.5);
|
pr = pr * junction_multiply;
|
PointF p = WorldPointToMapPoint(node, template.OffSet);
|
|
var ps_20 = GraphHelper.GetUlongByPoint(p, 0.1f);
|
|
dict_point.Add(ps_20);
|
if (NodeColour != null)
|
{
|
pen.Color = GraphHelper.getNodeColor(NodeColour, node);
|
brush = pen.Brush;
|
brushChoosed = penChoosed.Brush;
|
|
|
}
|
|
|
var rectangle = new RectangleF((float)p.X - 5 * pr, (float)p.Y - 5 * pr, 10 * pr, 10 * pr);
|
float zoomAtMin = 0;
|
for (int i = 0; i < diametersZoom.Count; i++)
|
{
|
PointF point = diametersZoom[i];
|
if (node.MaxDiameter >= point.X) continue;
|
zoomAtMin = diametersZoom[i - 1].Y;
|
break;
|
}
|
|
|
var p1 = new PointF((float)p.X - 4 * pr, (float)p.Y - 2 * pr);
|
var p2 = new PointF((float)p.X + 4 * pr, (float)p.Y - 2 * pr);
|
var p3 = new PointF((float)p.X, (float)p.Y - 4 * pr);
|
bufferG.DrawPolygon(node.Selected ? penChoosed : pen, new PointF[] { p1, p2, p3 });
|
|
var whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.FillEllipse(whiteBrush, whiteRect);
|
|
whiteRect = new RectangleF(rectangle.X - 4 * pr, rectangle.Y - 4 * pr, rectangle.Width + 8 * pr, rectangle.Height + 8 * pr);
|
bufferG.DrawEllipse(node.Selected ? penChoosed : pen, whiteRect);
|
|
}
|
}
|
}
|
|
//绘制辅助线
|
void DrawH(Graphics bufferG, Template template)
|
{
|
var r = 2f / zoom;
|
if (_isDragging && DragStartPos != new PointF(0, 0) && mousePosition != new PointF(0, 0))
|
{
|
label_center.Text = $"S:{DragStartPos.X}:{DragStartPos.Y} E:{mousePosition.X}:{mousePosition.Y}";
|
var _lastMousePosition = DragStartPos;
|
// 绘制矩形
|
var start = new PointF((float)Math.Min(mousePosition.X, _lastMousePosition.X), (float)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new SizeF((float)Math.Abs(_lastMousePosition.X - mousePosition.X), (float)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
if (size.Width == 0) size.Width = 0.01f;
|
if (size.Height == 0) size.Height = 0.01f;
|
var rectangle0 = new RectangleF(start, size);
|
using (var pen = new Pen(Color.Black, 0.5f * r))
|
{
|
bufferG.DrawRectangles(pen, new RectangleF[] { rectangle0 });
|
}
|
}
|
if (_isPainting)
|
{
|
if (_mouseState == MouseState.新增立管)
|
{
|
var wPos = GetZZWorldPoint(_select_junction1.Position3D, _MousePosition, new Vector3(0, 0, 1));
|
using (var pen = new Pen(Color.Black, 1 * r))
|
{
|
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
|
bufferG.DrawLine(pen, WorldPointToMapPoint(_select_junction1), WorldPointToMapPoint(wPos));
|
}
|
}
|
else
|
{
|
|
using (var pen = new Pen(Color.Black, 1 * r))
|
{
|
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
|
if (mapOption.IsOrtho)
|
{
|
var wPos = GetZZWorldPoint(_select_junction1.Position3D, _MousePosition, new Vector3(1, 1, 0));
|
var mapPos = WorldPointToMapPoint(wPos);
|
if (double.IsNaN(mapPos.X) || double.IsNaN(mapPos.Y))
|
{
|
return;
|
}
|
bufferG.DrawLine(pen, WorldPointToMapPoint(_select_junction1), mapPos);
|
}
|
else
|
{
|
bufferG.DrawLine(pen, WorldPointToMapPoint(_select_junction1), _MousePosition);
|
}
|
|
}
|
}
|
}
|
if (_isDrawingPolygon && polygonPoints.Count > 0)
|
{
|
List<PointF> pf = polygonPoints.ToList();
|
pf.Add(new PointF(mousePosition.X, mousePosition.Y));
|
using (var pen = new Pen(Color.Black, 1 * r))
|
{
|
// 绘制多边形虚线边框
|
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
|
bufferG.DrawLines(pen, pf.ToArray());
|
}
|
}
|
if (_isSettingBackGroundPictur)
|
{
|
|
var _lastMousePosition = DragStartPos;
|
// 绘制矩形
|
var start = new PointF((float)Math.Min(mousePosition.X, _lastMousePosition.X), (float)Math.Min(mousePosition.Y, _lastMousePosition.Y));
|
var size = new SizeF((float)Math.Abs(_lastMousePosition.X - mousePosition.X), (float)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
|
var rectangle0 = new RectangleF(start, size);
|
using (var pen = new Pen(Color.Black, 1 * r))
|
{
|
bufferG.DrawRectangles(pen, new RectangleF[] { rectangle0 });
|
}
|
}
|
|
if (_isMovingObject)
|
{
|
var newP = _MousePosition;
|
//var p = MapPointToWorldPoint(, _OperaNode.Elev);
|
var oldP3D = (PointF3D)_undoOldValue;
|
var oldP = WorldPointToMapPoint(new PointF(oldP3D.X, oldP3D.Y), oldP3D.Z);
|
List<PointF> pf = new List<PointF> { oldP, newP };
|
using (var pen = new Pen(Color.Black, 1 * r))
|
{
|
// 绘制多边形虚线边框
|
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
|
bufferG.DrawLines(pen, pf.ToArray());
|
}
|
}
|
}
|
|
#region 基础坐标转换方法
|
/// <summary>
|
/// 将屏幕坐标转换为世界坐标。输入屏幕坐标 (x,y),返回世界坐标 (wx, wy)。
|
/// </summary>
|
/// <param name="screenPos"></param>
|
/// <returns></returns>
|
private PointF ScreenToVMap(PointF screenPos, float z = 0)
|
{
|
|
var centerX = this.map.Width / 2;
|
var centerY = this.map.Height / 2;
|
var worldX = (screenPos.X - centerX - Z(z).X) / Zoom.X + MapCenter.X;
|
var worldY = (screenPos.Y - centerY) / Zoom.Y + 0;
|
return new PointF(worldX, worldY);
|
}
|
|
|
/// <summary>
|
/// 将屏幕坐标转换为世界坐标。输入屏幕坐标 (x,y),返回世界坐标 (wx, wy)。
|
/// </summary>
|
/// <param name="screenPos"></param>
|
/// <returns></returns>
|
private PointF ScreenToMap(PointF screenPos, float z = 0)
|
{
|
|
var centerX = this.map.Width / 2;
|
var centerY = this.map.Height / 2;
|
var worldX = (screenPos.X - centerX - Z(z).X) / Zoom.X + MapCenter.X;
|
var worldY = (screenPos.Y - centerY - Z(z).Y) / Zoom.Y + MapCenter.Y;
|
//if (is3Dview) worldY = -(screenPos.Y - centerY + 2 * z) / (0.5f* zoom) + center.Y;
|
return new PointF(worldX, worldY);
|
}
|
|
/// <summary>
|
/// 世界投影坐标转换为屏幕坐标
|
/// </summary>
|
/// <param name="mapPos"></param>
|
/// <param name="z"></param>
|
/// <returns></returns>
|
private PointF MapToScreen(PointF mapPos, float z = 0)
|
{
|
|
var centerX = this.map.Width / 2;
|
var centerY = this.map.Height / 2;
|
var screenX = (mapPos.X - MapCenter.X) * Zoom.X + centerX + Z(z).X;
|
var screenY = (mapPos.Y - MapCenter.Y) * Zoom.Y + centerY + Z(z).Y;
|
//if (is3Dview) screenY = -(mapPos.Y - center.Y) * (0.5f * zoom) + centerY - 2 * z;
|
return new PointF(screenX, screenY);
|
}
|
|
|
// 根据旋转角度计算旋转后的坐标
|
|
|
// 根据旋转角度计算旋转后的坐标
|
private PointF Get平面旋转Point(PointF p, PointF MapC)
|
{
|
|
PointF center = MapC;
|
double radian = Rotation * Math.PI / 180; // 角度转弧度
|
float x = (float)(Math.Cos(radian) * (p.X - center.X) - Math.Sin(radian) * (p.Y - center.Y) + center.X);
|
float y = (float)(Math.Sin(radian) * (p.X - center.X) + Math.Cos(radian) * (p.Y - center.Y) + center.Y);
|
return new PointF(x, y);
|
}
|
private PointF Get平面还原Point(PointF p, PointF MapC)
|
{
|
PointF center = MapC;
|
double radian = -Rotation * Math.PI / 180; // 角度转弧度
|
float x = (float)(Math.Cos(radian) * (p.X - center.X) - Math.Sin(radian) * (p.Y - center.Y) + center.X);
|
float y = (float)(Math.Sin(radian) * (p.X - center.X) + Math.Cos(radian) * (p.Y - center.Y) + center.Y);
|
return new PointF(x, y);
|
}
|
|
|
|
private PointF Get俯视角旋转Point(PointF p, float z, PointF MapC)
|
{
|
PointF center = MapC;
|
double radian_fushi = 俯视弧度;
|
float sin = (float)Math.Sin(radian_fushi);
|
float cos = (float)Math.Cos(radian_fushi);
|
float x = (float)p.X;
|
float y = (float)(sin * (p.Y - center.Y) + center.Y) + cos * z;
|
return new PointF(x, y);
|
}
|
private PointF Get俯视角还原Point(PointF p, float z, PointF MapC)
|
{
|
PointF center = MapC;
|
double radian_fushi = 俯视弧度;
|
float sin = (float)Math.Sin(radian_fushi);
|
float cos = (float)Math.Cos(radian_fushi);
|
float x = (float)p.X;
|
float y = (p.Y - center.Y - cos * z) / sin + center.Y;
|
return new PointF(x, y);
|
}
|
|
|
private PointF GetRotateVector(PointF p, PointF p0)
|
{
|
|
double radian = Rotation * Math.PI / 180; // 角度转弧度
|
float x = (float)(Math.Cos(radian) * (p.X - p0.X) - Math.Sin(radian) * (p.Y - p0.Y));
|
float y = (float)(Math.Sin(radian) * (p.X - p0.X) + Math.Cos(radian) * (p.Y - p0.Y));
|
return new PointF(x, y);
|
}
|
/// <summary>
|
/// 获取世界投影坐标
|
/// </summary>
|
/// <param name="point"></param>
|
/// <param name="z"></param>
|
/// <returns></returns>
|
private PointF WorldPointToMapPoint(PointF point, float z, PointF3D offset = null)
|
{
|
if (offset == null) offset = new PointF3D(0, 0, 0);
|
point = new PointF(point.X + offset.X, point.Y + offset.Y);
|
|
var pointR = Get平面旋转Point(point, MapCenter);
|
|
var pointT = Get俯视角旋转Point(pointR, z + offset.Z, MapCenter);
|
|
return pointT;
|
}
|
private PointF WorldPointToMapPoint(PointF3D point, PointF3D offset = null)
|
{
|
return WorldPointToMapPoint(new PointF(point.X, point.Y), point.Z, offset);
|
|
}
|
private PointF WorldPointToMapPoint(NodeViewModel junction, PointF3D offset = null)
|
{
|
PointF p;
|
if (junction == null) return new PointF(0, 0);
|
p = WorldPointToMapPoint(junction.Position, junction.Elev, offset);
|
return p;
|
}
|
private PointF CubeWorldPointToMapPoint(NodeViewModel junction, PointF3D offset = null)
|
{
|
PointF p;
|
if (junction == null) return new PointF(0, 0);
|
var point = junction.Position;
|
var z = junction.Elev;
|
if (offset == null) offset = new PointF3D(0, 0, 0);
|
point = new PointF(point.X + offset.X, point.Y + offset.Y);
|
var pointR = Get平面旋转Point(point, new PointF(0, 0));
|
var pointT = Get俯视角旋转Point(pointR, z + offset.Z, new PointF(0, 0));
|
return pointT;
|
}
|
|
/// <summary>
|
/// 获取正交投影坐标,返回的是世界坐标
|
/// </summary>
|
/// <param name="position3D">世界坐标</param>
|
/// <param name="mousePosition">地图坐标</param>
|
/// <param name="vector3">投影向量</param>
|
/// <returns></returns>
|
/// <exception cref="NotImplementedException"></exception>
|
private PointF3D GetZZWorldPoint(PointF3D position3D, PointF mousePosition, Vector3 vector3)
|
{
|
//做一条通过position3D的平行于vector3的直线,
|
if (vector3 == new Vector3(0, 0, 1))
|
{
|
return GetLGWorldPoint(position3D, mousePosition);
|
}
|
else
|
{
|
var p2 = MapPointToWorldPoint(mousePosition, position3D.Z);
|
var vector = new Vector2(p2.X - position3D.X, p2.Y - position3D.Y);
|
//判断二维向量vector在第几象限,距离哪个轴最近
|
var x = vector.X;
|
var y = vector.Y;
|
var x1 = Math.Abs(x);
|
var y1 = Math.Abs(y);
|
if (x1 > y1)
|
{
|
if (x > 0)
|
{
|
//第一象限
|
return new PointF3D(position3D.X + Math.Abs(x), position3D.Y, position3D.Z);
|
}
|
else
|
{
|
//第三象限
|
return new PointF3D(position3D.X - Math.Abs(x), position3D.Y, position3D.Z);
|
}
|
}
|
else
|
{
|
if (y > 0)
|
{
|
//第二象限
|
return new PointF3D(position3D.X, position3D.Y + Math.Abs(y), position3D.Z);
|
}
|
else
|
{
|
//第四象限
|
return new PointF3D(position3D.X, position3D.Y - Math.Abs(y), position3D.Z);
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取正交投影坐标,返回的是世界坐标
|
/// </summary>
|
/// <param name="position3D">世界坐标</param>
|
/// <param name="mousePosition">地图坐标</param>
|
/// <param name="vector3">投影向量</param>
|
/// <returns></returns>
|
/// <exception cref="NotImplementedException"></exception>
|
private PointF3D GetLGWorldPoint(PointF3D position3D, PointF p2)
|
{
|
double radian_fushi = 俯视弧度;
|
float sin = (float)Math.Sin(radian_fushi);
|
float cos = (float)Math.Cos(radian_fushi);
|
var p1 = WorldPointToMapPoint(position3D);
|
var dy = p2.Y - p1.Y;
|
float dz = dy / cos;
|
return new PointF3D(position3D.X, position3D.Y, position3D.Z + dz);
|
|
}
|
|
/// <summary>
|
/// 获取地图投影坐标
|
/// </summary>
|
/// <param name="point"></param>
|
/// <param name="z"></param>
|
/// <returns></returns>
|
public PointF MapPointToWorldPoint(PointF point, float z = 0)
|
{
|
var pointT = Get俯视角还原Point(point, z, MapCenter);
|
pointT = Get平面还原Point(pointT, MapCenter);
|
|
return pointT;
|
}
|
|
|
|
|
#endregion
|
|
#region 计算三维相机
|
public PointF3D GetCameraPosition(float distance = 1)
|
{
|
// 将角度转换为弧度
|
float rotationRadians = (float)Rotation * (float)Math.PI / 180;
|
float rotationFRadians = (float)RotationF * (float)Math.PI / 180;
|
|
// 计算相机的球坐标系位置
|
float x = distance * (float)Math.Sin(rotationFRadians) * (float)Math.Cos(rotationRadians);
|
float y = distance * (float)Math.Sin(rotationFRadians) * (float)Math.Sin(rotationRadians);
|
float z = distance * (float)Math.Cos(rotationFRadians);
|
|
return new PointF3D(x, y, z);
|
}
|
|
public bool IsFaceVisibleToCamera(AreaViewModel area, PointF3D cameraPosition)
|
{
|
|
//正面是2,右面是1,左面是3,背面是4
|
//Rotation为0时,只显示正面,Rotation为90时,只显示右面,Rotation为180时,只显示背面,Rotation为270时,只显示左面
|
int delta = 5;
|
bool flag = false;
|
switch (area.Name)
|
{
|
case "左":
|
//Rotation为0~180时显示
|
if (RotationF <= 90 - delta && Rotation >= 0 + delta && Rotation <= 180 - delta)
|
flag = true;
|
break;
|
case "前":
|
//Rotation为0~180时显示
|
if (RotationF <= 90 - delta && Rotation >= -90 + delta && Rotation <= 90 - delta)
|
flag = true;
|
break;
|
case "右":
|
//Rotation为0~180时显示
|
if (RotationF <= 90 - delta && Rotation >= -180 + delta && Rotation <= 0 - delta)
|
flag = true;
|
break;
|
case "后":
|
//Rotation为0~180时显示
|
if (RotationF <= 90 - delta && ((Rotation >= 90 + delta && Rotation <= 180) || (Rotation >= -180 && Rotation <= -90 - delta)))
|
flag = true;
|
break;
|
case "上":
|
if (RotationF >= 0 + delta)
|
flag = true;
|
break;
|
case "下":
|
if (RotationF <= 0 - delta)
|
flag = true;
|
break;
|
}
|
return flag;
|
|
}
|
|
|
#endregion
|
|
#region 判断可见性
|
|
private float Get_dist(PointF A, PointF B)
|
{
|
float dx = A.X - B.X;
|
float dy = A.Y - B.Y;
|
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
|
return dist;
|
}
|
|
//判断A距离线段B和C的距离,如果超出了线段的范围,则返回到最近的端点的距离;距离线段中心点越远,返回的距离越大;
|
private float Get_dist(PointF A, PointF B, PointF C, float MaxOff)
|
{
|
//PointF A, PointF B,PointF C,求点A到B、C构成线段的中心点的距离
|
|
float dist_off = GetDistanceFromPointAToMidpointOfLineSegmentBC(A, B, C);
|
//使用dist_off 跟 线段A、B的长度比较,如果大于1/2,则返回MaxOff,否则按照比例返回
|
float dist_len = Get_dist(B, C);
|
if (dist_len < 5) dist_len = 5;
|
float dist_add = (dist_off / dist_len > 0.5 ? MaxOff : dist_off / dist_len * 2 * MaxOff);
|
|
float dx = C.X - B.X;
|
float dy = C.Y - B.Y;
|
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
|
if (dist == 0) return Get_dist(A, B) + dist_add;
|
float t = ((A.X - B.X) * dx + (A.Y - B.Y) * dy) / (dist * dist);
|
if (t < 0) return Get_dist(A, B) + dist_add;
|
if (t > 1) return Get_dist(A, C) + dist_add;
|
float x = B.X + t * dx;
|
float y = B.Y + t * dy;
|
return Get_dist(A, new PointF(x, y)) + dist_add;
|
|
}
|
private float GetDistanceFromPointAToMidpointOfLineSegmentBC(PointF A, PointF B, PointF C)
|
{
|
// Calculate the midpoint of the line segment BC
|
PointF midpoint = new PointF((B.X + C.X) / 2, (B.Y + C.Y) / 2);
|
|
// Calculate the distance from point A to the midpoint
|
float dx = midpoint.X - A.X;
|
float dy = midpoint.Y - A.Y;
|
float distance = (float)Math.Sqrt(dx * dx + dy * dy);
|
|
return distance;
|
}
|
|
PointF PMin_Show, PMax_Show;
|
|
/// <summary>
|
/// 判断是否在屏幕坐标内
|
/// </summary>
|
/// <param name="screenPos"></param>
|
/// <returns></returns>
|
public bool isVisible(PointF MapPos)
|
{
|
if (MapPos.X < PMin_Show.X || MapPos.X > PMax_Show.X || MapPos.Y < PMin_Show.Y || MapPos.Y > PMax_Show.Y)
|
return false;
|
else
|
return true;
|
}
|
|
#endregion
|
#endregion
|
}
|
}
|