How does delete the component by script?

how does delete the component by script?
in this example all the components that name in panel
will be Enable Disable by c# or python script
also how delete them?


Enable Disable.gh (10.4 KB)
obj.Delete = !_active;????

  private void RunScript(DataTree<string> nickNames, bool active, ref object A)
  {
    _nickNames = nickNames;
    _active = active;
    GrasshopperDocument.ScheduleSolution(5, Callback);
  }

  // <Custom additional code> 
  private DataTree<string> _nickNames;
  private bool _active;
  private void Callback(GH_Document doc)
  {
    foreach(var obj in GrasshopperDocument.ActiveObjects())
      if(_nickNames.AllData().Contains(obj.NickName))
      {
        obj.Delete = !_active;
        obj.ExpireSolution(false);
      }
  }

@Mahdiyar

You could try calling GrasshopperDocument.RemoveObjects.

Note that you should not do that inside your current foreach on GrasshopperDocument.ActiveObjects(). Instead collect the list of objects you want to delete, then outside the loop remove them.

1 Like
 A = GrasshopperDocument.RemoveObjects(nickNames, active);


deledte components.gh (11.8 KB)
bug?

private void RunScript(List<string> nickNames, bool delete, ref object A)
{
  if (!delete) return;
  var objs = new List<IGH_DocumentObject>();
  foreach(var obj in GrasshopperDocument.Objects)
    if(nickNames.Contains(obj.NickName))
     objs.Add(obj);
  GrasshopperDocument.RemoveObjects(objs, false);
}

Ehsan.gh (4.9 KB)

1 Like

Thanks @Mahdiyar
Just one more question about this post, maybe we can replace all the x components in the same position with the y component? For example, wherever the tool was called panel, replace it with valueList or numbers slder …?all panel replace by numberslider

@Mahdiyar

  private void RunScript(List<string> nickNames, bool delete, object nickNamesReplace, ref object A)
  {
    if (!delete) return;
    var objs = new List<IGH_DocumentObject>();
    var objs2 = new List<IGH_DocumentObject>();
    foreach(var obj in GrasshopperDocument.Objects)
    {  if(nickNames.Contains(obj.NickName))
        objs.Add(obj);
      if(nickNames.Contains(obj.nickNamesReplace));
      objs2.Add(obj);}
    GrasshopperDocument.RemoveObjects(objs, false);
    GrasshopperDocument.AddObject(objs2, false);


replace.gh (20.5 KB)

Please actually read the error messages and try to understand them instead of trying to guess your way through typing code.

  1. nickNameReplace - you came up with this name. It does not exist in the Grasshopper API on IGH_DocumentObject. You can’t come up with names and expect them to work. You want to check whether obj.NickName is equal to nickNameReplace.
  2. Your code layout is horrible. Take time to write it properly: good indentation, good bracketing, etc.
  3. For error 3 in your screenshot: read the documentation on AddObject and now try to understand why the error happens. Then think about how to fix it (hint: foreach)

It is possible to add components and to wire them up. Here some old code I wrote to salvage shaders setup. Here some links into that code:

Creating the GH component instances

Creating attributes for each instance (with a random location) and add the instance to the GH document

Finally wire up all components

Code is in F#, but it shows that it is possible. For this particular usage I had AutoGraph plug-in to do the layout after creating the initial reverse engineered version of the shader graph.

3 Likes

Thanks for reply! Do you think Is this code possible for me to take the time to do a plugin for this purpose?

I am not entirely sure what you meant to ask, but it is possible to create a plug-in with which you can create parts of a definition automatically, or even complete ones. The code I showed is in F#, you probably want to grasp it enough to learn how to do it in C#.