Files
SzmediTools/Szmedi.RevitToolkit.Approval/Assists/PropertyAssists.cs

67 lines
2.3 KiB
C#
Raw Normal View History

2025-09-16 16:06:41 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Szmedi.RevitToolkit.Approval.Assists
{
internal class PropertyAssists
{
private static Dictionary<string, Dictionary<string, string>> _defaults;
// 默认初始值:程序目录下的 PropertyDefaults.json
private static string _filePath = Path.Combine(GlobalAssists.DirAssembly, "PropertyDefaults.json");
public static void Load(string filePath = null)
{
if (!string.IsNullOrEmpty(filePath))
_filePath = filePath;
if (!File.Exists(_filePath))
{
_defaults = new Dictionary<string, Dictionary<string, string>>();
var emptyJson = JsonConvert.SerializeObject(_defaults, Formatting.Indented);
File.WriteAllText(_filePath, emptyJson);
return;
}
var json = File.ReadAllText(_filePath);
_defaults = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);
}
public static string GetDefaultValue(string definitionName, string propName)
{
if (_defaults == null) return null;
if (_defaults.TryGetValue(definitionName, out var props))
{
if (props.TryGetValue(propName, out var value))
return value;
}
return null;
}
// 新增:设置属性默认值
public static void SetDefaultValue(string definitionName, string propName, string value)
{
if (_defaults == null)
_defaults = new Dictionary<string, Dictionary<string, string>>();
if (!_defaults.TryGetValue(definitionName, out var props))
{
props = new Dictionary<string, string>();
_defaults[definitionName] = props;
}
props[propName] = value;
}
// 新增:保存到文件
public static void Save()
{
if (string.IsNullOrEmpty(_filePath) || _defaults == null) return;
var json = JsonConvert.SerializeObject(_defaults, Formatting.Indented);
File.WriteAllText(_filePath, json);
}
}
}