C# Grasshopper Disconnect all components

Hi

I’m struggling with disconnecting all components.
I couldn’t find the component which disconnects components in Metahopper.

So, I tried to script by myself but I’m totally new to handle GrasshopperDocument…

private void RunScript(bool Button, ref object A)
  {
    GH_Document doc = GrasshopperDocument;
    List<IGH_DocumentObject> deleteList = new List<IGH_DocumentObject>();
    if(Button)
    {
      foreach(IGH_DocumentObject obj in doc.Objects)
      {
        string name = obj.Name;
        Guid guid = obj.ComponentGuid;
        PointF pt = obj.Attributes.Pivot;
        Grasshopper.Instances.ActiveCanvas.InstantiateNewObject(guid, pt, false);
        deleteList.Add(obj);
      }
      doc.RemoveObjects(deleteList, true);
    }
  }

First, in foreach loop, trying to get guid of component and adding component to delete list.
Second, after loop, remove components in delete list.

script above doesn’t work and rhino will crash.

Is there anyone how to disconnect all components?

You’re deleting all components and replacing them with new instances. That’s far more destructive than just disconnecting wires. It’s also not allowed during a solution. Here’s what you need to do:

In your RunScript method you call GrasshopperDocument.ScheduleSolution(10, ScheduleCallback). That’s all you do inside the solution.

Then add a method called ScheduleCallback(GH_Document doc) to your script. This method will be invoked before the next solution starts.

Inside you callback, iterate over all objects in the provided doc, top-level and subsidiary ones. This means both free-floating and input/output parameters.

Try to cast each object to IGH_Param and if successful, remove all its sources.

1 Like

Thanks for your replay, David

I tried your suggestion but only Panel component will be disconnected.

private void ScheduleCallback(GH_Document doc)
  {
    foreach(IGH_DocumentObject obj in doc.Objects)
    {
      try
      {
        IGH_Param param = (IGH_Param) obj;
        param.RemoveAllSources();
      }
      catch
      {
        Print("failed to disconnect");
      }
    }
  }

I think there is something wrong with casting.

You’re iterating over all the objects in the document, which means only the top-level ones like floating parameters and component. You also need to include all subsidiary objects such as input and output parameters. You can do this I think by iterating over all the attributes in the document and then getting the owner object of those attributes.

I can tell you for sure once I get into the office though.

David,

Finally I succeeded to disconnect components.

private void ScheduleCallback(GH_Document doc)
  {
    foreach(IGH_Attributes att in doc.Attributes)
    {
      IGH_DocumentObject obj = att.DocObject;
      try
      {
        IGH_Param param = (IGH_Param) obj;
        param.RemoveAllSources();
        Rhino.RhinoApp.WriteLine("Suceeded to disconnect");
      }
      catch
      {
        Rhino.RhinoApp.WriteLine("failed to disconnect");
      }
    }
  }

However, I couldn’t understand difference between number of attributes and objects.
I guess you mentioned this difference as top-level and subsidiary ones.

3 Likes

The cast operator throws an exception if the conversion doesn’t work. You can use the as keyword instead:

var param = obj as IGH_Param;
param?.RemoveAllSources();

Although the ?. operator won’t work in the C# component, so you’ll have to check for null yourself.

2 Likes