Skip to content

Commit fca7fa0

Browse files
committed
refactor(IB_Utility): remove SkipComponentLevelCheck, IsSavingHVACSystem, GlobalModel that was added to prevent crashing. Now no need to keep with new refactoring
1 parent 7aaa159 commit fca7fa0

File tree

5 files changed

+9
-101
lines changed

5 files changed

+9
-101
lines changed

src/Ironbug.Grasshopper/IronbugInfo.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public override GH_LoadingInstruction PriorityLoad()
2929
{
3030
try
3131
{
32-
GH.Instances.DocumentServer.DocumentAdded += DocumentServer_DocumentAdded;
3332
Action<string> logger = (string message) => Rhino.RhinoApp.WriteLine($"Ironbug {IronbugInfo.version} is loaded with {message}");
3433
Core.OpenStudio.OpenStudioHelper.LoadAssemblies(logger);
3534
}
@@ -45,40 +44,6 @@ public override GH_LoadingInstruction PriorityLoad()
4544
return GH_LoadingInstruction.Proceed;
4645
}
4746

48-
private void DocumentServer_DocumentAdded(GH_DocumentServer sender, GH_Document doc)
49-
{
50-
if (doc != null)
51-
{
52-
//Rhino.RhinoApp.WriteLine($"Document added. Doc Name: {doc.DisplayName} {System.DateTime.Now}");
53-
// Hook into the document events
54-
doc.SolutionStart += OnSolutionStart;
55-
doc.SolutionEnd += Doc_SolutionEnd;
56-
}
57-
}
58-
59-
private void Doc_SolutionEnd(object sender, GH_SolutionEventArgs e)
60-
{
61-
if (e.Document is GH_Document doc)
62-
{
63-
doc.SolutionEnd -= Doc_SolutionEnd;
64-
// Detect the start of document loading process
65-
//Rhino.RhinoApp.WriteLine($"Document is Ending its solution. Doc Name: {doc.DisplayName} {System.DateTime.Now}");
66-
IB_Utility.SkipComponentLevelCheck = false;
67-
68-
}
69-
}
70-
71-
private void OnSolutionStart(object sender, GH_SolutionEventArgs e)
72-
{
73-
74-
if (e.Document is GH_Document doc)
75-
{
76-
doc.SolutionStart -= OnSolutionStart;
77-
// Detect the start of document loading process
78-
//Rhino.RhinoApp.WriteLine($"Document is starting its solution. Doc Name: {doc.DisplayName} {System.DateTime.Now}");
79-
IB_Utility.SkipComponentLevelCheck = true;
80-
}
81-
}
8247

8348
}
8449
}

src/Ironbug.HVAC/BaseClass/IB_ModelObject.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,8 @@ public void SetFieldValue(IB_Field field, object value)
267267
//but it should have all the same field values as the real one, except handles.
268268
try
269269
{
270-
object realValue = value;
271270
// check and convert IB_Curve IB_Schedule and IB_AvailabilityManager to OpenStudio object
272-
if (!realValue.IsFieldValueRealType())
273-
{
274-
// the GH file is opening, so no need to do a real-time type checks for users
275-
if (IB_Utility.SkipComponentLevelCheck)
276-
return;
277-
278-
var md = this.GhostOSObject.TryGetObjectModel(this.GhostOSModel);
279-
realValue = value.TryGetRealFieldValue(md);
280-
}
281-
271+
var realValue = value.TryGetRealFieldValue(this.GhostOSModel);
282272
this.GhostOSObject?.SetFieldValue(field, realValue);
283273
}
284274
catch (Exception ex)

src/Ironbug.HVAC/IB_Utility.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ namespace Ironbug.HVAC
77
{
88
public class IB_Utility
99
{
10-
public static bool SkipComponentLevelCheck { get; set; }
11-
12-
private static bool _isSavingHVACSystem = false;
13-
public static bool IsSavingHVACSystem => _isSavingHVACSystem;
14-
15-
private static OpenStudio.Model _globalModel;
16-
public static OpenStudio.Model GlobalModel => _globalModel;
1710

1811
private static List<Func<bool>> _delaiedAMFuncs;
1912

@@ -47,24 +40,13 @@ private static void ExecuteDelayedFuns()
4740
private static void StartSaving()
4841
{
4942
OpsIDMapper.StartRecording();
50-
5143
_delaiedAMFuncs = new();
52-
_isSavingHVACSystem = true;
5344
}
5445

5546
private static void EndSaving()
5647
{
57-
try
58-
{
59-
_globalModel.Dispose();
60-
}
61-
finally
62-
{
63-
OpsIDMapper.EndRecording();
64-
_delaiedAMFuncs = null;
65-
_isSavingHVACSystem = false;
66-
}
67-
48+
OpsIDMapper.EndRecording();
49+
_delaiedAMFuncs = null;
6850
}
6951

7052

@@ -79,8 +61,7 @@ public static bool SaveHVAC(IB_HVACSystem hvac, string osmFile)
7961

8062

8163
//get Model from file if exists
82-
_globalModel = GetOrNewModel(osmFile);
83-
var model = _globalModel;
64+
var model = GetOrNewModel(osmFile);
8465

8566
//Add outdoor air temperature output variable
8667
var outT = new OpenStudio.OutputVariable("Site Outdoor Air Drybulb Temperature", model);

src/Ironbug.HVAC/LoopObjs/IB_ChillerElectricEIR.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public IB_ChillerElectricEIR(IB_CurveBiquadratic CCFofT, IB_CurveBiquadratic EIt
4444

4545
// add a fake condenser loop for water cooled chiller to be added to the demand side
4646
var ghost = this.GhostOSObject as ChillerElectricEIR;
47-
var ghostModel = ghost.TryGetObjectModel(this.GhostOSModel);
48-
var addGhostCondenserLoop = new PlantLoop(ghostModel);
49-
addGhostCondenserLoop.addDemandBranchForComponent(ghost);
47+
var addGhostCondenserLoop = new PlantLoop(this.GhostOSModel);
48+
var done = addGhostCondenserLoop.addDemandBranchForComponent(ghost);
49+
if (!done)
50+
throw new ArgumentException("Something when wrong, please provide this sample file to developers!");
5051

5152
}
5253

src/Ironbug.HVAC/OSExtensions/ModelObject_Extensions.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,7 @@ public static List<string> SetCustomAttributes(this ModelObject component, Model
262262
var field = item.Field;
263263
var value = item.Value;
264264
// check and convert IB_Curve IB_Schedule and IB_AvailabilityManager to OpenStudio object
265-
if (!value.IsFieldValueRealType())
266-
{
267-
// only assign the real curve, schedule, and AvailabilityManager when model is being saved to the real OpenStudio Model.
268-
// this helps preventing crashing the application when constantly requesting the object's model at a large mount scale
269-
// especially memory leak issue on OpenStudio SDK side
270-
if (!IB_Utility.IsSavingHVACSystem)
271-
continue;
272-
var md = component.TryGetObjectModel(model);
273-
value = value.TryGetRealFieldValue(md);
274-
}
265+
value = value.TryGetRealFieldValue(model);
275266

276267
var invokeResult = component.SetFieldValue(field, value);
277268

@@ -281,26 +272,6 @@ public static List<string> SetCustomAttributes(this ModelObject component, Model
281272
return invokeResults;
282273
}
283274

284-
public static OpenStudio.Model TryGetObjectModel(this ModelObject component, Model defaultModel)
285-
{
286-
OpenStudio.Model md = null;
287-
try
288-
{
289-
var isSavingToGlobalModel = IB_Utility.IsSavingHVACSystem;
290-
md = isSavingToGlobalModel ? IB_Utility.GlobalModel : defaultModel;
291-
}
292-
catch (System.ApplicationException ex)
293-
{
294-
if (ex.Message == "bad_weak_ptr")
295-
{
296-
//ignore error
297-
}
298-
else
299-
throw;
300-
301-
}
302-
return md;
303-
}
304275

305276
public static IEnumerable<string> GetUserFriendlyFieldInfo(this ModelObject component, bool ifIPUnits = false)
306277
{

0 commit comments

Comments
 (0)