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 SelectedParamKeys { get; set; } /// /// Subset of that should be treated as "零件尺寸". /// Keys not in this set are treated as "锻件尺寸". /// public List SelectedPartParamKeys { get; set; } /// /// Whether the user has explicitly configured ownership for main parameters. /// Used for backward-compatible migration of old configs. /// public bool PartOwnershipConfigured { get; set; } public string NoteTemplateText { get; set; } public List NoteBindings { get; set; } public TemplateSchemaDefinition() { SelectedParamKeys = new List(); SelectedPartParamKeys = new List(); PartOwnershipConfigured = false; NoteTemplateText = string.Empty; NoteBindings = new List(); } 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(); } SelectedParamKeys = SelectedParamKeys .Where(s => !string.IsNullOrWhiteSpace(s)) .Select(s => s.Trim()) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); if (SelectedPartParamKeys == null) { SelectedPartParamKeys = new List(); } 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(SelectedParamKeys, StringComparer.OrdinalIgnoreCase); SelectedPartParamKeys = SelectedPartParamKeys .Where(k => !string.IsNullOrWhiteSpace(k) && selectedSet.Contains(k.Trim())) .ToList(); if (NoteBindings == null) { NoteBindings = new List(); } 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(); } } }