Shuxia Ning
2025-02-13 2f1cbec203dcff25df7a5c2b51b13ec558f2c3db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using DevExpress.XtraEditors;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Basic
{
    public class ExportCurveHelper
    {
        public static void INPFile(string fileName, Model.FeatCurveExpressGroup curveInfo)
        {
            if (curveInfo == null)
                return;
            if (curveInfo.CurveQH == null)
                return;
 
            var dlg = new SaveFileDialog();
            dlg.Title = "导出 inp 曲线文件";
            dlg.FileName = $"{fileName}.crv";
            dlg.Filter = "crv (*.crv)|*.crv";
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
 
            var filePath = dlg.FileName;
            var qhCurveExpress = curveInfo.CurveQH;
            var qeCurveExpress = curveInfo.CurveQE;
 
            INPFile(filePath, fileName, qhCurveExpress, qeCurveExpress);
        }
 
        public static void INPFile(Model.PumpCurve pumpCurve)
        {
            if (pumpCurve == null)
                return;
            if (pumpCurve.CurveInfo?.CurveQH == null)
                return;
 
            var dlg = new SaveFileDialog();
            dlg.Title = "导出 inp 曲线文件";
            dlg.FileName = $"{pumpCurve.CurveCode}.crv";
            dlg.Filter = "crv (*.crv)|*.crv";
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
 
            var filePath = dlg.FileName;
            var qhCurveExpress = pumpCurve.CurveInfo.CurveQH;
            var qeCurveExpress = pumpCurve.CurveInfo.CurveQE;
 
            INPFile(filePath, pumpCurve.CurveCode, qhCurveExpress, qeCurveExpress);
        }
 
        public static void INPFile(string filePath, string curveCode, Model.CurveExpress qhCurveExpress, Model.CurveExpress qeCurveExpress)
        {
            if (qhCurveExpress == null)
                return;
 
            //均匀插值点,计算点坐标 
            var fitPointNumQH = 15;
            double space = (qhCurveExpress.Max - qhCurveExpress.Min) / (fitPointNumQH - 1);
            double Q = qhCurveExpress.Min;
 
            var qhStr = new StringBuilder();
            var qeStr = new StringBuilder();
 
            for (var i = 0; i < fitPointNumQH; i++)
            {
                var H = Model.FitCurveHelper.GetFitPointY(qhCurveExpress, Q);
                qhStr.AppendLine(Q + "  " + H);
 
                if (qeCurveExpress != null)
                {
                    var E = Model.FitCurveHelper.GetFitPointY(qeCurveExpress, Q);
                    qeStr.AppendLine(Q + "  " + E);
                }
 
                Q = Q + space;
                if (Q > qhCurveExpress.Max * 0.9999)
                    Q = qhCurveExpress.Max * 0.9999;
            }
 
            var qhFilePath = filePath.Insert(filePath.LastIndexOf("."), "水泵");
            if (File.Exists(qhFilePath))
                File.Delete(qhFilePath);
            WriteToFile(curveCode, qhFilePath, qhStr, "水泵");
 
            if (qeStr.Length > 0)
            {
                var qeFilePath = filePath.Insert(filePath.LastIndexOf("."), "效率");
                if (File.Exists(qeFilePath))
                    File.Delete(qeFilePath);
                WriteToFile(curveCode, qeFilePath, qeStr, "效率");
            }
 
            XtraMessageBox.Show("导出成功!");
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="qhCurveExpress"></param>
        /// <param name="qeCurveExpress"></param>
        /// <param name="fitPointNumQH"> 均匀插值点,计算点坐标  </param>
        public static void INPFile(string filePath, Model.CurveExpress qhCurveExpress, Model.CurveExpress qeCurveExpress,int fitPointNumQH=20)
        {
            if (qhCurveExpress == null)
                return;
 
          
            double space = (qhCurveExpress.Max - qhCurveExpress.Min) / (fitPointNumQH - 1);
            double Q = qhCurveExpress.Min;
 
            var qhStr = new StringBuilder();
            var qeStr = new StringBuilder();
 
            for (var i = 0; i < fitPointNumQH; i++)
            {
                var H = Model.FitCurveHelper.GetFitPointY(qhCurveExpress, Q);
                qhStr.AppendLine(Q + "  " + H);
 
                if (qeCurveExpress != null)
                {
                    var E = Model.FitCurveHelper.GetFitPointY(qeCurveExpress, Q);
                    qeStr.AppendLine(Q + "  " + E);
                }
 
                Q = Q + space;
                if (Q > qhCurveExpress.Max * 0.9999)
                    Q = qhCurveExpress.Max * 0.9999;
            }
 
 
            var qhFilePath = filePath.Insert(filePath.LastIndexOf("\\")+1, "水泵");
            //  var qhFilePath = filePath.Insert(0, "水泵");
            if (File.Exists(qhFilePath))
                File.Delete(qhFilePath);
            WriteToFile(qhFilePath, qhStr, "水泵");
 
            if (qeStr.Length > 0)
            {
                var qeFilePath = filePath.Insert(filePath.LastIndexOf("\\") + 1, "效率");
                //   var qeFilePath = filePath.Insert(0, "效率");
                if (File.Exists(qeFilePath))
                    File.Delete(qeFilePath);
                WriteToFile(qeFilePath, qeStr, "效率");
            }
 
        }
 
 
        //写入文件
        private static void WriteToFile(string filePath, StringBuilder curveStr, string fileType)
        {
            StreamWriter sw = new StreamWriter(new FileStream(filePath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
            StringBuilder strQhFile = new StringBuilder();
            strQhFile.AppendLine("EPANETH曲线数据");
            strQhFile.AppendLine(fileType);
            var describe = $"{DateTime.Now}";
            strQhFile.AppendLine(describe);
            strQhFile.AppendLine(curveStr.ToString());
            sw.Write(strQhFile);
            sw.Close();
        }
 
        //写入文件
        private static void WriteToFile(string description, string filePath, StringBuilder curveStr, string fileType)
        {
            StreamWriter sw = new StreamWriter(new FileStream(filePath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
            StringBuilder strQhFile = new StringBuilder();
            strQhFile.AppendLine("EPANETH曲线数据");
            strQhFile.AppendLine(fileType);
            var describe = $"{description + "-" + DateTime.Now}";
            strQhFile.AppendLine(describe);
            strQhFile.AppendLine(curveStr.ToString());
            sw.Write(strQhFile);
            sw.Close();
        }
    }
}