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(); } } }