diff --git a/CadParamPluging.csproj b/CadParamPluging.csproj
index 496bd60..778600f 100644
--- a/CadParamPluging.csproj
+++ b/CadParamPluging.csproj
@@ -118,6 +118,7 @@
Form
+
diff --git a/Common/DropdownOptions.cs b/Common/DropdownOptions.cs
index ce19aa9..f38200e 100644
--- a/Common/DropdownOptions.cs
+++ b/Common/DropdownOptions.cs
@@ -9,6 +9,7 @@ namespace CadParamPluging.Common
public List ProcessMethods { get; set; }
public List StructuralFeatures { get; set; }
public List SpecialConditions { get; set; }
+ public string DefaultTemplatePath { get; set; }
public DropdownOptions()
{
diff --git a/UI/ParamDrawingPanel.cs b/UI/ParamDrawingPanel.cs
index 4b6290f..484be1c 100644
--- a/UI/ParamDrawingPanel.cs
+++ b/UI/ParamDrawingPanel.cs
@@ -21,7 +21,9 @@ namespace CadParamPluging.UI
private readonly ComboBox _cbDrawingType;
private readonly ComboBox _cbSheetSize;
private readonly ComboBox _cbScale;
- private Label _lblTemplateInfo;
+ private ComboBox _cbTemplateList;
+ private Button _btnRefreshTemplates;
+
private TextBox _txtLog;
private TextBox _txtTemplatePath;
@@ -53,6 +55,18 @@ namespace CadParamPluging.UI
_cbSheetSize = CreateComboBox(dropdownOptions.StructuralFeatures);
_cbScale = CreateComboBox(dropdownOptions.SpecialConditions);
+ // Initialize template controls here in constructor to follow readonly/non-readonly best practices,
+ // though they are no longer readonly.
+ _btnRefreshTemplates = new Button { Text = "刷新列表", AutoSize = true };
+ _btnRefreshTemplates.Click += (_, __) => RefreshTemplateList();
+
+ _cbTemplateList = new ComboBox
+ {
+ DropDownStyle = ComboBoxStyle.DropDownList,
+ Dock = DockStyle.Fill
+ };
+ _cbTemplateList.SelectedIndexChanged += OnTemplateSelectionChanged;
+
var templateGroup = BuildTemplateGroup();
var drawingGroup = BuildDrawingGroup();
var actionPanel = BuildActionPanel();
@@ -62,8 +76,26 @@ namespace CadParamPluging.UI
layout.Controls.Add(drawingGroup, 0, 1);
layout.Controls.Add(actionPanel, 0, 2);
layout.Controls.Add(logGroup, 0, 3);
-
+
Controls.Add(layout);
+
+ if (!string.IsNullOrWhiteSpace(dropdownOptions.DefaultTemplatePath)
+ && Directory.Exists(dropdownOptions.DefaultTemplatePath))
+ {
+ _selectedFolderPath = dropdownOptions.DefaultTemplatePath;
+ _txtTemplatePath.Text = dropdownOptions.DefaultTemplatePath;
+ _cadFilePaths = LoadCadFilesFromFolder(_selectedFolderPath);
+
+ // Use BeginInvoke to ensure UI is ready
+ var timer = new Timer { Interval = 100 };
+ timer.Tick += (s, e) =>
+ {
+ timer.Stop();
+ timer.Dispose();
+ RefreshTemplateList();
+ };
+ timer.Start();
+ }
}
private GroupBox BuildTemplateGroup()
@@ -104,19 +136,8 @@ namespace CadParamPluging.UI
grid.Controls.Add(btnSelect, 0, 4);
grid.Controls.Add(_txtTemplatePath, 1, 4);
- var btnMatch = new Button { Text = "匹配模板", AutoSize = true };
- btnMatch.Click += (_, __) => OnMatchTemplate();
-
- _lblTemplateInfo = new Label
- {
- Text = "未匹配模板",
- AutoSize = true,
- ForeColor = Color.DarkBlue,
- TextAlign = ContentAlignment.MiddleLeft
- };
-
- grid.Controls.Add(btnMatch, 0, 5);
- grid.Controls.Add(_lblTemplateInfo, 1, 5);
+ grid.Controls.Add(_btnRefreshTemplates, 0, 5);
+ grid.Controls.Add(_cbTemplateList, 1, 5);
group.Controls.Add(grid);
return group;
@@ -190,162 +211,105 @@ namespace CadParamPluging.UI
return group;
}
- private void OnMatchTemplate()
+ private void RefreshTemplateList()
{
try
{
- if (_cadFilePaths == null || _cadFilePaths.Length == 0)
- {
- AppendLog("请先选择路径。");
- return;
- }
-
- AppendLog("开始匹配模板...");
-
- var tplParams = CollectTemplateParams();
-
- foreach (var cadPath in _cadFilePaths)
- {
- if (string.IsNullOrWhiteSpace(cadPath) || !File.Exists(cadPath))
- {
- continue;
- }
-
- // 1) 优先按 Layout(PaperSpace) 匹配
- var layoutCandidates = TemplateLayoutAnnotationExtractor.ExtractPerLayout(cadPath);
- var layoutMatch = layoutCandidates.FirstOrDefault(c => LayoutMatches(c, tplParams));
- if (layoutMatch != null)
- {
- _selectedTemplate = new TemplateInfo
- {
- Name = Path.GetFileName(cadPath),
- FilePath = cadPath
- };
-
- _selectedTemplate.LayoutName = layoutMatch.LayoutName;
- _selectedModelWindow = null;
- _selectedSheetName = layoutMatch.LayoutName;
-
- _lblTemplateInfo.Text = $"{cadPath} | {layoutMatch.LayoutName}";
- AppendLog($"匹配成功: {Path.GetFileName(cadPath)} | Layout: {layoutMatch.LayoutName}");
- return;
- }
-
- // 2) Layout 未命中时,尝试按 ModelSpace 窗口候选匹配
- var modelCandidates = TemplateModelSheetExtractor.ExtractCandidates(cadPath);
- var modelMatch = modelCandidates.FirstOrDefault(c => ModelMatches(c, tplParams));
- if (modelMatch != null)
- {
- _selectedTemplate = new TemplateInfo
- {
- Name = Path.GetFileName(cadPath),
- FilePath = cadPath
- };
-
- _selectedTemplate.LayoutName = null;
- _selectedModelWindow = modelMatch.Window;
- _selectedSheetName = modelMatch.Name;
-
- _lblTemplateInfo.Text = $"{cadPath} | {modelMatch.Name}";
- AppendLog($"匹配成功: {Path.GetFileName(cadPath)} | Model: {modelMatch.Name}");
- return;
- }
- }
-
+ _cbTemplateList.Items.Clear();
_selectedTemplate = null;
_selectedModelWindow = null;
_selectedSheetName = null;
- _lblTemplateInfo.Text = "未匹配模板";
- AppendLog("未找到匹配模板。");
- }
- catch (BusinessException bex)
- {
- if (_selectedTemplate != null)
+
+ if (_cadFilePaths == null || _cadFilePaths.Length == 0)
{
- _selectedTemplate.LayoutName = null;
+ AppendLog("未加载CAD文件,请先选择路径。");
+ return;
+ }
+
+ AppendLog("开始扫描模板...");
+ var items = new System.Collections.Generic.List();
+
+ foreach (var cadPath in _cadFilePaths)
+ {
+ if (string.IsNullOrWhiteSpace(cadPath) || !File.Exists(cadPath)) continue;
+
+ try
+ {
+ // 1) Layouts
+ var layouts = TemplateLayoutAnnotationExtractor.ExtractPerLayout(cadPath);
+ foreach (var l in layouts)
+ {
+ items.Add(new TemplateSelectionItem(new TemplateInfo
+ {
+ Name = Path.GetFileName(cadPath),
+ FilePath = cadPath,
+ LayoutName = l.LayoutName
+ }));
+ }
+
+ // 2) Model Space Windows
+ var models = TemplateModelSheetExtractor.ExtractCandidates(cadPath);
+ foreach (var m in models)
+ {
+ var item = new TemplateSelectionItem(new TemplateInfo
+ {
+ Name = Path.GetFileName(cadPath),
+ FilePath = cadPath,
+ LayoutName = null // Model space
+ });
+ item.ModelWindow = m.Window;
+ // Hint: TemplateInfo doesn't hold the 'Name' of the sheet if it's ModelSpace named candidate
+ // But usually DisplayName handles it. We can store the sheet name in Info.LayoutName or just use ToString override
+ // Actually TemplateInfo.LayoutName is used for opening.
+ // For model space candidates, we usually matched them by 'Name' in auto-match.
+ // Here we just list them.
+ items.Add(item);
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex); // silent fail for single file
+ }
+ }
+
+ if (items.Count == 0)
+ {
+ AppendLog("未在路径下找到有效的模板/布局。");
+ return;
+ }
+
+ _cbTemplateList.Items.AddRange(items.Cast