diff --git a/UI/ParamDrawingPanel.cs b/UI/ParamDrawingPanel.cs index e9bb712..324fb00 100644 --- a/UI/ParamDrawingPanel.cs +++ b/UI/ParamDrawingPanel.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Globalization; +using System.IO; using System.Linq; using System.Windows.Forms; using Autodesk.AutoCAD.ApplicationServices; @@ -25,6 +26,9 @@ namespace CadParamPluging.UI private TextBox _txtLog; private TextBox _txtTemplatePath; + private string _selectedFolderPath; + private string[] _cadFilePaths = new string[0]; + private TemplateInfo _selectedTemplate; private Extents3d? _selectedModelWindow; private string _selectedSheetName; @@ -95,7 +99,7 @@ namespace CadParamPluging.UI grid.Controls.Add(new Label { Text = "特殊条件", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 3); grid.Controls.Add(_cbScale, 1, 3); - var btnSelect = new Button { Text = "选择模板", AutoSize = true }; + var btnSelect = new Button { Text = "选择路径", AutoSize = true }; btnSelect.Click += (_, __) => OnSelectTemplate(); _txtTemplatePath = new TextBox { Width = 160, ReadOnly = true }; @@ -364,38 +368,67 @@ namespace CadParamPluging.UI { try { - using (var dialog = new OpenFileDialog()) + using (var dialog = new FolderBrowserDialog()) { - dialog.Filter = "CAD 模板文件 (*.dwt;*.dwg)|*.dwt;*.dwg|所有文件 (*.*)|*.*"; - dialog.Title = "选择模板文件"; - if (dialog.ShowDialog() == DialogResult.OK) + dialog.Description = "选择包含 CAD 文件的文件夹"; + + if (!string.IsNullOrWhiteSpace(_selectedFolderPath) && Directory.Exists(_selectedFolderPath)) { - var filePath = dialog.FileName; - var fileName = System.IO.Path.GetFileNameWithoutExtension(filePath); - - _selectedTemplate = new TemplateInfo + dialog.SelectedPath = _selectedFolderPath; + } + + if (dialog.ShowDialog(this) == DialogResult.OK) + { + var folderPath = dialog.SelectedPath; + if (string.IsNullOrWhiteSpace(folderPath) || !Directory.Exists(folderPath)) { - Name = fileName, - FilePath = filePath, - Description = "手动选择" - }; - - _txtTemplatePath.Text = filePath; + AppendLog("选择路径无效。"); + return; + } - _lblTemplateInfo.Text = filePath; - AppendLog($"手动选择模板: {filePath}"); + _selectedFolderPath = folderPath; + _txtTemplatePath.Text = folderPath; - TryReplaceDropdownOptionsFromTemplate(filePath); + _cadFilePaths = LoadCadFilesFromFolder(folderPath); + + AppendLog($"已选择路径: {folderPath}"); + AppendLog($"检测到 CAD 文件数量: {_cadFilePaths.Length}"); + + foreach (var p in _cadFilePaths.Take(10)) + { + AppendLog($"- {Path.GetFileName(p)}"); + } + + if (_cadFilePaths.Length > 10) + { + AppendLog($"... 还有 {_cadFilePaths.Length - 10} 个文件"); + } } } } catch (Exception ex) { - AppendLog($"选择模板失败: {ex.Message}"); - Logger.Error("SelectTemplate", ex); + AppendLog($"选择路径失败: {ex.Message}"); + Logger.Error("SelectCadFolder", ex); } } + private static string[] LoadCadFilesFromFolder(string folderPath) + { + if (string.IsNullOrWhiteSpace(folderPath) || !Directory.Exists(folderPath)) + { + return new string[0]; + } + + var patterns = new[] { "*.dwg", "*.dwt", "*.dxf" }; + + return patterns + .SelectMany(p => Directory.EnumerateFiles(folderPath, p, SearchOption.TopDirectoryOnly)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + private void TryReplaceDropdownOptionsFromTemplate(string templatePath) { try