Modify gh file with GH_IO.dll

I have a lot of grasshopper files with text panels which i would like to modify using a simple CLI app (rather than opening each file)
I hope to change:

  • colour
  • font
  • text size
  • text
  • nickname

Is this possible?

GH_IO.dll appears to provide the necessary infrastructure to do this, however i haven’t been able to successfully actually modify a gh file.

I can access the information i care about thanks to davids help here

A simple example of what i’m trying to achieve is this:
image (1.9 KB) (1.6 KB)

My attempt so far reads the panel colour, font and text size, but only writes a copy of the existing file
Program.cs (4.5 KB)

@DavidRutten Do you have an example of this kind of modification?

        private static void ShowPanelData(GH_Archive archive)
            int missing = 0;
            var root = archive.GetRootNode;
            var definition = root.FindChunk("Definition");
            var objects = definition.FindChunk("DefinitionObjects");
            var count = objects.GetInt32("ObjectCount");
            for (int i = 0; i < count; i++)
                var chunk = objects.FindChunk("Object", i);
                if (chunk is null)
                    var name = chunk.GetString("Name");
                    if (name == "Panel")
                        var container = chunk.FindChunk("Container");
                        var properties = container.FindChunk("PanelProperties");
                        var nickName = container.GetString("NickName");
                        var userText = container.GetString("UserText");
                        var colour = properties.GetDrawingColor("Colour");
                        var font = properties.FindChunk("Font");
                        var fontFamily = font.GetString("Family");
                        var fontSize = font.GetSingle("Size");
                        string panel = string.Format("Panel({0}, {1}, {2}, {3}, {4})", nickName, colour, fontFamily, fontSize, userText);
                        WriteLine(ConsoleColor.Gray, panel);
                        fontSize = 6;

I can’t answer in detail until I get to the office, but you’ll have to call SetString, SetSingle, SetDrawingColor, etc. to make those changes. The only question I don’t know the answer to right now is if you can just overwrite those values or if you need to remove the old ones first.

Removal is probably best, just in case. GH_Chunk.RemoveItem(string name) is what you want. So (top of my head):

String value = chunk.GetString("name");
if (value != "what I want")
  chunk.SetString("name", "what I want");

Thanks David! That did the trick.
Yes, as you suggested it is necessary to remove the item first.
I found that I also needed to cast everything to chunks in order to access the RemoveItem() and SetString() functions etc

GH_Chunk chunk = objects.FindChunk("Object", i) as GH_Chunk;

I’ll provide an example of the result shortly.

1 Like