Skip to content

Commit

Permalink
Re-implemented GetMaterial and GetTypology methods (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd authored Nov 15, 2023
2 parents e5b3083 + ef02c8a commit a12cd35
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Method to wrap for access to pre-defined materials."""
"""Method to wrap for access to pre-defined typologies."""
# pylint: disable=C0415,E0401,W0703
import argparse
import traceback
Expand Down
67 changes: 67 additions & 0 deletions LadybugTools_Engine/Query/GetMaterial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Attributes;
using BH.oM.LadybugTools;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using BH.oM.Python;
using System.IO;
using System;

namespace BH.Engine.LadybugTools
{
public static partial class Query
{
[Description("Returns a list of materials from the Python Materials list.")]
[Input("filter", "Text to filter the resultant list by. Filter applies to the material identifier. Leave blank to return all materials.")]
[Output("materials", "A list of materials.")]
public static List<IEnergyMaterialOpaque> GetMaterial(string filter = "")
{
PythonEnvironment env = Compute.InstallPythonEnv_LBT(true);

string jsonFile = Path.Combine(Path.GetTempPath(), $"LBTBHoM_Materials_{DateTime.Now:yyyyMMdd}.json");

if (!File.Exists(jsonFile))
{
string script = Path.Combine(Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_material.py");

string command = $"{env.Executable} {script} -j \"{jsonFile}\"";

Python.Compute.RunCommandStdout(command: command, hideWindows: true);
}

string jsonContent = File.ReadAllText(jsonFile);

List<object> materials = (List<object>)BH.Engine.Serialiser.Convert.FromJsonArray(jsonContent);

List<IEnergyMaterialOpaque> materialObjects = new List<IEnergyMaterialOpaque>();
foreach (object materialObject in materials)
{
materialObjects.Add((IEnergyMaterialOpaque)materialObject);
}

return materialObjects.Where(m => m.Identifier.Contains(filter)).ToList();
}
}
}
67 changes: 67 additions & 0 deletions LadybugTools_Engine/Query/GetTypology.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Attributes;
using BH.oM.LadybugTools;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using BH.oM.Python;
using System.IO;
using System;

namespace BH.Engine.LadybugTools
{
public static partial class Query
{
[Description("Returns a list of Typology objects from the Python predefined Typologies list.")]
[Input("filter", "Text to filter the resultant list by. Filter applies to the typology identifier. Leave blank to return all typologies.")]
[Output("typologies", "A list of Typology objects.")]
public static List<Typology> GetTypology(string filter = "")
{
PythonEnvironment env = Compute.InstallPythonEnv_LBT(true);

string jsonFile = Path.Combine(Path.GetTempPath(), $"LBTBHoM_Typologies_{DateTime.Now:yyyyMMdd}.json");

if (!File.Exists(jsonFile))
{
string script = Path.Combine(Python.Query.DirectoryCode(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped", "get_typology.py");

string command = $"{env.Executable} {script} -j \"{jsonFile}\"";

Python.Compute.RunCommandStdout(command: command, hideWindows: true);
}

string jsonContent = File.ReadAllText(jsonFile);

List<object> typologies = (List<object>)BH.Engine.Serialiser.Convert.FromJsonArray(jsonContent);

List<Typology> typologyObjects = new List<Typology>();
foreach (object typologyObject in typologies)
{
typologyObjects.Add((Typology)typologyObject);
}

return typologyObjects.Where(m => m.Identifier.Contains(filter)).ToList();
}
}
}
2 changes: 0 additions & 2 deletions LadybugTools_Engine/Versioning_70.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"BH.Engine.LadybugTools.Create.Typology(System.Collections.Generic.List<BH.oM.LadybugTools.Shelter>, System.String, System.Double, System.Double, System.Double)": "This method only did validation for creating the object, which has been moved to methods that take a Typology as an input.",
"BH.Engine.LadybugTools.Convert.SnakeCaseToPascalCase(System.String)": "This method and other case conversion methods in the LBT Toolkit have been replaced by a single method that can do case conversions when given a case type.",
"BH.Engine.LadybugTools.Convert.PascalCaseToSnakeCase(System.String)": "This method and other case conversion methods in the LBT Toolkit have been replaced by a single method that can do case conversions when given a case type.",
"BH.Engine.LadybugTools.Query.GetMaterial(System.String)": "Due to a refactor of the underlying python libraries, this method was broken. To get materials for use within python scripts, get them within the scripts.",
"BH.Engine.LadybugTools.Query.GetTypology(System.String)": "Due to a refactor of the underlying python libraries, this method was broken. To get typologies for use within python scripts, get them within the scripts.",
"BH.Engine.LadybugTools.Compute.EPWtoCustomObject(System.String)": "Due to inclusion of EPW as an object, this method is no longer required and has been removed."
}
}

0 comments on commit a12cd35

Please sign in to comment.