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
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static Hydro.MapView.MapViewEnum;
 
namespace Hydro.MapView
{
    [Serializable]
    public class TemplateList
    {
        public List<Template> templates = new List<Template>();
        public static TemplateList instance;
        public static string _tempListPath = null;
        public static bool Inited = false;
        public static List<Template> GetTemplates()
        {
            if (instance != null)
            {
                return instance.templates;
            }
            else
                return new List<Template>();
        }
        public static void Init(string filePath = null)
        {
            if (filePath == null)
                _tempListPath = Path.Combine(Directory.GetCurrentDirectory(), "templates.json");
            else
                _tempListPath = filePath;
            if (File.Exists(_tempListPath))
            {
                string jsonString = File.ReadAllText(_tempListPath);
                
                if (jsonString!=null && jsonString!="")
                {
                    try
                    {
                        instance = JsonConvert.DeserializeObject<TemplateList>(jsonString);
                    }
                    catch
                    {
 
                    }
                    
                }    
                    
 
            }
            if (instance==null)
            {
                instance = new TemplateList();
                // 添加测试数据
                AddTemp(new Template(null, "小区模板", @"template\小区模板\", TemplateType.小区模板));
                AddTemp(new Template(null, "分区模板", @"template\单元分区模板\", TemplateType.单元分区模板));
                AddTemp(new Template(null, "单元模板", @"template\单元模板\", TemplateType.单元模板));
                AddTemp(new Template(null, "楼层1", @"template\楼层模板\楼层1.inp", TemplateType.楼层模板));
            }
            Inited = true;
        }
        // 保存模板数据到文件中
        public static void SaveToFile(string filePath = null)
        {
            if (filePath == null) filePath = _tempListPath;
            // 备份历史保存文件
            if (File.Exists(filePath))
            {
                string backupPath = filePath + ".bak";
                if (File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                }
 
                File.Copy(filePath, backupPath);
            }
 
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                ContractResolver = new ShouldSerializeContractResolver(),
            };
            string json = JsonConvert.SerializeObject(instance, Formatting.Indented, settings);
            File.WriteAllText(_tempListPath, json);
 
            //MessageCompressHelper.SaveCompressedBase64ToFile<TemplateList>(instance, filePath);
        }
 
        // 从文件中读取已保存的模板数据
        //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 static Template AddNewTemp(TemplateType selectedType = TemplateType.其他)
        {
 
            int i = 0;
            while (true)
            {
                if (instance.templates.Find(t => t.Name == $"{selectedType}_{i}") == null) break;
                i++;
            }
            Template temp = new Template(Guid.NewGuid().ToString(), $"{selectedType}_{i}", $@"template\{selectedType}\{selectedType}_{i}.inp", selectedType);
            temp.view = null;// new MapView() { Center = new PointF(0, 0), zoom = 50f, rotation = 0, rotationF = 90 };
            if (File.Exists(temp.FullPath))
            {
                try
                {
                    //Global.ClearFileReadOnly(temp.FullPath);
                    File.Delete(temp.FullPath);
                }
                catch (Exception ex)
                {
                }
 
            }
            instance.templates.Add(temp);
            return temp;
        }
        public static Template AddTemp(Template temp)
        {
            instance.templates.Add(temp);
            return temp;
        }
        public static Template GetTemplate(string ID)
        {
            return instance.templates.Find(t => t.ID == ID);
        }
 
 
    }
 
    
}