Save data from Net on Gh Component

script
grasshopper
sdk

#1

Hi, everyone
Well, I am writing this c# script-component, which has to recieve data from UDP(network) or within Grasshopper if no UDP recieved. In case a valid UDP recieved I want to save it in Component/ Gh file so it persists and not necessary to continue sending udp s!
For that I tried to write on GH file itself, it works . Problem happens when two or more on the active canvas should be added.
I used: an "GH_archive object then writeToFile method
I can not use the write() method , not for component or GH_Archive s ad they need GH_IWriter type and it is Interface
and can not be initialized.
the data i have is of double[] type.

I would be very thankfull for any hint/ help!
cheers, Kaveh


(David Rutten) #2

Hi Kaveh,

I’m a little bit confused about exactly what you’re trying to do. Are you looking to serialize your own data somewhere to a separate file, or would you like to ‘cache’ your data inside the gh file itself so it only has to load it once?


#3

I guess Cache is the term. This is the part of code

double[] ld ;
ld = listDouble.ToArray();
string key = this.Component.InstanceGuid.ToString(); //I m using guid to make it possible for multiple of this component write separately
String fName = this.GrasshopperDocument.FilePath;
GH_Archive archive = new GH_Archive();

//save the recieve data in GH component/GH file
if (setPermanent) //this a boolean triggered by a toggle button in GH canvas
{

//I guess somewhere here I have to clean of data saved on file previously
archive.CreateNewRoot(true);
GH_Chunk root = archive.GetRootNode;
if (root == null)
throw new Exception(“no root”);
root.SetDoubleArray(key, ld);
GH_IWriter writer = root.CreateChunk(key);
this.Component.Write(writer);
archive.WriteToFile(fName, true, false);
}
try{
archive_.ReadFromFile(fName);
ld = archive_.GetRootNode.FindChunk(key).GetDoubleArray(key);
}
catch (NullReferenceException ){Print(“Not yet any Data saved!”);}

pData = ld;

pData is the output in GH
now this is working(but still with bugs). but the problem is it is not overwritten. pData become biger and biger!
the main issue I have is to ’ cache’ data in component , so it would be incorporated with the component so to speak so I do not leave information when component moved, deleted etc.


(David Rutten) #4

Surely it would be better if the data was stored directly into the individual component itself, rather than as a loose file associated by InstanceGuid?


(David Rutten) #5

I attached C# code for a component which downloads an entire webpage and caches the result. It writes the data to the GH file, meaning the cache survives Copy/Paste and Save/Open cycles.
GetRequest.cs (3.1 KB)