Skip to content

Commit

Permalink
Loading material Name via UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
Curiosit committed Mar 9, 2024
1 parent 31fee2e commit 3b3b383
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<!--
packages which are independent of Rhino release
-->
<Import Project="$(MSBuildThisFileDirectory)\Packages.props" />
<!--
packages specific to Rhino 7
-->
<Import Project="$(MSBuildThisFileDirectory)\PackagesRhino7.props" Condition="$(Configuration.Contains('R7'))"/>
<Import Project="$(MSBuildThisFileDirectory)\PackagesRhino7.props" Condition="$(Configuration.Contains('R7'))" />
<!--
packages specific to Rhino 8
-->
<Import Project="$(MSBuildThisFileDirectory)\PackagesRhino8.props" Condition="$(Configuration.Contains('R8'))"/>
<Import Project="$(MSBuildThisFileDirectory)\PackagesRhino8.props" Condition="$(Configuration.Contains('R8'))" />
</Project>
36 changes: 33 additions & 3 deletions PluginGrasshopper/Components/FindMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.IO;
using System.Net;
using System.Xml;
using Newtonsoft.Json;

namespace PluginTemplate.PluginGrasshopper
{
Expand All @@ -19,6 +21,7 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM
{
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)
Expand All @@ -30,18 +33,23 @@ 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);
string loadedData = LoadDataFromDatabase(uuid, timeout * 1000); // Convert timeout to milliseconds
DA.SetData(0, loadedData);
}
}

private string LoadDataFromDatabase(string uuid)
private string LoadDataFromDatabase(string uuid, int timeoutMilliseconds)
{
string apiUrl = $"https://www.oekobaudat.de/OEKOBAU.DAT/resource/processes/{uuid}";

Expand All @@ -50,7 +58,7 @@ private string LoadDataFromDatabase(string uuid)
try
{
WebRequest request = WebRequest.Create(apiUrl);
request.Timeout = 5000; // Reduced timeout to 5 seconds
request.Timeout = timeoutMilliseconds; // Set the timeout
request.Method = "GET";

using (WebResponse response = request.GetResponse())
Expand All @@ -59,6 +67,9 @@ private string LoadDataFromDatabase(string uuid)
{
responseData = reader.ReadToEnd();
}

// Process XML data and convert to JSON
responseData = ProcessXmlData(responseData);
}
catch (WebException e)
{
Expand All @@ -68,6 +79,25 @@ private string LoadDataFromDatabase(string uuid)
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");
Expand Down
4 changes: 4 additions & 0 deletions PluginGrasshopper/PluginGrasshopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>

<Import Project="../CommonSettingsFinal.csproj" />

</Project>

0 comments on commit 3b3b383

Please sign in to comment.