Add import of forces from excel
This commit is contained in:
@@ -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; }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
{
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user