Grasshopper

algorithmic modeling for Rhino

Creating background record using static variables of Grasshopper document

Hi, I am trying to set up a few plugin components using VS in C#.

 

The idea is to keep a record (in the form of lists of strings) of the items that are passed trough the components in the plugin (for instance points). Since this list needs to be accessible for all my components it needs to be stored in some class that is accessible for all components. This class should provide for the components to check values or to append/ assign them from within their SolveInstance domain.

 

Apart from storing their coordinates I would like to keep track of their 'numbers'. These numbers should be assigned automatically and the way this is done can be expressed in the following pseudo code:

input = somearrayofpoints

 

for each point in input:

    check = checkifcoordinatesexist(point)  (true is yes, false is no)

    if check == True:

        nr= getLastNr() (returns last item from some record database)

        store as string (nr + point)

    else:

        nr = getNrAt(point.coords) (returns the node number at given coordinates in   record database)

        store as string (nr + point)

How would I go about setting some structure like this up?

I have tried a following simpler set up using a Singleton class where I have a public class that basically returns the value of a number. Then I have a Grasshopper component that just counts the amount of points it gets and add it to the recorded amount of points like:

int counter = points.Count;

SingletonClass singleton = SingletonClass.getInstance();
singleton.getValue += counter;

No multiple of these components 'add' their counts together but with every run they keep increasing. So I need to in this set up I need to clear the data for every run. 

So two questions:

1. is this a good start to achieve the above mentioned result, if not any suggestions? references?

2. if so, ho do I deal with the Grasshopper runtime and clear the data send to the Singleton class? Can I use event handlers for solver events for this?

Any suggestion would be greatly appreciated!

(I have attached the singletonclass.cs class and the counter.cs component for reference)

 

Views: 1298

Attachments:

Replies are closed for this discussion.

Replies to This Discussion

Update:

I have made some progress with this strategy I have extended the Control (Singleton method) class to deal with event handlers like, these event handlers are created at initiation of the Control instance (to ensure that this only happens once per document).

public class Control
{

private Control() { registered = false; }

public static Control getInstance(GH_Document document)
{
if (_instance == null)
{
_instance = new Control();
if (!_instance.registered)
{
document.RaiseEvents = true;
document.SolutionStart += _instance.document_SolutionStart;
document.SolutionEnd += _instance.document_SolutionEnd;
}
}
return _instance;
}

Then upon these events methods can be called such as;


void document_SolutionStart(object sender, GH_SolutionEventArgs e)
{
// clearing databases (this actually fixes the above question)

// read jobs (from external source)

// setting slider values

}

void document_SolutionEnd(object sender, GH_SolutionEventArgs e)
{
// write results (to external source)

// schedule next run (immediate to create loop)

// optionally you can schedule the solution over longer periods (per second or x amount of seconds to poll some data source for items)

}

I any case if no one has anything to add to this I will close this discussion soon.

cheers dion

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service