Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Basic
{
    public class ExportInpHelper
    {
 
 
        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("导出成功!");
        }
 
        public static void INPDefaultFile(string filePath, string curveCode, Model.CurveExpress curveQH, Model.CurveExpress curveQE)
        {
            if (curveQH == null)
                return;
 
            //均匀插值点,计算点坐标 
 
 
            var qhStr = new StringBuilder();
            List<Model.CurvePoint> pointQH = curveQH.GetFitPoints(12);
            if (curveQH.DefinePoints != null)
                pointQH = curveQH.DefinePoints;
 
            for (var i = 0; i < pointQH.Count; i++)
            {
                var point = pointQH[i];
                var Q = point.X;
                var H = point.Y;
                qhStr.AppendLine(Q + "  " + H);
            }
 
            var qeStr = new StringBuilder();
            List<Model.CurvePoint> pointQE = curveQE.GetFitPoints(12);
            if (curveQE.DefinePoints != null)
                pointQE = curveQE.DefinePoints;
 
            for (var i = 0; i < pointQE.Count; i++)
            {
                var point = pointQE[i];
                var Q = point.X;
                var E = point.Y;
                qeStr.AppendLine(Q + "  " + E);
            }
 
 
            WriteToFile(curveCode, filePath, qhStr, "水泵");
            if (qeStr.Length > 0)
            {
                var qeFilePath = filePath.Insert(filePath.LastIndexOf("."), "效率");
                if (File.Exists(qeFilePath))
                    File.Delete(qeFilePath);
                WriteToFile(curveCode, qeFilePath, qeStr, "效率");
            }
        }
 
        //写入文件
        private static void WriteToFile(string curveCode, 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 = $"{curveCode + "-" + DateTime.Now}";
            strQhFile.AppendLine(describe);
            strQhFile.AppendLine(curveStr.ToString());
            sw.Write(strQhFile);
            sw.Close();
        }
    }
}