GH - Error message on Input.RemoveAllSources() - How to avoid? [Solved]

rhino5

#1

Hi,

I want to remove all sources (wires) connected to an Inport using the command

    ...Params.Input[Index].RemoveAllSources();

… but when & where should I remove while avoiding to expire a solution while its running?

I tried searching on both forums, and GrassHopper SDK documentation doesn’t seem to explain these rather murky areas of GH… :face_with_monocle:

Awards for a solution to this one: :medal_military:

// Rolf


(David Rutten) #2

There’s two main situations in which code that modifies the file runs:

  1. As a response to a UI event, this always happens outside of solutions because while a solution runs the UI is not processing messages.
  2. As a part of a scheduled solution.

I assume you need #2

Inside your RunScript or SolveInstance or whatever method, get the document that contains your component. In a C# script that’s just GrasshopperDocument, inside a compiled GH_Component class that’s by calling the OnPingDocument() method and making sure you didn’t get null in return. Then you schedule a solution on that document, usually picking a short interval. Something between 5 to 50 milliseconds for example. You also must provide a schedule callback. This callback method will be called whenever the next solution kicks off, even if it is way earlier than your schedule request. Inside the callback you are allowed to make all the changes you want, add objects, delete objects, modify values, add or remove connections etc.


#3

OK, thanks for you reply.

I tried the following code but I don’t get expected response (“Scheduled response”). What am I doing wrong?

  private void RunScript(object x, object y, ref object A)
  {
    var callbackDelegate = new GH_Document.GH_ScheduleDelegate(this.Callback);
    if (callbackDelegate != null)
    {
      GrasshopperDocument.ScheduleSolution(5, callbackDelegate);
      Print("Yup, seems OK.");
    }
    else
    {
      Print("Scheduling Callback returns null!");
    }

  }

  // <Custom additional code> 

  private void Callback(GH_Document doc)
  {
    this.Print("Scheduled response!");
  }

  // </Custom additional code> 

callback_test.gh (2.4 KB)

// Rolf


(David Rutten) #4

Not sure what the mistake is, but you can just use the method name without parenthesis:

GrasshopperDocument.ScheduleSolution(5, Callback); 

#5

Eh, it actually works. Only Print doesn’t work…

Problem solved.

// Rolf


(David Rutten) #6

ah! That’s the problem.

It’s too early to start printing stuff, the solution isn’t really running yet. Also your component is not automatically expired, so there’s no guarantee it will run at all in this solution.

Use RhinoApp.WriteLine() if you want to test code like this, that always works.