aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs11
-rw-r--r--RhSolutions.AddIn/RhSolutions.AddIn.csproj1
-rw-r--r--RhSolutions.AddIn/Services/RhDxfWriter.cs49
-rw-r--r--RhSolutions.AddIn/Services/WriterFactory.cs24
-rw-r--r--RhSolutions.AddIn/Tools/ConvertTool.cs5
-rw-r--r--RhSolutions.AddIn/Tools/DxfTool.cs5
-rw-r--r--RhSolutions.AddIn/Tools/EventsUtil.cs2
-rw-r--r--RhSolutions.AddIn/Tools/ExportTool.cs5
-rw-r--r--RhSolutions.AddIn/Tools/MergeTool.cs9
-rw-r--r--RhSolutions.AddIn/Tools/Tool.cs9
-rw-r--r--RhSolutions.AddIn/Tools/ToolFactory.cs8
11 files changed, 113 insertions, 15 deletions
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<IDatabaseClient, RhDatabaseClient>()
.AddSingleton<IAddInConfiguration, RhAddInConfiguration>()
+ .AddSingleton<IDatabaseClient, RhDatabaseClient>()
.AddTransient<IFileDialog, ExcelFileDialog>()
- .AddTransient<IExcelReader, RhExcelReader>()
- .AddTransient<IExcelWriter, RhExcelWriter>();
+ .AddTransient<IExcelReader, RhExcelReader>();
+
+ 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>();
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 @@
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.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="RhSolutions.Sku" Version="0.1.1" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
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<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()
+ {
+
+ }
+} \ 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<IFileDialog>();
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<IExcelReader>();
- _writer = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IExcelWriter>();
+ _reader = provider.GetRequiredService<IExcelReader>();
+ _writerFactory = provider.GetRequiredService<WriterFactory>();
}
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;