CadParamPluging/Common/TemplateSchemaDefinition.cs

108 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace CadParamPluging.Common
{
[Serializable]
public class TemplateSchemaDefinition
{
public string TemplateKey { get; set; }
public string ProjectType { get; set; }
public string DrawingType { get; set; }
public string SheetSize { get; set; }
public string Scale { get; set; }
public string DisplayName { get; set; }
public List<string> SelectedParamKeys { get; set; }
/// <summary>
/// Subset of <see cref="SelectedParamKeys"/> that should be treated as "零件尺寸".
/// Keys not in this set are treated as "锻件尺寸".
/// </summary>
public List<string> SelectedPartParamKeys { get; set; }
/// <summary>
/// Whether the user has explicitly configured ownership for main parameters.
/// Used for backward-compatible migration of old configs.
/// </summary>
public bool PartOwnershipConfigured { get; set; }
public string NoteTemplateText { get; set; }
public List<NotePlaceholderBinding> NoteBindings { get; set; }
public TemplateSchemaDefinition()
{
SelectedParamKeys = new List<string>();
SelectedPartParamKeys = new List<string>();
PartOwnershipConfigured = false;
NoteTemplateText = string.Empty;
NoteBindings = new List<NotePlaceholderBinding>();
}
public void Normalize()
{
ProjectType = (ProjectType ?? string.Empty).Trim();
DrawingType = (DrawingType ?? string.Empty).Trim();
SheetSize = (SheetSize ?? string.Empty).Trim();
Scale = (Scale ?? string.Empty).Trim();
DisplayName = (DisplayName ?? string.Empty).Trim();
NoteTemplateText = NoteTemplateText ?? string.Empty;
TemplateKey = TemplateKeyBuilder.Build(ProjectType, DrawingType, SheetSize, Scale);
if (SelectedParamKeys == null)
{
SelectedParamKeys = new List<string>();
}
SelectedParamKeys = SelectedParamKeys
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
if (SelectedPartParamKeys == null)
{
SelectedPartParamKeys = new List<string>();
}
SelectedPartParamKeys = SelectedPartParamKeys
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
// Backward-compat migration: old persisted XML doesn't have ownership info.
// If not configured yet, infer default ownership based on key naming convention.
if (!PartOwnershipConfigured && SelectedParamKeys.Count > 0)
{
SelectedPartParamKeys = SelectedParamKeys
.Where(k => k != null && k.Trim().EndsWith("Prime", StringComparison.OrdinalIgnoreCase))
.ToList();
}
// Keep only keys that are currently selected.
var selectedSet = new HashSet<string>(SelectedParamKeys, StringComparer.OrdinalIgnoreCase);
SelectedPartParamKeys = SelectedPartParamKeys
.Where(k => !string.IsNullOrWhiteSpace(k) && selectedSet.Contains(k.Trim()))
.ToList();
if (NoteBindings == null)
{
NoteBindings = new List<NotePlaceholderBinding>();
}
NoteBindings = NoteBindings
.Where(b => b != null && b.Index > 0 && !string.IsNullOrWhiteSpace(b.ParamKey))
.GroupBy(b => b.Index)
.Select(g => g.First())
.OrderBy(b => b.Index)
.ToList();
}
}
}