How to trigger a component serialization?

How can I trigger a component serialization in code, using C#?

I have implemented Read and Write (which works when, for example, saving the definition), but would like to trigger Write also by myself.

Are you looking for a new way to save your component? Then you could implement your own

IGH_Writer:
https://developer.rhino3d.com/api/grasshopper/html/T_GH_IO_Serialization_GH_IWriter.htm

and call the Write method yourself. Or, easier, just use a new empty
GH_Chunk:
https://developer.rhino3d.com/api/grasshopper/html/T_GH_IO_Serialization_GH_Chunk.htm

In general, though, this is the task of the
GH_Archive,
https://developer.rhino3d.com/api/grasshopper/html/T_GH_IO_Serialization_GH_Archive.htm
which you can use to read/write a definition.

@DavidRutten might be able to tell more.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

I’m happy to write to the existing archive, I’m just not sure how to do that.

I’m confused too. What problem are you solving? And do you want to end up with a byte[], a MemoryStream, a *.gh file, or…?

Either way, it’s quite straightforward as long as you have access to GH_IO.dll, which you almost always do.
Writecomponent.gh (31.7 KB)

var chunk = new GH_LooseChunk(null);
Component.Write(chunk);
    
Bin = chunk.Serialize_Binary();
Xml = chunk.Serialize_Xml();

3 Likes

Got it! I realize the question wasn’t very clear, but you’ve answered it.
(Basically I wasn’t sure how to use Write.)