Save data from Net on Gh Component



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?


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
GH_Chunk root = archive.GetRootNode;
if (root == null)
throw new Exception(“no root”);
root.SetDoubleArray(key, ld);
GH_IWriter writer = root.CreateChunk(key);
archive.WriteToFile(fName, true, false);
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)