cloudflight
2023-12-02 1a5ef6b2bf25de50b685b44299d9a049a2feac80
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace CloudWaterNetwork
{
    
    public class Templates
    {
        public List<Template> templates = new List<Template>();
 
        // 保存模板数据到文件中
        public void SaveToFile(string filePath)
        {
            // 备份历史保存文件
            if (File.Exists(filePath))
            {
                string backupPath = filePath + ".bak";
                if (File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                }
 
                File.Copy(filePath, backupPath);
            }
 
            // 将模板数据序列化为文本格式
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                foreach (Template template in templates)
                {
                    writer.WriteLine(template.ID);
                    writer.WriteLine(template.名称);
                    writer.WriteLine(template.filePath);
                    writer.WriteLine(template.Type);
                    writer.WriteLine(template.派生关系.Count);
 
                    foreach (Relation relation in template.派生关系)
                    {
                        writer.WriteLine(relation.名称);
                        writer.WriteLine(relation.关键字);
                        writer.WriteLine(relation.模板名称);
                    }
                }
            }
        }
 
        // 从文件中读取已保存的模板数据
        public void LoadFromFile(string filePath)
        {
 
            // 如果文件不存在,则无需读取
            if (!File.Exists(filePath))
            {
                return;
            }
 
            // 反序列化文件内容为模板数据
            using (StreamReader reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    // 读取模板基本信息
                    string id=reader.ReadLine();
                    string name = reader.ReadLine();
                    string path = reader.ReadLine();
                    TemplateType type = (TemplateType)Enum.Parse(typeof(TemplateType), reader.ReadLine());
                    int relationCount = int.Parse(reader.ReadLine());
 
                    // 读取派生关系信息
                    List<Relation> relations = new List<Relation>();
                    for (int i = 0; i < relationCount; i++)
                    {
                       
                        string relationName = reader.ReadLine();
                        string relationKeyword = reader.ReadLine();
                        string relationTemplateName = reader.ReadLine();
                        Relation relation = new Relation() { 名称 = relationName, 关键字 = relationKeyword, 模板名称 = relationTemplateName };
                        relations.Add(relation);
                    }
 
                    // 创建模板对象并添加到列表中
                    Template template = new Template(id,name, path, type) { 派生关系 = relations };
                    templates.Add(template);
                }
            }
        }
 
        public Template GetTemplate(string ID)
        {
            return  templates.Find(t=>t.ID== ID);
        }
 
 
    }
    [Serializable]
    public class Template:Link
    {
        private string _ID;
        [Category("1、基本信息")]
        [Description("对象的ID唯一标识")]
        [DisplayName("模板ID")]
        [Browsable(true)]
        public string ID { get { if (_ID == null || _ID == "") _ID = Guid.NewGuid().ToString(); return _ID; } set { _ID = value; } }
        [Category("1、基本信息")]
        [DisplayName("名称")]
        public string 名称 { get; set; }
 
        public MapOption mapOption { get; set; } = new MapOption();
 
        private string _filePath = null;  
 
        [Category("2、参数")]
        [DisplayName("路径INP")]
        public string filePath 
        {
            get
            {
                return _filePath;
            }
            set
            {
                var filePath= value;
                var CurDir = Directory.GetCurrentDirectory();
                if (filePath!=null && filePath.IndexOf(CurDir) >= 0)
                {
                    
                    filePath = filePath.Replace(CurDir, "");
                    if (filePath[0] == '\\')
                        filePath = filePath.Substring(1);
                }
                    
                
                _filePath = filePath;
            }
        }
 
        [Browsable(false)]
        public string FullPath
        {
            get
            {
                return Path.Combine(Directory.GetCurrentDirectory(), filePath==null?"":filePath );
            }
        }
 
 
        [Category("2、参数")]
        [DisplayName("最高级数")]
        public int MaxLevel { get; set; } = 99;
        [Category("1、基本信息")]
        [Description("类型")]
        [DisplayName("类型")]
        [Browsable(true)]
        public TemplateType Type { get; set; }
 
        [Category("3、其他")]
        [Description("对象的ID唯一标识")]
        [Browsable(false)]
        public List<Relation> 派生关系 { get; set; } = new List<Relation>();
        public Template()
        {
            
            ID = new Guid().ToString();
            名称 = "临时";
            filePath = null;
           
        }
        public Template(string ID,string name, string address, TemplateType type)
        {
            if (ID==null)
                this.ID = new Guid().ToString();
            名称 = name;
            filePath = address;
            Type = type;
        }
 
 
        [Category("3、其他")]
        [Description("对象的ID唯一标识")]
        [Browsable(false)]
        [NonSerialized]
        public Network network = null;
 
 
        [Category("1、基本信息")]
        [DisplayName("标高")]
        [Browsable(false)]
        public new float Elev { get { return 0; } }
 
   
        [Browsable(false)]
        public new float Diameter { get { return 0; } }
 
        [Browsable(false)]
        public new float Level { get { return 0; } }
 
 
 
        [Category("3、其他")]
        [DisplayName("开始节点")]
        public string Node1 { get; set; }
        
 
        [Category("3、其他")]
        [DisplayName("结束节点")]
        public string Node2 { get; set; }
 
        [Browsable(false)]
        public string X { get; set; }
        [Browsable(false)]
        public string Y { get; set; }
 
        [Browsable(false)]
        public string Visible { get; set; }
 
        [Category("4、默认视角")]
        [DisplayName("视角")]
        [Browsable(false)]
        public MapView view { get; set; } = null;
 
 
        [Category("4、默认视角")]
        [DisplayName("中心X")]
        [Browsable(true)]
        public float CenterX
        { 
            get { if (view != null) return view.Center.X; 
                else return 0; } 
            set
            {
                if (view == null) view = new MapView();
                view.Center = new PointF(value,view.Center.Y);
            }
        }
        [Category("4、默认视角")]
        [DisplayName("中心Y")]
        [Browsable(true)]
        public float CenterY
        {
            get
            {
                if (view != null) return view.Center.Y;
                else return  0;
            }
            set
            {
                if (view == null) view = new MapView();
                view.Center = new PointF( view.Center.X, value);
            }
        }
 
        [Category("4、默认视角")]
        [DisplayName("缩放")]
        [Browsable(true)]
        public float zoom 
        { 
            get 
            { if (view != null) 
                    return view.zoom; else return 1f; 
            }
            set
            {
                if (view == null) view = new MapView();
                view.zoom = value;
            }
        }
 
        [Category("4、默认视角")]
        [DisplayName("旋转角度")]
        [Browsable(true)]
        public double rotation 
        { 
            get { if (view != null) return view.rotation; else return 0; }
            set
            {
                if (view == null) view = new MapView();
                view.rotation = value;
            }
        }
 
        [Category("4、默认视角")]
        [DisplayName("俯视角度")]
        [Browsable(true)]
        public double rotationF
        {
            get { if (view != null) return view.rotationF; else return 90; }
            set
            {
                if (view == null) view = new MapView();
                view.rotationF = value;
            }
        }
 
 
 
        public override string ToString()
        {
            return 名称;
        }
        public void Export(string path)
        {
            network.saveInpFile(path);
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "Temporary Files (*.temp)|*.temp";
            //saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory() + $@"\template\{类型}";
            //saveFileDialog.FileName = 名称;
            //if (saveFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    string filePath = saveFileDialog.FileName;
            //    // 使用 filePath 变量来保存文件
            //    //temp.路径 = filePath;
            //}
        }
 
        public bool loadInpFile()
        {
            network = new Network();
            var result= network.loadInpFile(FullPath);
            if (result)
            {
                network.MapObjects.ForEach(o => 
                {
                    if (o.Level > MaxLevel) 
                        o.Visible = false;
                    else 
                        o.Visible = true;
                });
                
 
                return true;
            }
            else
                return false;
        }
 
 
        public Trigger triggers;
    }
 
    [Serializable]
    public class Relation
    {
 
        public string 名称 { get; set; }
        public string 关键字 { get; set; }
        public string 模板名称 { get; set; }
    }
    public enum TemplateType
    {
        全部,
        小区模板,
        单元模板,
        单元分区模板,
        楼层模板,
        其他
    }
 
    public enum TriggerType
    {
        onLoaded
    }
    public class  Trigger
    {
        public string Name;
        public string Description;
        public TriggerType Type;
        public string Code;
        
    }
 
    [Serializable]
    public class MapView
    {
        [Category("4、视角")]
        [DisplayName("中心")]
        [Browsable(true)]
        public PointF Center { get; set; }
 
        [Category("4、视角")]
        [DisplayName("缩放")]
        [Browsable(true)]
        public float zoom { get; set; }
 
        [Category("4、视角")]
        [DisplayName("旋转角度")]
        [Browsable(true)]
        public double rotation { get; set; }
 
        [Category("4、视角")]
        [DisplayName("俯视角度")]
        [Browsable(true)]
        public double rotationF { get; set; }
    }
}