Compare commits

...

4 Commits

Author SHA1 Message Date
b68a2b2107 Version 1.10.0.0 2024-11-14 23:56:21 +03:00
2dd664df0a Add env variables config 2024-11-14 23:51:44 +03:00
ded0520756 Handle HttpClient errors 2024-11-14 23:51:27 +03:00
b18e573da4 AutoFit optimization 2024-11-14 22:44:22 +03:00
6 changed files with 50 additions and 31 deletions

View File

@ -32,6 +32,6 @@ using Microsoft.Extensions.Configuration.UserSecrets;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.9.5.1")] [assembly: AssemblyVersion("1.10.0.0")]
[assembly: AssemblyFileVersion("1.9.5.1")] [assembly: AssemblyFileVersion("1.10.0.0")]
[assembly: UserSecretsId("d4bb704e-14a5-421f-8f2d-0ffb66d090a2")] [assembly: UserSecretsId("d4bb704e-14a5-421f-8f2d-0ffb66d090a2")]

View File

@ -8,6 +8,7 @@
<Reference Path="Microsoft.Extensions.Caching.Memory.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Caching.Memory.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.Abstractions.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Configuration.Abstractions.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Configuration.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.EnvironmentVariables.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.FileExtensions.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Configuration.FileExtensions.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.Json.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Configuration.Json.dll" Pack="true" />
<Reference Path="Microsoft.Extensions.Configuration.UserSecrets.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Configuration.UserSecrets.dll" Pack="true" />

View File

@ -21,8 +21,9 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="netDxf" Version="2022.11.2" /> <PackageReference Include="netDxf" Version="2022.11.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@ -22,6 +22,7 @@ public class AddInConfiguration : IAddInConfiguration
{ {
_configuration = new ConfigurationBuilder() _configuration = new ConfigurationBuilder()
.AddUserSecrets<RhSolutionsAddIn>() .AddUserSecrets<RhSolutionsAddIn>()
.AddEnvironmentVariables("RHS_ADDIN__")
.Build(); .Build();
_rootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\RhSolutions\RhSolutions-AddIn"); _rootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\RhSolutions\RhSolutions-AddIn");

View File

@ -2,6 +2,7 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace RhSolutions.Services; namespace RhSolutions.Services;
@ -36,39 +37,46 @@ public class YandexOcrClient : IOcrClient
_httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true"); _httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true");
using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent); using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent);
response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode)
string jsonResponse = await response.Content.ReadAsStringAsync();
OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
if (deserialized != null)
{ {
var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>(); string jsonResponse = await response.Content.ReadAsStringAsync();
if (tables.Any()) OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
if (deserialized != null)
{ {
List<object[,]> result = new(); var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>();
foreach (var table in tables) if (tables.Any())
{ {
if (table.Cells == null || table.Cells.Count == 0) List<object[,]> result = new();
foreach (var table in tables)
{ {
continue; if (table.Cells == null || table.Cells.Count == 0)
{
continue;
}
int columnCount = int.Parse(table.ColumnCount);
int rowCount = int.Parse(table.RowCount);
object[,] cells = new object[rowCount, columnCount];
foreach (Cell cell in table.Cells)
{
int rowIndex = int.Parse(cell.RowIndex);
int columnIndex = int.Parse(cell.ColumnIndex);
cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ?
v : cell.Text ?? string.Empty;
}
result.Add(cells);
} }
int columnCount = int.Parse(table.ColumnCount); return result;
int rowCount = int.Parse(table.RowCount);
object[,] cells = new object[rowCount, columnCount];
foreach (Cell cell in table.Cells)
{
int rowIndex = int.Parse(cell.RowIndex);
int columnIndex = int.Parse(cell.ColumnIndex);
cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ?
v : cell.Text ?? string.Empty;
}
result.Add(cells);
} }
return result;
} }
} }
else
{
string content = await response.Content.ReadAsStringAsync();
MessageBox.Show($"{response.StatusCode}: {content}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return Enumerable.Empty<object[,]>(); return Enumerable.Empty<object[,]>();
} }
} }

View File

@ -55,10 +55,18 @@ internal class OcrTool : ITool
{ {
Range excelCell = app.ActiveSheet.Cells(currentCell.Row + row, Range excelCell = app.ActiveSheet.Cells(currentCell.Row + row,
currentCell.Column + column); currentCell.Column + column);
excelCell.Value2 = table[row,column]; excelCell.Value2 = table[row, column];
excelCell.EntireColumn.AutoFit();
excelCell.EntireRow.AutoFit();
} }
foreach (Range row in tableRange.Rows)
{
row.EntireRow.AutoFit();
}
foreach (Range column in tableRange.Columns)
{
column.EntireColumn.AutoFit();
}
app.ActiveSheet.Cells(currentCell.Row + rowCount + 1, currentCell.Column).Activate(); app.ActiveSheet.Cells(currentCell.Row + rowCount + 1, currentCell.Column).Activate();
} }
} }