修改模板配置,对图纸参数增加了区分锻件和零件类别
This commit is contained in:
parent
c1d18663bb
commit
1ec011c398
@ -33,7 +33,14 @@ namespace CadParamPluging.Common
|
||||
"OuterDiameter1Prime",
|
||||
"InnerDiameter2Prime",
|
||||
"Height1Prime"
|
||||
}
|
||||
},
|
||||
SelectedPartParamKeys = new List<string>
|
||||
{
|
||||
"OuterDiameter1Prime",
|
||||
"InnerDiameter2Prime",
|
||||
"Height1Prime"
|
||||
},
|
||||
PartOwnershipConfigured = true
|
||||
});
|
||||
|
||||
schemas.Normalize();
|
||||
|
||||
@ -18,12 +18,26 @@ namespace CadParamPluging.Common
|
||||
|
||||
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>();
|
||||
}
|
||||
@ -51,6 +65,32 @@ namespace CadParamPluging.Common
|
||||
.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>();
|
||||
|
||||
@ -198,9 +198,16 @@ namespace CadParamPluging.UI
|
||||
.Select(g => g.First())
|
||||
.ToDictionary(o => o.Index, o => o);
|
||||
|
||||
var mainDefs = new List<ParamDefinition>();
|
||||
var forgingDefs = new List<ParamDefinition>();
|
||||
var partDefs = new List<ParamDefinition>();
|
||||
var seenMain = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var partKeySet = new HashSet<string>(
|
||||
(_schema?.SelectedPartParamKeys ?? new List<string>())
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(s => s.Trim()),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var key in (_schema?.SelectedParamKeys ?? new List<string>()))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
@ -225,7 +232,14 @@ namespace CadParamPluging.UI
|
||||
continue;
|
||||
}
|
||||
|
||||
mainDefs.Add(def);
|
||||
if (partKeySet.Contains(def.Key))
|
||||
{
|
||||
partDefs.Add(def);
|
||||
}
|
||||
else
|
||||
{
|
||||
forgingDefs.Add(def);
|
||||
}
|
||||
}
|
||||
|
||||
var noteBindings = (_schema?.NoteBindings ?? new List<NotePlaceholderBinding>())
|
||||
@ -233,7 +247,7 @@ namespace CadParamPluging.UI
|
||||
.OrderBy(b => b.Index)
|
||||
.ToList();
|
||||
|
||||
if (mainDefs.Count == 0 && noteBindings.Count == 0)
|
||||
if (forgingDefs.Count == 0 && partDefs.Count == 0 && noteBindings.Count == 0)
|
||||
{
|
||||
var rowIdx = _grid.RowCount++;
|
||||
_grid.RowStyles.Add(new RowStyle(SizeType.AutoSize));
|
||||
@ -242,10 +256,19 @@ namespace CadParamPluging.UI
|
||||
return;
|
||||
}
|
||||
|
||||
if (mainDefs.Count > 0)
|
||||
if (forgingDefs.Count > 0)
|
||||
{
|
||||
AddSectionHeader("模板参数");
|
||||
foreach (var def in mainDefs)
|
||||
AddSectionHeader("锻件尺寸");
|
||||
foreach (var def in forgingDefs)
|
||||
{
|
||||
AddField(def, def.Key, def.Label, null);
|
||||
}
|
||||
}
|
||||
|
||||
if (partDefs.Count > 0)
|
||||
{
|
||||
AddSectionHeader("零件尺寸");
|
||||
foreach (var def in partDefs)
|
||||
{
|
||||
AddField(def, def.Key, def.Label, null);
|
||||
}
|
||||
|
||||
@ -9,6 +9,9 @@ namespace CadParamPluging.UI
|
||||
{
|
||||
public class TemplateSchemaBindingForm : Form
|
||||
{
|
||||
private const string OwnershipForging = "锻件尺寸";
|
||||
private const string OwnershipPart = "零件尺寸";
|
||||
|
||||
private readonly ComboBox _cbProjectType;
|
||||
private readonly ComboBox _cbDrawingType;
|
||||
private readonly ComboBox _cbSheetSize;
|
||||
@ -153,6 +156,7 @@ namespace CadParamPluging.UI
|
||||
};
|
||||
_lvParams.Columns.Add("Label", 220);
|
||||
_lvParams.Columns.Add("Key", 160);
|
||||
_lvParams.Columns.Add("归属", 100);
|
||||
|
||||
var paramsButtonPanel = new FlowLayoutPanel
|
||||
{
|
||||
@ -168,10 +172,16 @@ namespace CadParamPluging.UI
|
||||
btnSelectAll.Click += (_, __) => SetAllChecked(true);
|
||||
var btnUnselectAll = new Button { Text = "全不选", AutoSize = true };
|
||||
btnUnselectAll.Click += (_, __) => SetAllChecked(false);
|
||||
var btnSetForging = new Button { Text = "设为锻件尺寸", AutoSize = true };
|
||||
btnSetForging.Click += (_, __) => SetSelectedOwnership(false);
|
||||
var btnSetPart = new Button { Text = "设为零件尺寸", AutoSize = true };
|
||||
btnSetPart.Click += (_, __) => SetSelectedOwnership(true);
|
||||
paramsButtonPanel.Controls.Add(btnUp);
|
||||
paramsButtonPanel.Controls.Add(btnDown);
|
||||
paramsButtonPanel.Controls.Add(btnSelectAll);
|
||||
paramsButtonPanel.Controls.Add(btnUnselectAll);
|
||||
paramsButtonPanel.Controls.Add(btnSetForging);
|
||||
paramsButtonPanel.Controls.Add(btnSetPart);
|
||||
|
||||
_txtNoteTemplate = new TextBox
|
||||
{
|
||||
@ -365,7 +375,7 @@ namespace CadParamPluging.UI
|
||||
.Where(x => !string.Equals(x.Group ?? string.Empty, "备注参数", StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(x => x.Order))
|
||||
{
|
||||
var it = new ListViewItem(new[] { p.Label, p.Key })
|
||||
var it = new ListViewItem(new[] { p.Label, p.Key, GuessOwnershipText(p.Key) })
|
||||
{
|
||||
Tag = p.Key,
|
||||
Checked = false
|
||||
@ -384,9 +394,35 @@ namespace CadParamPluging.UI
|
||||
var selectedKeys = (schema?.SelectedParamKeys ?? new List<string>()).ToList();
|
||||
var keySet = new HashSet<string>(selectedKeys, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var partKeySet = new HashSet<string>(
|
||||
(schema?.SelectedPartParamKeys ?? new List<string>()).Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var all = _lvParams.Items.Cast<ListViewItem>().ToList();
|
||||
var byKey = all.ToDictionary(i => (string)i.Tag, i => i, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var it in all)
|
||||
{
|
||||
var key = (string)it.Tag;
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
EnsureOwnershipSubItem(it);
|
||||
if (partKeySet.Contains(key))
|
||||
{
|
||||
it.SubItems[2].Text = OwnershipPart;
|
||||
}
|
||||
else if (keySet.Contains(key))
|
||||
{
|
||||
it.SubItems[2].Text = OwnershipForging;
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(it.SubItems[2].Text))
|
||||
{
|
||||
it.SubItems[2].Text = GuessOwnershipText(key);
|
||||
}
|
||||
}
|
||||
|
||||
var ordered = new List<ListViewItem>();
|
||||
foreach (var k in selectedKeys)
|
||||
{
|
||||
@ -608,6 +644,56 @@ namespace CadParamPluging.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void SetSelectedOwnership(bool part)
|
||||
{
|
||||
if (_lvParams.SelectedItems.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_lvParams.BeginUpdate();
|
||||
try
|
||||
{
|
||||
foreach (ListViewItem it in _lvParams.SelectedItems)
|
||||
{
|
||||
if (it == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
EnsureOwnershipSubItem(it);
|
||||
it.SubItems[2].Text = part ? OwnershipPart : OwnershipForging;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lvParams.EndUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureOwnershipSubItem(ListViewItem it)
|
||||
{
|
||||
if (it == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while (it.SubItems.Count < 3)
|
||||
{
|
||||
it.SubItems.Add(string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsPartKey(string key)
|
||||
{
|
||||
var k = (key ?? string.Empty).Trim();
|
||||
return k.EndsWith("Prime", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static string GuessOwnershipText(string key)
|
||||
{
|
||||
return IsPartKey(key) ? OwnershipPart : OwnershipForging;
|
||||
}
|
||||
|
||||
private void SetAllChecked(bool value)
|
||||
{
|
||||
_lvParams.BeginUpdate();
|
||||
@ -840,6 +926,30 @@ namespace CadParamPluging.UI
|
||||
|
||||
schema.SelectedParamKeys = selectedParamKeys;
|
||||
|
||||
var selectedPartKeys = _lvParams.Items
|
||||
.Cast<ListViewItem>()
|
||||
.Where(i => i != null && i.Checked)
|
||||
.Select(i =>
|
||||
{
|
||||
var key = ((string)i.Tag ?? string.Empty).Trim();
|
||||
EnsureOwnershipSubItem(i);
|
||||
var own = (i.SubItems[2].Text ?? string.Empty).Trim();
|
||||
if (own.Length == 0)
|
||||
{
|
||||
own = GuessOwnershipText(key);
|
||||
i.SubItems[2].Text = own;
|
||||
}
|
||||
var isPart = string.Equals(own, OwnershipPart, StringComparison.OrdinalIgnoreCase);
|
||||
return new { Key = key, IsPart = isPart };
|
||||
})
|
||||
.Where(x => x.Key.Length > 0 && x.IsPart)
|
||||
.Select(x => x.Key)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
schema.SelectedPartParamKeys = selectedPartKeys;
|
||||
schema.PartOwnershipConfigured = true;
|
||||
|
||||
schema.NoteTemplateText = _txtNoteTemplate.Text ?? string.Empty;
|
||||
var placeholderCount = NoteTemplateEngine.CountPlaceholders(schema.NoteTemplateText);
|
||||
if (_gvNoteBindings.Rows.Count != placeholderCount)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user