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
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(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("导出成功!");
        }
 
 
 
        //写入文件
        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();
        }
    }
}