From 00700f5ef12d33da898e9b4391aa5b4a09828ee3 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 10 十一月 2022 10:16:53 +0800
Subject: [PATCH] 1

---
 ExportMap/cs/ExportUtil.cs |   56 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/ExportMap/cs/ExportUtil.cs b/ExportMap/cs/ExportUtil.cs
index 9a65e0c..bdc1333 100644
--- a/ExportMap/cs/ExportUtil.cs
+++ b/ExportMap/cs/ExportUtil.cs
@@ -119,44 +119,76 @@
         /// <param name="py">Python鏂囦欢</param>
         /// <param name="qgz">QGIS宸ョ▼</param>
         /// <param name="qpt">QGIS妯℃澘</param>
-        public static void ExecPython(string py, string qgz, string qpt)
+        /// <returns>鎵ц缁撴灉</returns>
+        public static string ExecPython(string py, string qgz, string qpt)
         {
+            string str = null;
             try
             {
-                string args = string.Format("{0} -qgz {1} -qpt {2}", py, qgz, qpt);
+                string cmdText = string.Format("python {0} -qgz {1} -qpt {2}", py, qgz, qpt);
 
                 Process p = new Process();
+                p.StartInfo.FileName = "cmd.exe";
                 p.StartInfo.UseShellExecute = false;
-                p.StartInfo.ErrorDialog = true;
-                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
-                p.StartInfo.RedirectStandardError = false;
-                p.StartInfo.FileName = "python";
-                p.StartInfo.Arguments = args;
                 p.StartInfo.CreateNoWindow = true;
-
+                p.StartInfo.RedirectStandardInput = true;
+                p.StartInfo.RedirectStandardOutput = true;
+                p.StartInfo.RedirectStandardError = true;
                 p.Start();
+
+                StreamWriter si = p.StandardInput; // 鏍囧噯杈撳叆娴� 
+                StreamReader so = p.StandardOutput; // 鏍囧噯杈撳嚭娴� 
+                StreamReader se = p.StandardError; // 鏍囧噯閿欒娴�
+
+                si.AutoFlush = true;
+                si.WriteLine(cmdText);
+                si.WriteLine("exit");
+
+                string info = so.ReadToEnd();
+                str = se.ReadToEnd();
+                if (p.HasExited == false)
+                {
+                    p.Kill();
+                }
+
+                si.Close();
+                so.Close();
+                se.Close();
+                p.Close();
             }
-            catch
+            catch (Exception ex)
             {
+                str = ex.Message;
             }
+
+            return str;
         }
 
         /// <summary>
         /// 鐢熸垚
         /// </summary>
         /// <param name="args">鍑哄浘鍙傛暟</param>
-        public static void Generate(ExportArgs args)
+        /// <returns>鍥剧墖璺緞</returns>
+        public static string Generate(ExportArgs args)
         {
             string date = DateStr;
             string sub = GetExportSubFolder();
             string qgz = "Test.qgz";
 
-            args.imgPath = sub + "/" + date + ".png";
+            args.imgPath = sub + "\\" + date + ".png";
             args.qpt = date + ".qpt";
 
+            args.SetDefault();
             CreateTemplate(args);
+            string info = ExecPython(PyFile, qgz, args.qpt);
 
-            ExecPython(PyFile, qgz, args.qpt);
+            string qptFile = Path.Combine(SourcesPath, args.qpt);
+            if (File.Exists(qptFile))
+            {
+                File.Delete(qptFile);
+            }
+
+            return args.imgPath;
         }
 
         /// <summary>

--
Gitblit v1.9.3