using HydroUI;
namespace PBS.WinFrmUI.Hydro
{
public partial class ModelEditPage : DocumentPage
{
public ModelEditPage()
{
InitializeComponent();
this.facilityTreeListCtrl1.SelectFacilityEvent += FacilityTreeListCtrl1_SelectFacilityEvent; ;
this.facilityTreeListCtrl1.RefreshDataEvent += () => { RefreshData(); };
}
private FacilityVmo _facility = null;
private Template _template = null;
private MapViewer _mapView;
private PropertyForm _propertyForm;
private bool _isInitial = false;
///
/// 初始化数据源
///
public override async void InitialDataSource()
{
var overlay = this.ShowOverlay();
base.InitialDataSource();
if (!_isInitial)
{
InitialMapViewer();
_isInitial = true;
}
var allFacilityList = await BLLFactory.Instance.GetAll();
this.facilityTreeListCtrl1.SetBindingData(allFacilityList);
overlay.Close();
}
//选择设施
private void FacilityTreeListCtrl1_SelectFacilityEvent(FacilityVmo obj)
{
InitialData(obj);
}
///
/// 初始化数据
///
public void InitialData(FacilityVmo obj)
{
if (obj == null)
{
_facility = null;
_template = null;
_mapView.Clear();
return;
}
if (string.IsNullOrEmpty(obj.ModelPath))
{
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\Inp\\" + obj.ID + ".inp");
var directoryPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
File.Create(fullPath).Close();
obj.ModelPath = fullPath;
}
if (string.IsNullOrEmpty(obj.ModelInfo))
{
var templateTemp = new Template()
{
ID = obj.ID.ToString(),
Name = obj.Name,
Type = eModelTemplateType.Custom,
filePath = obj.ModelPath,
};
obj.ModelInfo = Yw.JsonHelper.Object2Json(templateTemp);
}
_facility = obj;
_template = Yw.JsonHelper.Json2Object(obj.ModelInfo);
var template = _template;
template.network = new MapViewNetWork();
template.network.BuildFromInp(Path.Combine(Directory.GetCurrentDirectory(), _template.filePath));
_mapView.SetData(template);
}
//初始化视图
private void InitialMapViewer()
{
_mapView = new MapViewer();
_mapView.ShowPropertyForm = false;
_mapView.Dock = DockStyle.Fill;
_mapView.mapOption.isShowPic = true;
_mapView.mapOption.IsOrtho = false;
this.panelControl1.Controls.Add(_mapView);
_propertyForm = new PropertyForm();
_propertyForm.Dock = DockStyle.Fill;
this.dockPanelModelProperty.Controls.Add(_propertyForm);
GlobalObject.PropertyForm = _propertyForm;
GlobalObject.map = _mapView;
}
///
/// 刷新数据源
///
public override void RefreshData()
{
base.RefreshData();
InitialDataSource();
}
//设置背景图
private void SetMapBackground(string backgroundUrl, float width, float height)
{
_mapView.mapOption.isShowPic = true;
_mapView.mapOption.isAutoBackgroundImage = true;
var img = Image.FromFile(backgroundUrl);
var imgWidth = img.Width;
var imgHeight = img.Height;
var w = imgWidth / width;
var h = imgHeight / height;
var zoom = w > h ? w : h;
_mapView.MapCenter = _mapView.mapOption.Center = new PointF(width / 2, height / 2);
_mapView.zoom = _mapView.mapOption.zoom = zoom;
var temp = _mapView._Template;
var directory = Path.GetDirectoryName(temp.BackGroundImg_FullPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
File.Copy(backgroundUrl, temp.BackGroundImg_FullPath, true);
temp.BackGroundImg_FullPath = temp.BackGroundImg_FullPath;
temp.BackGroundImgWidth = width;
temp.BackGroundImgHeight = height;
temp.BackGroundImgRotaAngle = 0;
temp.BackGroundPoint1 = new PointF(0, 0);
_mapView._Template = temp;
_mapView.SetMapInvalidate();
}
#region Event
#region Draw
private void toolboxControlDraw_ItemClick(object sender, DevExpress.XtraToolbox.ToolboxItemClickEventArgs e)
{
if (e.Item.Tag is not string tag)
return;
switch (tag)
{
case "Select":
{
_mapView.toolStripButton_普通_Click(1, new EventArgs());
}
break;
case "AddNode":
{
_mapView.toolStripButton_新建节点_Click(1, new EventArgs());
}
break;
case "AddVerticalPipe":
{
_mapView.toolStripButton_新建立管_Click(1, new EventArgs());
}
break;
case "AddHorizontalPipe":
{
_mapView.toolStripButton_新建管线_Click(1, new EventArgs());
}
break;
case "AddValve":
{
_mapView.toolStripButton_添加阀门_Click(1, new EventArgs());
}
break;
case "AddPump":
{
_mapView.toolStripButton_添加水泵_Click(1, new EventArgs());
}
break;
case "AddWaterMeter":
{
_mapView.toolStripButton_添加水表_Click(1, new EventArgs());
}
break;
case "AddTank":
{
_mapView.toolStripButton_添加水池_Click(1, new EventArgs());
}
break;
case "AddReservoir":
{
_mapView.toolStripButton_添加水库_Click(1, new EventArgs());
}
break;
case "Copy":
{
_mapView.复制ToolStripMenuItem_Click(1, new EventArgs());
}
break;
case "Paste":
{
_mapView.粘贴ToolStripMenuItem1_Click(1, new EventArgs());
}
break;
case "Delete":
{
_mapView.删除ToolStripMenuItem_Click(1, new EventArgs());
}
break;
case "Undo":
{
_mapView.buttonUndo_Click(1, new EventArgs());
}
break;
case "Redo":
{
_mapView.buttonRedo_Click(1, new EventArgs());
}
break;
case "HorizontalAlign":
{
_mapView.东西对齐ToolStripMenuItem_Click(1, new EventArgs());
}
break;
case "VerticalAlign":
{
_mapView.南北对齐ToolStripMenuItem_Click(1, new EventArgs());
}
break;
case "TopBottomAlign":
{
_mapView.竖直对齐ToolStripMenuItem_Click(1, new EventArgs());
}
break;
case "AutoAlign":
{
_mapView.自动对齐ToolStripMenuItem_Click(1, new EventArgs());
}
break;
default:
break;
}
}
private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
var hi = this.toolboxControlDraw.CalcHitInfo(e.ControlMousePosition);
if (hi.IsInItem)
{
var toolItem = hi.ItemInfo.Item;
this.toolTipController1.ShowHint(toolItem.Caption, MousePosition);
}
else
{
this.toolTipController1.HideHint();
}
}
#endregion
#region QuickModeling
private void barBtnModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var dlg = new SelectModelTemplateDlg();
dlg.SetBindingData();
dlg.ReloadDataEvent += (template) =>
{
_mapView.InsertNet(template);
};
dlg.ShowDialog();
}
private void barBtnConnectivityCheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.btn_拓扑检查_Click(1, new EventArgs());
}
private void barBtnVerticalPipeDiameter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var dlg = new InputDlg();
dlg.SetBindingData("输入口径");
dlg.ReloadDataEvent += (value) =>
{
_mapView._Template.network.Links.ForEach(l =>
{
if (l.StartNode != null && l.EndNode != null)
{
if (l.StartNode.Position == l.EndNode.Position)
{
l.Diameter = (float)value;
}
}
});
};
}
private void barBtnHorizontalPipeDiameter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var dlg = new InputDlg();
dlg.SetBindingData("输入口径");
dlg.ReloadDataEvent += (value) =>
{
_mapView._Template.network.Links.ForEach(l =>
{
if (l.StartNode.Elev == 0 && l.EndNode.Elev == 0)
{
l.Diameter = (float)value;
}
});
};
}
private void barBtnElevationTranslation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var dlg = new InputDlg();
dlg.SetBindingData("输入标高偏移量");
dlg.ReloadDataEvent += (value) =>
{
_mapView._Template.network.Nodes.ForEach(n =>
{
if (n.Elev > 0)
{
n.Elev = n.Elev + (float)value;
}
});
};
dlg.ShowDialog();
}
///
/// 保存
///
private async void SaveModelTemplate()
{
if (_facility == null)
{
TipFormHelper.ShowWarn("请选择模板");
return;
}
if (_template == null)
{
TipFormHelper.ShowWarn("请选择模板");
return;
}
SaveView();
SetZoom();
if (_facility.ModelPath.Contains("temp"))
{
var tempInpFile = _facility.ModelPath;
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\Inp\\" + _facility.ID + ".inp");
File.Copy(tempInpFile, fullPath, true);
if (File.Exists(tempInpFile))
File.Delete(tempInpFile);
_facility.ModelPath = fullPath;
_template.filePath = fullPath;
}
_facility.ModelInfo = Yw.JsonHelper.Object2Json(_template);
this._mapView.toolStripButton_save_ButtonClick(1, new EventArgs());
if (!await BLLFactory.Instance.Update(_facility))
{
TipFormHelper.ShowError("保存模型信息失败!");
return;
}
TipFormHelper.ShowSucceed("保存模型信息成功!");
}
private void barBtnSaveModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SaveModelTemplate();
}
private void barBtnResetModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
RefreshData();
}
#endregion
#region Import
private void ImportInp()
{
if (_facility == null)
{
return;
}
if (_template == null)
{
return;
}
var openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "INP文件|*.inp";
if (openFileDlg.ShowDialog() != DialogResult.OK)
return;
_mapView.Clear();
var inpFilePath = openFileDlg.FileName;
var templateFilePath = _template.FullPath;
HelperC.ClearFileReadOnly(templateFilePath);
File.Copy(inpFilePath, templateFilePath, true);
_template.network.BuildFromInp(templateFilePath);
_mapView.SetData(_template);
}
private void barBtnImportInp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
ImportInp();
}
#endregion
#region BaseMap
private void SetBaseMap()
{
var dlg = new SetMapBaseMapDlg();
dlg.ReloadDataEvent += (filePath, widht, height) =>
{
SetMapBackground(filePath, (float)widht, (float)height);
};
dlg.ShowDialog();
}
private void barBtnAddBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SetBaseMap();
}
private void barBtnSetBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.设置底图ToolStripMenuItem_Click(1, null);
}
private void barBtnClearBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.清除底图ToolStripMenuItem_Click(1, null);
}
private void barCekBaseMapVisible_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.显示隐藏底图ToolStripMenuItem_Click(1, null);
}
#endregion
#region View
private void SetDefaultView()
{
var mapOption0 = _mapView.mapOption.Copy();
var p = PointF.Empty;
float x0 = 99999999999f, y0 = 99999999999f, x1 = -99999999999f, y1 = -99999999999f;
foreach (NodeViewModel junction in _mapView._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;
_mapView.zoom = Math.Min(this._mapView.Width / x_span, this._mapView.Height / y_span);
_mapView.zoom = Math.Max(_mapView.zoom, 0.1f);
_mapView.zoom = Math.Min(_mapView.zoom, 1000.0f);
_mapView.Rotation = 0;
_mapView.RotationF = 90;
if (_mapView._Nodes.Count > 0)
{
p.X /= _mapView._Nodes.Count;
p.Y /= _mapView._Nodes.Count;
}
_mapView.MapCenter = p;
_mapView.RotationF = 45;
_mapView.Rotation = -45;
MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
_mapView.SetMapInvalidate();
}
private void barBtnDefaultView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SetDefaultView();
}
private void barBtnFullView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.重置视角ToolStripMenuItem_Click(1, new EventArgs());
}
private void SetTopView()
{
var mapOption0 = _mapView.mapOption.Copy();
_mapView.RotationF = 90;
MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
_mapView.SetMapInvalidate();
}
private void barBtnTopView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SetTopView();
}
private void SetFrontView()
{
var mapOption0 = _mapView.mapOption.Copy();
_mapView.RotationF = 0;
MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
_mapView.SetMapInvalidate();
}
private void barBtnFrontView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SetFrontView();
}
private void barBtnResetView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
_mapView.重置视角ToolStripMenuItem_Click(1, new EventArgs());
}
private void SetZoom()
{
if (_template == null) return;
_template.view ??= new MapDimensions();
_template.view.Center = _mapView.MapCenter;
_template.view.zoom = _mapView.zoom;
_template.view.rotation = _mapView.Rotation;
_template.view.rotationF = _mapView.RotationF;
}
private void SaveView()
{
if (_template == null) return;
_template.view ??= new MapDimensions();
_template.view.Center = _mapView.MapCenter;
_template.view.zoom = _mapView.zoom;
_template.view.rotation = _mapView.Rotation;
_template.view.rotationF = _mapView.RotationF;
}
private void barBtnSaveView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
SaveView();
}
#endregion
#endregion
}
}