//
// Copyright (c) PlaceholderCompany. All rights reserved.
//
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;
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 AI.AgentIntegration;
using SigmaDrawerElement;
using UCDraw;
namespace PcgDrawR
{
///
/// 软件主窗体.
///
public partial class FormMain : RibbonForm
{
///
/// Tab 页签最长长度
///
private const int TABMAXLENGTH = 10;
private readonly string fileLayout = Path.Combine(Application.StartupPath, "layout.xml");
private readonly string workspaceName1 = "MyLayout";
private readonly LicHelp licHelp;
// 启动参数
private readonly string startFile = string.Empty;
private readonly List lstArgs = new List();
private bool batchSetZColorFlag = false;
private Security security;
private bool isDQLogin = false;
private bool isLogin = false;
///
/// Gets or sets the z color width.
///
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(), dllName);
}
}
finally
{
if (hModule != IntPtr.Zero)
{
FreeLibrary(hModule);
}
}
}
///
/// Initializes a new instance of the class.
///
public FormMain()
{
this.InitializeComponent();
this.barButtonItem8.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
this.tabbedView1.Ribbon = this.ribbonControl1;
if (this.tabbedView1.InvalidDocumentTypes == null)
{
this.tabbedView1.InvalidDocumentTypes = new List();
}
this.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
this.dckPanelProperty.ClosedPanel += (sender, e) =>
{
this.btnViewProperty.Checked = false;
};
// 关闭图层时把对应属性改为 unchecked
this.dckPanelLayer.ClosedPanel += (sender, e) =>
{
this.btnViewLayer.Checked = false;
};
// 关闭统计元素时把对应属性改为 unchecked
this.dckPanelStatic.ClosedPanel += (sender, e) =>
{
this.btnViewStatic.Checked = false;
};
this.ribbonControl1.OptionsMenuMinWidth = 0;
this.KeyPreview = true;
NativeLibraryPathRegistrar.Register();
}
///
/// 获取一下当前 TabbedView,方便后续进行操作
///
public DrawerTabbedView TabbedView1 => tabbedView1;
///
/// 设置登录状态.
///
/// The status code.
private async Task setDQLoginStatusAsync(int statusCode)
{
if (this.InvokeRequired)
{
// 使用 BeginInvoke 异步调用
this.Invoke(
new Action(async (code) =>
await this.setDQLoginStatusAsync(code)), statusCode);
return;
}
if (statusCode == 0)
{ // 登录成功
this.isDQLogin = true;
await this.setLoginStatusAsync();
}
else if (statusCode == 2)
{
// 不需要登录大庆服务器,不设置登录状态。
this.isDQLogin = true;
}
else
{ // 登录失败
this.isDQLogin = false;
await this.setLoginStatusAsync();
// 大庆版本
MessageBox.Show(this, "登录失败,请尽快保存文件!", "登录许可失败");
}
}
///
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)
{
this.btnFileOpen_ItemClick(this, null);
}
else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
{
this.btnFileNew_ItemClick(this, null);
}
}
///
/// Initializes a new instance of the class.
///
/// 参数 /ZC-按块显示散点Z值
public FormMain(string[] args)
: this()
{
if (args.Length > 0)
{
this.startFile = args[0].Trim();
}
if (args.Length > 1)
{
this.lstArgs.Clear();
for (int i = 1; i < args.Length; i++)
{
this.lstArgs.Add(args[i]);
}
}
this.bbtnMeshAntiAlias.Checked = DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable;
this.ribbonControl1.OptionsMenuMinWidth = 0;
// 注册
AppController.Instance.MainForm = this;
NativeLibraryPathRegistrar.Register();
}
protected override void OnLoad(EventArgs e)
{
// 必须在事件循环启动之后调用
UiDispatcher.Instance.Initialize(SynchronizationContext.Current);
base.OnLoad(e);
}
///
/// Gets 当前激活的文档
///
private UCDrawEdit CurrentForm
{
get
{
if (this.tabbedView1.ActiveDocument != null)
{
return this.tabbedView1.ActiveDocument.Control as UCDrawEdit;
}
else if (this.tabbedView1.ActiveFloatDocument != null)
{
return this.tabbedView1.ActiveFloatDocument.Control as UCDrawEdit;
}
return null;
}
}
///
/// Gets 所有打开的标签页
///
public List OpenTabs
{
get
{
return tabbedView1
.Documents
.Cast()
.Select(document => document.Caption)
.ToList();
}
}
///
/// Gets 当前激活标签
///
public string ActivateTab
{
get
{
return tabbedView1.ActiveDocument?.Caption;
}
set
{
if (value != null)
{
foreach (Document document in tabbedView1.Documents.Cast())
{
if (document.Caption == value)
{
tabbedView1.ActivateDocument(document.Control);
break;
}
}
}
}
}
///
/// 保存所有文件
///
public void SaveAll()
{
foreach (Document document in tabbedView1.Documents.Cast())
{
if (document.Control is UCDrawEdit edit)
{
edit.SaveFile();
}
}
}
///
/// 重新加载当前文件
///
public void ReloadFile()
{
if (tabbedView1.ActiveDocument != null && tabbedView1.ActiveDocument.Control is UCDrawEdit edit)
{
edit.DrawerView.ReloadFile();
}
}
///
/// Gets or sets drawerGlobalConfig
///
public DrawerGlobalConfig DrawerGlobalConfig { get; set; } = DrawerGlobalConfig.Instance;
private FileDropHandler fileDroper;
///
/// Forms the main_ load.
///
/// The sender.
/// The e.
private async void FormMain_Load(object sender, EventArgs e)
{
try
{
if (Security.CurentLicenseInfo.IsNetServer == true)
{
this.isLogin = false;
await this.setLoginStatusAsync();
}
else
{
this.isLogin = true;
}
this.security = new Security();
this.security.LicenseInvalidateEvent += async (status, message) =>
{
this.isLogin = status == 1;
await this.setLoginStatusAsync();
};
this.security.CheckLicense(this);
}
catch
{
Application.Exit();
}
this.fileDroper = new FileDropHandler(this);
try
{
// Disable layout load animation effects
this.workspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False;
WorkspaceManager.SetSerializationEnabled(this.ribbonControl1, false);
// Load DevExpress controls' layouts from a file
if (this.workspaceManager1.LoadWorkspace(this.workspaceName1, this.fileLayout, true))
{
this.workspaceManager1.ApplyWorkspace(this.workspaceName1);
this.dckPanelLayer.Text = "图层";
}
this.InitRecentConfig();
}
finally
{
this.licHelp.DoLogin();
}
#if !DEBUG
CheckEnvironment();
#endif
}
private async Task setLoginStatusAsync()
{
bool isLoggedIn = this.isDQLogin && this.isLogin;
string tooltipText = isLoggedIn ? "已登录" : "未登录";
string resourceName = isLoggedIn
? "bsiLoginInfo.ImageOptions.SvgImage"
: "bsiLoginInfo.ImageOptions.DisabledSvgImage";
// 更新非资源依赖的UI部分
this.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线程)
this.bsiLoginInfo.ImageOptions.SvgImage = (DevExpress.Utils.Svg.SvgImage)svgImage;
}
///
/// Forms the main_ shown.
///
/// The sender.
/// The e.
private void FormMain_Shown(object sender, EventArgs e)
{
this.LoadStartFile();
// SplashScreenManager.CloseForm(true);
this.ribbonStatusBar1.Visible = true;
}
///
/// Inits the recent config.
///
private void InitRecentConfig()
{
this.recentConfig = new RecentFileConfig();
this.ResetOpenMenu();
}
///
/// 加载启动参数指定的文件async
///
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))
{
this.OpenVtkView();
return;
}
string strZC = this.lstArgs.Find(it => { return it.ToUpper().StartsWith("/ZC"); });
if (!string.IsNullOrEmpty(strZC))
{
int nSizeIndex = strZC.IndexOf(':');
if (nSizeIndex > 0)
{
this.ZColorWidth = Convert.ToInt32(strZC.Substring(nSizeIndex + 1));
}
// string ext = Path.GetExtension(this.startFile);
// if(ext == ".xyz")
// {
this.batchSetZColorFlag = true;
// }
this.OpenFile(this.startFile, true);
this.lstArgs.Clear();
}
}
else
{
this.OpenFile(this.startFile);
}
}
///
/// Sets the VTK main mesh.
///
/// The file path.
/// The p mesh.
public void SetVtkMainMesh(string filePath, IntPtr pMesh)
{
this.CurrentForm.SetVtkMainMesh(pMesh);
this.CurrentForm.MainMeshProperty = this.CurrentForm.PropertyControl.ElementProperty;
var vtkEdit = this.CurrentForm.VtkEdit;
if (vtkEdit != null && !vtkEdit.IsDisposed)
{
vtkEdit.VtkMainMesh = pMesh;
vtkEdit.SetMainMeshProperty(this.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 tabbedView1_DocumentActivated(object sender, DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e)
{
if (e.Document == null) return;
// 判断当前激活的文档内容是否为三维编辑控件
if (e.Document.Control is UCVtkEdit)
{
// 处于三维模式:屏蔽成图
if (this.dcpCreateMap != null)
{
this.dcpCreateMap.Visibility = DockVisibility.Hidden;
}
}
else
{
// 切换到了其他文档(如二维图):还原成图并选中属性
if (this.dcpCreateMap != null && this.dckPanelProperty != null)
{
// 还原成图面板可见性
this.dcpCreateMap.Visibility = DockVisibility.Visible;
// 确保属性面板可见
this.dckPanelProperty.Visibility = DockVisibility.Visible;
// 切换 Tab 到属性页
if (this.dckPanelProperty.ParentPanel != null)
{
this.dckPanelProperty.ParentPanel.ActiveChild = this.dckPanelProperty;
}
}
}
}
private void ShowVtkHandler(object sender, ShowVtkEventArgs e)
{
UCDrawEdit ucdraw = this.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();
//vtkEdit.Disposed += (s, args) => {
// if (this.dcpCreateMap != null)
// {
// // 还原成图面板
// this.dcpCreateMap.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
// }
//};
// 双向绑定
this.CurrentForm.VtkEdit = vtkEdit;
vtkEdit.DrawEdit = this.CurrentForm;
// 设置主网格
vtkEdit.VtkMainMesh = this.CurrentForm.VtkMainMesh;
vtkEdit.SetMainMeshProperty(this.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();
this.splashScreenManager1.ShowWaitForm();
this.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;
this.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;
this.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();
//切换标签 是否显示成图
this.tabbedView1.DocumentActivated += tabbedView1_DocumentActivated;
if (this.tabbedView1.OtherDocumentActivated == null)
{
this.tabbedView1.OtherDocumentActivated += vtkEdit.AfterDocumentActived;
}
this.dcpCreateMap.Visibility = DockVisibility.Hidden;
this.splashScreenManager1.CloseWaitForm();
this.ResumeLayout();
}
///
/// 新建文件
///
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();
this.splashScreenManager1.ShowWaitForm();
this.splashScreenManager1.SetWaitFormDescription("正在打开三维...");
this.SuspendLayout();
vtkChild.VtkShowEventHandler += (sender, e) =>
{
this.splashScreenManager1.CloseWaitForm();
};
vtkChild.ShowXyzData(this.startFile);
vtkdoc.Caption = this.startFile + "三维";
}
///
/// Forms the main_ form closed.
///
/// The sender.
/// The e.
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
// Save DevExpress controls' layouts to a file
this.workspaceManager1.CaptureWorkspace(this.workspaceName1, true);
this.workspaceManager1.SaveWorkspace(this.workspaceName1, this.fileLayout, true);
this.licHelp.LogOut();
}
///
/// 合并子窗体控件
///
/// ribbon控件
/// 事件参数
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 (this.btnViewProperty.Checked != child.PropertyPanelVisible)
{
this.btnViewProperty.Checked = child.PropertyPanelVisible;
}
if (this.btnViewLayer.Checked != child.PropertyPanelVisible)
{
this.btnViewLayer.Checked = child.PropertyPanelVisible;
}
if (this.btnViewStatic.Checked != child.StatisticPanelVisible)
{
this.btnViewStatic.Checked = child.StatisticPanelVisible;
}
}
else if (childRibbon.Parent is UCVtkEdit)
{
UCVtkEdit child = childRibbon.Parent as UCVtkEdit;
child.ViewGroupVisible = false;
child.MergePanels();
if (this.btnViewProperty.Checked != child.PropertyPanelVisible)
{
this.btnViewProperty.Checked = child.PropertyPanelVisible;
}
if (this.btnViewLayer.Checked != child.PropertyPanelVisible)
{
this.btnViewLayer.Checked = child.PropertyPanelVisible;
}
if (this.btnViewStatic.Checked != child.StatisticPanelVisible)
{
this.btnViewStatic.Checked = child.StatisticPanelVisible;
}
}
}
///
/// ribbons the control1_ un merge.
///
/// The sender.
/// The e.
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);
}
}
///
/// ribbons the control1_ show customization menu.
///
/// The sender.
/// The e.
private void ribbonControl1_ShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e)
{
e.ShowCustomizationMenu = false;
}
///
/// 激活图件
///
/// 名称
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;
}
///
/// 打开文件
///
/// 事件按钮
/// 事件参数
private void btnFileOpen_ItemClick(object sender, ItemClickEventArgs e)
{
string[] fileNames = DocHelper.SelectMutiFiles();
foreach (var item in fileNames)
{
if (!string.IsNullOrEmpty(item))
{
this.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;
}
}
}
///
/// 打开文件
///
/// 文件路径
/// 是否以方块方式显示散点数据
// private Task OpenFile(string strFile, bool setZColor = false)
public 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 += this.OnOpenNewFile;
if (this.batchSetZColorFlag)
{
drawerChild.BatchSetZColorFlag = this.batchSetZColorFlag;
this.batchSetZColorFlag = !this.batchSetZColorFlag;
}
drawerChild.LayerPanel = this.dckPanelLayer;
drawerChild.PropertyPanel = this.dckPanelProperty;
drawerChild.CreateMapPanel = this.dcpCreateMap;
drawerChild.StatisticPanel = this.dckPanelStatic;
drawerChild.DrawerStatusBar = this.ribbonControl1.StatusBar;
this.TryRecoverFileWhenUnexpectedShutdown(drawerChild, strFile);
this.splashScreenManager1.ShowWaitForm();
this.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)
{
this.splashScreenManager1.CloseWaitForm();
// frmChild.Close();
MessageBox.Show($"打开文件{strFile}失败", "打开");
// return Task.FromResult(false);
return false;
}
var doc = this.tabbedView1.AddDocument(drawerChild);
// 标签页上显示短名称
doc.Caption = this.TabName(drawerChild.Text);
// 光标移动上去时显示全名
if (doc is Document document)
{
document.Tooltip = drawerChild.Text;
}
this.ribbonControl1.MergeRibbon(drawerChild.ribbonMain, false);
drawerChild.NewFileSavedEvent += new NewFileSavedHandler((fileName) =>
{
this.SetRecent(fileName);
});
// 打开文件时,三维初始化设置
drawerChild.DrawerView.ViewControl.Drawer.SetVtkMainMeshEvent += new SetVtkMainMeshHandler((filePath, pMesh) =>
{
this.SetVtkMainMesh(filePath, pMesh);
});
drawerChild.ShowVtkEvent += new ShowVtkEventHandler(this.ShowVtkHandler);
drawerChild.CloseVtkEvent = new CloseVtkHandler(this.CloseVtkHandler);
// 加载主网格与图层设置
drawerChild.LoadVtkMainMesh();
drawerChild.LoadLayerSetting();
this.SetRecent(strFile);
this.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();
}
}
///
/// 获取 Tab 页显示名称,如果太长会被截断,防止一个标签页占非常长的位置
///
/// 要显示的目标名称
/// 最终会被显示的名称
private string TabName(string caption)
{
return caption.Length > TABMAXLENGTH ? caption.Substring(0, TABMAXLENGTH) : caption;
}
///
/// Tests the event handler.
///
/// The editor.
private void TestEventHandler(UCDrawEdit editor)
{
editor.DrawerView.InsertCommonCommand("克隆到其它井组",
new EventHandler((obj, args) =>
{
SelectedElementsArgs selArgs = args as SelectedElementsArgs;
if (selArgs == null)
{ return; }
List 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;
///
/// 新建文件
///
/// 按钮
/// 事件参数
private void btnFileNew_ItemClick(object sender, ItemClickEventArgs e)
{
this.documentIndex++;
string strFile = $"新建文件{this.documentIndex}";
UCDrawEdit frmChild = new UCDrawEdit();
frmChild.OpenNewFile += this.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)
{
this.documentIndex--;
return;
}
frmChild.NewFileSavedEvent += new NewFileSavedHandler((fileName) =>
{
this.SetRecent(fileName);
});
// 加载子窗体布局
frmChild.Show();
var doc = this.tabbedView1.AddDocument(frmChild);
doc.Caption = frmChild.Text;
// 打开文件时,三维初始化设置
frmChild.DrawerView.ViewControl.Drawer.SetVtkMainMeshEvent += new SetVtkMainMeshHandler((filePath, pMesh) =>
{
this.SetVtkMainMesh(filePath, pMesh);
});
frmChild.ShowVtkEvent += new ShowVtkEventHandler(this.ShowVtkHandler);
frmChild.CloseVtkEvent = new CloseVtkHandler(this.CloseVtkHandler);
this.ribbonControl1.MergeRibbon(frmChild.ribbonMain);
}
///
/// Files the closed.
///
/// The sender.
/// The e.
private void FileClosed(object sender, EventArgs e)
{
if (sender is UCDrawEdit child)
{
child.ShowStatisticResult -= this.ShowStatic;
}
}
///
/// 显示统计面板.
///
/// The sender.
/// The instance containing the event data.
private void ShowStatic(object sender, EventArgs e)
{
try
{
this.dckPanelStatic.Visibility = DockVisibility.Visible;
}
finally
{
}
}
///
/// Sets the recent.
///
/// The file.
private void SetRecent(string file)
{
if (string.IsNullOrEmpty(file))
{
return;
}
if (this.recentConfig.AddTop(file) == false)
{
return;
}
this.ResetOpenMenu();
this.recentConfig.Save();
}
///
/// Resets the open menu.
///
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 = this.imgsMain.Images["fileNew"];
item.ItemClick += new ItemClickEventHandler(async (object sender, ItemClickEventArgs e) =>
{
this.OpenFile(strFile);
});
this.ppmOpen.AddItem(item);
}
}
///
/// 保存文件
///
/// 按钮
/// 事件参数
private void btnSaveMain_ItemClick(object sender, ItemClickEventArgs e)
{
if (this.CurrentForm == null)
{
return;
}
if (this.CurrentForm.SaveFile())
{
this.SetRecent(this.CurrentForm.FileFullName);
}
}
///
/// 另存为
///
/// 事件按钮
/// 事件参数
private void btnSaveAs_ItemClick(object sender, ItemClickEventArgs e)
{
if (this.CurrentForm == null)
{
return;
}
if (this.CurrentForm.SaveAs())
{
this.SetRecent(this.CurrentForm.FileFullName);
}
}
///
/// 符号管理
///
/// 按钮
/// 事件参数
private void btnSymbolManager_ItemClick(object sender, ItemClickEventArgs e)
{
//DirectoryInfo symbolFilePath = Directory.GetParent(Application.StartupPath);
string parentPath = Path.GetFullPath(Path.Combine(Application.StartupPath, ".."));
string symbolFile = Path.Combine(parentPath, "Symbol");
if (this.CurrentForm == null)
{
SymbolLibInterface.SymbolHelp.ShowSymbolUI(null, symbolFile);
}
else
{
SymbolLibInterface.SymbolHelp.ShowSymbolUI(this.CurrentForm.DrawerView, symbolFile);
}
}
///
/// 选项按纽
///
/// 菜单项
/// 事件参数
private void btnSetting_Clicked(object sender, ItemClickEventArgs e)
{
// FormGlobalOptions formGlobal = new FormGlobalOptions();
FrmOptions formGlobal = new FrmOptions();
formGlobal.ShowDialog(this);
}
///
/// 图层面板
///
/// 菜单项
/// 事件参数
private void btnViewLayer_CheckedChanged(object sender, ItemClickEventArgs e)
{
try
{
if (this.CurrentForm == null)
{
if (this.btnViewLayer.Checked)
{
this.dckPanelLayer.Visibility = DockVisibility.AutoHide;
}
else
{
if (!this.dockManager1.AutoHideContainers[0].Controls.Contains(this.dckPanelLayer))
{
// this.dockManager1.AutoHideContainers[0].Controls.Add(dckPanelLayer);
this.dockManager1.AddPanel(DockingStyle.Left, this.dckPanelLayer);
}
this.dckPanelLayer.Visibility = DockVisibility.Visible;
}
}
else
{
this.CurrentForm.LayerPanelVisible = this.btnViewLayer.Checked;
}
}
finally
{
}
}
///
/// 属性面板可见性设置
///
/// 菜单项
/// 事件参数
private void btnViewProperty_CheckedChanged(object sender, ItemClickEventArgs e)
{
// LayerPanelVIsible = btnViewProperty.Checked;
try
{
if (this.CurrentForm == null)
{
if (this.btnViewProperty.Checked)
{
this.dckPanelProperty.Visibility = DockVisibility.AutoHide;
}
else
{
this.dckPanelProperty.Visibility = DockVisibility.Visible;
this.dckPanelProperty.ParentPanel.ActiveChild = this.dckPanelProperty;
}
}
else
{
this.CurrentForm.PropertyPanelVisible = this.btnViewProperty.Checked;
}
}
catch
{
}
}
///
/// 统计面板
///
/// 菜单项
/// 事件参数
private void btnViewStatic_CheckedChanged(object sender, ItemClickEventArgs e)
{
try
{
if (this.CurrentForm == null)
{
if (this.btnViewStatic.Checked)
{
this.dckPanelStatic.Visibility = DockVisibility.AutoHide;
}
else
{
this.dckPanelStatic.Visibility = DockVisibility.Visible;
}
}
else
{
this.CurrentForm.StatisticPanelVisible = this.btnViewStatic.Checked;
}
}
catch
{
}
}
///
/// 默认消除网格锯齿
///
/// 事件菜单
/// 事件参数
private void bbtnMeshAntiAlias_CheckedChanged(object sender, ItemClickEventArgs e)
{
Task.Run(() =>
{
DrawerGlobalConfig.Instance.MeshNode.AntiAliasingEnable = this.bbtnMeshAntiAlias.Checked;
DrawerGlobalConfig.Instance.Save();
});
}
///
/// 拖放文件
///
/// 窗体
/// 事件参数
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))
{
this.OpenFile(path, false);
}
}
}
}
///
/// 拖放进入事件
///
/// 窗体
/// 事件参数
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))
{
this.OpenFile(path, false);
}
}
}
///
/// Gets the neighbor document.
///
/// The documents.
/// The document.
/// A BaseDocument.
protected BaseDocument GetNeighborDocument(BaseDocument[] documents, BaseDocument document)
{
int nLength = documents.Length;
if (nLength == 1)
{
return null;
}
int nIndex = Array.IndexOf(documents, document);
if (nIndex == nLength - 1)
{
return documents[nIndex - 1];
}
return documents[nIndex + 1];
}
private bool isCloseCanceled = false;
private DrawerTabbedView tabbedView11;
///
/// tabbeds the view1_ document closing.
///
/// The sender.
/// The e.
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);
this.isCloseCanceled = e.Cancel = ea.Cancel;
if (e.Cancel)
{
// child.UnMergePanels();
if (doc.IsActive)
{
this.ribbonControl1.MergeRibbon(child.ribbonMain, false);
}
}
else
{
child.SaveLayout();
}
}
}
///
/// 窗体关闭事件
///
/// 窗体
/// 事件参数
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < this.tabbedView1.Documents.Count; i++)
{
this.tabbedView1.Controller.Close(this.tabbedView1.Documents[i]);
if (this.isCloseCanceled == true)
{
e.Cancel = true;
break;
}
}
if (e.Cancel != true)
{
this.security?.CloseLicense();
}
}
///
/// 关于.
///
/// The source of the event.
/// The instance containing the event data.
private void btnAbout_ItemClick(object sender, ItemClickEventArgs e)
{
FrmAbout frmAbout = new FrmAbout();
frmAbout.Show(this);
}
private void OnOpenNewFile(string filePath)
{
foreach (var doc in this.tabbedView1.Documents)
{
if (doc.Control is UCDrawEdit ucDrawEdit && ucDrawEdit.FileFullName == filePath)
{
ucDrawEdit.OpenFile(filePath);
return;
}
}
this.OpenFile(filePath);
}
///
/// 检测运行环境
///
private void CheckEnvironment()
{
try
{
this.CheckBaseDependencies();
this.CheckGriddingDependencies();
this.CheckMeshProcessDependencies();
}
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 FormMain_MdiChildActivate(object sender, EventArgs e)
{
// string strText = this.Text;
}
///
/// 登录信息点击事件处理函数
///
/// 事件发送者
/// 事件参数,包含点击项的详细信息
private async void bsiLoginInfo_ItemClick(object sender, ItemClickEventArgs e)
{
await this.security.ShowServerConfig();
}
}
}