Dxf export button activation fix

This commit is contained in:
Sergey Chebotar 2023-04-17 08:49:26 +03:00
parent b6254b1565
commit e295709e65
11 changed files with 113 additions and 15 deletions

View File

@ -20,11 +20,16 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
Services.AddHttpClient() Services.AddHttpClient()
.AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton((Application)ExcelDnaUtil.Application)
.AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddSingleton<IAddInConfiguration, RhAddInConfiguration>() .AddSingleton<IAddInConfiguration, RhAddInConfiguration>()
.AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddTransient<IFileDialog, ExcelFileDialog>() .AddTransient<IFileDialog, ExcelFileDialog>()
.AddTransient<IExcelReader, RhExcelReader>() .AddTransient<IExcelReader, RhExcelReader>();
.AddTransient<IExcelWriter, RhExcelWriter>();
Services.AddSingleton<WriterFactory>();
Services.AddTransient<RhExcelWriter>()
.AddTransient<IExcelWriter, RhExcelWriter>(s => s.GetService<RhExcelWriter>());
Services.AddTransient<RhDxfWriter>()
.AddTransient<IExcelWriter, RhDxfWriter>(s => s.GetService<RhDxfWriter>());
Services.AddSingleton<ToolFactory>(); Services.AddSingleton<ToolFactory>();

View File

@ -34,6 +34,7 @@
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="netDxf" Version="2022.11.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RhSolutions.Sku" Version="0.1.1" /> <PackageReference Include="RhSolutions.Sku" Version="0.1.1" />
<PackageReference Include="System.Buffers" Version="4.5.1" /> <PackageReference Include="System.Buffers" Version="4.5.1" />

View File

@ -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<Product, double> products)
{
WriteProducts(new[] { (string.Empty, products) });
}
public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> 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()
{
}
}

View File

@ -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));
}
}
}

View File

@ -10,11 +10,16 @@ namespace RhSolutions.Tools;
#endif #endif
internal class ConvertTool : Tool internal class ConvertTool : Tool
{ {
public ConvertTool(IServiceProvider provider) : base(provider)
{
}
public override void Execute() public override void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
var products = _reader.ReadProducts(new[] { worksheet }); var products = _reader.ReadProducts(new[] { worksheet });
_writer = _writerFactory.GetWriter("Excel");
_writer.WriteProducts(products); _writer.WriteProducts(products);
} }
} }

View File

@ -9,11 +9,16 @@ namespace RhSolutions.Tools;
#endif #endif
internal class DxfTool : Tool internal class DxfTool : Tool
{ {
public DxfTool(IServiceProvider provider) : base(provider)
{
}
public override void Execute() public override void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
var products = _reader.ReadProducts(new[] { worksheet }); var products = _reader.ReadProducts(new[] { worksheet });
_writer = _writerFactory.GetWriter("Dxf");
_writer.WriteProducts(products); _writer.WriteProducts(products);
} }
} }

View File

@ -19,6 +19,7 @@ namespace RhSolutions.Tools
RhSolutionsAddIn.Excel.SheetActivate += RefreshConvertButton; RhSolutionsAddIn.Excel.SheetActivate += RefreshConvertButton;
RhSolutionsAddIn.Excel.SheetActivate += RefreshDxfButton; RhSolutionsAddIn.Excel.SheetActivate += RefreshDxfButton;
RhSolutionsAddIn.Excel.WorkbookActivate += RefreshConvertButton; RhSolutionsAddIn.Excel.WorkbookActivate += RefreshConvertButton;
RhSolutionsAddIn.Excel.WorkbookActivate += RefreshDxfButton;
RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle;
} }
@ -28,6 +29,7 @@ namespace RhSolutions.Tools
RhSolutionsAddIn.Excel.SheetActivate -= RefreshConvertButton; RhSolutionsAddIn.Excel.SheetActivate -= RefreshConvertButton;
RhSolutionsAddIn.Excel.SheetActivate -= RefreshDxfButton; RhSolutionsAddIn.Excel.SheetActivate -= RefreshDxfButton;
RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshConvertButton; RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshConvertButton;
RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshDxfButton;
RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle;
} }

View File

@ -10,10 +10,15 @@ namespace RhSolutions.Tools;
#endif #endif
internal class ExportTool : Tool internal class ExportTool : Tool
{ {
public ExportTool(IServiceProvider provider) : base(provider)
{
}
public override void Execute() public override void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
var products = _reader.ReadProducts(app.Selection); var products = _reader.ReadProducts(app.Selection);
_writer = _writerFactory.GetWriter("Excel");
_writer.WriteProducts(products); _writer.WriteProducts(products);
} }
} }

View File

@ -1,11 +1,7 @@
#if !NET472 #if !NET472
using System.Runtime.Versioning; using System.Runtime.Versioning;
using RhSolutions;
using RhSolutions.Models;
using RhSolutions.Tools;
#endif #endif
namespace RhSolutions.Tools; namespace RhSolutions.Tools;
#if !NET472 #if !NET472
@ -13,11 +9,16 @@ namespace RhSolutions.Tools;
#endif #endif
internal class MergeTool : Tool internal class MergeTool : Tool
{ {
public MergeTool(IServiceProvider provider) : base(provider)
{
}
public override void Execute() public override void Execute()
{ {
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>(); IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
string[] files = dialog.GetFiles(); string[] files = dialog.GetFiles();
var products = _reader.ReadProducts(files); var products = _reader.ReadProducts(files);
_writer = _writerFactory.GetWriter("Excel");
_writer.WriteProducts(products); _writer.WriteProducts(products);
} }
} }

View File

@ -10,12 +10,13 @@ namespace RhSolutions.Tools;
internal abstract class Tool : IDisposable internal abstract class Tool : IDisposable
{ {
protected readonly IExcelReader _reader; 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<IExcelReader>(); _reader = provider.GetRequiredService<IExcelReader>();
_writer = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IExcelWriter>(); _writerFactory = provider.GetRequiredService<WriterFactory>();
} }
public void Dispose() public void Dispose()

View File

@ -6,10 +6,10 @@ internal class ToolFactory
{ {
Tool tool = toolName switch Tool tool = toolName switch
{ {
"export" => new ExportTool(), "export" => new ExportTool(RhSolutionsAddIn.ServiceProvider),
"convert" => new ConvertTool(), "convert" => new ConvertTool(RhSolutionsAddIn.ServiceProvider),
"merge" => new MergeTool(), "merge" => new MergeTool(RhSolutionsAddIn.ServiceProvider),
"dxfexport" => new DxfTool(), "dxfexport" => new DxfTool(RhSolutionsAddIn.ServiceProvider),
_ => throw new Exception("Неизвестный инструмент"), _ => throw new Exception("Неизвестный инструмент"),
}; };
return tool; return tool;