From e295709e65b0a5ef479554c011b0d3f2005c66d0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 17 Apr 2023 08:49:26 +0300 Subject: [PATCH] Dxf export button activation fix --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 11 +++-- RhSolutions.AddIn/RhSolutions.AddIn.csproj | 1 + RhSolutions.AddIn/Services/RhDxfWriter.cs | 49 +++++++++++++++++++++ RhSolutions.AddIn/Services/WriterFactory.cs | 24 ++++++++++ RhSolutions.AddIn/Tools/ConvertTool.cs | 5 +++ RhSolutions.AddIn/Tools/DxfTool.cs | 5 +++ RhSolutions.AddIn/Tools/EventsUtil.cs | 2 + RhSolutions.AddIn/Tools/ExportTool.cs | 5 +++ RhSolutions.AddIn/Tools/MergeTool.cs | 9 ++-- RhSolutions.AddIn/Tools/Tool.cs | 9 ++-- RhSolutions.AddIn/Tools/ToolFactory.cs | 8 ++-- 11 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 RhSolutions.AddIn/Services/RhDxfWriter.cs create mode 100644 RhSolutions.AddIn/Services/WriterFactory.cs diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 1cf6db1..ba9cd2f 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -20,11 +20,16 @@ public sealed class RhSolutionsAddIn : IExcelAddIn Services.AddHttpClient() .AddSingleton((Application)ExcelDnaUtil.Application) - .AddSingleton() .AddSingleton() + .AddSingleton() .AddTransient() - .AddTransient() - .AddTransient(); + .AddTransient(); + + Services.AddSingleton(); + Services.AddTransient() + .AddTransient(s => s.GetService()); + Services.AddTransient() + .AddTransient(s => s.GetService()); Services.AddSingleton(); diff --git a/RhSolutions.AddIn/RhSolutions.AddIn.csproj b/RhSolutions.AddIn/RhSolutions.AddIn.csproj index 7073a63..d5ae431 100644 --- a/RhSolutions.AddIn/RhSolutions.AddIn.csproj +++ b/RhSolutions.AddIn/RhSolutions.AddIn.csproj @@ -34,6 +34,7 @@ + diff --git a/RhSolutions.AddIn/Services/RhDxfWriter.cs b/RhSolutions.AddIn/Services/RhDxfWriter.cs new file mode 100644 index 0000000..7088714 --- /dev/null +++ b/RhSolutions.AddIn/Services/RhDxfWriter.cs @@ -0,0 +1,49 @@ +using netDxf; +using netDxf.Header; +using System.IO; +using Line = netDxf.Entities.Line; + +namespace RhSolutions.Services; + +public class RhDxfWriter : IExcelWriter +{ + public void WriteProducts(Dictionary products) + { + WriteProducts(new[] { (string.Empty, products) }); + } + + public void WriteProducts(IEnumerable<(string, Dictionary)> products) + { // your DXF file name + string file = "sample.dxf"; + + // create a new document, by default it will create an AutoCad2000 DXF version + DxfDocument doc = new DxfDocument(); + // an entity + Line entity = new Line(new Vector2(5, 5), new Vector2(10, 5)); + // add your entities here + doc.Entities.Add(entity); + // save to file + doc.Save(file); + + // this check is optional but recommended before loading a DXF file + DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file); + // netDxf is only compatible with AutoCad2000 and higher DXF versions + if (dxfVersion < DxfVersion.AutoCad2000) return; + // load file + DxfDocument loaded = DxfDocument.Load(file); + + + string docPath = + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + + using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, file))) + { + outputFile.Write(loaded); + } + } + + public void Dispose() + { + + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Services/WriterFactory.cs b/RhSolutions.AddIn/Services/WriterFactory.cs new file mode 100644 index 0000000..8238629 --- /dev/null +++ b/RhSolutions.AddIn/Services/WriterFactory.cs @@ -0,0 +1,24 @@ +namespace RhSolutions.Services; + +public class WriterFactory +{ + private readonly IServiceProvider _serviceProvider; + + public WriterFactory(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IExcelWriter GetWriter(string writerName) + { + if (writerName.Equals("Dxf")) + { + return (IExcelWriter)_serviceProvider.GetService(typeof(RhDxfWriter)); + } + + else + { + return (IExcelWriter)_serviceProvider.GetService(typeof(RhExcelWriter)); + } + } +} diff --git a/RhSolutions.AddIn/Tools/ConvertTool.cs b/RhSolutions.AddIn/Tools/ConvertTool.cs index 2d8ded4..cbaaad6 100644 --- a/RhSolutions.AddIn/Tools/ConvertTool.cs +++ b/RhSolutions.AddIn/Tools/ConvertTool.cs @@ -10,11 +10,16 @@ namespace RhSolutions.Tools; #endif internal class ConvertTool : Tool { + public ConvertTool(IServiceProvider provider) : base(provider) + { + } + public override void Execute() { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; var products = _reader.ReadProducts(new[] { worksheet }); + _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); } } \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/DxfTool.cs b/RhSolutions.AddIn/Tools/DxfTool.cs index 4364e62..e011e5d 100644 --- a/RhSolutions.AddIn/Tools/DxfTool.cs +++ b/RhSolutions.AddIn/Tools/DxfTool.cs @@ -9,11 +9,16 @@ namespace RhSolutions.Tools; #endif internal class DxfTool : Tool { + public DxfTool(IServiceProvider provider) : base(provider) + { + } + public override void Execute() { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; var products = _reader.ReadProducts(new[] { worksheet }); + _writer = _writerFactory.GetWriter("Dxf"); _writer.WriteProducts(products); } } diff --git a/RhSolutions.AddIn/Tools/EventsUtil.cs b/RhSolutions.AddIn/Tools/EventsUtil.cs index 99f435f..afcfd64 100644 --- a/RhSolutions.AddIn/Tools/EventsUtil.cs +++ b/RhSolutions.AddIn/Tools/EventsUtil.cs @@ -19,6 +19,7 @@ namespace RhSolutions.Tools RhSolutionsAddIn.Excel.SheetActivate += RefreshConvertButton; RhSolutionsAddIn.Excel.SheetActivate += RefreshDxfButton; RhSolutionsAddIn.Excel.WorkbookActivate += RefreshConvertButton; + RhSolutionsAddIn.Excel.WorkbookActivate += RefreshDxfButton; RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; } @@ -28,6 +29,7 @@ namespace RhSolutions.Tools RhSolutionsAddIn.Excel.SheetActivate -= RefreshConvertButton; RhSolutionsAddIn.Excel.SheetActivate -= RefreshDxfButton; RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshConvertButton; + RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshDxfButton; RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; } diff --git a/RhSolutions.AddIn/Tools/ExportTool.cs b/RhSolutions.AddIn/Tools/ExportTool.cs index 838ce11..ec60d10 100644 --- a/RhSolutions.AddIn/Tools/ExportTool.cs +++ b/RhSolutions.AddIn/Tools/ExportTool.cs @@ -10,10 +10,15 @@ namespace RhSolutions.Tools; #endif internal class ExportTool : Tool { + public ExportTool(IServiceProvider provider) : base(provider) + { + } + public override void Execute() { Application app = RhSolutionsAddIn.Excel.Application; var products = _reader.ReadProducts(app.Selection); + _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); } } diff --git a/RhSolutions.AddIn/Tools/MergeTool.cs b/RhSolutions.AddIn/Tools/MergeTool.cs index e005007..345dea7 100644 --- a/RhSolutions.AddIn/Tools/MergeTool.cs +++ b/RhSolutions.AddIn/Tools/MergeTool.cs @@ -1,11 +1,7 @@ #if !NET472 using System.Runtime.Versioning; -using RhSolutions; -using RhSolutions.Models; -using RhSolutions.Tools; #endif - namespace RhSolutions.Tools; #if !NET472 @@ -13,11 +9,16 @@ namespace RhSolutions.Tools; #endif internal class MergeTool : Tool { + public MergeTool(IServiceProvider provider) : base(provider) + { + } + public override void Execute() { IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); string[] files = dialog.GetFiles(); var products = _reader.ReadProducts(files); + _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); } } diff --git a/RhSolutions.AddIn/Tools/Tool.cs b/RhSolutions.AddIn/Tools/Tool.cs index c5a9738..2869dff 100644 --- a/RhSolutions.AddIn/Tools/Tool.cs +++ b/RhSolutions.AddIn/Tools/Tool.cs @@ -10,12 +10,13 @@ namespace RhSolutions.Tools; internal abstract class Tool : IDisposable { protected readonly IExcelReader _reader; - protected readonly IExcelWriter _writer; + protected readonly WriterFactory _writerFactory; + protected IExcelWriter _writer; - public Tool() + public Tool(IServiceProvider provider) { - _reader = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); - _writer = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); + _reader = provider.GetRequiredService(); + _writerFactory = provider.GetRequiredService(); } public void Dispose() diff --git a/RhSolutions.AddIn/Tools/ToolFactory.cs b/RhSolutions.AddIn/Tools/ToolFactory.cs index 8b746df..49f4db6 100644 --- a/RhSolutions.AddIn/Tools/ToolFactory.cs +++ b/RhSolutions.AddIn/Tools/ToolFactory.cs @@ -6,10 +6,10 @@ internal class ToolFactory { Tool tool = toolName switch { - "export" => new ExportTool(), - "convert" => new ConvertTool(), - "merge" => new MergeTool(), - "dxfexport" => new DxfTool(), + "export" => new ExportTool(RhSolutionsAddIn.ServiceProvider), + "convert" => new ConvertTool(RhSolutionsAddIn.ServiceProvider), + "merge" => new MergeTool(RhSolutionsAddIn.ServiceProvider), + "dxfexport" => new DxfTool(RhSolutionsAddIn.ServiceProvider), _ => throw new Exception("Неизвестный инструмент"), }; return tool;