tangxu
2024-05-06 f01d639477141399fd3e092f3f0acb3ced7e02bf
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Hydro.Core.ObjectEnum;
using static Hydro.MapView.MapViewEnum;
using System.Windows.Forms;
using Hydro.CommonBase;
 
namespace Hydro.MapView
{
    [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");
                File.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();
            }
        }
    }
}