Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robot_Toolkit: Fix case and combination pull #492

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions Robot_Adapter/CRUD/Read/Loads/LoadCombinations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using RobotOM;
using System.Collections.Generic;
using System;
using BH.Engine.Adapters.Robot;

namespace BH.Adapter.Robot
{
Expand All @@ -39,29 +40,36 @@ private List<LoadCombination> ReadLoadCombinations(List<string> ids = null)
List<LoadCombination> bhomLoadCombinations = new List<LoadCombination>();
IRobotCaseCollection rLoadCollection = m_RobotApplication.Project.Structure.Cases.GetAll();

Dictionary<int, ICase> pulledCases = new Dictionary<int, ICase>(); //Store all cases and combinations for later assignment to the combinations
Dictionary<int, List<Tuple<int, double>>> comboCaseFactors = new Dictionary<int, List<Tuple<int, double>>>(); //Case factors for postprocessing of combinations
for (int i = 1; i <= rLoadCollection.Count; i++)
{
try
{
IRobotCase rCase = rLoadCollection.Get(i) as IRobotCase;
if (rCase.Type == IRobotCaseType.I_CT_COMBINATION) // No support for code combinations for now as it needs building support for components
{
IRobotCaseCombination rCaseCombination = rCase as IRobotCaseCombination;
List<System.Tuple<double, ICase>> bCaseFactors = new List<System.Tuple<double, ICase>>();

for (int j = 1; j <= rCaseCombination.CaseFactors.Count; j++)
IRobotCase rCase = rLoadCollection.Get(i) as IRobotCase;
if (rCase.Type == IRobotCaseType.I_CT_SIMPLE) //Pull simple cases to temporary dictionary to be used for assigning to combinations
{
IRobotCase rCaseIn = rLoadCollection.Get(rCaseCombination.CaseFactors.Get(j).CaseNumber) as IRobotCase;
Loadcase lCase = BH.Engine.Structure.Create.Loadcase(rCaseIn.Name, rCaseIn.Number, Convert.FromRobot(rCaseIn.Nature));
SetAdapterId(lCase, rCaseCombination.Number);
bCaseFactors.Add(new System.Tuple<double, ICase>(rCaseCombination.CaseFactors.Get(j).Factor, lCase));
Loadcase lCase = BH.Engine.Structure.Create.Loadcase(rCase.Name, rCase.Number, Convert.FromRobot(rCase.Nature));
SetAdapterId(lCase, rCase.Number);
pulledCases[lCase.Number] = lCase;
}
else if (rCase.Type == IRobotCaseType.I_CT_COMBINATION) // No support for code combinations for now as it needs building support for components
{
IRobotCaseCombination rCaseCombination = rCase as IRobotCaseCombination;
List<Tuple<int, double>> caseFactors = new List<Tuple<int, double>>();
for (int j = 1; j <= rCaseCombination.CaseFactors.Count; j++)
{
RobotCaseFactor rCaseFactor = rCaseCombination.CaseFactors.Get(j);
caseFactors.Add(new Tuple<int, double>(rCaseFactor.CaseNumber, rCaseFactor.Factor)); //Temporary storage of int-double, being casenumber-factor
}

LoadCombination lCombination = new LoadCombination { Name = rCaseCombination.Name, Number = rCaseCombination.Number, LoadCases = bCaseFactors };
comboCaseFactors[rCaseCombination.Number] = caseFactors;
LoadCombination lCombination = new LoadCombination { Name = rCaseCombination.Name, Number = rCaseCombination.Number };

SetAdapterId(lCombination, rCaseCombination.Number);
bhomLoadCombinations.Add(lCombination);
}
SetAdapterId(lCombination, rCaseCombination.Number);
pulledCases[lCombination.Number] = lCombination;
bhomLoadCombinations.Add(lCombination);
}
}
catch (Exception e)
{
Expand All @@ -75,7 +83,35 @@ private List<LoadCombination> ReadLoadCombinations(List<string> ids = null)
Engine.Base.Compute.RecordError(message);
}
}

m_RobotApplication.Project.Structure.Cases.EndMultiOperation();

//Post process - Run through each combination, and make use of the comboCaseFactors and pulled cases to correctly assign inner cases/combinations to each combinations.
foreach (LoadCombination combination in bhomLoadCombinations)
{
List<Tuple<int, double>> caseFactors;
List<Tuple<double, ICase>> bCaseFactors = new List<Tuple<double, ICase>>();

if (comboCaseFactors.TryGetValue(combination.Number, out caseFactors))
{
foreach (Tuple<int, double> caseFactor in caseFactors)
{
ICase lCase;
if (!pulledCases.TryGetValue(caseFactor.Item1, out lCase))
{
lCase = new Loadcase { Number = caseFactor.Item1 };
SetAdapterId(lCase, caseFactor.Item1);
Engine.Base.Compute.RecordWarning($"Failed to extract case with number {lCase.Number} for combination {combination.Number}. A Loadcase with the correct number, but no name or nature set has been used in its place.");
}
bCaseFactors.Add(new Tuple<double, ICase>(caseFactor.Item2, lCase));
}
}
else
Engine.Base.Compute.RecordError($"Failed to extract case factors for LoadCombination with number {combination.Number}.");

combination.LoadCases = bCaseFactors;
}

return bhomLoadCombinations;
}

Expand Down
10 changes: 6 additions & 4 deletions Robot_Adapter/CRUD/Read/Loads/Loadcases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ private List<Loadcase> ReadLoadCase(List<string> ids = null)
try
{
IRobotCase rLoadCase = rLoadCases.Get(i) as IRobotCase;
Loadcase lCase = BH.Engine.Structure.Create.Loadcase(rLoadCase.Name, rLoadCase.Number, Convert.FromRobot(rLoadCase.Nature));
SetAdapterId(lCase, rLoadCase.Number);
bhomLoadCases.Add(lCase);

if (rLoadCase.Type == IRobotCaseType.I_CT_SIMPLE)
{
Loadcase lCase = BH.Engine.Structure.Create.Loadcase(rLoadCase.Name, rLoadCase.Number, Convert.FromRobot(rLoadCase.Nature));
SetAdapterId(lCase, rLoadCase.Number);
bhomLoadCases.Add(lCase);
}
}
catch (Exception e)
{
Expand Down