diff --git a/Cad/CadContext.cs b/Cad/CadContext.cs new file mode 100644 index 0000000..551a382 --- /dev/null +++ b/Cad/CadContext.cs @@ -0,0 +1,30 @@ +using System; +using Autodesk.AutoCAD.ApplicationServices; +using Autodesk.AutoCAD.DatabaseServices; + +namespace CadParamPluging.Cad +{ + public class CadContext : IDisposable + { + public Document Document { get; } + public Database Database => Document.Database; + public Transaction Transaction { get; private set; } + + public CadContext(Document document) + { + Document = document; + Transaction = Database.TransactionManager.StartTransaction(); + } + + public void Commit() + { + Transaction?.Commit(); + } + + public void Dispose() + { + Transaction?.Dispose(); + Transaction = null; + } + } +} diff --git a/Cad/CadDrawingService.cs b/Cad/CadDrawingService.cs new file mode 100644 index 0000000..63b8e1c --- /dev/null +++ b/Cad/CadDrawingService.cs @@ -0,0 +1,30 @@ +using Autodesk.AutoCAD.DatabaseServices; +using Autodesk.AutoCAD.Geometry; + +namespace CadParamPluging.Cad +{ + public class CadDrawingService + { + private readonly CadContext _context; + + public CadDrawingService(CadContext context) + { + _context = context; + } + + public ObjectId DrawLine(Point3d start, Point3d end) + { + var db = _context.Database; + var tr = _context.Transaction; + var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); + var blockTableRecord = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); + + using (var line = new Line(start, end)) + { + var id = blockTableRecord.AppendEntity(line); + tr.AddNewlyCreatedDBObject(line, true); + return id; + } + } + } +} diff --git a/Cad/TemplateDrawingService.cs b/Cad/TemplateDrawingService.cs new file mode 100644 index 0000000..b6af407 --- /dev/null +++ b/Cad/TemplateDrawingService.cs @@ -0,0 +1,15 @@ +using Autodesk.AutoCAD.ApplicationServices; +using CadParamPluging.Domain.Models; + +namespace CadParamPluging.Cad +{ + public static class TemplateDrawingService + { + public static Document CreateDocumentFromTemplate(TemplateInfo template) + { + var doc = Application.DocumentManager.Add(template.FilePath); + Application.DocumentManager.MdiActiveDocument = doc; + return doc; + } + } +} diff --git a/CadParamPluging.csproj b/CadParamPluging.csproj index 1b6d625..09abb53 100644 --- a/CadParamPluging.csproj +++ b/CadParamPluging.csproj @@ -40,10 +40,16 @@ C:\Program Files\Autodesk\AutoCAD 2014\acmgd.dll + + C:\Program Files\Autodesk\AutoCAD 2014\AdWindows.dll + + + + @@ -53,6 +59,24 @@ + + + + + + + + + + + + + + UserControl + + + Form + diff --git a/Common/BusinessException.cs b/Common/BusinessException.cs new file mode 100644 index 0000000..5afe21b --- /dev/null +++ b/Common/BusinessException.cs @@ -0,0 +1,15 @@ +using System; + +namespace CadParamPluging.Common +{ + public class BusinessException : Exception + { + public BusinessException(string message) : base(message) + { + } + + public BusinessException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/Common/Logger.cs b/Common/Logger.cs new file mode 100644 index 0000000..ce139e9 --- /dev/null +++ b/Common/Logger.cs @@ -0,0 +1,22 @@ +using System.Diagnostics; + +namespace CadParamPluging.Common +{ + public static class Logger + { + public static void Info(string message) + { + Debug.WriteLine(message); + } + + public static void Error(string message) + { + Debug.WriteLine(message); + } + + public static void Error(string message, System.Exception ex) + { + Debug.WriteLine($"{message}: {ex}"); + } + } +} diff --git a/Data/DefaultTemplateRepository.cs b/Data/DefaultTemplateRepository.cs new file mode 100644 index 0000000..aec4097 --- /dev/null +++ b/Data/DefaultTemplateRepository.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CadParamPluging.Domain.Models; + +namespace CadParamPluging.Data +{ + public class DefaultTemplateRepository : ITemplateRepository + { + private readonly List _templates; + + public DefaultTemplateRepository() + { + _templates = new List + { + new TemplateDefinition + { + ProjectType = "结构", + DrawingType = "基础平面", + SheetSize = "A1", + Scale = "1:50", + FilePath = "C\\CadTemplates\\Stru_Base_A1_50.dwt", + Description = "结构基础平面 A1 1:50" + }, + new TemplateDefinition + { + ProjectType = "示例", + DrawingType = "示例图", + SheetSize = "A3", + Scale = "1:100", + FilePath = "C\\CadTemplates\\Sample_A3_100.dwt", + Description = "示例模板 A3 1:100" + } + }; + } + + public TemplateDefinition FindTemplate(TemplateParams templateParams) + { + if (templateParams == null) + { + return null; + } + + var matches = _templates.Where(t => Matches(t.ProjectType, templateParams.ProjectType) + && Matches(t.DrawingType, templateParams.DrawingType) + && Matches(t.SheetSize, templateParams.SheetSize) + && Matches(t.Scale, templateParams.Scale)) + .ToList(); + + if (matches.Count == 1) + { + return matches.First(); + } + + return null; + } + + private static bool Matches(string source, string input) + { + if (string.IsNullOrWhiteSpace(input)) + { + return true; + } + + return source != null && input != null && source.Equals(input, StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/Data/ITemplateRepository.cs b/Data/ITemplateRepository.cs new file mode 100644 index 0000000..1bdbb3e --- /dev/null +++ b/Data/ITemplateRepository.cs @@ -0,0 +1,9 @@ +using CadParamPluging.Domain.Models; + +namespace CadParamPluging.Data +{ + public interface ITemplateRepository + { + TemplateDefinition FindTemplate(TemplateParams templateParams); + } +} diff --git a/Data/TemplateDefinition.cs b/Data/TemplateDefinition.cs new file mode 100644 index 0000000..7879d2b --- /dev/null +++ b/Data/TemplateDefinition.cs @@ -0,0 +1,12 @@ +namespace CadParamPluging.Data +{ + public class TemplateDefinition + { + public string ProjectType { get; set; } + public string DrawingType { get; set; } + public string SheetSize { get; set; } + public string Scale { get; set; } + public string FilePath { get; set; } + public string Description { get; set; } + } +} diff --git a/Domain/DomainFacade.cs b/Domain/DomainFacade.cs new file mode 100644 index 0000000..91c50a9 --- /dev/null +++ b/Domain/DomainFacade.cs @@ -0,0 +1,82 @@ +using System; +using Autodesk.AutoCAD.Geometry; +using CadParamPluging.Cad; +using CadParamPluging.Common; +using CadParamPluging.Data; +using CadParamPluging.Domain.Models; + +namespace CadParamPluging.Domain +{ + public static class DomainFacade + { + private static readonly ITemplateRepository TemplateRepository = new DefaultTemplateRepository(); + + public static TemplateInfo SelectTemplate(TemplateParams templateParams) + { + var definition = TemplateRepository.FindTemplate(templateParams); + + if (definition == null) + { + throw new BusinessException("未找到匹配的模板,或匹配结果不唯一。"); + } + + return new TemplateInfo + { + Name = definition.Description ?? definition.FilePath, + FilePath = definition.FilePath, + Description = definition.Description + }; + } + + public static void ValidateParameters(TemplateParams templateParams, DrawingParams drawingParams) + { + if (templateParams == null) + { + throw new BusinessException("模板参数不能为空。"); + } + + if (string.IsNullOrWhiteSpace(templateParams.ProjectType) || + string.IsNullOrWhiteSpace(templateParams.DrawingType)) + { + throw new BusinessException("请填写项目类型和图纸类型。"); + } + + if (drawingParams == null || !drawingParams.HasValidSize()) + { + throw new BusinessException("请填写有效的出图尺寸(宽度和高度需大于 0)。"); + } + } + + public static void DrawByParams(TemplateInfo template, DrawingParams drawingParams, CadDrawingService cadService) + { + if (template == null) + { + throw new ArgumentNullException(nameof(template)); + } + + if (cadService == null) + { + throw new ArgumentNullException(nameof(cadService)); + } + + var width = drawingParams?.Width ?? 0; + var height = drawingParams?.Height ?? 0; + + if (width <= 0 || height <= 0) + { + width = width <= 0 ? 5000 : width; + height = height <= 0 ? 3000 : height; + } + + var p1 = new Point3d(0, 0, 0); + var p2 = new Point3d(width, 0, 0); + var p3 = new Point3d(width, height, 0); + var p4 = new Point3d(0, height, 0); + + cadService.DrawLine(p1, p2); + cadService.DrawLine(p2, p3); + cadService.DrawLine(p3, p4); + cadService.DrawLine(p4, p1); + } + } +} diff --git a/Domain/Models/DrawingParams.cs b/Domain/Models/DrawingParams.cs new file mode 100644 index 0000000..cdea194 --- /dev/null +++ b/Domain/Models/DrawingParams.cs @@ -0,0 +1,13 @@ +namespace CadParamPluging.Domain.Models +{ + public class DrawingParams + { + public double Width { get; set; } + public double Height { get; set; } + + public bool HasValidSize() + { + return Width > 0 && Height > 0; + } + } +} diff --git a/Domain/Models/TemplateInfo.cs b/Domain/Models/TemplateInfo.cs new file mode 100644 index 0000000..089fa79 --- /dev/null +++ b/Domain/Models/TemplateInfo.cs @@ -0,0 +1,9 @@ +namespace CadParamPluging.Domain.Models +{ + public class TemplateInfo + { + public string Name { get; set; } + public string FilePath { get; set; } + public string Description { get; set; } + } +} diff --git a/Domain/Models/TemplateParams.cs b/Domain/Models/TemplateParams.cs new file mode 100644 index 0000000..39f432d --- /dev/null +++ b/Domain/Models/TemplateParams.cs @@ -0,0 +1,10 @@ +namespace CadParamPluging.Domain.Models +{ + public class TemplateParams + { + public string ProjectType { get; set; } + public string DrawingType { get; set; } + public string SheetSize { get; set; } + public string Scale { get; set; } + } +} diff --git a/PluginEntry.cs b/PluginEntry.cs index 6eaed49..4a1e593 100644 --- a/PluginEntry.cs +++ b/PluginEntry.cs @@ -1,17 +1,30 @@ +using System; using System.Drawing; -using System.Windows.Forms; +using System.IO; +using System.Linq; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Windows; - +using Autodesk.Windows; +using CadParamPluging.UI; namespace CadParamPluging { public class PluginEntry : IExtensionApplication { private static PaletteSet _palette; + private static bool _ribbonButtonCreated; + private const string RibbonTabId = "CadParamPlugingTab"; + private const string RibbonPanelId = "CadParamPlugingPanel"; + private const string RibbonButtonId = "CadParamPlugingPanelButton"; public void Initialize() { - ShowPalette(); + EnsurePaletteCreated(); + CreateRibbonButton(); + Application.Idle += OnApplicationIdle; } public void Terminate() @@ -19,7 +32,7 @@ namespace CadParamPluging // 可在此释放资源或记录日志 } - private void ShowPalette() + private void EnsurePaletteCreated() { if (_palette == null) { @@ -27,21 +40,169 @@ namespace CadParamPluging { Style = PaletteSetStyles.ShowCloseButton | PaletteSetStyles.ShowAutoHideButton | - PaletteSetStyles.Snappable + PaletteSetStyles.Snappable, + DockEnabled = DockSides.Right, + Dock = DockSides.Right }; - var placeholder = new Label - { - Text = "Param panel placeholder", - Dock = DockStyle.Fill, - TextAlign = ContentAlignment.MiddleCenter - }; - - _palette.Add("Main", placeholder); + var panel = new ParamDrawingPanel(); + _palette.Add("Main", panel); _palette.MinimumSize = new Size(240, 200); } + } + private void ShowPalette() + { + EnsurePaletteCreated(); + + _palette.Dock = DockSides.Right; _palette.Visible = true; + _palette.Activate(0); + } + + private void CreateRibbonButton() + { + var ribbon = ComponentManager.Ribbon; + if (ribbon == null) + { + return; + } + + if (_ribbonButtonCreated) + { + return; + } + + var tab = ribbon.Tabs.FirstOrDefault(t => t.Id == RibbonTabId); + + if (tab == null) + { + tab = ribbon.Tabs.FirstOrDefault(t => + string.Equals(t.Title, "插件", StringComparison.OrdinalIgnoreCase) || + string.Equals(t.Title, "Add-Ins", StringComparison.OrdinalIgnoreCase) || + string.Equals(t.Title, "Add-ins", StringComparison.OrdinalIgnoreCase)); + } + if (tab == null) + { + tab = new RibbonTab + { + Title = "参数化出图", + Id = RibbonTabId + }; + ribbon.Tabs.Add(tab); + } + + var panel = tab.Panels.FirstOrDefault(p => p.Source != null && p.Source.Id == RibbonPanelId); + if (panel == null) + { + var source = new RibbonPanelSource + { + Id = RibbonPanelId, + Title = "插件" + }; + panel = new RibbonPanel { Source = source }; + tab.Panels.Add(panel); + } + + var existingButton = panel.Source.Items + .OfType() + .FirstOrDefault(b => b.Id == RibbonButtonId); + + if (existingButton == null) + { + panel.Source.Items.Clear(); + + var button = new RibbonButton + { + Id = RibbonButtonId, + Name = "参数面板", + Text = "参数面板", + ShowText = true, + ShowImage = true, + Size = RibbonItemSize.Large, + Orientation = System.Windows.Controls.Orientation.Vertical, + CommandHandler = new RelayCommand(ShowPalette) + }; + + var img = LoadImage("param_panel.png"); + if (img != null) + { + button.LargeImage = img; + button.Image = img; + } + + var row = new RibbonRowPanel(); + row.Items.Add(button); + panel.Source.Items.Add(row); + } + + tab.IsActive = true; + _ribbonButtonCreated = true; + } + + private void OnApplicationIdle(object sender, EventArgs e) + { + if (_ribbonButtonCreated) + { + Application.Idle -= OnApplicationIdle; + return; + } + + CreateRibbonButton(); + } + + private static ImageSource LoadImage(string fileName) + { + try + { + var asmPath = typeof(PluginEntry).Assembly.Location; + var dir = Path.GetDirectoryName(asmPath); + if (string.IsNullOrEmpty(dir)) + { + return null; + } + + var fullPath = Path.Combine(dir, fileName); + if (!File.Exists(fullPath)) + { + return null; + } + + var img = new BitmapImage(); + img.BeginInit(); + img.UriSource = new Uri(fullPath, UriKind.Absolute); + img.CacheOption = BitmapCacheOption.OnLoad; + img.EndInit(); + img.Freeze(); + return img; + } + catch + { + return null; + } + } + + private class RelayCommand : ICommand + { + private readonly System.Action _action; + + public RelayCommand(System.Action action) + { + _action = action; + } + + public bool CanExecute(object parameter) => true; + + public void Execute(object parameter) + { + _action?.Invoke(); + } + + public event System.EventHandler CanExecuteChanged + { + add { } + remove { } + } } [CommandMethod("PARAM_PANEL")] diff --git a/UI/ParamDrawingPanel.cs b/UI/ParamDrawingPanel.cs new file mode 100644 index 0000000..c5f38bc --- /dev/null +++ b/UI/ParamDrawingPanel.cs @@ -0,0 +1,400 @@ +using System; +using System.Drawing; +using System.Globalization; +using System.Windows.Forms; +using Autodesk.AutoCAD.ApplicationServices; +using Autodesk.AutoCAD.DatabaseServices; +using CadParamPluging.Cad; +using CadParamPluging.Common; +using CadParamPluging.Domain; +using CadParamPluging.Domain.Models; +using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application; + +namespace CadParamPluging.UI +{ + public class ParamDrawingPanel : UserControl + { + private readonly ComboBox _cbProjectType; + private readonly ComboBox _cbDrawingType; + private readonly ComboBox _cbSheetSize; + private readonly ComboBox _cbScale; + private readonly TextBox _txtWidth; + private readonly TextBox _txtHeight; + private Label _lblTemplateInfo; + private TextBox _txtLog; + private TextBox _txtTemplatePath; + + private TemplateInfo _selectedTemplate; + + public ParamDrawingPanel() + { + Dock = DockStyle.Fill; + + var layout = new TableLayoutPanel + { + Dock = DockStyle.Fill, + ColumnCount = 1, + RowCount = 4, + AutoSize = true, + AutoSizeMode = AutoSizeMode.GrowAndShrink, + Padding = new Padding(8) + }; + + _cbProjectType = CreateComboBox(new[] { "结构", "示例" }); + _cbDrawingType = CreateComboBox(new[] { "基础平面", "示例图" }); + _cbSheetSize = CreateComboBox(new[] { "A1", "A3" }); + _cbScale = CreateComboBox(new[] { "1:50", "1:100" }); + _txtWidth = CreateTextBox("5000"); + _txtHeight = CreateTextBox("3000"); + + var templateGroup = BuildTemplateGroup(); + var drawingGroup = BuildDrawingGroup(); + var actionPanel = BuildActionPanel(); + var logGroup = BuildLogGroup(); + + layout.Controls.Add(templateGroup, 0, 0); + layout.Controls.Add(drawingGroup, 0, 1); + layout.Controls.Add(actionPanel, 0, 2); + layout.Controls.Add(logGroup, 0, 3); + + Controls.Add(layout); + } + + private GroupBox BuildTemplateGroup() + { + var group = new GroupBox + { + Text = "模板参数", + Dock = DockStyle.Top, + AutoSize = true, + AutoSizeMode = AutoSizeMode.GrowAndShrink + }; + + var grid = new TableLayoutPanel + { + Dock = DockStyle.Fill, + ColumnCount = 2, + RowCount = 6, + AutoSize = true + }; + + grid.Controls.Add(new Label { Text = "交付状态", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 0); + grid.Controls.Add(_cbProjectType, 1, 0); + + grid.Controls.Add(new Label { Text = "工艺方法", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 1); + grid.Controls.Add(_cbDrawingType, 1, 1); + + grid.Controls.Add(new Label { Text = "结构特征", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 2); + grid.Controls.Add(_cbSheetSize, 1, 2); + + 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 }; + btnSelect.Click += (_, __) => OnSelectTemplate(); + + _txtTemplatePath = new TextBox { Width = 160, ReadOnly = true }; + + 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); + + group.Controls.Add(grid); + return group; + } + + private GroupBox BuildDrawingGroup() + { + var group = new GroupBox + { + Text = "出图参数", + Dock = DockStyle.Top, + AutoSize = true, + AutoSizeMode = AutoSizeMode.GrowAndShrink + }; + + var grid = new TableLayoutPanel + { + Dock = DockStyle.Fill, + ColumnCount = 2, + RowCount = 3, + AutoSize = true + }; + + grid.Controls.Add(new Label { Text = "宽度(mm)", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 0); + grid.Controls.Add(_txtWidth, 1, 0); + + grid.Controls.Add(new Label { Text = "高度(mm)", AutoSize = true, TextAlign = ContentAlignment.MiddleLeft }, 0, 1); + grid.Controls.Add(_txtHeight, 1, 1); + + group.Controls.Add(grid); + return group; + } + + private FlowLayoutPanel BuildActionPanel() + { + var panel = new FlowLayoutPanel + { + Dock = DockStyle.Top, + AutoSize = true + }; + + var btnGenerate = new Button { Text = "生成图纸", AutoSize = true }; + btnGenerate.Click += (_, __) => OnGenerateDrawing(); + + var btnSaveAs = new Button { Text = "保存当前图纸", AutoSize = true }; + btnSaveAs.Click += (_, __) => OnSaveDrawing(); + + var btnSettings = new Button { Text = "设置", AutoSize = true }; + btnSettings.Click += (_, __) => OnOpenSettings(); + + panel.Controls.Add(btnGenerate); + panel.Controls.Add(btnSaveAs); + panel.Controls.Add(btnSettings); + return panel; + } + + private GroupBox BuildLogGroup() + { + var group = new GroupBox + { + Text = "日志", + Dock = DockStyle.Fill + }; + + _txtLog = new TextBox + { + Multiline = true, + ScrollBars = ScrollBars.Vertical, + Dock = DockStyle.Fill, + Height = 140 + }; + + group.Controls.Add(_txtLog); + return group; + } + + private void OnMatchTemplate() + { + try + { + var tplParams = CollectTemplateParams(); + _selectedTemplate = DomainFacade.SelectTemplate(tplParams); + _lblTemplateInfo.Text = $"已匹配: {_selectedTemplate.Name}"; + _txtTemplatePath.Text = _selectedTemplate.FilePath; + AppendLog($"模板: {_selectedTemplate.FilePath}"); + } + catch (BusinessException bex) + { + _selectedTemplate = null; + _lblTemplateInfo.Text = "未匹配模板"; + _txtTemplatePath.Text = ""; + AppendLog($"模板选择错误: {bex.Message}"); + } + catch (Exception ex) + { + _selectedTemplate = null; + _lblTemplateInfo.Text = "未匹配模板"; + _txtTemplatePath.Text = ""; + AppendLog($"异常: {ex.Message}"); + Logger.Error("MatchTemplate", ex); + } + } + + private void OnSelectTemplate() + { + try + { + using (var dialog = new OpenFileDialog()) + { + dialog.Filter = "CAD 模板文件 (*.dwt;*.dwg)|*.dwt;*.dwg|所有文件 (*.*)|*.*"; + dialog.Title = "选择模板文件"; + if (dialog.ShowDialog() == DialogResult.OK) + { + var filePath = dialog.FileName; + var fileName = System.IO.Path.GetFileNameWithoutExtension(filePath); + + _selectedTemplate = new TemplateInfo + { + Name = fileName, + FilePath = filePath, + Description = "手动选择" + }; + + _txtTemplatePath.Text = filePath; + _lblTemplateInfo.Text = $"已选择: {fileName}"; + AppendLog($"手动选择模板: {filePath}"); + } + } + } + catch (Exception ex) + { + AppendLog($"选择模板失败: {ex.Message}"); + Logger.Error("SelectTemplate", ex); + } + } + + private void OnGenerateDrawing() + { + try + { + var tplParams = CollectTemplateParams(); + var drawingParams = CollectDrawingParams(); + + if (_selectedTemplate == null) + { + _selectedTemplate = DomainFacade.SelectTemplate(tplParams); + _lblTemplateInfo.Text = $"已匹配: {_selectedTemplate.Name}"; + } + + DomainFacade.ValidateParameters(tplParams, drawingParams); + + var doc = TemplateDrawingService.CreateDocumentFromTemplate(_selectedTemplate); + + using (doc.LockDocument()) + using (var ctx = new CadContext(doc)) + { + var cadService = new CadDrawingService(ctx); + DomainFacade.DrawByParams(_selectedTemplate, drawingParams, cadService); + ctx.Commit(); + } + + AppendLog("图纸生成完成。"); + } + catch (BusinessException bex) + { + AppendLog($"参数错误: {bex.Message}"); + } + catch (Exception ex) + { + AppendLog($"生成失败: {ex.Message}"); + Logger.Error("GenerateDrawing", ex); + } + } + + private void OnSaveDrawing() + { + try + { + var doc = AcadApp.DocumentManager.MdiActiveDocument; + if (doc == null) + { + AppendLog("未找到活动图纸,无法保存。"); + return; + } + + using (var dialog = new SaveFileDialog + { + Filter = "AutoCAD Drawing (*.dwg)|*.dwg", + FileName = "新图纸.dwg" + }) + { + if (dialog.ShowDialog() == DialogResult.OK) + { + doc.Database.SaveAs(dialog.FileName, true, DwgVersion.Current, doc.Database.SecurityParameters); + AppendLog($"图纸已保存到: {dialog.FileName}"); + } + } + } + catch (Exception ex) + { + AppendLog($"保存失败: {ex.Message}"); + Logger.Error("SaveDrawing", ex); + } + } + + private void OnOpenSettings() + { + try + { + using (var form = new SettingsForm()) + { + if (form.ShowDialog() == DialogResult.OK) + { + // TODO: Save settings + AppendLog("设置已保存 (暂无实际生效项)"); + } + } + } + catch (Exception ex) + { + AppendLog($"打开设置失败: {ex.Message}"); + Logger.Error("OpenSettings", ex); + } + } + + private TemplateParams CollectTemplateParams() + { + return new TemplateParams + { + ProjectType = _cbProjectType.Text?.Trim(), + DrawingType = _cbDrawingType.Text?.Trim(), + SheetSize = _cbSheetSize.Text?.Trim(), + Scale = _cbScale.Text?.Trim() + }; + } + + private DrawingParams CollectDrawingParams() + { + return new DrawingParams + { + Width = ParseDouble(_txtWidth.Text), + Height = ParseDouble(_txtHeight.Text) + }; + } + + private static ComboBox CreateComboBox(string[] items) + { + var cb = new ComboBox + { + DropDownStyle = ComboBoxStyle.DropDownList, + Width = 160 + }; + cb.Items.AddRange(items); + if (cb.Items.Count > 0) + { + cb.SelectedIndex = 0; + } + return cb; + } + + private static TextBox CreateTextBox(string defaultText) + { + return new TextBox { Width = 160, Text = defaultText }; + } + + private static double ParseDouble(string text) + { + if (double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) + { + return value; + } + + if (double.TryParse(text, NumberStyles.Float, CultureInfo.CurrentCulture, out value)) + { + return value; + } + + return 0; + } + + private void AppendLog(string message) + { + var time = DateTime.Now.ToString("HH:mm:ss"); + _txtLog.AppendText($"[{time}] {message}{Environment.NewLine}"); + } + } +} diff --git a/UI/SettingsForm.cs b/UI/SettingsForm.cs new file mode 100644 index 0000000..406b013 --- /dev/null +++ b/UI/SettingsForm.cs @@ -0,0 +1,67 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace CadParamPluging.UI +{ + public class SettingsForm : Form + { + public SettingsForm() + { + Text = "设置"; + Size = new Size(400, 300); + StartPosition = FormStartPosition.CenterScreen; + FormBorderStyle = FormBorderStyle.FixedDialog; + MaximizeBox = false; + MinimizeBox = false; + ShowInTaskbar = false; + + var layout = new TableLayoutPanel + { + Dock = DockStyle.Fill, + Padding = new Padding(10), + RowCount = 2, + ColumnCount = 1 + }; + layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); // Content area + layout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Buttons area + + // Placeholder for future settings + var contentPanel = new GroupBox + { + Text = "常规设置", + Dock = DockStyle.Fill + }; + var lblPlaceholder = new Label + { + Text = "更多设置项即将推出...", + AutoSize = true, + Location = new Point(20, 30), + ForeColor = Color.Gray + }; + contentPanel.Controls.Add(lblPlaceholder); + + // Bottom buttons + var flowButtons = new FlowLayoutPanel + { + Dock = DockStyle.Fill, + FlowDirection = FlowDirection.RightToLeft, + AutoSize = true + }; + + var btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel }; + var btnOk = new Button { Text = "确定", DialogResult = DialogResult.OK }; + + flowButtons.Controls.Add(btnCancel); + flowButtons.Controls.Add(btnOk); + + layout.Controls.Add(contentPanel, 0, 0); + layout.Controls.Add(flowButtons, 0, 1); + + Controls.Add(layout); + + AcceptButton = btnOk; + CancelButton = btnCancel; + } + } +}