Add import of forces from excel

This commit is contained in:
Evgeny Redikultsev
2025-01-11 21:58:58 +05:00
parent 932f87f566
commit 690af15e2a
43 changed files with 843 additions and 166 deletions

View File

@@ -1,4 +1,6 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Forces.Logics;
using StructureHelperCommon.Models.Shapes;
using System;
using System.Collections.Generic;
@@ -8,9 +10,13 @@ using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces
{
internal class ForceCombinationFromFile : IForceCombinationFromFile
public class ForceCombinationFromFile : IForceCombinationFromFile
{
private IForceFactoredCombination factoredCombination;
IUpdateStrategy<IForceCombinationFromFile> updateStrategy;
IUpdateStrategy<IFactoredCombinationProperty> propertyUpdateStrategy;
IGetTupleFromFileLogic getTupleFromFileLogic;
private IForceFactoredList factoredCombination;
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<IForceFileProperty> ForceFiles { get; set; } = new();
@@ -21,12 +27,25 @@ namespace StructureHelperCommon.Models.Forces
public object Clone()
{
throw new NotImplementedException();
ForceCombinationFromFile newItem = new();
updateStrategy ??= new ForceCombinationFromFileUpdateStrategy();
updateStrategy.Update(newItem, this);
return newItem;
}
public List<IForceCombinationList> GetCombinations()
{
getTupleFromFileLogic ??= new GetTupleFromFileLogic() { TraceLogger = new ShiftTraceLogger()};
factoredCombination = new ForceFactoredList();
factoredCombination.ForceTuples.Clear();
propertyUpdateStrategy ??= new FactoredCombinationPropertyUpdateStrategy();
propertyUpdateStrategy.Update(factoredCombination.CombinationProperty, CombinationProperty);
foreach (var file in ForceFiles)
{
getTupleFromFileLogic.ForceFileProperty = file;
var tuples = getTupleFromFileLogic.GetTuples();
factoredCombination.ForceTuples.AddRange(tuples);
}
return factoredCombination.GetCombinations();
}
}

View File

@@ -10,15 +10,13 @@ namespace StructureHelperCommon.Models.Forces
public class ForceFileProperty : IForceFileProperty
{
public Guid Id { get; private set; }
public LimitStates LimitState { get; set; } = LimitStates.ULS;
public CalcTerms CalcTerm { get; set; } = CalcTerms.ShortTerm;
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 Mx { get; set; } = new ColumnProperty("N");
public IColumnProperty My { get; set; } = new ColumnProperty("My");
public IColumnProperty Nz { get; set; } = new ColumnProperty("Mz");
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)
{

View File

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

View File

@@ -9,7 +9,7 @@ namespace StructureHelperCommon.Models.Forces
/// <summary>
/// Supports list of files which provides import of combination of forces
/// </summary>
internal interface IForceCombinationFromFile : IForceFactoredCombination
public interface IForceCombinationFromFile : IForceFactoredCombination
{
/// <summary>
/// List of file properties for import combination

View File

@@ -11,11 +11,8 @@ namespace StructureHelperCommon.Models.Forces
/// <summary>
/// Settings for extracting force combination from MSExcel file
/// </summary>
public interface IForceFileProperty : ISaveable
public interface IForceFileProperty : IFileProperty
{
LimitStates LimitState { get; set; }
CalcTerms CalcTerm { get; set; }
string FilePath { get; set; }
int SkipRowBeforeHeaderCount { get; set; }
int SkipRowHeaderCount { get; set; }
double GlobalFactor { get; set; }

View File

@@ -11,9 +11,10 @@ namespace StructureHelperCommon.Models.Forces
{
public class ActionUpdateStrategy : IUpdateStrategy<IAction>
{
readonly IUpdateStrategy<IForceAction> forceUpdateStrategy = new ForceActionUpdateStrategy();
private IUpdateStrategy<IForceAction> forceUpdateStrategy;
public void Update(IAction targetObject, IAction sourceObject)
{
forceUpdateStrategy ??= new ForceActionUpdateStrategy();
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }

View File

@@ -0,0 +1,45 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces.Logics
{
public class CheckForceActionsLogic : ICheckEntityLogic<IEnumerable<IForceAction>>
{
private bool result;
private string checkResult;
public string CheckResult => checkResult;
public IShiftTraceLogger? TraceLogger { get; set; }
public IEnumerable<IForceAction> Entity { get; set; }
public bool Check()
{
result = true;
foreach (var item in Entity)
{
try
{
item.GetCombinations();
}
catch (Exception ex)
{
result = false;
string errorString = $"Error of getting of combination of forces from action {item.Name}: {ex.Message}";
TraceMessage(errorString);
}
}
return result;
}
private void TraceMessage(string errorString)
{
checkResult += errorString + "\n";
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
}
}
}

View File

@@ -7,7 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Models.Forces.Logics
namespace StructureHelperCommon.Models.Forces
{
public class ForceActionBaseUpdateStrategy : IUpdateStrategy<IForceAction>
{

View File

@@ -10,6 +10,9 @@ using System.Threading.Tasks;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Models.Forces.Logics;
//Copyright (c) 2025 Redikultsev Evgeny, Ekaterinburg, Russia
//All rights reserved.
namespace StructureHelperCommon.Models.Forces
{
public class ForceActionUpdateStrategy : IUpdateStrategy<IForceAction>
@@ -18,24 +21,28 @@ namespace StructureHelperCommon.Models.Forces
private readonly IUpdateStrategy<IDesignForcePair> forcePairUpdateStrategy;
private readonly IUpdateStrategy<IForceFactoredList> factorUpdateStrategy;
private readonly IUpdateStrategy<IForceCombinationList> forceListUpdateStrategy;
private readonly IUpdateStrategy<IForceCombinationFromFile> fileCombinationUpdateStrategy;
public ForceActionUpdateStrategy(
IUpdateStrategy<IForceAction> forceActionUpdateStrategy,
IUpdateStrategy<IDesignForcePair> forcePairUpdateStrategy,
IUpdateStrategy<IForceFactoredList> factorUpdateStrategy,
IUpdateStrategy<IForceCombinationList> forceListUpdateStrategy)
IUpdateStrategy<IForceCombinationList> forceListUpdateStrategy,
IUpdateStrategy<IForceCombinationFromFile> fileCombinationUpdateStrategy)
{
this.forceActionUpdateStrategy = forceActionUpdateStrategy;
this.forcePairUpdateStrategy = forcePairUpdateStrategy;
this.factorUpdateStrategy = factorUpdateStrategy;
this.forceListUpdateStrategy = forceListUpdateStrategy;
this.fileCombinationUpdateStrategy = fileCombinationUpdateStrategy;
}
public ForceActionUpdateStrategy() : this(
new ForceActionBaseUpdateStrategy(),
new ForcePairUpdateStrategy(),
new ForceFactoredListUpdateStrategy(),
new ForceCombinationListUpdateStrategy()
new ForceCombinationListUpdateStrategy(),
new ForceCombinationFromFileUpdateStrategy()
)
{
@@ -64,6 +71,10 @@ namespace StructureHelperCommon.Models.Forces
{
forceListUpdateStrategy.Update(forceCombinationList, (IForceCombinationList)sourceObject);
}
else if (targetObject is IForceCombinationFromFile fileCombination)
{
fileCombinationUpdateStrategy.Update(fileCombination, (IForceCombinationFromFile)sourceObject);
}
else
{
ErrorCommonProcessor.ObjectTypeIsUnknown(typeof(IForceAction), targetObject.GetType());

View File

@@ -13,17 +13,15 @@ namespace StructureHelperCommon.Models.Forces
private IUpdateStrategy<IForceAction> baseUpdateStrategy;
private IUpdateStrategy<IForceFileProperty> fileUpdateStrategy;
public ForceCombinationFromFileUpdateStrategy(IUpdateStrategy<IForceAction> baseUpdateStrategy, IUpdateStrategy<IForceFileProperty> fileUpdateStrategy)
public ForceCombinationFromFileUpdateStrategy(IUpdateStrategy<IForceAction> baseUpdateStrategy,
IUpdateStrategy<IForceFileProperty> fileUpdateStrategy)
{
this.baseUpdateStrategy = baseUpdateStrategy;
this.fileUpdateStrategy = fileUpdateStrategy;
}
public ForceCombinationFromFileUpdateStrategy() : this (
new ForceActionUpdateStrategy(),
new ForceFilePropertyUpdateStrategy())
{
public ForceCombinationFromFileUpdateStrategy()
{
}
void IUpdateStrategy<IForceCombinationFromFile>.Update(IForceCombinationFromFile targetObject, IForceCombinationFromFile sourceObject)
@@ -31,12 +29,21 @@ namespace StructureHelperCommon.Models.Forces
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
InitializeLogics();
baseUpdateStrategy.Update(targetObject, sourceObject);
targetObject.ForceFiles.Clear();
foreach (var file in sourceObject.ForceFiles)
{
throw new NotImplementedException();
ForceFileProperty newProperty = new();
fileUpdateStrategy.Update(newProperty, file);
targetObject.ForceFiles.Add(newProperty);
}
}
private void InitializeLogics()
{
baseUpdateStrategy ??= new ForceActionBaseUpdateStrategy();
fileUpdateStrategy ??= new ForceFilePropertyUpdateStrategy();
}
}
}

View File

@@ -27,8 +27,6 @@ namespace StructureHelperCommon.Models.Forces
CheckObject.IsNull(targetObject);
CheckObject.IsNull(sourceObject);
if (ReferenceEquals(targetObject, sourceObject)) { return; }
targetObject.LimitState = sourceObject.LimitState;
targetObject.CalcTerm = sourceObject.CalcTerm;
targetObject.FilePath = sourceObject.FilePath;
targetObject.GlobalFactor = sourceObject.GlobalFactor;
targetObject.SkipRowBeforeHeaderCount = sourceObject.SkipRowBeforeHeaderCount;

View File

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