Creating Collections in C# speckle-sharp Rhino/GH

Hi! Looking for some help regarding Speckle Collections in C# (RhinoCommon + Grasshopper)

I am attempting to send some geometry to Speckle as nested Collections with 3 levels.

Collection: Level 1
├── elements
│   ├── Collection: Level 2
│   │   ├── elements
│   │   │   ├── Collection: Level 3
│   │   │   │   └── elements
│   │   │   │       ├── Brep1
│   │   │   │       ├── Brep2
│   │   │   │       └── Brep3
│   │   │   └── Collection: Level 3
│   │   │       └── elements
│   │   │           ├── Brep1
│   │   │           └── Brep2
│   └── Collection: Level 2
│       ├── elements
│       │   ├── Collection: Level 3
│       │   │   └── elements
│       │   │       ├── Brep 1
│       │   │       ├── Brep 2
│       │   │       └── Brep 3
│       │   └── Collection: Level 3
│       │       └── elements
│       │           ├── Brep 1
│       │           └── Brep 2

Sending works fine and the data is showing on the frontend in my model but the collection structure is not. Could there be something I am missing?

I have versions SpeckleCore2, Version=2.21.3.16520 and Speckle.Objects 2.21.3

To simply test sending Collections currently what I am doing is this (simplified for compactness):

//Setup converter
private readonly static ISpeckleConverter _converter = KitManager.GetDefaultKit().LoadConverter("Grasshopper8");
public SpeckleBridge()
{
    // Set Converter context document
    _converter.SetContextDocument(RhinoDoc.ActiveDoc);
}

// Set collection to root Base object for sending
    Speckle.Core.Models.Base testSend = new();

    if (_site != null)
    {
       testSend["Elements"] = converter.ElementsToSpeckle(_site);
    }


// Method converts elements and returns the collection
public Speckle.Core.Models.Collection ElementsToSpeckle(siteGeometry site)
{
      // A Speckle Collection to contain all the site Elements to export
      Speckle.Core.Models.Collection elementsCollection = new("Site Elements", "Elements")
      {
          applicationId = Guid.NewGuid().ToString()
      };
      // Converts the geometry to breps 
     ....
      //
      Speckle.Core.Models.Collection elementCollection = new("ElementLayers", "Layers");
      elementsCollection.elements.Add(elementCollection);
      return elementsCollection;   
}

And the Speckle.Core.Models.Base object is sent with the send node (sending works fine as mentioned above). Very grateful for any help!

Hi @katjat !

Do you need to create collections via C# in Grasshopper, or are you fine with creating them with a Speckle component?

I see you’re currently using our V2 Grasshopper connector and kit - we just released the Next-Gen Grasshopper connector that has a Create Collection node which would make this a lot easier:

I would recommend switching to our Next-Gen Rhino and Grasshopper connectors if you can!

Otherwise, the root Base object should actually be a Collection, try:

    // Set collection to root Base object for sending
    Speckle.Core.Models.Collection root = new();
    if (_site != null)
    {
       root.elements.Add(converter.ElementsToSpeckle(_site));
    }

Thank you for the quick response! The new connectors seem great and I will discuss this with the rest of the team but for the time being I would need to solve this in the code. I changed the root to a collection and added the data as you suggested but unfortunately the collection(s) are still not visible in the model.

Do you have an example model version you can share? I can set up a support project for you on app.speckle.systems if that helps

1 Like

I see, if you’re using our Send component to send, it will still attach everything to a Base at the root (this prevents our viewer from parsing the collection structure).
In order to get the Collections to show up, you would need to send programmatically via our sdk

I see, thank you so much for the help! I will move ahead accordingly.