diff --git a/Robot_Adapter/CRUD/Update/Loads/LoadCombinations.cs b/Robot_Adapter/CRUD/Update/Loads/LoadCombinations.cs index 357de43a..37638304 100644 --- a/Robot_Adapter/CRUD/Update/Loads/LoadCombinations.cs +++ b/Robot_Adapter/CRUD/Update/Loads/LoadCombinations.cs @@ -20,6 +20,7 @@ * along with this code. If not, see . */ +using System; using System.Collections.Generic; using BH.oM.Base; using BH.oM.Structure.Elements; @@ -29,6 +30,7 @@ using BH.oM.Structure.Loads; using BH.oM.Physical.Materials; using BH.oM.Adapter; +using BH.Engine.Base; using RobotOM; namespace BH.Adapter.Robot @@ -42,7 +44,78 @@ public partial class RobotAdapter protected bool Update(IEnumerable loadCombinations) { bool success = true; - success = ICreate(loadCombinations); + + m_RobotApplication.Project.Structure.Cases.BeginMultiOperation(); + + foreach (LoadCombination lComb in loadCombinations) + { + //Check combination itself is not null + if (!CheckNotNull(lComb)) + continue; + + // Use the Number property directly and try to get the combination + int combinationId = lComb.Number; + + // Check if the RobotId matches the LoadCombination Number + int robotId = GetAdapterId(lComb); + + // If the LoadCombination doesn't have a RobotId, assign it from the Number + if (robotId == 0) + { + this.SetAdapterId(lComb, combinationId); + robotId = combinationId; + Engine.Base.Compute.RecordNote($"LoadCombination with number {combinationId} did not have a RobotId. RobotId has been set to the LoadCombination number."); + } + else if (robotId != combinationId) + { + Engine.Base.Compute.RecordError($"Load combination has mismatched IDs: RobotId = {robotId}, Number = {combinationId}. Using Number property for update."); + } + + // Get the existing combination from Robot (following pattern from Loadcases Update method) + RobotCaseCombination rCaseCombination = m_RobotApplication.Project.Structure.Cases.Get(combinationId) as RobotCaseCombination; + if (rCaseCombination == null) + { + Engine.Base.Compute.RecordWarning("Load combination with number " + combinationId.ToString() + " does not exist in Robot. Load combination could not be updated!"); + success = false; + continue; + } + + // Update the combination name if provided + if (!string.IsNullOrWhiteSpace(lComb.Name)) + rCaseCombination.Name = lComb.Name; + + // Clear existing case factors by deleting them individually + // Note: Robot API requires deleting case factors in reverse order to avoid index shifting + for (int i = rCaseCombination.CaseFactors.Count; i >= 1; i--) + { + rCaseCombination.CaseFactors.Delete(i); + } + + // Add new case factors from the BHoM LoadCombination + if (lComb.LoadCases != null && lComb.LoadCases.Count > 0) + { + for (int i = 0; i < lComb.LoadCases.Count; i++) + { + //Check tuple as well as case not null + if (CheckNotNull(lComb.LoadCases[i], oM.Base.Debugging.EventType.Error, typeof(LoadCombination)) && + CheckNotNull(lComb.LoadCases[i].Item2, oM.Base.Debugging.EventType.Error, typeof(LoadCombination))) + { + System.Tuple loadcase = lComb.LoadCases[i]; + rCaseCombination.CaseFactors.New(lComb.LoadCases[i].Item2.Number, lComb.LoadCases[i].Item1); + } + } + } + else + { + Engine.Base.Compute.RecordWarning("Load combination with number " + combinationId.ToString() + " has no load cases. The combination has been cleared of all case factors."); + } + + // Set the adapter ID to maintain the connection between BHoM and Robot objects + this.SetAdapterId(lComb, lComb.Number); + } + + m_RobotApplication.Project.Structure.Cases.EndMultiOperation(); + return success; }