214 lines
6.8 KiB
C#
214 lines
6.8 KiB
C#
|
|
using Newtonsoft.Json;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.ComponentModel;
|
||
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Runtime.CompilerServices;
|
||
|
|
|
||
|
|
namespace Laservall.Solidworks.Model
|
||
|
|
{
|
||
|
|
public class BomColumnConfig : INotifyPropertyChanged
|
||
|
|
{
|
||
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
||
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||
|
|
{
|
||
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||
|
|
}
|
||
|
|
|
||
|
|
private string name;
|
||
|
|
public string Name
|
||
|
|
{
|
||
|
|
get => name;
|
||
|
|
set { if (name != value) { name = value; OnPropertyChanged(); } }
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool isVisible = true;
|
||
|
|
public bool IsVisible
|
||
|
|
{
|
||
|
|
get => isVisible;
|
||
|
|
set { if (isVisible != value) { isVisible = value; OnPropertyChanged(); } }
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool isExport = true;
|
||
|
|
public bool IsExport
|
||
|
|
{
|
||
|
|
get => isExport;
|
||
|
|
set { if (isExport != value) { isExport = value; OnPropertyChanged(); } }
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 固定列(图号、零件名称等内置列)不可删除
|
||
|
|
/// </summary>
|
||
|
|
public bool IsFixed { get; set; }
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 用户手动添加的自定义属性键(非从零件中自动收集)
|
||
|
|
/// </summary>
|
||
|
|
public bool IsUserAdded { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public class BomSettings
|
||
|
|
{
|
||
|
|
public List<BomColumnConfig> Columns { get; set; } = new List<BomColumnConfig>();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 用户主动删除的动态列名称,防止 MergeDynamicKeys 重新添加
|
||
|
|
/// </summary>
|
||
|
|
public HashSet<string> RemovedKeys { get; set; } = new HashSet<string>();
|
||
|
|
|
||
|
|
private static readonly List<(string Name, bool DefaultVisible)> BuiltInColumns = new List<(string, bool)>
|
||
|
|
{
|
||
|
|
("层级", true),
|
||
|
|
("图号", true),
|
||
|
|
("零件名称", true),
|
||
|
|
("属性", true),
|
||
|
|
("材料", true),
|
||
|
|
("材质", true),
|
||
|
|
("数量", true),
|
||
|
|
("装配体", true),
|
||
|
|
("外购件", true)
|
||
|
|
};
|
||
|
|
|
||
|
|
private static string GetSettingsFilePath()
|
||
|
|
{
|
||
|
|
string dir = Path.Combine(
|
||
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||
|
|
"Laservall", "Solidworks");
|
||
|
|
if (!Directory.Exists(dir))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(dir);
|
||
|
|
}
|
||
|
|
return Path.Combine(dir, "bom_columns.json");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Save()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||
|
|
File.WriteAllText(GetSettingsFilePath(), json);
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static BomSettings Load()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
string path = GetSettingsFilePath();
|
||
|
|
if (File.Exists(path))
|
||
|
|
{
|
||
|
|
string json = File.ReadAllText(path);
|
||
|
|
var settings = JsonConvert.DeserializeObject<BomSettings>(json);
|
||
|
|
if (settings != null && settings.Columns != null && settings.Columns.Count > 0)
|
||
|
|
{
|
||
|
|
if (settings.RemovedKeys == null)
|
||
|
|
{
|
||
|
|
settings.RemovedKeys = new HashSet<string>();
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (var builtIn in BuiltInColumns)
|
||
|
|
{
|
||
|
|
if (!settings.Columns.Any(c => c.Name == builtIn.Name))
|
||
|
|
{
|
||
|
|
settings.Columns.Add(new BomColumnConfig
|
||
|
|
{
|
||
|
|
Name = builtIn.Name,
|
||
|
|
IsVisible = builtIn.DefaultVisible,
|
||
|
|
IsExport = builtIn.DefaultVisible,
|
||
|
|
IsFixed = true,
|
||
|
|
IsUserAdded = false
|
||
|
|
});
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var col = settings.Columns.First(c => c.Name == builtIn.Name);
|
||
|
|
col.IsFixed = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return settings;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
return CreateDefault();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static BomSettings CreateDefault()
|
||
|
|
{
|
||
|
|
var settings = new BomSettings();
|
||
|
|
foreach (var builtIn in BuiltInColumns)
|
||
|
|
{
|
||
|
|
settings.Columns.Add(new BomColumnConfig
|
||
|
|
{
|
||
|
|
Name = builtIn.Name,
|
||
|
|
IsVisible = builtIn.DefaultVisible,
|
||
|
|
IsExport = builtIn.DefaultVisible,
|
||
|
|
IsFixed = true,
|
||
|
|
IsUserAdded = false
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return settings;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 将从零件收集到的动态属性键合并到配置中(保留已有配置状态)
|
||
|
|
/// </summary>
|
||
|
|
public void MergeDynamicKeys(List<string> discoveredKeys)
|
||
|
|
{
|
||
|
|
var builtInNames = new HashSet<string>(BuiltInColumns.Select(b => b.Name));
|
||
|
|
foreach (var key in discoveredKeys)
|
||
|
|
{
|
||
|
|
if (builtInNames.Contains(key))
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (RemovedKeys.Contains(key))
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (!Columns.Any(c => c.Name == key))
|
||
|
|
{
|
||
|
|
Columns.Add(new BomColumnConfig
|
||
|
|
{
|
||
|
|
Name = key,
|
||
|
|
IsVisible = true,
|
||
|
|
IsExport = true,
|
||
|
|
IsFixed = false,
|
||
|
|
IsUserAdded = false
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public BomColumnConfig GetColumn(string name)
|
||
|
|
{
|
||
|
|
return Columns.FirstOrDefault(c => c.Name == name);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsColumnVisible(string name)
|
||
|
|
{
|
||
|
|
var col = GetColumn(name);
|
||
|
|
return col == null || col.IsVisible;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsColumnExport(string name)
|
||
|
|
{
|
||
|
|
var col = GetColumn(name);
|
||
|
|
return col == null || col.IsExport;
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<BomColumnConfig> GetDynamicColumns()
|
||
|
|
{
|
||
|
|
var builtInNames = new HashSet<string>(BuiltInColumns.Select(b => b.Name));
|
||
|
|
return Columns.Where(c => !builtInNames.Contains(c.Name)).ToList();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|