Skip to content

Commit c065c9b

Browse files
authored
9.0 Deployment (#331)
2 parents 80f6711 + 5ec891b commit c065c9b

60 files changed

Lines changed: 1721 additions & 920 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LadybugTools_Adapter/AdapterActions/Execute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@
3838
using System.Drawing;
3939
using BH.Engine.Serialiser;
4040
using System.Reflection;
41+
using System.Net.Http;
4142

4243
namespace BH.Adapter.LadybugTools
4344
{
4445
public partial class LadybugToolsAdapter : BHoMAdapter
4546
{
4647
bool m_executeSuccess = false;
48+
4749
public override Output<List<object>, bool> Execute(IExecuteCommand command, ActionConfig actionConfig = null)
4850
{
4951
m_executeSuccess = false;
52+
5053
Output<List<object>, bool> output = new Output<List<object>, bool>() { Item1 = new List<object>(), Item2 = false };
5154

5255
List<object> temp = IRunCommand(command, actionConfig);

LadybugTools_Adapter/AdapterActions/Execute/DiurnalPlotCommand.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,27 @@
3131
using System.IO;
3232
using System.Linq;
3333
using System.Text;
34+
using System.Threading.Tasks;
3435

3536
namespace BH.Adapter.LadybugTools
3637
{
3738
public partial class LadybugToolsAdapter : BHoMAdapter
3839
{
3940
private List<object> RunCommand(DiurnalPlotCommand command, ActionConfig actionConfig)
4041
{
42+
bool ignoreEPWCheck = false;
43+
if (actionConfig is LadybugConfig config)
44+
ignoreEPWCheck = config.SkipEPWCheck;
45+
4146
if (command.EPWFile == null)
4247
{
4348
BH.Engine.Base.Compute.RecordError($"{nameof(command.EPWFile)} input cannot be null.");
4449
return null;
4550
}
4651

47-
if (!System.IO.File.Exists(command.EPWFile.GetFullFileName()))
52+
if (!ignoreEPWCheck & !System.IO.File.Exists(command.EPWFile.GetFullFileName()))
4853
{
49-
BH.Engine.Base.Compute.RecordError($"File '{command.EPWFile}' does not exist.");
54+
BH.Engine.Base.Compute.RecordError($"File '{command.EPWFile.GetFullFileName()}' does not exist.");
5055
return null;
5156
}
5257

@@ -66,29 +71,41 @@ private List<object> RunCommand(DiurnalPlotCommand command, ActionConfig actionC
6671

6772
string epwFile = System.IO.Path.GetFullPath(command.EPWFile.GetFullFileName());
6873

69-
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "diurnal.py");
70-
71-
string returnFile = Path.GetTempFileName();
74+
//string returnFile = Path.GetTempFileName();
7275

7376
// run the process
74-
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -dtk \"{command.EPWKey.ToText()}\" -c \"{command.Colour.ToHexCode()}\" -t \"{command.Title}\" -ap \"{command.Period.ToString().ToLower()}\" -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
75-
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
77+
List<string> args = new List<string>() { "--command", "plot/diurnal", "-e", epwFile.Replace('\\', '/'), "-dtk", command.EPWKey.ToText(), "--colour", command.Colour.ToHexCode(), "-t", command.Title, "-ap", command.Period.ToString().ToLower(), "-p", command.OutputLocation.Replace('\\', '/') };
7678

77-
string resultFile = result.Split('\n').Last();
79+
string result = "";
80+
bool success;
7881

79-
if (!File.Exists(resultFile))
82+
if (m_httpClient != null)
8083
{
81-
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
82-
File.Delete(returnFile);
83-
return new List<object>();
84+
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
85+
task.Wait();
86+
(result, success) = task.Result;
8487
}
88+
else
89+
{
90+
//if the server was not running or some other error happened, try running the python directly.
91+
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
92+
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";
8593

86-
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
87-
File.Delete(returnFile);
88-
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
94+
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true).Split('\n').Last();
95+
}
8996

90-
m_executeSuccess = true;
91-
return new List<object>() { info };
97+
try
98+
{
99+
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(result);
100+
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
101+
m_executeSuccess = true;
102+
return new List<object>() { info };
103+
}
104+
catch (Exception ex)
105+
{
106+
BH.Engine.Base.Compute.RecordError(ex, $"An error occurred when deserialising the output from the script.\n Python output: {result}");
107+
return new List<object>();
108+
}
92109
}
93110
}
94111
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2025, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.oM.Adapter;
25+
using BH.oM.LadybugTools;
26+
using System;
27+
using System.Collections.Generic;
28+
using System.IO;
29+
using System.Linq;
30+
using System.Text;
31+
using System.Threading.Tasks;
32+
33+
34+
namespace BH.Adapter.LadybugTools
35+
{
36+
public partial class LadybugToolsAdapter : BHoMAdapter
37+
{
38+
private List<object> RunCommand(EPWToCSVCommand command, ActionConfig actionConfig)
39+
{
40+
bool ignoreEPWCheck = false;
41+
if (actionConfig is LadybugConfig config)
42+
ignoreEPWCheck = config.SkipEPWCheck;
43+
44+
if (command.EPWFile == null)
45+
{
46+
BH.Engine.Base.Compute.RecordError($"{nameof(command.EPWFile)} input cannot be null.");
47+
return null;
48+
}
49+
50+
if (!Directory.Exists(command.OutputDirectory))
51+
{
52+
BH.Engine.Base.Compute.RecordError("The given output directory does not exist.");
53+
return null;
54+
}
55+
56+
if (!ignoreEPWCheck & !System.IO.File.Exists(command.EPWFile.GetFullFileName()))
57+
{
58+
BH.Engine.Base.Compute.RecordError($"File '{command.EPWFile.GetFullFileName()}' does not exist.");
59+
return null;
60+
}
61+
62+
List<string> args = new List<string>() { "--command", "epw_to_csv", "-e", command.EPWFile.GetFullFileName().Replace('\\', '/'), "-a", command.IncludeAdditionalCalculated.ToString() };
63+
64+
string result = "";
65+
bool success = true;
66+
67+
if (m_httpClient != null)
68+
{
69+
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
70+
task.Wait();
71+
(result, success) = task.Result; //in this case, result is the text of the csv file.
72+
}
73+
else
74+
{
75+
//if the server was not running or some other error happened, try running the python directly.
76+
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
77+
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";
78+
79+
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
80+
}
81+
82+
//as the file output is hard to verify by itself, check that no errors got output to stderr log
83+
success &= !result.Contains("Traceback (most recent call last):");
84+
85+
if (!success)
86+
{
87+
BH.Engine.Base.Compute.RecordError($"An error occurred while converting the file to csv.\nPython output: {result}.");
88+
return new List<object>();
89+
}
90+
91+
string outputFileName = Path.Combine(command.OutputDirectory, Path.GetFileNameWithoutExtension(command.EPWFile.FileName) + ".csv");
92+
File.WriteAllText(outputFileName, result);
93+
94+
m_executeSuccess = success;
95+
return new List<object> { outputFileName };
96+
}
97+
}
98+
}

LadybugTools_Adapter/AdapterActions/Execute/FacadeCondensationRiskCommand.cs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,25 @@
3131
using System.IO;
3232
using System.Linq;
3333
using System.Text;
34+
using System.Threading.Tasks;
3435

3536
namespace BH.Adapter.LadybugTools
3637
{
3738
public partial class LadybugToolsAdapter : BHoMAdapter
3839
{
3940
private List<object> RunCommand(FacadeCondensationRiskCommand command, ActionConfig actionConfig)
4041
{
42+
bool ignoreEPWCheck = false;
43+
if (actionConfig is LadybugConfig config)
44+
ignoreEPWCheck = config.SkipEPWCheck;
45+
4146
if (command.EPWFile == null)
4247
{
4348
BH.Engine.Base.Compute.RecordError($"{nameof(command.EPWFile)} input cannot be null.");
4449
return null;
4550
}
4651

47-
if (!System.IO.File.Exists(command.EPWFile.GetFullFileName()))
52+
if (!ignoreEPWCheck & !System.IO.File.Exists(command.EPWFile.GetFullFileName()))
4853
{
4954
BH.Engine.Base.Compute.RecordError($"File '{command.EPWFile.GetFullFileName()}' does not exist.");
5055
return null;
@@ -62,38 +67,46 @@ private List<object> RunCommand(FacadeCondensationRiskCommand command, ActionCon
6267
string thresholdsStr = string.Join(" ", thresholds);
6368

6469
string epwFile = System.IO.Path.GetFullPath(command.EPWFile.GetFullFileName());
65-
string script;
6670

71+
string commandArg;
6772
if (command.Heatmap)
68-
{
69-
script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "facade_condensation_risk_heatmap.py");
70-
}
73+
commandArg = "plot/facade_condensation_risk_heatmap";
7174
else
72-
{
73-
script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "facade_condensation_risk_chart.py");
74-
}
75-
76-
string returnFile = Path.GetTempFileName();
75+
commandArg = "plot/facade_condensation_risk_chart";
7776

7877
// run the process
79-
string cmdCommand = $"{m_environment.Executable} \"{script}\" -e \"{epwFile}\" -t {thresholdsStr} -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
80-
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
78+
List<string> args = new List<string>() { "-command", commandArg, "-e", epwFile.Replace('\\', '/'), "-t", thresholdsStr, "-p", command.OutputLocation.Replace('\\', '/') };
8179

82-
string resultFile = result.Split('\n').Last();
80+
string result = "";
81+
bool success;
8382

84-
if (!File.Exists(resultFile))
83+
if (m_httpClient != null)
8584
{
86-
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
87-
File.Delete(returnFile);
88-
return new List<object>();
85+
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
86+
task.Wait();
87+
(result, success) = task.Result;
8988
}
89+
else
90+
{
91+
//if the server was not running or some other error happened, try running the python directly.
92+
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
93+
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";
9094

91-
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
92-
File.Delete(returnFile);
93-
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
95+
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true).Split('\n').Last();
96+
}
9497

95-
m_executeSuccess = true;
96-
return new List<object> { info };
98+
try
99+
{
100+
CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(result);
101+
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());
102+
m_executeSuccess = true;
103+
return new List<object>() { info };
104+
}
105+
catch (Exception ex)
106+
{
107+
BH.Engine.Base.Compute.RecordError(ex, $"An error occurred when deserialising the output from the script.\n Python output: {result}");
108+
return new List<object>();
109+
}
97110
}
98111
}
99112
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2025, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Adapter;
24+
using BH.oM.Adapter;
25+
using BH.oM.LadybugTools;
26+
using System;
27+
using System.Collections.Generic;
28+
using System.IO;
29+
using System.Linq;
30+
using System.Text;
31+
using System.Threading.Tasks;
32+
33+
namespace BH.Adapter.LadybugTools
34+
{
35+
public partial class LadybugToolsAdapter
36+
{
37+
public List<object> RunCommand(GEMToHBJSONCommand command, ActionConfig actionConfig)
38+
{
39+
bool ignoreEPWCheck = false;
40+
if (actionConfig is LadybugConfig config)
41+
ignoreEPWCheck = config.SkipEPWCheck;
42+
43+
if (command.GEMFile == null)
44+
{
45+
BH.Engine.Base.Compute.RecordError($"{nameof(command.GEMFile)} input cannot be null.");
46+
return null;
47+
}
48+
49+
if (!Directory.Exists(command.OutputDirectory))
50+
{
51+
BH.Engine.Base.Compute.RecordError("The given output directory does not exist.");
52+
return null;
53+
}
54+
55+
if (!System.IO.File.Exists(command.GEMFile.GetFullFileName()))
56+
{
57+
BH.Engine.Base.Compute.RecordError($"File '{command.GEMFile.GetFullFileName()}' does not exist.");
58+
return null;
59+
}
60+
61+
List<string> args = new List<string>() { "--command", "gem_to_hbjson", "-g", command.GEMFile.GetFullFileName().Replace('\\', '/') };
62+
63+
string result = "";
64+
bool success = true;
65+
66+
if (m_httpClient != null)
67+
{
68+
Task<(string, bool)> task = Compute.SendHttp(m_httpClient, args);
69+
task.Wait();
70+
(result, success) = task.Result; //in this case, result is the text of the csv file.
71+
}
72+
else
73+
{
74+
//if the server was not running or some other error happened, try running the python directly.
75+
string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom", "run_wrapped.py");
76+
string cmdCommand = $"{m_environment.Executable} {script} {args.Select(x => x.Contains(' ') || string.IsNullOrEmpty(x) ? '"' + x + '"' : x).Aggregate((a, b) => a + " " + b)}";
77+
78+
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
79+
}
80+
81+
//as the file output is hard to verify by itself, check that no errors got output to stderr log
82+
success &= !result.Contains("Traceback (most recent call last):");
83+
84+
if (!success)
85+
{
86+
BH.Engine.Base.Compute.RecordError($"An error occurred while converting the file to hbjson.\nPython output: {result}.");
87+
return new List<object>();
88+
}
89+
90+
string outputFileName = Path.Combine(command.OutputDirectory, Path.GetFileNameWithoutExtension(command.GEMFile.FileName) + ".hbjson");
91+
File.WriteAllText(outputFileName, result);
92+
93+
m_executeSuccess = success;
94+
return new List<object> { outputFileName };
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)