Grasshopper C# Script - grouping and naming imported geometry

Hi GH Community!

I am hoping to get some help with a c# grasshopper script :pray:

  1. I am trying to group some imported geometry using c#

  2. I would also like to “remember” this imported group/geometry somehow so that it can be deleted on demand later on.

I have gotten this far with the c# script - I can’t seem to get the ObjectID from the list…

private void importGeo(string filePath){

       // Import the geometry
        RhinoDoc.ActiveDoc.Objects.UnselectAll();
        string cmd = "!_-Import " + "\"" + filePath + "\"" + " _Enter";
        Rhino.RhinoApp.RunScript(cmd, false);

       // add selected geometries to a list
        var selectedObjs = RhinoDoc.ActiveDoc.Objects.GetSelectedObjects(false, false).ToList();

        List<Guid> ids = new List<Guid>();
        for(int i = 0; i < selectedObjs.Count; i++){
            ids.Add(selectedObjs(i).ObjectId); // this line seems to be the issue!
        }

        var doc = RhinoDoc.ActiveDoc;

        int index = doc.Groups.Add(ids);
        doc.Views.Redraw();
    }

Any help/direction would be greatly appreciated :pray:
Thank you!

??? Maybe I’m missing something, but if you “Unselect”, you will receive nothing when calling “GetSelectedObjects”. Does selectedObjs contain any element?

Hey @mikity_kogekoge - I am unselecting before I run the import command in case there is something selected before importing. Then only the newly imported objects are selected.

The issue regarding getting the Object Id’s seems to be at this line where the editor is throwing a scope error :
ids.Add(selectedObjs(i).ObjectId);

Oh, I see, you mean when importing a file, new objects that came in are automatically selected? Okaay.
Seems like you need to say selectedObjs[i].Id or selectedObjs[i].Attributes.ObjectId instead. Note also that parentheses must be replaced with square brackets.

DId it work?

Hey @mikity_kogekoge - It worked thank you for that! :zap: