using Hydro.CommonBase;
|
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);
|
}
|
|
FileCopy.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);
|
}
|
|
|
}
|
|
|
}
|