管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-09 da528e999d6538a12f357b6c745974316d48c086
SimuTools/FrmMain.cs
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimuTools.Domain;
using SimuTools.Tools;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace SimuTools
@@ -19,27 +16,140 @@
        private void FrmMain_Load(object sender, EventArgs e)
        {
            LogOut.Info("启动程序...");
            try
            {
                GdalHelper gdal = GdalHelper.Instance;
                LogOut.Info("> 初始化GDAL完成 <");
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.StackTrace);
                ShowErr("GDAL初始化出错:" + ex.Message);
            }
        }
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            LogOut.Info("关闭程序...");
        }
        private void btnTerrainPath_Click(object sender, EventArgs e)
        {
            selectFile(this.txtTerrainPath);
        }
        private void btnBuildingPath_Click(object sender, EventArgs e)
        {
            selectFile(this.txtBuildingPath);
        }
        private void selectFile(TextBox tb)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Tif files(*.tif)|*.tif|Tiff files (*.tiff)|*.tiff";
            dialog.RestoreDirectory = true;
            string path = tb.Text.Trim();
            if (File.Exists(path))
            {
                dialog.InitialDirectory = Path.GetDirectoryName(path);
            }
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tb.Text = dialog.FileName;
            }
        }
        private void btnWaterPath_Click(object sender, EventArgs e)
        {
            selectFolder(this.txtWaterPath, "水面");
        }
        private void btnFlowPath_Click(object sender, EventArgs e)
        {
            selectFolder(this.txtFlowPath, "流速");
        }
        private void btnOutPath_Click(object sender, EventArgs e)
        {
            selectFolder(this.txtOutPath, "输出");
        }
        private void selectFolder(TextBox tb, string title)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                string path = tb.Text.Trim();
                if (Directory.Exists(path))
                {
                    dialog.SelectedPath = path;
                }
                dialog.Description = "请选择" + title + "目录...";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    tb.Text = dialog.SelectedPath;
                }
            }
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            string serviceName = this.txtServiceName.Text.Trim();
            string terrainFile = this.txtTerrainPath.Text.Trim();
            string buildingFile = this.txtBuildingPath.Text.Trim();
            string waterPath = this.txtWaterPath.Text.Trim();
            string flowPath = this.txtFlowPath.Text.Trim();
            string outPath = this.txtOutPath.Text.Trim();
            if (string.IsNullOrEmpty(serviceName))
            {
                ShowErr("服务名称,要求必填!");
                return;
            }
            if (!File.Exists(terrainFile) || !File.Exists(buildingFile))
            {
                ShowErr("地形和建筑文件,要求必须存在!");
                return;
            }
            if (!Directory.Exists(waterPath) || !Directory.Exists(flowPath) || !Directory.Exists(outPath))
            {
                ShowErr("水面、流速和输出目录,要求必须存在!");
                return;
            }
            try
            {
                this.btnRun.Enabled = false;
                LogOut.Info("开始运行 >>");
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                Tools.Handle.Run(new Args(serviceName, terrainFile, buildingFile, waterPath, flowPath, outPath));
                stopWatch.Stop();
                this.btnRun.Enabled = true;
                LogOut.Info("运行结束 <<");
                MessageBox.Show("运行结束!\n耗时:" + GetSeconds(stopWatch.ElapsedMilliseconds) + " 秒", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.btnRun.Enabled = true;
                LogOut.Error(ex.StackTrace);
                ShowErr("运行出错:" + ex.Message);
            }
        }
        private void ShowErr(string message)
        {
            MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private string GetSeconds(long milliseconds)
        {
            return (milliseconds / 1000.0).ToString();
        }
    }
}