diff --git a/PluginGrasshopper/Components/FindMaterial.cs b/PluginGrasshopper/Components/FindMaterial.cs deleted file mode 100644 index 356dc65..0000000 --- a/PluginGrasshopper/Components/FindMaterial.cs +++ /dev/null @@ -1,105 +0,0 @@ -using Grasshopper; -using Grasshopper.Kernel; -using System; -using System.IO; -using System.Net; -using System.Xml; -using Newtonsoft.Json; - -namespace PluginTemplate.PluginGrasshopper -{ - public class FindMaterial : GH_Component - { - public FindMaterial() - : base("Find Material by UUID", "FindMat", - "Loads material through API", - "goeko", "API") - { - } - - protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) - { - pManager.AddBooleanParameter("Run", "Run", "Set to true to execute loading process", GH_ParamAccess.item); - pManager.AddTextParameter("UUID", "UUID", "UUID of the material to retrieve", GH_ParamAccess.item); - pManager.AddIntegerParameter("Timeout", "Timeout", "Timeout in seconds (up to 20 seconds)", GH_ParamAccess.item, 5); - } - - protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) - { - pManager.AddTextParameter("Data", "Data", "Loaded data from the database", GH_ParamAccess.item); - } - - protected override void SolveInstance(IGH_DataAccess DA) - { - bool run = false; - string uuid = ""; - int timeout = 5; - - DA.GetData(0, ref run); - DA.GetData(1, ref uuid); // Get the UUID parameter - DA.GetData(2, ref timeout); // Get the Timeout parameter - - // Ensure the timeout value is within the range of 1 to 20 seconds - timeout = Math.Max(1, Math.Min(timeout, 20)); - - if (run && !string.IsNullOrWhiteSpace(uuid)) - { - string loadedData = LoadDataFromDatabase(uuid, timeout * 1000); // Convert timeout to milliseconds - DA.SetData(0, loadedData); - } - } - - private string LoadDataFromDatabase(string uuid, int timeoutMilliseconds) - { - string apiUrl = $"https://www.oekobaudat.de/OEKOBAU.DAT/resource/processes/{uuid}"; - - string responseData = ""; - - try - { - WebRequest request = WebRequest.Create(apiUrl); - request.Timeout = timeoutMilliseconds; // Set the timeout - request.Method = "GET"; - - using (WebResponse response = request.GetResponse()) - using (Stream stream = response.GetResponseStream()) - using (StreamReader reader = new StreamReader(stream)) - { - responseData = reader.ReadToEnd(); - } - - // Process XML data and convert to JSON - responseData = ProcessXmlData(responseData); - } - catch (WebException e) - { - responseData = e.Message; - } - - return responseData; - } - - private string ProcessXmlData(string xmlData) - { - // Load XML data into XmlDocument - XmlDocument xmlDoc = new XmlDocument(); - xmlDoc.LoadXml(xmlData); - - // Create a namespace manager - XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable); - nsManager.AddNamespace("sapi", "http://www.ilcd-network.org/ILCD/ServiceAPI"); - - // Select the 'sapi:name' element using the namespace manager - XmlNode nameNode = xmlDoc.SelectSingleNode("//sapi:name", nsManager); - - // Get the value of the 'sapi:name' element - string nameValue = nameNode.InnerText; - - return nameValue; - } - - protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); - - public override Guid ComponentGuid => new Guid("f9fa9c30-76c4-45a9-92ac-df104238634e"); - } -} diff --git a/PluginGrasshopper/Components/GetEPDbyUUID.cs b/PluginGrasshopper/Components/GetEPDbyUUID.cs new file mode 100644 index 0000000..1b4173d --- /dev/null +++ b/PluginGrasshopper/Components/GetEPDbyUUID.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; +using System.Net; +using System.Xml; +using Grasshopper.Kernel; +using Newtonsoft.Json; + +namespace PluginTemplate.PluginGrasshopper +{ + public class GetEPDbyUUID : GH_Component + { + private const string OKOBAU_URL = "https://oekobaudat.de/OEKOBAU.DAT/resource/datastocks/cd2bda71-760b-4fcc-8a0b-3877c10000a8"; + + public GetEPDbyUUID() + : base("Get EPD by UUID", "GetEPDbyUUID", + "Get EPD by UUID from Ökobau", + "goeko", "API") + { + } + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddBooleanParameter("Run", "Run", "Set to true to execute loading process", GH_ParamAccess.item, false); + pManager.AddTextParameter("UUID", "UUID", "UUID of the EPD to retrieve", GH_ParamAccess.item); + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddTextParameter("Data", "Data", "Loaded EPD data from Ökobau", GH_ParamAccess.item); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + bool run = false; + string uuid = ""; + + DA.GetData(0, ref run); + DA.GetData(1, ref uuid); + + if (run && !string.IsNullOrWhiteSpace(uuid)) + { + string epdData = GetFullEPDData(uuid); + DA.SetData(0, epdData); + } + else + { + DA.SetData(0, "Component not executed. Set 'Run' to true and provide a valid UUID to retrieve EPD data."); + } + } + + private string GetFullEPDData(string uuid) + { + string responseData = ""; + + try + { + string apiUrl = $"{OKOBAU_URL}/processes/{uuid}?format=json&view=extended"; + WebRequest request = WebRequest.Create(apiUrl); + request.Method = "GET"; + + using (WebResponse response = request.GetResponse()) + using (Stream stream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(stream)) + { + responseData = reader.ReadToEnd(); + } + } + catch (WebException e) + { + responseData = e.Message; + } + + return responseData; + } + + protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); + + public override Guid ComponentGuid => new Guid("f9fa9c30-76c4-45a9-92ab-df114238634e"); + } +} + diff --git a/PluginGrasshopper/Components/GetEPDs.cs b/PluginGrasshopper/Components/GetEPDs.cs new file mode 100644 index 0000000..768134c --- /dev/null +++ b/PluginGrasshopper/Components/GetEPDs.cs @@ -0,0 +1,80 @@ +using System; +using System.IO; +using System.Net; +using System.Xml; +using Grasshopper.Kernel; +using Newtonsoft.Json; + +namespace PluginTemplate.PluginGrasshopper +{ + public class GetEPDs : GH_Component + { + private const string OKOBAU_URL = "https://oekobaudat.de/OEKOBAU.DAT/resource/datastocks/cd2bda71-760b-4fcc-8a0b-3877c10000a8"; + + public GetEPDs() + : base("Get EPDs from Ökobau", "GetEPDs", + "Get EPDs from Ökobau", + "goeko", "API") + { + } + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddBooleanParameter("Run", "Run", "Set to true to execute loading process", GH_ParamAccess.item, false); + pManager.AddIntegerParameter("Limit", "Limit", "Limit the number of EPDs to retrieve", GH_ParamAccess.item, 10); + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddTextParameter("Data", "Data", "Loaded EPD data from Ökobau", GH_ParamAccess.item); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + bool run = false; + int limit = 10; + + DA.GetData(0, ref run); + DA.GetData(1, ref limit); + + if (run) + { + string epdData = GetEPDData(limit); + DA.SetData(0, epdData); + } + else + { + DA.SetData(0, "Component not executed. Set 'Run' to true to retrieve EPD data."); + } + } + + private string GetEPDData(int limit) + { + string responseData = ""; + + try + { + string apiUrl = $"{OKOBAU_URL}/processes?format=json&pageSize={limit}"; + WebRequest request = WebRequest.Create(apiUrl); + request.Method = "GET"; + + using (WebResponse response = request.GetResponse()) + using (Stream stream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(stream)) + { + responseData = reader.ReadToEnd(); + } + } + catch (WebException e) + { + responseData = e.Message; + } + + return responseData; + } + + protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); + + public override Guid ComponentGuid => new Guid("32c2713d-749c-4903-9c9e-d2ef70ad38fc"); + } +} diff --git a/PluginGrasshopper/Components/ListEPDs.cs b/PluginGrasshopper/Components/ListEPDs.cs new file mode 100644 index 0000000..2495ec6 --- /dev/null +++ b/PluginGrasshopper/Components/ListEPDs.cs @@ -0,0 +1,72 @@ +using Grasshopper.Kernel; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PluginTemplate.PluginGrasshopper +{ + public class ListEPDs : GH_Component + { + public ListEPDs() + : base("List EPDs", "ListEPDs", + "List EPDs names and UUIDs from JSON data", + "goeko", "Read") + { + } + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddTextParameter("JSONData", "JSONData", "JSON data to parse", GH_ParamAccess.item); + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddTextParameter("Names", "Names", "Names extracted from JSON data", GH_ParamAccess.list); + pManager.AddTextParameter("UUIDs", "UUIDs", "UUIDs extracted from JSON data", GH_ParamAccess.list); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + string jsonData = ""; + + DA.GetData(0, ref jsonData); + + if (!string.IsNullOrWhiteSpace(jsonData)) + { + try + { + var names = new List(); + var uuids = new List(); + + JObject jsonObject = JObject.Parse(jsonData); + JArray data = (JArray)jsonObject["data"]; + + foreach (JToken item in data) + { + string name = item["name"].ToString(); + string uuid = item["uuid"].ToString(); + + names.Add(name); + uuids.Add(uuid); + } + + DA.SetDataList(0, names); + DA.SetDataList(1, uuids); + } + catch (Exception ex) + { + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Error parsing JSON data: {ex.Message}"); + } + } + else + { + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "JSON data is empty."); + } + } + + protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); + + public override Guid ComponentGuid => new Guid("9874febf-56d9-40f6-8aa0-7b9efec4c5f2"); + } +} diff --git a/PluginGrasshopper/Components/ParseEPD.cs b/PluginGrasshopper/Components/ParseEPD.cs new file mode 100644 index 0000000..adb4309 --- /dev/null +++ b/PluginGrasshopper/Components/ParseEPD.cs @@ -0,0 +1,164 @@ +using Grasshopper.Kernel; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PluginTemplate.PluginGrasshopper +{ + public class ParseEPD : GH_Component + { + public ParseEPD() + : base("Parse EPD", "ParseEPD", + "Parse EPD result and extract information", + "goeko", "Read") + { + } + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddTextParameter("EPDResult", "EPDResult", "EPD result to parse", GH_ParamAccess.item); + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddTextParameter("Name", "Name", "Name parsed from EPD result", GH_ParamAccess.item); + pManager.AddTextParameter("Unit", "Unit", "Unit parsed from EPD result", GH_ParamAccess.item); + pManager.AddNumberParameter("Amount", "Amount", "Amount parsed from EPD result", GH_ParamAccess.item); + pManager.AddTextParameter("GWPData", "GWPData", "GWP data parsed from EPD result", GH_ParamAccess.item); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + string epdResult = ""; + + DA.GetData(0, ref epdResult); + + if (!string.IsNullOrWhiteSpace(epdResult)) + { + try + { + string name = ""; + string unit = ""; + double amount = 0; + JObject epdObject = JObject.Parse(epdResult); + + // Extract English name + JToken nameToken = epdObject.SelectToken("processInformation.dataSetInformation.name.baseName") + .FirstOrDefault(x => x["lang"].ToString() == "en")?["value"]; + if (nameToken != null) + name = nameToken.ToString(); + + // Extract unit and amount + JToken flowPropertiesToken = epdObject.SelectToken("exchanges.exchange[0].flowProperties[0]"); + if (flowPropertiesToken != null) + { + unit = flowPropertiesToken["referenceUnit"]?.ToString(); + if (double.TryParse(flowPropertiesToken["meanValue"]?.ToString(), out double parsedAmount)) + { + amount = parsedAmount; + } + else + { + throw new Exception("Failed to parse meanValue to double."); + } + } + + // Extract GWP data + string gwpData = ExtractGWP(epdObject); + + DA.SetData(0, name); + DA.SetData(1, unit); + DA.SetData(2, amount); + DA.SetData(3, gwpData); + } + catch (Exception ex) + { + DA.SetData(0, "Error parsing EPD result: " + ex.Message); + DA.SetData(1, ""); + DA.SetData(2, 0); + DA.SetData(3, ""); + } + } + else + { + DA.SetData(0, "EPD result is empty."); + DA.SetData(1, ""); + DA.SetData(2, 0); + DA.SetData(3, ""); + } + } + + private string ExtractGWP(JObject epdObject) + { + var gwpData = new Dictionary(); + + try + { + var gwpResults = epdObject.SelectToken("LCIAResults.LCIAResult[0].other.anies"); + if (gwpResults != null) + { + foreach (var result in gwpResults) + { + var module = result["module"]?.ToString(); + var value = result["value"]?.ToString(); + + if (!string.IsNullOrEmpty(module) && !string.IsNullOrEmpty(value)) + { + // Parse value to double + double parsedValue; + if (double.TryParse(value, out parsedValue)) + { + // If the module exists in the dictionary, add the value + if (gwpData.ContainsKey(module)) + { + gwpData[module] += parsedValue; + } + else + { + gwpData.Add(module, parsedValue); + } + } + } + } + } + } + catch (Exception e) + { + throw new Exception("Error extracting GWP data: " + e.Message); + } + + // If A1-A3 is not present, calculate and add it + if (!gwpData.ContainsKey("A1-A3")) + { + double sumA1A3 = (gwpData.ContainsKey("A1") ? gwpData["A1"] : 0) + + (gwpData.ContainsKey("A2") ? gwpData["A2"] : 0) + + (gwpData.ContainsKey("A3") ? gwpData["A3"] : 0); + gwpData.Add("A1-A3", sumA1A3); + } + + // Remove individual A1, A2, A3 entries + foreach (var module in new List { "A1", "A2", "A3" }) + { + gwpData.Remove(module); + } + + // Construct the output string + List formattedResults = new List(); + foreach (var kv in gwpData) + { + formattedResults.Add($"{kv.Key}: {kv.Value}"); + } + + return string.Join(", ", formattedResults); + } + + + + + + protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); + + public override Guid ComponentGuid => new Guid("98a9c3b5-9911-4a7f-85ec-d56c1cfe62f8"); + } +} diff --git a/PluginGrasshopper/Components/ParseGWP.cs b/PluginGrasshopper/Components/ParseGWP.cs new file mode 100644 index 0000000..79fe79a --- /dev/null +++ b/PluginGrasshopper/Components/ParseGWP.cs @@ -0,0 +1,70 @@ +using Grasshopper.Kernel; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; + +namespace PluginTemplate.PluginGrasshopper +{ + public class ParseGWP : GH_Component + { + public ParseGWP() + : base("Parse GWP Data", "ParseGWP", + "Parse GWP data from EPD result", + "goeko", "Read") + { + } + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddTextParameter("EPDResult", "EPDResult", "EPD result to parse", GH_ParamAccess.item); + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddNumberParameter("A1-A3", "A1-A3", "GWP data for A1-A3", GH_ParamAccess.item); + pManager.AddNumberParameter("A4", "A4", "GWP data for A4", GH_ParamAccess.item); + pManager.AddNumberParameter("A5", "A5", "GWP data for A5", GH_ParamAccess.item); + pManager.AddNumberParameter("B1", "B1", "GWP data for B1", GH_ParamAccess.item); + pManager.AddNumberParameter("C1", "C1", "GWP data for C1", GH_ParamAccess.item); + pManager.AddNumberParameter("C2", "C2", "GWP data for C2", GH_ParamAccess.item); + pManager.AddNumberParameter("C3", "C3", "GWP data for C3", GH_ParamAccess.item); + pManager.AddNumberParameter("C4", "C4", "GWP data for C4", GH_ParamAccess.item); + pManager.AddNumberParameter("D", "D", "GWP data for D", GH_ParamAccess.item); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + string epdResult = ""; + + DA.GetData(0, ref epdResult); + + if (!string.IsNullOrWhiteSpace(epdResult)) + { + try + { + string[] results = epdResult.Split(','); + + foreach (string result in results) + { + string[] parts = result.Split(':'); + string category = parts[0].Trim(); + double value = double.Parse(parts[1].Trim()); + DA.SetData(category, value); + } + } + catch (Exception ex) + { + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Error parsing GWP data: {ex.Message}"); + } + } + else + { + AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "EPD result is empty."); + } + } + + protected override System.Drawing.Bitmap Icon => ResourceLoader.LoadBitmap("PluginGrasshopper_24.png"); + + public override Guid ComponentGuid => new Guid("bf9e2c05-3179-4c38-a145-4b3dfe0c0f79"); + } +} diff --git a/gh-examples/read-epd.gh b/gh-examples/read-epd.gh new file mode 100644 index 0000000..ef6778f Binary files /dev/null and b/gh-examples/read-epd.gh differ