using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using Yw.WinFrmUI.Q3d;
|
using Yw.WinFrmUI.Q3d;
|
using static Yw.WinFrmUI.Q3d.MapViewEnum;
|
using static Yw.WinFrmUI.Q3d.ObjectEnum;
|
|
namespace Yw.WinFrmUI.Q3d
|
{
|
[Serializable]
|
public class FactoryList
|
{
|
public List<Factory> Factories { get; set; }
|
|
public FactoryList()
|
{
|
Factories = new List<Factory>();
|
}
|
public void AddFactory(string name, FactoryType type)
|
{
|
Factories.Add(new Factory { Name = name, Type = type, Pumps = new List<PumpViewModel>() });
|
}
|
public PumpViewModel AddPump(Factory factory, string name, PumpType type)
|
{
|
PumpViewModel pump = new PumpViewModel { Name = name, Type = type };
|
factory.Pumps.Add(pump);
|
return pump;
|
}
|
public void RemoveFactory(Factory factory)
|
{
|
Factories.Remove(factory);
|
}
|
private string _filePath = null;// "data.txt";
|
private string _bakPath = Path.Combine(Directory.GetCurrentDirectory(), "bak\\");
|
private string _fileDirectory = Path.Combine(Directory.GetCurrentDirectory(), "sav\\");
|
|
//public FactoryListFileManager(string filePath)
|
//{
|
// _filePath = filePath;
|
//}
|
|
public string Save(string filePath)
|
{
|
|
|
if (filePath == null)
|
{
|
using (SaveFileDialog ofd = new SaveFileDialog())
|
{
|
ofd.InitialDirectory = _fileDirectory;
|
ofd.Filter = "txt|*.txt|All|*.*";
|
var result = ofd.ShowDialog();
|
if (result == DialogResult.OK) _filePath = ofd.FileName;
|
else return null;
|
}
|
}
|
else
|
{
|
_filePath = filePath;
|
}
|
|
if (File.Exists(_filePath))
|
{
|
FileInfo fi = new FileInfo(_filePath);
|
if (!Directory.Exists(_bakPath)) Directory.CreateDirectory(_bakPath);
|
string bakName = Path.Combine(_bakPath, $"{fi.Name}_bak_{fi.LastWriteTime.ToString("yyyy_MM_dd_HH_mm_ss")}.txt");
|
FileCopy.Copy(_filePath, bakName, true);
|
}
|
|
var sb = new StringBuilder();
|
foreach (var factory in Factories)
|
{
|
// 写入工厂信息
|
sb.AppendLine($"Factory,{factory.Type},{factory.Name}");
|
|
foreach (var pump in factory.Pumps)
|
{
|
// 写入水泵信息
|
sb.AppendLine($"Pump,{pump.Type},{pump.Name},{pump.额定转速},{pump.额定流量},{pump.额定扬程},{pump.额定功率},{pump.当前转速}");
|
|
foreach (var dataset in pump.Datasets)
|
{
|
// 写入数据集信息
|
sb.AppendLine($"Dataset,{dataset.Value.Name},{dataset.Value.degree}");
|
if (dataset.Value.range_X != null)
|
sb.AppendLine($"Dataset_Range_X,{dataset.Value.range_X.Min},{dataset.Value.range_X.Max}");
|
if (dataset.Value.range_Y != null)
|
sb.AppendLine($"Dataset_Range_Y,{dataset.Value.range_Y.Min},{dataset.Value.range_Y.Max}");
|
if (dataset.Value._data != null)
|
foreach (var point in dataset.Value._data)
|
{
|
// 写入数据点信息
|
sb.AppendLine($"{point.X},{point.Y}");
|
}
|
}
|
|
|
}
|
}
|
|
// 将数据写入文件
|
File.WriteAllText(_filePath, sb.ToString());
|
return _filePath;
|
}
|
|
public string Load(string fileName)
|
{
|
//var factoryList = new FactoryList();
|
|
if (!Directory.Exists(_fileDirectory)) Directory.CreateDirectory(_fileDirectory);
|
if (fileName == null)
|
{
|
using (OpenFileDialog ofd = new OpenFileDialog())
|
{
|
|
ofd.InitialDirectory = _fileDirectory;
|
ofd.Filter = "txt|*.txt|All|*.*";
|
var result = ofd.ShowDialog();
|
if (result == DialogResult.OK) _filePath = ofd.FileName;
|
else return null;
|
}
|
}
|
else
|
{
|
_filePath = fileName;
|
}
|
|
|
if (!File.Exists(_filePath))
|
{
|
throw new Exception("文件不存在");
|
//return null;
|
}
|
Factories.Clear();
|
var currentFactory = default(Factory);
|
var currentPump = default(PumpViewModel);
|
var currentDataset = default(Dataset);
|
List<PointF> list = null;
|
foreach (var line in File.ReadAllLines(_filePath))
|
{
|
var fields = line.Split(',');
|
|
switch (fields[0])
|
{
|
case "Factory":
|
// 读取工厂信息
|
var factoryType = (FactoryType)Enum.Parse(typeof(FactoryType), fields[1]);
|
var factoryName = fields[2];
|
currentFactory = new Factory { Name = factoryName, Type = factoryType, Pumps = new List<PumpViewModel>() };
|
this.Factories.Add(currentFactory);
|
currentPump = null;
|
currentDataset = null;
|
break;
|
|
case "Pump":
|
// 读取水泵信息
|
var pumpType = (PumpType)Enum.Parse(typeof(PumpType), fields[1]);
|
var pumpName = fields[2];
|
currentPump = new PumpViewModel { Name = pumpName, Type = pumpType, 额定转速 = double.Parse(fields[3]), 额定流量 = double.Parse(fields[4]), 额定扬程 = double.Parse(fields[5]), 额定功率 = fields.Length > 6 ? double.Parse(fields[6]) : 75, 当前转速 = fields.Length > 7 ? double.Parse(fields[7]) : 1500 };
|
|
currentFactory.Pumps.Add(currentPump);
|
currentDataset = null;
|
break;
|
|
case "Dataset":
|
// 读取数据集信息
|
var datasetName = fields[1];
|
int degree = 2;
|
if (fields.Length > 2) degree = int.Parse(fields[2]);
|
currentDataset = new Dataset(datasetName, currentPump, degree) { Data = new List<PointF>() };
|
list = new List<PointF>();
|
currentPump.Datasets[currentDataset.Name] = currentDataset;
|
break;
|
case "Dataset_Range_X":
|
{
|
double d1 = 0; double d2 = 0;
|
if (double.TryParse(fields[1], out d1) && double.TryParse(fields[2], out d2))
|
{
|
currentDataset.range_X = new DRange(d1, d2);
|
}
|
|
break;
|
}
|
|
case "Dataset_Range_Y":
|
{
|
double d1 = 0; double d2 = 0;
|
if (double.TryParse(fields[1], out d1) && double.TryParse(fields[2], out d2))
|
{
|
currentDataset.range_Y = new DRange(d1, d2);
|
}
|
|
break;
|
}
|
default:
|
{
|
// 读取数据点信息
|
var x = float.Parse(fields[0]);
|
var y = float.Parse(fields[1]);
|
list.Add(new PointF(x, y));
|
currentDataset.Data = list;
|
break;
|
}
|
|
}
|
}
|
|
CurveFit();
|
//return factoryList;
|
return _filePath;
|
}
|
|
|
|
public void CurveFit()
|
{
|
foreach (var f in Factories)
|
{
|
f.CurveFit();
|
}
|
}
|
}
|
}
|