You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1404 lines
50 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// <copyright file="FormMain.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Pdf.Native;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Docking2010.Views.Tabbed;
using DevExpress.XtraBars.Ribbon;
using FlexenabledLic;
using GeoSigma.SigmaDrawerUtil;
using GeoSigma.UCDraw;
using GeoSigmaDrawLib;
using SigmaDrawerElement;
using UCDraw;
namespace PcgDrawR
{
/// <summary>
/// 软件主窗体.
/// </summary>
public partial class FormMain : RibbonForm
{
/// <summary>
/// Tab 页签最长长度
/// </summary>
private const int TABMAXLENGTH = 10;
private readonly string fileLayout = Path.Combine(Application.StartupPath, "layout.xml");
private readonly string workspaceName1 = "MyLayout";
// 启动参数
private readonly string startFile = string.Empty;
private readonly List<string> lstArgs = new List<string>();
private bool batchSetZColorFlag = false;
private readonly LicHelp licHelp;
private Security security;
private bool isDQLogin = false;
private bool isLogin = false;
/// <summary>
/// Gets or sets the z color width.
/// </summary>
public double ZColorWidth { get; set; } = 25;
private RecentFileConfig recentConfig = null;
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32",EntryPoint="FreeLibrary",SetLastError=true)]
private static extern bool FreeLibrary(IntPtr hModule);
private static void ValidateDll(string dllName)
{
string dllPath = Path.Combine(Application.StartupPath, dllName);
IntPtr hModule = IntPtr.Zero;
try
{
hModule = LoadLibrary(dllPath);
if (hModule == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
finally
{
if (hModule != IntPtr.Zero)
{
FreeLibrary(hModule);
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FormMain"/> class.
/// </summary>
public FormMain()
{
InitializeComponent();
this.barButtonItem8.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
tabbedView1.Ribbon = this.ribbonControl1;
tabbedView1.InvalidDocumentTypes.Add(
typeof(DevExpress.XtraBars.Docking.FloatForm));
this.licHelp = new LicHelp("KEPlatform");
this.licHelp.LoginResultEvent += async (LoginResult result) =>
{
int nStatus = result.Status;
await this.setDQLoginStatusAsync(nStatus);
// Task.Delay 和 DoLogin 可以在后台线程继续执行,不会影响 UI
if (nStatus != 2 && nStatus != 0)
{
await Task.Delay(TimeSpan.FromSeconds(60));
this.licHelp.DoLogin();
}
};
// 属性面板关闭时,将主菜单开关闭“属性”按钮设置为 unchecked
dckPanelProperty.ClosedPanel += (sender, e) =>
{
btnViewProperty.Checked = false;
};
// 关闭图层时把对应属性改为 unchecked
dckPanelLayer.ClosedPanel += (sender, e) =>
{
btnViewLayer.Checked = false;
};
// 关闭统计元素时把对应属性改为 unchecked
dckPanelStatic.ClosedPanel += (sender, e) =>
{
btnViewStatic.Checked = false;
};
ribbonControl1.OptionsMenuMinWidth = 0;
KeyPreview = true;
}
/// <summary>
/// 设置大庆服务器登录状态.
/// </summary>
/// <param name="statusCode">The status code.</param>
private async Task setDQLoginStatusAsync(int statusCode)
{
if (this.InvokeRequired)
{
// 使用 BeginInvoke 异步调用
this.Invoke(
new Action<int>(async (code) =>
await setDQLoginStatusAsync(code)), statusCode);
return;
}
if (statusCode == 0)
{ // 登录成功
isDQLogin = true;
await this.setLoginStatusAsync();
}
else if (statusCode == 2)
{
// 不需要登录大庆服务器,不设置登录状态。
isDQLogin = true;
}
else
{ // 登录失败
isDQLogin = false;
await this.setLoginStatusAsync();
// 大庆版本
MessageBox.Show(this, "登录失败,请尽快保存文件!", "登录许可失败");
}
}
private async Task setLoginStatusAsync()
{
bool isLoggedIn = isDQLogin && isLogin;
string tooltipText = isLoggedIn ? "已登录" : "未登录";
string resourceName = isLoggedIn
? "bsiLoginInfo.ImageOptions.SvgImage"
: "bsiLoginInfo.ImageOptions.DisabledSvgImage";
// 更新非资源依赖的UI部分
bsiLoginInfo.Enabled = true;
((ToolTipItem)this.bsiLoginInfo.SuperTip.Items[0]).Text = tooltipText;
// 异步加载资源
var resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
var svgImage = await Task.Run(() => resources.GetObject(resourceName));
// 回到UI线程更新图像实际上await会自动回到UI线程
bsiLoginInfo.ImageOptions.SvgImage = (DevExpress.Utils.Svg.SvgImage)svgImage;
}
///// <summary>
///// 设置登录状态按钮.
///// </summary>
///// <param name="statusCode">The status code.</param>
///// <summary>
///// 设置登录状态.
///// </summary>
///// <param name="statusCode">The status code.</param>
//private void setLoginStatus(int statusCode)
//{
// if (this.InvokeRequired)
// {
// this.Invoke((MethodInvoker)(() => this.setLoginStatus(statusCode)));
// return;
// }
// if (statusCode == 0)
// { // 登录成功
// bsiLoginInfo.Enabled = true;
// ((ToolTipItem)this.bsiLoginInfo.SuperTip.Items[0]).Text = "已登录";
// System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
// bsiLoginInfo.ImageOptions.SvgImage = (DevExpress.Utils.Svg.SvgImage)resources.GetObject("bsiLoginInfo.ImageOptions.SvgImage");
// }
// else
// { // 登录失败
// bsiLoginInfo.Enabled = true;
// ((ToolTipItem)this.bsiLoginInfo.SuperTip.Items[0]).Text = "未登录";
// System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
// bsiLoginInfo.ImageOptions.SvgImage = (DevExpress.Utils.Svg.SvgImage)resources.GetObject("bsiLoginInfo.ImageOptions.DisabledSvgImage");
// // 大庆版本
// MessageBox.Show(this, "登录失败,请尽快保存文件!", "登录许可失败");
// }
//}
/// <inheritdoc/>
protected override void OnKeyUp(KeyEventArgs e)
{
//if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
//{
// CurrentForm.SaveFile();
//}
if (e.KeyCode == Keys.O && e.Modifiers == Keys.Control)
{
btnFileOpen_ItemClick(this, null);
}
else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
{
btnFileNew_ItemClick(this, null);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FormMain"/> class.
/// </summary>
/// <param name="args">参数 /ZC-按块显示散点Z值</param>
public FormMain(string[] args)
: this()
{
if (args.Length > 0)
{
this.startFile = args[0].Trim();
}
if (args.Length > 1)
{
lstArgs.Clear();
for (int i = 1; i < args.Length; i++)
{
lstArgs.Add(args[i]);
}
}
bbtnMeshAntiAlias.Checked = DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable;
ribbonControl1.OptionsMenuMinWidth = 0;
string executablePath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
string vtkPath = "vtk";
//string vtkPath = "3d";
string combinedPath = Path.Combine(executablePath, vtkPath);
GeoSigmaLib.SetDllDirectory(combinedPath);
}
/// <summary>
/// 当前激活的文档
/// </summary>
private UCDrawEdit CurrentForm
{
get
{
if (this.tabbedView1.ActiveDocument != null)
{
return tabbedView1.ActiveDocument.Control as UCDrawEdit;
}
else if (this.tabbedView1.ActiveFloatDocument != null)
{
return tabbedView1.ActiveFloatDocument.Control as UCDrawEdit;
}
return null;
}
}
/// <summary>
/// DrawerGlobalConfig
/// </summary>
public DrawerGlobalConfig DrawerGlobalConfig { get; set; } = DrawerGlobalConfig.Instance;
private FileDropHandler fileDroper;
/// <summary>
/// Forms the main_ load.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private async void FormMain_Load(object sender, EventArgs e)
{
try
{
if (Security.CurentLicenseInfo.IsNetServer == true)
{
bsiLoginInfo.Enabled = true;
((ToolTipItem)this.bsiLoginInfo.SuperTip.Items[0]).Text = "未登录";
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain));
bsiLoginInfo.ImageOptions.SvgImage = (DevExpress.Utils.Svg.SvgImage)resources.GetObject("bsiLoginInfo.ImageOptions.DisabledSvgImage");
}
if (Security.CurentLicenseInfo.IsNetServer == true)
{
isLogin = false;
await this.setLoginStatusAsync();
}
else
{
isLogin = true;
}
this.security = new Security();
security.LicenseInvalidateEvent += async (status, message) =>
{
isLogin = status == 1;
await this.setLoginStatusAsync();
};
security.CheckLicense(this);
}
catch
{
Application.Exit();
}
fileDroper = new FileDropHandler(this);
try
{
// Disable layout load animation effects
workspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False;
WorkspaceManager.SetSerializationEnabled(this.ribbonControl1, false);
// Load DevExpress controls' layouts from a file
if (workspaceManager1.LoadWorkspace(workspaceName1, fileLayout, true))
{
workspaceManager1.ApplyWorkspace(workspaceName1);
this.dckPanelLayer.Text = "图层";
}
InitRecentConfig();
}
finally
{
licHelp.DoLogin();
}
#if !DEBUG
CheckEnvironment();
#endif
}
/// <summary>
/// Forms the main_ shown.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void FormMain_Shown(object sender, EventArgs e)
{
LoadStartFile();
//SplashScreenManager.CloseForm(true);
ribbonStatusBar1.Visible = true;
}
/// <summary>
/// Inits the recent config.
/// </summary>
private void InitRecentConfig()
{
recentConfig = new RecentFileConfig();
this.ResetOpenMenu();
}
/// <summary>
/// 加载启动参数指定的文件async
/// </summary>
private void LoadStartFile()
{
if (string.IsNullOrEmpty(this.startFile))
{
return;
}
string ext = Path.GetExtension(this.startFile);
if (this.lstArgs.Count > 0)
{
string strVtk = this.lstArgs.Find(it => { return it.ToUpper().StartsWith("/VTK"); });
if (ext == ".xyz" && !string.IsNullOrEmpty(strVtk))
{
OpenVtkView();
return;
}
string strZC = this.lstArgs.Find(it => { return it.ToUpper().StartsWith("/ZC"); });
if (!string.IsNullOrEmpty(strZC))
{
int nSizeIndex = strZC.IndexOf(':');
if (nSizeIndex > 0)
{
ZColorWidth = Convert.ToInt32(strZC.Substring(nSizeIndex + 1));
}
//string ext = Path.GetExtension(this.startFile);
//if(ext == ".xyz")
//{
batchSetZColorFlag = true;
//}
OpenFile(this.startFile, true);
this.lstArgs.Clear();
}
}
else
{
OpenFile(this.startFile);
}
}
/// <summary>
/// Sets the VTK main mesh.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="pMesh">The p mesh.</param>
public void SetVtkMainMesh(string filePath, IntPtr pMesh)
{
CurrentForm.SetVtkMainMesh(pMesh);
CurrentForm.MainMeshProperty = CurrentForm.PropertyControl.ElementProperty;
var vtkEdit = this.CurrentForm.VtkEdit;
if (vtkEdit != null && !vtkEdit.IsDisposed)
{
vtkEdit.VtkMainMesh = pMesh;
vtkEdit.SetMainMeshProperty(CurrentForm.MainMeshProperty);
vtkEdit.PropertyControl.SelectedObject = vtkEdit.VtkProerty;
}
}
private void CloseVtkHandler(UCVtkEdit vtkEdit)
{
if (vtkEdit != null && vtkEdit.DrawEdit != null)
{
vtkEdit.DrawEdit.SetVtkMainMesh(IntPtr.Zero);
vtkEdit.DrawEdit.VtkEdit = null;
this.tabbedView1.RemoveDocument(vtkEdit);
}
}
private void ShowVtkHandler(object sender, ShowVtkEventArgs e)
{
UCDrawEdit ucdraw = CurrentForm;
if (ucdraw == null)
{
return;
}
// 按分号分割(移除空条目)
string[] segments = e.WellLayer.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
//一个二维图只打开一个三维窗口,已经打开了三维窗口的情况
if (ucdraw.VtkEdit != null)
{
ucdraw.VtkEdit.VtkProerty.InitWellTypeColors(segments);
ucdraw.VtkEdit.propertyGridControl.UpdateData();
//激活文档
this.tabbedView1.ActivateDocument(ucdraw.VtkEdit);
return;
}
Drawer drawer = ucdraw.DrawerView.ViewControl.Drawer;
// 初始化vtk控件
UCVtkEdit vtkEdit = new UCVtkEdit();
// 双向绑定
CurrentForm.VtkEdit = vtkEdit;
vtkEdit.DrawEdit = CurrentForm;
//设置主网格
vtkEdit.VtkMainMesh = CurrentForm.VtkMainMesh;
vtkEdit.SetMainMeshProperty(CurrentForm.MainMeshProperty);
vtkEdit.PropertyControl.SelectedObject = vtkEdit.VtkProerty;
// 初始化井类别颜色
vtkEdit.VtkProerty.InitWellTypeColors(segments);
vtkEdit.propertyGridControl.UpdateData();
vtkEdit.LayerPanel = this.dckPanelLayer;
vtkEdit.PropertyPanel = this.dckPanelProperty;
vtkEdit.StatisticPanel = this.dckPanelStatic;
vtkEdit.DrawerStatusBar = this.ribbonControl1.StatusBar;
var vtkdoc = this.tabbedView1.AddDocument(vtkEdit);
string fileName = Path.GetFileName(e.FilePath) + "三维";
vtkdoc.Caption = fileName;
vtkEdit.Show();
this.ribbonControl1.MergeRibbon(vtkEdit.RibbonControlMain);
this.SuspendLayout();
splashScreenManager1.ShowWaitForm();
splashScreenManager1.SetWaitFormDescription("正在打开三维...");
//初始化vtk数据树
vtkEdit.InitTreeList(e);
//显示图数据
vtkEdit.ShowDrawData(e.FilePath);
//检测是否有网络数据
IntPtr pMeshData = vtkEdit.GetMainMeshData();
if (pMeshData == IntPtr.Zero)
{
this.tabbedView1.RemoveDocument(vtkEdit);
ucdraw.VtkEdit = null;
splashScreenManager1.CloseWaitForm();
MessageBox.Show("没有网格数据!", "数据错误!");
return;
}
double rx0 = vtkEdit.GetMainMeshInfo(pMeshData, 0);
double rx1 = vtkEdit.GetMainMeshInfo(pMeshData, 1);
double ry0 = vtkEdit.GetMainMeshInfo(pMeshData, 2);
double ry1 = vtkEdit.GetMainMeshInfo(pMeshData, 3);
//IntPtr hBitmap = drawer.Geo.Draw2ImageMemory(rx0, ry1, rx1, ry0);
IntPtr hBitmap = drawer.Geo.GetKevMeshImage(pMeshData);
if (hBitmap == IntPtr.Zero)
{
this.tabbedView1.RemoveDocument(vtkEdit);
ucdraw.VtkEdit = null;
splashScreenManager1.CloseWaitForm();
MessageBox.Show("图像显示比例过大,无法生成!", "数据错误!");
return;
}
vtkEdit.UpdateMeshBitmap(hBitmap, rx0, ry1, Math.Abs(rx0 - rx1), Math.Abs(ry0 - ry1));
drawer.Geo.DeleteImage(hBitmap);
// 加载三维配置信息
ucdraw.SetVtkSettings(vtkEdit);
ucdraw.VtkEdit.UpdateWellTypeColor();
if (tabbedView1.OtherDocumentActivated == null)
{
tabbedView1.OtherDocumentActivated += vtkEdit.AfterDocumentActived;
}
splashScreenManager1.CloseWaitForm();
this.ResumeLayout();
}
/// <summary>
/// 新建文件
/// </summary>
private void OpenVtkView()
{
//documentIndex++;
//string strFile = $"新建文件{documentIndex}";
//UCDrawEdit drawEdit = new UCDrawEdit();
//drawEdit.LayerPanel = this.dckPanelLayer;
//drawEdit.PropertyPanel = this.dckPanelProperty;
//drawEdit.StatisticPanel = this.dckPanelStatic;
//drawEdit.DrawerStatusBar = this.ribbonControl1.StatusBar;
//drawEdit.EnableMeshPack = DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable;
//drawEdit.ZColorWidth = this.ZColorWidth;
//if (drawEdit.CreateNewFile(strFile) == false)
//{
// documentIndex--;
// return;
//}
//drawEdit.NewFileSavedEvent += new NewFileSavedHandler((fileName) =>
//{
// SetRecent(fileName);
//});
//var doc = this.tabbedView1.AddDocument(drawEdit);
//drawEdit.Show();
//doc.Caption = drawEdit.Text;
//this.ribbonControl1.MergeRibbon(drawEdit.ribbonMain);
UCVtkEdit vtkChild = new UCVtkEdit();
vtkChild.LayerPanel = this.dckPanelLayer;
vtkChild.PropertyPanel = this.dckPanelProperty;
vtkChild.StatisticPanel = this.dckPanelStatic;
vtkChild.DrawerStatusBar = this.ribbonControl1.StatusBar;
this.ribbonControl1.MergeRibbon(vtkChild.RibbonControlMain);
var vtkdoc = this.tabbedView1.AddDocument(vtkChild);
vtkChild.Show();
vtkChild.ClearVtkShowEventHandler();
splashScreenManager1.ShowWaitForm();
splashScreenManager1.SetWaitFormDescription("正在打开三维...");
this.SuspendLayout();
vtkChild.VtkShowEventHandler += (sender, e) =>
{
splashScreenManager1.CloseWaitForm();
};
vtkChild.ShowXyzData(this.startFile);
vtkdoc.Caption = this.startFile + "三维";
}
/// <summary>
/// Forms the main_ form closed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
//Save DevExpress controls' layouts to a file
workspaceManager1.CaptureWorkspace(workspaceName1, true);
workspaceManager1.SaveWorkspace(workspaceName1, fileLayout, true);
licHelp.LogOut();
}
/// <summary>
/// 合并子窗体控件
/// </summary>
/// <param name="sender">ribbon控件</param>
/// <param name="e">事件参数</param>
private void ribbonControl1_Merge(object sender, DevExpress.XtraBars.Ribbon.RibbonMergeEventArgs e)
{
//RibbonControl parentRRibbon = sender as RibbonControl;
RibbonControl childRibbon = e.MergedChild;
childRibbon.Dock = DockStyle.None;
if (childRibbon.Parent is UCDrawEdit)
{
UCDrawEdit child = childRibbon.Parent as UCDrawEdit;
child.ViewGroupVisible = false;
child.MergePanels();
if (btnViewProperty.Checked != child.PropertyPanelVisible)
{
btnViewProperty.Checked = child.PropertyPanelVisible;
}
if (btnViewLayer.Checked != child.PropertyPanelVisible)
{
btnViewLayer.Checked = child.PropertyPanelVisible;
}
if (btnViewStatic.Checked != child.StatisticPanelVisible)
{
btnViewStatic.Checked = child.StatisticPanelVisible;
}
}
else if (childRibbon.Parent is UCVtkEdit)
{
UCVtkEdit child = childRibbon.Parent as UCVtkEdit;
child.ViewGroupVisible = false;
child.MergePanels();
if (btnViewProperty.Checked != child.PropertyPanelVisible)
{
btnViewProperty.Checked = child.PropertyPanelVisible;
}
if (btnViewLayer.Checked != child.PropertyPanelVisible)
{
btnViewLayer.Checked = child.PropertyPanelVisible;
}
if (btnViewStatic.Checked != child.StatisticPanelVisible)
{
btnViewStatic.Checked = child.StatisticPanelVisible;
}
}
}
/// <summary>
/// ribbons the control1_ un merge.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ribbonControl1_UnMerge(object sender, RibbonMergeEventArgs e)
{
try
{
RibbonControl childRibbon = e.MergedChild;
if (childRibbon.Parent is UCDrawEdit)
{
UCDrawEdit child = childRibbon.Parent as UCDrawEdit;
child.UnMergePanels();
}
else if (childRibbon.Parent is UCVtkEdit)
{
UCVtkEdit child = childRibbon.Parent as UCVtkEdit;
child.UnMergePanels();
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
/// <summary>
/// ribbons the control1_ show customization menu.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ribbonControl1_ShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e)
{
e.ShowCustomizationMenu = false;
}
/// <summary>
/// 激活图件
/// </summary>
/// <param name="fullFile">名称</param>
private bool ActiveDoc(string fullFile)
{
BaseDocument frmDoc =
this.docManager.View.Documents.FirstOrDefault(it =>
{
if (it.Control is UCDrawEdit child)
{
if (fullFile.Equals(child.FileFullName, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
}
return false;
});
if (frmDoc != null)
{
this.docManager.View.Controller.Activate(frmDoc);
return true;
}
return false;
}
/// <summary>
/// 打开文件
/// </summary>
/// <param name="sender">事件按钮</param>
/// <param name="e">事件参数</param>
private void btnFileOpen_ItemClick(object sender, ItemClickEventArgs e)
{
string[] fileNames = DocHelper.SelectMutiFiles();
foreach (var item in fileNames)
{
if (!string.IsNullOrEmpty(item))
{
OpenFile(item);
}
}
}
private void TryRecoverFileWhenUnexpectedShutdown(UCDrawEdit drawEdit, string filePath)
{
if (GeoSigmaXY.SigmaDocBackupExists(filePath))
{
var result = MessageBox.Show("检测到文件异常关闭,是否恢复?", "恢复选项"
, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
drawEdit.DrawerView.ViewControl.Drawer.RecoverMode = true;
}
}
}
/// <summary>
/// 打开文件
/// </summary>
/// <param name="strFile">文件路径</param>
/// <param name="setZColor">是否以方块方式显示散点数据</param>
//private Task<bool> OpenFile(string strFile, bool setZColor = false)
private bool OpenFile(string strFile, bool setZColor = false)
{
// 已存在的文件仅激活
if (this.ActiveDoc(strFile))
{
return true;
//return Task.FromResult(true);
}
try
{
bool bOpend = false;
UCDrawEdit drawerChild = new UCDrawEdit();
drawerChild.OpenNewFile += OnOpenNewFile;
if (batchSetZColorFlag)
{
drawerChild.BatchSetZColorFlag = batchSetZColorFlag;
batchSetZColorFlag = !batchSetZColorFlag;
}
drawerChild.LayerPanel = this.dckPanelLayer;
drawerChild.PropertyPanel = this.dckPanelProperty;
drawerChild.CreateMapPanel = this.dcpCreateMap;
drawerChild.StatisticPanel = this.dckPanelStatic;
drawerChild.DrawerStatusBar = this.ribbonControl1.StatusBar;
TryRecoverFileWhenUnexpectedShutdown(drawerChild, strFile);
splashScreenManager1.ShowWaitForm();
splashScreenManager1.SetWaitFormDescription("正在打开文件...");
this.SuspendLayout();
// drawerChild.ribbonMain.Visible = false;
drawerChild.ZColorWidth = this.ZColorWidth;
drawerChild.EnableMeshPack = DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable;
bOpend = drawerChild.OpenFile(strFile, setZColor);
if (bOpend == false)
{
splashScreenManager1.CloseWaitForm();
// frmChild.Close();
MessageBox.Show($"打开文件{strFile}失败", "打开");
//return Task.FromResult(false);
return false;
}
var doc = this.tabbedView1.AddDocument(drawerChild);
// 标签页上显示短名称
doc.Caption = TabName(drawerChild.Text);
// 光标移动上去时显示全名
if (doc is Document document)
{
document.Tooltip = drawerChild.Text;
}
this.ribbonControl1.MergeRibbon(drawerChild.ribbonMain, false);
drawerChild.NewFileSavedEvent += new NewFileSavedHandler((fileName) =>
{
SetRecent(fileName);
});
// 打开文件时,三维初始化设置
drawerChild.DrawerView.ViewControl.Drawer.SetVtkMainMeshEvent += new SetVtkMainMeshHandler((filePath, pMesh) =>
{
SetVtkMainMesh(filePath, pMesh);
});
drawerChild.ShowVtkEvent += new ShowVtkEventHandler(ShowVtkHandler);
drawerChild.CloseVtkEvent = new CloseVtkHandler(CloseVtkHandler);
// 加载主网格与图层设置
drawerChild.LoadVtkMainMesh();
drawerChild.LoadLayerSetting();
SetRecent(strFile);
splashScreenManager1.CloseWaitForm();
#if DEBUG
this.TestEventHandler(drawerChild);
#endif
//return Task.FromResult(true);
return true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
//return Task.FromResult(false);
return false;
}
finally
{
this.ResumeLayout();
}
}
/// <summary>
/// 获取 Tab 页显示名称,如果太长会被截断,防止一个标签页占非常长的位置
/// </summary>
/// <param name="caption">要显示的目标名称</param>
/// <returns>最终会被显示的名称</returns>
private string TabName(string caption)
{
return caption.Length > TABMAXLENGTH ? caption.Substring(0, TABMAXLENGTH) : caption;
}
/// <summary>
/// Tests the event handler.
/// </summary>
/// <param name="editor">The editor.</param>
private void TestEventHandler(UCDrawEdit editor)
{
editor.DrawerView.InsertCommonCommand("克隆到其它井组",
new EventHandler((obj, args) =>
{
SelectedElementsArgs selArgs = args as SelectedElementsArgs;
if (selArgs == null)
{ return; }
List<SigmaDrawerElement.DrawerElementProperty> elements = selArgs.Elements;
if (elements != null)
{
//editor.DrawerView.GetSelectedElements()[0];
foreach (DrawerElementProperty ele in elements)
{
//ele.Element
//strbContent.AppendLine(ele.Element.ToDfdString());
}
}
}), true, DrawElementType.ELEMENT_WELL_GROUP);
}
private int documentIndex = 0;
/// <summary>
/// 新建文件
/// </summary>
/// <param name="sender">按钮</param>
/// <param name="e">事件参数</param>
private void btnFileNew_ItemClick(object sender, ItemClickEventArgs e)
{
documentIndex++;
string strFile = $"新建文件{documentIndex}";
UCDrawEdit frmChild = new UCDrawEdit();
frmChild.OpenNewFile += OnOpenNewFile;
frmChild.LayerPanel = this.dckPanelLayer;
frmChild.CreateMapPanel = this.dcpCreateMap;
frmChild.PropertyPanel = this.dckPanelProperty;
frmChild.StatisticPanel = this.dckPanelStatic;
frmChild.DrawerStatusBar = this.ribbonControl1.StatusBar;
frmChild.EnableMeshPack = DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable;
frmChild.ZColorWidth = this.ZColorWidth;
if (frmChild.CreateNewFile(strFile) == false)
{
documentIndex--;
return;
}
frmChild.NewFileSavedEvent += new NewFileSavedHandler((fileName) =>
{
SetRecent(fileName);
});
// 加载子窗体布局
frmChild.Show();
var doc = this.tabbedView1.AddDocument(frmChild);
doc.Caption = frmChild.Text;
// 打开文件时,三维初始化设置
frmChild.DrawerView.ViewControl.Drawer.SetVtkMainMeshEvent += new SetVtkMainMeshHandler((filePath, pMesh) =>
{
SetVtkMainMesh(filePath, pMesh);
});
frmChild.ShowVtkEvent += new ShowVtkEventHandler(ShowVtkHandler);
frmChild.CloseVtkEvent = new CloseVtkHandler(CloseVtkHandler);
this.ribbonControl1.MergeRibbon(frmChild.ribbonMain);
}
/// <summary>
/// Files the closed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void FileClosed(object sender, EventArgs e)
{
if (sender is UCDrawEdit child)
{
child.ShowStatisticResult -= ShowStatic;
}
}
/// <summary>
/// 显示统计面板.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void ShowStatic(object sender, EventArgs e)
{
try
{
this.dckPanelStatic.Visibility = DockVisibility.Visible;
}
finally
{
}
}
/// <summary>
/// Sets the recent.
/// </summary>
/// <param name="file">The file.</param>
private void SetRecent(string file)
{
if (string.IsNullOrEmpty(file))
{
return;
}
if (this.recentConfig.AddTop(file) == false)
{
return;
}
ResetOpenMenu();
this.recentConfig.Save();
}
/// <summary>
/// Resets the open menu.
/// </summary>
private void ResetOpenMenu()
{
this.ppmOpen.ClearLinks();
for (int i = 0; i < this.recentConfig.Files.Count; i++)
{
string strFile = this.recentConfig.Files[i];
BarButtonItem item = new BarButtonItem();
item.Caption = strFile;
item.ImageOptions.Image = imgsMain.Images["fileNew"];
item.ItemClick += new ItemClickEventHandler(async (object sender, ItemClickEventArgs e) =>
{
OpenFile(strFile);
});
this.ppmOpen.AddItem(item);
}
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="sender">按钮</param>
/// <param name="e">事件参数</param>
private void btnSaveMain_ItemClick(object sender, ItemClickEventArgs e)
{
if (CurrentForm == null)
{
return;
}
if (CurrentForm.SaveFile())
{
SetRecent(CurrentForm.FileFullName);
}
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="sender">事件按钮</param>
/// <param name="e">事件参数</param>
private void btnSaveAs_ItemClick(object sender, ItemClickEventArgs e)
{
if (CurrentForm == null)
{
return;
}
if (CurrentForm.SaveAs())
{
SetRecent(CurrentForm.FileFullName);
}
}
/// <summary>
/// 符号管理
/// </summary>
/// <param name="sender">按钮</param>
/// <param name="e">事件参数</param>
private void btnSymbolManager_ItemClick(object sender, ItemClickEventArgs e)
{
// Assembly.GetExecutingAssembly().Location
DirectoryInfo symbolFilePath = Directory.GetParent(Application.StartupPath);
string symbolFile = Path.Combine(symbolFilePath.FullName, "Symbol");
if (CurrentForm == null)
{
SymbolLibInterface.SymbolHelp.ShowSymbolUI(null, symbolFile);
}
else
{
SymbolLibInterface.SymbolHelp.ShowSymbolUI(CurrentForm.DrawerView, symbolFile);
}
}
/// <summary>
/// 选项按纽
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件参数</param>
private void btnSetting_Clicked(object sender, ItemClickEventArgs e)
{
//FormGlobalOptions formGlobal = new FormGlobalOptions();
FrmOptions formGlobal = new FrmOptions();
formGlobal.ShowDialog(this);
}
/// <summary>
/// 图层面板
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件参数</param>
private void btnViewLayer_CheckedChanged(object sender, ItemClickEventArgs e)
{
try
{
if (CurrentForm == null)
{
if (btnViewLayer.Checked)
{
this.dckPanelLayer.Visibility = DockVisibility.AutoHide;
}
else
{
if (!this.dockManager1.AutoHideContainers[0].Controls.Contains(dckPanelLayer))
{
//this.dockManager1.AutoHideContainers[0].Controls.Add(dckPanelLayer);
this.dockManager1.AddPanel(DockingStyle.Left, dckPanelLayer);
}
this.dckPanelLayer.Visibility = DockVisibility.Visible;
}
}
else
{
CurrentForm.LayerPanelVisible = btnViewLayer.Checked;
}
}
finally
{
}
}
/// <summary>
/// 属性面板可见性设置
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件参数</param>
private void btnViewProperty_CheckedChanged(object sender, ItemClickEventArgs e)
{
//LayerPanelVIsible = btnViewProperty.Checked;
try
{
if (CurrentForm == null)
{
if (btnViewProperty.Checked)
{
this.dckPanelProperty.Visibility = DockVisibility.AutoHide;
}
else
{
this.dckPanelProperty.Visibility = DockVisibility.Visible;
this.dckPanelProperty.ParentPanel.ActiveChild = this.dckPanelProperty;
}
}
else
{
CurrentForm.PropertyPanelVisible = btnViewProperty.Checked;
}
}
catch
{
}
}
/// <summary>
/// 统计面板
/// </summary>
/// <param name="sender">菜单项</param>
/// <param name="e">事件参数</param>
private void btnViewStatic_CheckedChanged(object sender, ItemClickEventArgs e)
{
try
{
if (CurrentForm == null)
{
if (btnViewStatic.Checked)
{
this.dckPanelStatic.Visibility = DockVisibility.AutoHide;
}
else
{
this.dckPanelStatic.Visibility = DockVisibility.Visible;
}
}
else
{
CurrentForm.StatisticPanelVisible = btnViewStatic.Checked;
}
}
catch
{
}
}
/// <summary>
/// 默认消除网格锯齿
/// </summary>
/// <param name="sender">事件菜单</param>
/// <param name="e">事件参数</param>
private void bbtnMeshAntiAlias_CheckedChanged(object sender, ItemClickEventArgs e)
{
Task.Run(() =>
{
DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable = bbtnMeshAntiAlias.Checked;
DrawerGlobalConfig.Instance.Save();
});
}
/// <summary>
/// 拖放文件
/// </summary>
/// <param name="sender">窗体</param>
/// <param name="e">事件参数</param>
private void FormMain_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
for (int i = 0; i < filePaths.Length; i++)
{
// 数组中的每个元素都是一个文件或目录的完整路径
string path = filePaths[i];
if (File.Exists(path))
{
OpenFile(path, false);
}
}
}
}
/// <summary>
/// 拖放进入事件
/// </summary>
/// <param name="sender">窗体</param>
/// <param name="e">事件参数</param>
private void FormMain_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
// 管理员模式处理文件拖放,不要删除,有作用
string[] filePaths = (string[])e.Data.GetData(typeof(string[]));
for (int i = 0; i < filePaths.Length; i++)
{
// 数组中的每个元素都是一个文件或目录的完整路径
string path = filePaths[i];
if (File.Exists(path))
{
OpenFile(path, false);
}
}
}
/// <summary>
/// Gets the neighbor document.
/// </summary>
/// <param name="documents">The documents.</param>
/// <param name="document">The document.</param>
/// <returns>A BaseDocument.</returns>
protected BaseDocument GetNeighborDocument(BaseDocument[] documents, BaseDocument document)
{
int nLength = documents.Length;
if (nLength == 1)
{
return null;
}
int nIndex = Array.IndexOf<BaseDocument>(documents, document);
if (nIndex == nLength - 1)
{
return documents[nIndex - 1];
}
return documents[nIndex + 1];
}
private bool isCloseCanceled = false;
/// <summary>
/// tabbeds the view1_ document closing.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void tabbedView1_DocumentClosing(object sender, DocumentCancelEventArgs e)
{
BaseDocument doc = e.Document as BaseDocument;
if (doc.Control is UCDrawEdit child)
{
FormClosingEventArgs ea = new FormClosingEventArgs(CloseReason.MdiFormClosing, false);
child.UCDrawEdit_FormClosing(sender, ea);
isCloseCanceled = e.Cancel = ea.Cancel;
if (e.Cancel)
{
//child.UnMergePanels();
if (doc.IsActive)
{
this.ribbonControl1.MergeRibbon(child.ribbonMain, false);
}
}
else
{
child.SaveLayout();
}
}
}
/// <summary>
/// 窗体关闭事件
/// </summary>
/// <param name="sender">窗体</param>
/// <param name="e">事件参数</param>
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < tabbedView1.Documents.Count; i++)
{
tabbedView1.Controller.Close(tabbedView1.Documents[i]);
if (isCloseCanceled == true)
{
e.Cancel = true;
break;
}
}
if (e.Cancel != true)
{
this.security?.CloseLicense();
}
}
/// <summary>
/// 关于.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ItemClickEventArgs"/> instance containing the event data.</param>
private void btnAbout_ItemClick(object sender, ItemClickEventArgs e)
{
FrmAbout frmAbout = new FrmAbout();
frmAbout.Show(this);
}
private void OnOpenNewFile(string filePath)
{
foreach (var doc in tabbedView1.Documents)
{
if (doc.Control is UCDrawEdit ucDrawEdit && ucDrawEdit.FileFullName == filePath)
{
ucDrawEdit.OpenFile(filePath);
return;
}
}
OpenFile(filePath);
}
/// <summary>
/// 检测运行环境
/// </summary>
private void CheckEnvironment()
{
try
{
CheckBaseDependencies();
CheckGriddingDependencies();
CheckMeshProcessDependencies();
CheckMeshSymbolLibManagerDependencies();
}
catch (Exception ex)
{
MessageBox.Show($"运行错误(E003){ex.Message}, 请联系服务人员!");
Application.Exit();
}
}
private void CheckBaseDependencies()
{
try
{
GeoSigmaLib.GeoSigmaLibInit();
}
catch
{
throw new DllNotFoundException("加载基础库失败");
}
}
private void CheckGriddingDependencies()
{
string[] griddingDependencies =
{
// 生成等值线,和 GridModel.exe 配合使用
"CurveModel.exe",
// 最小张力
"GridModel.exe",
// 最小曲率
"SurfaceGrid.exe",
// 快速网格化
"QuikGridCS.dll",
// 反距离加权
"ModelCreateIDW.dll",
// 自然临近
"NaturalNeighbor.dll",
};
foreach (string depend in griddingDependencies)
{
if (!File.Exists(Path.Combine(Application.StartupPath, depend)))
{
throw new FileNotFoundException($"依赖的 {depend} 文件未找到");
}
if (depend.EndsWith(".dll"))
{
ValidateDll(depend);
}
}
}
private void CheckMeshProcessDependencies()
{
string fileName = "MeshProcess.exe";
if (!File.Exists(Path.Combine(Application.StartupPath, fileName)))
{
throw new FileNotFoundException($"依赖的 {fileName} 文件未找到");
}
}
private void CheckMeshSymbolLibManagerDependencies()
{
string fileName = "SymbolLibManager.exe";
if (!File.Exists(Path.Combine(Application.StartupPath, fileName)))
{
throw new FileNotFoundException($"依赖的 {fileName} 文件未找到");
}
}
private void FormMain_MdiChildActivate(object sender, EventArgs e)
{
//string strText = this.Text;
}
}
}