Add logic for import of combination from xls files

This commit is contained in:
Evgeny Redikultsev
2025-01-18 22:13:11 +05:00
parent 13c3022c2f
commit f508399846
43 changed files with 1140 additions and 227 deletions

View File

@@ -6,20 +6,34 @@ using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
/// <inheritdoc/>
public class ColumnProperty : IColumnProperty
{
/// <inheritdoc/>
public Guid Id { get; private set; }
public string ColumnName { get; set; } = string.Empty;
public int ColumnIndex { get; set; } = 0;
public double ColumnFactor { get; set; } = 1d;
/// <inheritdoc/>
public string Name { get; set; }
/// <inheritdoc/>
public string SearchingName { get; set; } = string.Empty;
/// <inheritdoc/>
public int Index { get; set; } = 0;
/// <inheritdoc/>
public double Factor { get; set; } = 1d;
public ColumnProperty(Guid id, string columnName)
{
Id = id;
ColumnName = columnName;
Name = columnName;
}
public ColumnProperty(string columnName) : this(Guid.NewGuid(), columnName)
{
}
public object Clone()
{
var cloneLogic = new ColumnPropertyCloningStrategy();
return cloneLogic.GetClone(this);
}
}
}

View File

@@ -0,0 +1,45 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
/// <inheritdoc/>
public class ColumnedFileProperty : IColumnedFileProperty
{
private IUpdateStrategy<IColumnedFileProperty> updateStrategy;
/// <inheritdoc/>
public Guid Id { get; private set; }
/// <inheritdoc/>
public string FilePath { get; set; } = string.Empty;
/// <inheritdoc/>
public int SkipRowBeforeHeaderCount { get; set; } = 2;
/// <inheritdoc/>
public int SkipRowHeaderCount { get; set; } = 1;
/// <inheritdoc/>
public double GlobalFactor { get; set; } = 1d;
/// <inheritdoc/>
public List<IColumnProperty> ColumnProperties { get; } = new();
public ColumnedFileProperty(Guid id)
{
Id = id;
}
public ColumnedFileProperty() : this (Guid.NewGuid())
{
}
public object Clone()
{
updateStrategy ??= new ColumnedFilePropertyUpdateStrategy();
ColumnedFileProperty newItem = new();
updateStrategy.Update(newItem, this);
return newItem;
}
}
}

View File

@@ -0,0 +1,40 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public enum FilePropertyType
{
Forces
}
public static class FilePropertyFactory
{
public static IColumnedFileProperty GetForceFileProperty(FilePropertyType propertyType)
{
if (propertyType == FilePropertyType.Forces)
{
ColumnedFileProperty fileProperty = new();
List<IColumnProperty> columnProperties = GetForceColumns();
fileProperty.ColumnProperties.AddRange(columnProperties);
return fileProperty;
}
else
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(propertyType));
}
}
private static List<IColumnProperty> GetForceColumns()
{
List<IColumnProperty> columnProperties = new();
columnProperties.Add(new ColumnProperty("Nz") { SearchingName = "N", Index = 6 });
columnProperties.Add(new ColumnProperty("Mx") { SearchingName = "My", Index = 8 });
columnProperties.Add(new ColumnProperty("My") { SearchingName = "Mz", Index = 10 });
return columnProperties;
}
}
}

View File

@@ -14,12 +14,12 @@ namespace StructureHelperCommon.Models.Forces
{
IUpdateStrategy<IForceCombinationFromFile> updateStrategy;
IUpdateStrategy<IFactoredCombinationProperty> propertyUpdateStrategy;
IGetTupleFromFileLogic getTupleFromFileLogic;
IGetTuplesFromFileLogic getTupleFromFileLogic;
private IForceFactoredList factoredCombination;
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<IForceFileProperty> ForceFiles { get; set; } = new();
public List<IColumnedFileProperty> ForceFiles { get; set; } = new();
public bool SetInGravityCenter { get; set; } = true;
public IPoint2D ForcePoint { get; set; } = new Point2D();
@@ -35,7 +35,7 @@ namespace StructureHelperCommon.Models.Forces
public List<IForceCombinationList> GetCombinations()
{
getTupleFromFileLogic ??= new GetTupleFromFileLogic() { TraceLogger = new ShiftTraceLogger()};
getTupleFromFileLogic ??= new GetTuplesFromFileLogic() { TraceLogger = new ShiftTraceLogger()};
factoredCombination = new ForceFactoredList();
factoredCombination.ForceTuples.Clear();
propertyUpdateStrategy ??= new FactoredCombinationPropertyUpdateStrategy();

View File

@@ -1,30 +0,0 @@
using StructureHelperCommon.Infrastructures.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class ForceFileProperty : IForceFileProperty
{
public Guid Id { get; private set; }
public string FilePath { get; set; } = string.Empty;
public int SkipRowBeforeHeaderCount { get; set; } = 2;
public int SkipRowHeaderCount { get; set; } = 1;
public double GlobalFactor { get; set; } = 1d;
public IColumnProperty Nz { get; set; } = new ColumnProperty("N") { ColumnIndex = 6};
public IColumnProperty Mx { get; set; } = new ColumnProperty("My") { ColumnIndex = 8};
public IColumnProperty My { get; set; } = new ColumnProperty("Mz") { ColumnIndex = 10};
public ForceFileProperty(Guid id)
{
Id = id;
}
public ForceFileProperty() : this (Guid.NewGuid())
{
}
}
}

View File

@@ -10,19 +10,23 @@ namespace StructureHelperCommon.Models.Forces
/// <summary>
/// Settingth for column reading from MSExcel file
/// </summary>
public interface IColumnProperty : ISaveable
public interface IColumnProperty : ISaveable, ICloneable
{
/// <summary>
/// Name of column
/// </summary>
string Name { get; set; }
/// <summary>
/// Name of column for searching
/// </summary>
string ColumnName { get; set; }
string SearchingName { get; set; }
/// <summary>
/// Column index
/// </summary>
int ColumnIndex { get; set; }
int Index { get; set; }
/// <summary>
/// Factor for obtaining value from column
/// </summary>
double ColumnFactor { get; set; }
double Factor { get; set; }
}
}

View File

@@ -11,14 +11,23 @@ namespace StructureHelperCommon.Models.Forces
/// <summary>
/// Settings for extracting force combination from MSExcel file
/// </summary>
public interface IForceFileProperty : IFileProperty
public interface IColumnedFileProperty : IFileProperty
{
/// <summary>
/// Count of rows before header
/// </summary>
int SkipRowBeforeHeaderCount { get; set; }
/// <summary>
/// Count of rows of header
/// </summary>
int SkipRowHeaderCount { get; set; }
/// <summary>
/// Factor which imported value multyply to
/// </summary>
double GlobalFactor { get; set; }
IColumnProperty Mx { get; set; }
IColumnProperty My { get; set; }
IColumnProperty Nz { get; set; }
/// <summary>
/// Collection of column's properties which will be imported
/// </summary>
List<IColumnProperty> ColumnProperties { get; }
}
}

View File

@@ -1,8 +1,9 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
namespace StructureHelperCommon.Models.Forces
{
public interface IFileProperty : ISaveable
public interface IFileProperty : ISaveable, ICloneable
{
string FilePath { get; set; }
}

View File

@@ -14,6 +14,6 @@ namespace StructureHelperCommon.Models.Forces
/// <summary>
/// List of file properties for import combination
/// </summary>
List<IForceFileProperty> ForceFiles { get; set; }
List<IColumnedFileProperty> ForceFiles { get; set; }
}
}

View File

@@ -0,0 +1,48 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class CheckColumnedFilePropertyLogic : ICheckEntityLogic<IColumnedFileProperty>
{
private string checkResult;
private bool result;
public IColumnedFileProperty Entity { get; set; }
public string CheckResult => checkResult;
public IShiftTraceLogger? TraceLogger { get; set; }
public bool Check()
{
result = true;
int skipRows = Entity.SkipRowBeforeHeaderCount + Entity.SkipRowHeaderCount;
if (skipRows < 0)
{
string errorString = ErrorStrings.DataIsInCorrect + $": skip row count must be greater or equal to 0, bu was {skipRows}";
TraceMessage(errorString);
return false;
}
if (File.Exists(Entity.FilePath) == false)
{
string errorString = ErrorStrings.DataIsInCorrect + $": File {Entity.FilePath} does not exist";
TraceMessage(errorString);
return false;
}
return result;
}
private void TraceMessage(string errorString)
{
checkResult += errorString;
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
}
}
}

View File

@@ -0,0 +1,37 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class ColumnPropertyCloningStrategy : ICloneStrategy<IColumnProperty>
{
private IUpdateStrategy<IColumnProperty> updateStrategy;
public ColumnPropertyCloningStrategy(IUpdateStrategy<IColumnProperty> updateStrategy)
{
this.updateStrategy = updateStrategy;
}
public ColumnPropertyCloningStrategy()
{
}
public IColumnProperty GetClone(IColumnProperty sourceObject)
{
CheckObject.IsNull(sourceObject);
if (updateStrategy is null)
{
updateStrategy = new ColumnPropertyUpdateStrategy();
}
ColumnProperty newItem = new(sourceObject.Name);
updateStrategy.Update(newItem, sourceObject);
return newItem;
}
}
}

View File

@@ -8,16 +8,17 @@ using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class ForceColumnPropertyUpdateStrategy : IUpdateStrategy<IColumnProperty>
public class ColumnPropertyUpdateStrategy : IUpdateStrategy<IColumnProperty>
{
public void Update(IColumnProperty targetObject, IColumnProperty sourceObject)
{
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
targetObject.ColumnName = sourceObject.ColumnName;
targetObject.ColumnIndex = sourceObject.ColumnIndex;
targetObject.ColumnFactor = sourceObject.ColumnFactor;
targetObject.Name = sourceObject.Name;
targetObject.SearchingName = sourceObject.SearchingName;
targetObject.Index = sourceObject.Index;
targetObject.Factor = sourceObject.Factor;
}
}
}

View File

@@ -0,0 +1,39 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
/// <inheritdoc/>
public class ColumnedFilePropertyUpdateStrategy : IUpdateStrategy<IColumnedFileProperty>
{
/// <inheritdoc/>
public void Update(IColumnedFileProperty targetObject, IColumnedFileProperty sourceObject)
{
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
UpdateObjects(targetObject, sourceObject);
}
private static void UpdateObjects(IColumnedFileProperty targetObject, IColumnedFileProperty sourceObject)
{
targetObject.FilePath = sourceObject.FilePath;
targetObject.GlobalFactor = sourceObject.GlobalFactor;
targetObject.SkipRowBeforeHeaderCount = sourceObject.SkipRowBeforeHeaderCount;
targetObject.SkipRowHeaderCount = sourceObject.SkipRowHeaderCount;
CheckObject.IsNull(targetObject.ColumnProperties);
CheckObject.IsNull(sourceObject.ColumnProperties);
targetObject.ColumnProperties.Clear();
foreach (var item in sourceObject.ColumnProperties)
{
IColumnProperty clone = (IColumnProperty)item.Clone();
targetObject.ColumnProperties.Add(clone);
}
}
}
}

View File

@@ -0,0 +1,36 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class FillTupleArrayByColumnNameLogic : IFillTupleArrayByColumnNameLogic
{
public IShiftTraceLogger? TraceLogger { get; set; }
public void ProceeForceTupleArray(double[] nDouble, double doubleValue, string columnName, string filePath)
{
if (columnName == "Nz")
{
nDouble[0] = doubleValue;
}
else if (columnName == "Mx")
{
nDouble[1] = doubleValue;
}
else if (columnName == "My")
{
nDouble[2] = doubleValue;
}
else
{
string errorString = ErrorStrings.DataIsInCorrect + $": column {columnName} in file {filePath}, ";
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
throw new StructureHelperException(errorString);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Forces.Logics;
using StructureHelperCommon.Services;
using System;
using System.Collections.Generic;
@@ -11,31 +12,37 @@ namespace StructureHelperCommon.Models.Forces
public class ForceCombinationFromFileUpdateStrategy : IUpdateStrategy<IForceCombinationFromFile>
{
private IUpdateStrategy<IForceAction> baseUpdateStrategy;
private IUpdateStrategy<IForceFileProperty> fileUpdateStrategy;
private IUpdateStrategy<IColumnedFileProperty> filePropertyUpdateStrategy;
private IUpdateStrategy<IFactoredCombinationProperty> combinationPropertyUpdateStrategy;
public ForceCombinationFromFileUpdateStrategy(IUpdateStrategy<IForceAction> baseUpdateStrategy,
IUpdateStrategy<IForceFileProperty> fileUpdateStrategy)
IUpdateStrategy<IColumnedFileProperty> filePropertyUpdateStrategy,
IUpdateStrategy<IFactoredCombinationProperty> combinationPropertyUpdateStrategy)
{
this.baseUpdateStrategy = baseUpdateStrategy;
this.fileUpdateStrategy = fileUpdateStrategy;
this.filePropertyUpdateStrategy = filePropertyUpdateStrategy;
this.combinationPropertyUpdateStrategy = combinationPropertyUpdateStrategy;
}
public ForceCombinationFromFileUpdateStrategy()
{
}
void IUpdateStrategy<IForceCombinationFromFile>.Update(IForceCombinationFromFile targetObject, IForceCombinationFromFile sourceObject)
public void Update(IForceCombinationFromFile targetObject, IForceCombinationFromFile sourceObject)
{
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
InitializeLogics();
baseUpdateStrategy.Update(targetObject, sourceObject);
CheckObject.IsNull(targetObject.CombinationProperty, "Target object combination property");
CheckObject.IsNull(sourceObject.CombinationProperty, "Source object combination property");
combinationPropertyUpdateStrategy.Update(targetObject.CombinationProperty, sourceObject.CombinationProperty);
targetObject.ForceFiles.Clear();
foreach (var file in sourceObject.ForceFiles)
{
ForceFileProperty newProperty = new();
fileUpdateStrategy.Update(newProperty, file);
ColumnedFileProperty newProperty = new();
filePropertyUpdateStrategy.Update(newProperty, file);
targetObject.ForceFiles.Add(newProperty);
}
}
@@ -43,7 +50,8 @@ namespace StructureHelperCommon.Models.Forces
private void InitializeLogics()
{
baseUpdateStrategy ??= new ForceActionBaseUpdateStrategy();
fileUpdateStrategy ??= new ForceFilePropertyUpdateStrategy();
filePropertyUpdateStrategy ??= new ColumnedFilePropertyUpdateStrategy();
combinationPropertyUpdateStrategy ??= new FactoredCombinationPropertyUpdateStrategy();
}
}
}

View File

@@ -1,39 +0,0 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class ForceFilePropertyUpdateStrategy : IUpdateStrategy<IForceFileProperty>
{
private IUpdateStrategy<IColumnProperty> columnUpdateStrategy;
public ForceFilePropertyUpdateStrategy(IUpdateStrategy<IColumnProperty> columnUpdateStrategy)
{
this.columnUpdateStrategy = columnUpdateStrategy;
}
public ForceFilePropertyUpdateStrategy() : this (new ForceColumnPropertyUpdateStrategy())
{
}
public void Update(IForceFileProperty targetObject, IForceFileProperty sourceObject)
{
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
targetObject.FilePath = sourceObject.FilePath;
targetObject.GlobalFactor = sourceObject.GlobalFactor;
targetObject.SkipRowBeforeHeaderCount = sourceObject.SkipRowBeforeHeaderCount;
targetObject.SkipRowHeaderCount = sourceObject.SkipRowHeaderCount;
columnUpdateStrategy.Update(targetObject.Mx, sourceObject.Mx);
columnUpdateStrategy.Update(targetObject.My, sourceObject.My);
columnUpdateStrategy.Update(targetObject.Nz, sourceObject.Nz);
}
}
}

View File

@@ -0,0 +1,52 @@
using ExcelDataReader;
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public class GetTupleByExcelReaderLogic : IGetTupleByExcelReaderLogic
{
IFillTupleArrayByColumnNameLogic fillArrayLogic;
public GetTupleByExcelReaderLogic(IFillTupleArrayByColumnNameLogic fillArrayLogic)
{
this.fillArrayLogic = fillArrayLogic;
}
public GetTupleByExcelReaderLogic()
{
}
public IShiftTraceLogger? TraceLogger { get; set; }
public IColumnedFileProperty ForceFileProperty { get; set; }
public IForceTuple GetForceTuple(IExcelDataReader? reader)
{
// Ensure ExcelDataReader's encoding provider is registered
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
double[] nDouble = new double[3];
foreach (var item in ForceFileProperty.ColumnProperties)
{
if (reader is null)
{
throw new StructureHelperException(ErrorStrings.NullReference + $": reader value for column {item.Name} in file {ForceFileProperty.FilePath}");
}
var readerValue = reader.GetValue(item.Index);
var strValue = readerValue.ToString();
double factor = ForceFileProperty.GlobalFactor * item.Factor * 1000d;
var doubleValue = Convert.ToDouble(strValue) * factor;
fillArrayLogic ??= new FillTupleArrayByColumnNameLogic() { TraceLogger = TraceLogger };
fillArrayLogic.ProceeForceTupleArray(nDouble, doubleValue, item.Name, ForceFileProperty.FilePath);
}
TraceLogger?.AddMessage($"String values: Nz = {nDouble[0]}, Mx = {nDouble[1]}, My = {nDouble[2]} were gained", TraceLogStatuses.Debug);
ForceTuple newTuple = new() { Nz = nDouble[0], Mx = nDouble[1], My = nDouble[2] };
return newTuple;
}
}
}

View File

@@ -1,79 +0,0 @@
using ExcelDataReader;
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces.Logics
{
public class GetTupleFromFileLogic : IGetTupleFromFileLogic
{
public IForceFileProperty ForceFileProperty { get; set; }
public IShiftTraceLogger? TraceLogger { get; set; }
public List<IForceTuple> GetTuples()
{
// Ensure ExcelDataReader's encoding provider is registered
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var result = ReadDataFromFile();
return result;
}
private List<IForceTuple> ReadDataFromFile()
{
List<IForceTuple> result = new();
// Open the Excel file stream
using (var stream = File.Open(ForceFileProperty.FilePath, FileMode.Open, FileAccess.Read))
{
// Create an Excel reader
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Skip the first two header rows if necessary (adjust based on structure)
int skipRows = ForceFileProperty.SkipRowBeforeHeaderCount + ForceFileProperty.SkipRowHeaderCount;
for (int i = 0; i < skipRows; i++)
{
reader.Read(); // Skip row
}
// Loop through the rows
while (reader.Read())
{
var nValue = reader.GetValue(ForceFileProperty.Nz.ColumnIndex)?.ToString();
var mxValue = reader.GetValue(ForceFileProperty.Mx.ColumnIndex)?.ToString();
var myValue = reader.GetValue(ForceFileProperty.My.ColumnIndex)?.ToString();
TraceLogger?.AddMessage($"Values: Nz = {nValue}(N), Mx = {mxValue}(N*m), My = {myValue}(N*m) were gained", TraceLogStatuses.Debug);
if (nValue is not null && mxValue is not null && myValue is not null)
{
ForceTuple newTuple = GetForceTuple(nValue, mxValue, myValue);
result.Add(newTuple);
}
}
}
}
return result;
}
private ForceTuple GetForceTuple(string? nValue, string? mxValue, string? myValue)
{
try
{
double nDouble = Convert.ToDouble(nValue);
double mxDouble = Convert.ToDouble(mxValue);
double myDouble = Convert.ToDouble(myValue);
TraceLogger?.AddMessage($"Values: Nz = {nDouble}(N), Mx = {mxDouble}(N*m), My = {myDouble}(N*m) were converted successfully", TraceLogStatuses.Debug);
ForceTuple newTuple = new() { Nz = nDouble, Mx = mxDouble, My = myDouble };
return newTuple;
}
catch (Exception ex)
{
string errorString = ErrorStrings.DataIsInCorrect + $": incorrect data in file {ForceFileProperty.FilePath}, " + ex.Message;
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
throw new StructureHelperException(errorString);
}
}
}
}

View File

@@ -0,0 +1,105 @@
using ExcelDataReader;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
//Copyright (c) 2025 Redikultsev Evgeny, Ekaterinburg, Russia
//All rights reserved.
namespace StructureHelperCommon.Models.Forces
{
public class GetTuplesFromFileLogic : IGetTuplesFromFileLogic
{
private IGetTupleByExcelReaderLogic excelReaderLogic;
private ICheckEntityLogic<IColumnedFileProperty> checkEntityLogic;
public GetTuplesFromFileLogic(IGetTupleByExcelReaderLogic excelReaderLogic,
ICheckEntityLogic<IColumnedFileProperty> checkEntityLogic)
{
this.excelReaderLogic = excelReaderLogic;
this.checkEntityLogic = checkEntityLogic;
}
public GetTuplesFromFileLogic()
{
}
private List<IForceTuple> result;
private int skipRows;
public IColumnedFileProperty ForceFileProperty { get; set; }
public IShiftTraceLogger? TraceLogger { get; set; }
public List<IForceTuple> GetTuples()
{
Check();
// Ensure ExcelDataReader's encoding provider is registered
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
result = ReadDataFromFile();
return result;
}
private List<IForceTuple> ReadDataFromFile()
{
result = new();
skipRows = ForceFileProperty.SkipRowBeforeHeaderCount + ForceFileProperty.SkipRowHeaderCount;
// Open the Excel file stream
using (var stream = File.Open(ForceFileProperty.FilePath, FileMode.Open, FileAccess.Read))
{
// Create an Excel reader
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Skip the first two header rows if necessary (adjust based on structure)
for (int i = 0; i < skipRows; i++)
{
reader.Read(); // Skip row
}
excelReaderLogic ??= new GetTupleByExcelReaderLogic()
{
ForceFileProperty = ForceFileProperty,
TraceLogger = TraceLogger
};
// Loop through the rows
while (reader.Read())
{
try
{
IForceTuple newTuple = excelReaderLogic.GetForceTuple(reader);
result.Add(newTuple);
}
catch (Exception ex)
{
string errorString = ErrorStrings.DataIsInCorrect + $": incorrect data in file {ForceFileProperty.FilePath}, " + ex.Message;
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
throw new StructureHelperException(errorString);
}
}
}
}
return result;
}
private void Check()
{
checkEntityLogic ??= new CheckColumnedFilePropertyLogic()
{
Entity = ForceFileProperty,
TraceLogger = TraceLogger
};
if (checkEntityLogic.Check() == false)
{
throw new StructureHelperException(checkEntityLogic.CheckResult);
}
}
}
}

View File

@@ -0,0 +1,9 @@
using StructureHelperCommon.Infrastructures.Interfaces;
namespace StructureHelperCommon.Models.Forces
{
public interface IFillTupleArrayByColumnNameLogic : ILogic
{
void ProceeForceTupleArray(double[] nDouble, double doubleValue, string columnName, string filePath);
}
}

View File

@@ -0,0 +1,16 @@
using ExcelDataReader;
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
public interface IGetTupleByExcelReaderLogic : ILogic
{
IColumnedFileProperty ForceFileProperty { get; set;}
IForceTuple GetForceTuple(IExcelDataReader? reader);
}
}

View File

@@ -1,12 +0,0 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using System.Collections.Generic;
namespace StructureHelperCommon.Models.Forces.Logics
{
public interface IGetTupleFromFileLogic : ILogic
{
IForceFileProperty ForceFileProperty { get; set; }
List<IForceTuple> GetTuples();
}
}

View File

@@ -0,0 +1,12 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using System.Collections.Generic;
namespace StructureHelperCommon.Models.Forces
{
public interface IGetTuplesFromFileLogic : ILogic
{
IColumnedFileProperty ForceFileProperty { get; set; }
List<IForceTuple> GetTuples();
}
}