using DevExpress.Drawing.Internal.Fonts.Interop;
|
using DevExpress.XtraPrinting.Native;
|
using Hydro;
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Taskbar;
|
|
namespace PBS.WinFrmUI.Hydro
|
{
|
public partial class ModelEditPage : DocumentPage, IWizardPageAsync<QuickModelingViewModel>
|
{
|
public ModelEditPage()
|
{
|
InitializeComponent();
|
//this.modelTemplateTreeListCtrl1.SelectModelTemplateEvent += ModelTemplateTreeListCtrl1_SelectModelTemplateEvent;
|
//this.modelTemplateTreeListCtrl1.RefreshDataEvent += () => { RefreshData(); };
|
}
|
|
|
private FacilityVmo _facility = null;
|
private Template _template = null;
|
private MapViewer _mapView;
|
private PropertyForm _propertyForm;
|
|
/// <summary>
|
/// 初始化数据源
|
/// </summary>
|
public override void InitialDataSource()
|
{
|
base.InitialDataSource();
|
InitialMapViewer();
|
}
|
|
|
/// <summary>
|
/// 初始化数据
|
/// </summary>
|
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);
|
if (!File.Exists(fullPath))
|
{
|
fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\Inp\\temp.inp");
|
}
|
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,
|
};
|
if (_vm != null)
|
{
|
templateTemp.BackGroundImg_FullPath = _vm.tempBackgroundImageUrl;
|
templateTemp.BackGroundImgWidth = (float)_vm.BackgroundWidth;
|
templateTemp.BackGroundImgHeight = (float)_vm.BackgroundHeight;
|
templateTemp.BackGroundImgRotaAngle = 0;
|
templateTemp.BackGroundPoint1 = new PointF(0, 0);
|
}
|
obj.ModelInfo = Yw.JsonHelper.Object2Json(templateTemp);
|
|
}
|
|
_facility = obj;
|
_template = Yw.JsonHelper.Json2Object<Template>(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.Lock2DView = true;
|
_mapView.ShowPropertyForm = false;
|
//_mapView.Location = new System.Drawing.Point(0, 0);
|
_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();
|
}
|
|
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 EditFloors dlg = new EditFloors();
|
private void barBtnModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
if (dlg.ShowDialog() == DialogResult.OK)
|
{
|
}
|
}
|
|
private void barBtnConnectivityCheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
_mapView.关阀搜索ToolStripMenuItem_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();
|
|
}
|
|
|
/// <summary>
|
/// 保存
|
/// </summary>
|
private void SaveModelTemplate()
|
{
|
if (_facility == null)
|
{
|
TipFormHelper.ShowWarn("请选择模板");
|
return;
|
}
|
if (_template == null)
|
{
|
TipFormHelper.ShowWarn("请选择模板");
|
return;
|
}
|
SaveView();
|
SetZoom();
|
|
_facility.ModelInfo = Yw.JsonHelper.Object2Json(_template);
|
this._mapView.toolStripButton_save_ButtonClick(1, new EventArgs());
|
|
}
|
|
|
|
private void barBtnSaveModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
|
{
|
SaveModelTemplate();
|
}
|
|
#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;
|
|
Global.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());
|
}
|
|
|
public void SetMapCenterAndZoom()
|
{
|
_mapView.MapCenter = _mapView.mapOption.Center = new PointF((float)_vm.BackgroundWidth / 2, (float)_vm.BackgroundHeight / 2);
|
_mapView.zoom = _mapView.mapOption.zoom = (float)_vm.Zoom;
|
_mapView.RotationF = 90;
|
_mapView.mapOption.isAutoBackgroundImage = true;
|
_mapView._Template.Colours = null;
|
_mapView.SetMapInvalidate();
|
}
|
|
|
|
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
|
|
|
/// <summary>
|
/// 状态改变事件
|
/// </summary>
|
public event Action PageStateChangedEvent;
|
|
private QuickModelingViewModel _vm = null;//操作对象
|
private bool _isCompleted = false;//是否创建完成
|
private bool _isInitialMapViewer = false;//是否初始化地图
|
|
|
/// <summary>
|
/// 初始化页面
|
/// </summary>
|
public void InitialPage(QuickModelingViewModel vm)
|
{
|
if (vm == null)
|
{
|
return;
|
}
|
_vm = vm;
|
_isCompleted = false;
|
if (!_isInitialMapViewer)
|
{
|
InitialMapViewer();
|
}
|
|
|
InitialData(_vm.Facility);
|
SetMapCenterAndZoom();
|
_isCompleted = true;
|
this.PageStateChangedEvent?.Invoke();
|
}
|
|
|
//验证
|
public bool Verify()
|
{
|
return this.Invoke(() =>
|
{
|
bool isExist = true;
|
|
if (_mapView==null)
|
{
|
isExist = false;
|
}
|
if (_template==null)
|
{
|
isExist = false;
|
}
|
|
return isExist;
|
});
|
}
|
|
//保存
|
private bool Save()
|
{
|
if (!Verify())
|
{
|
return false;
|
}
|
|
_vm.Facility.ModelInfo = Yw.JsonHelper.Object2Json(_template);
|
|
|
return true;
|
}
|
|
//保存
|
private async Task<bool> Complete()
|
{
|
if (!Verify())
|
{
|
return false;
|
}
|
|
var place = _vm.Place;
|
var facility = _vm.Facility;
|
|
place.ID = await BLLFactory<BLL.Place>.Instance.Insert(place);
|
if (place.ID < 1)
|
{
|
TipFormHelper.ShowError("新增场所失败!");
|
return false;
|
}
|
facility.PlaceID = place.ID;
|
facility.ID = await BLLFactory<BLL.Facility>.Instance.Insert(facility);
|
if (facility.ID < 1)
|
{
|
TipFormHelper.ShowError("新增设施失败!");
|
return false;
|
}
|
|
facility = await BLLFactory<BLL.Facility>.Instance.GetByID(facility.ID);
|
|
var tempInpFile = facility.ModelPath;
|
|
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\Inp\\" + facility.ID + ".inp");
|
File.Copy(tempInpFile, fullPath, true);
|
var bgFile = tempInpFile.Replace(".inp", ".png");
|
if (File.Exists(bgFile))
|
{
|
File.Copy(bgFile, fullPath.Replace(".inp", ".png"), true);
|
File.Delete(bgFile);
|
}
|
if (File.Exists(tempInpFile))
|
File.Delete(tempInpFile);
|
_template.filePath = fullPath;
|
_template.BackGroundImg_FullPath = fullPath.Replace(".inp", ".png");
|
|
facility.ModelInfo = Yw.JsonHelper.Object2Json(_template);
|
facility.ModelPath = fullPath;
|
|
if (!await BLLFactory<BLL.Facility>.Instance.Update(facility))
|
{
|
TipFormHelper.ShowError("新增模型信息失败!");
|
return false;
|
}
|
|
return true;
|
}
|
|
|
/// <summary>
|
/// 允许上一步
|
/// </summary>
|
public bool AllowPrev
|
{
|
get
|
{
|
if (!_isCompleted)
|
{
|
return false;
|
}
|
return true;
|
}
|
}
|
|
/// <summary>
|
/// 允许下一步
|
/// </summary>
|
public bool AllowNext
|
{
|
get
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 允许取消
|
/// </summary>
|
public bool AllowCancel
|
{
|
get
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 允许完成
|
/// </summary>
|
public bool AllowComplete
|
{
|
get
|
{
|
return true;
|
}
|
}
|
|
/// <summary>
|
/// 能否上一步
|
/// </summary>
|
/// <returns></returns>
|
public Task<bool> CanPrev()
|
{
|
return Task.Run(() =>
|
{
|
Save();
|
return this.AllowPrev;
|
});
|
}
|
|
/// <summary>
|
/// 能否下一步
|
/// </summary>
|
public Task<bool> CanNext()
|
{
|
return Task.Run(() => this.AllowNext);
|
}
|
|
/// <summary>
|
/// 能否取消
|
/// </summary>
|
public Task<bool> CanCancel()
|
{
|
return Task.Run(() => this.AllowCancel);
|
}
|
|
/// <summary>
|
/// 能否完成
|
/// </summary>
|
public Task<bool> CanComplete()
|
{
|
return Task.Run(() => Complete());
|
}
|
|
|
|
}
|
|
}
|