Skip to content

Commit

Permalink
Added 5 basic components
Browse files Browse the repository at this point in the history
Added 5 basic components: GetEPDbyUUID; GetEPDs; ListEPDs; ParseEPD, ParseGWP
  • Loading branch information
Curiosit committed Mar 10, 2024
1 parent 3b3b383 commit 01bd02e
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 105 deletions.
105 changes: 0 additions & 105 deletions PluginGrasshopper/Components/FindMaterial.cs

This file was deleted.

81 changes: 81 additions & 0 deletions PluginGrasshopper/Components/GetEPDbyUUID.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}

80 changes: 80 additions & 0 deletions PluginGrasshopper/Components/GetEPDs.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
72 changes: 72 additions & 0 deletions PluginGrasshopper/Components/ListEPDs.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
var uuids = new List<string>();

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");
}
}
Loading

0 comments on commit 01bd02e

Please sign in to comment.