Grasshopper

algorithmic modeling for Rhino

Hi Everyone,

I am working on a custom component and ran into some problem with EnabledChanged event in GH_Document. What I want to do is delete a layer in rhino when grasshopper window is closed or change to a different document. Everything works fine with the exception that if a new component is added by copying an existing component on the canvas, somehow the EnabledChanged event handler will never get called and the layer will not be deleted. I would like to know if there is any problem with my code or if there is a better approach. I have attached both gh and gha file. This is my code:

static int count = 0;
int id = 0;

public override void AddedToDocument(GH_Document document)
{
id = count;
count++;
Rhino.RhinoDoc.ActiveDoc.Layers.Add("dummy"+id.ToString(), System.Drawing.Color.Red);
document.EnabledChanged += new GH_Document.EnabledChangedEventHandler(document_EnabledChanged);
base.AddedToDocument(document);
}

void document_EnabledChanged(object sender, GH_DocEnabledEventArgs e)
{
if (e.Enabled)
Rhino.RhinoDoc.ActiveDoc.Layers.Add("dummy" + id.ToString(), System.Drawing.Color.Red);
else
{
int layer = Rhino.RhinoDoc.ActiveDoc.Layers.Find("dummy" + id.ToString(), true);
Rhino.RhinoDoc.ActiveDoc.Layers.Delete(layer, false);
}
}

Thanks,

Alan

Views: 604

Attachments:

Replies to This Discussion

From debugging, it seems like when I create a new component by copy and paste, the GH_Document document argument passed into AddedToDocument method is an unamed document instead of the current GH_Document. Therefore, the event handler is not binding to the event in the current GH_Document. Any thoughts?

Alan

Looking into it.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Ok, got it. It's a bit of a mess, but not a bug as such.

When components are pasted from the clipboard, they will always be added to a blank document first. There's no way around this. So you will get a AddedToDocument call with an unnamed document.

When this blank document has been created, it will be merged with the current document. This does not result in two calls to RemovedFromDocument(unnamed) and AddedToDocument(currentdoc). You could argue that it should, and I might even agree with you, but it doesn't at present.

Instead, the MovedBetweenDocuments method is called with the unnamed and current documents both provided.

This is a prime example of how the GH 1.0 SDK is a bit of a mess. This sort of thing needs to be redesigned from scratch but I won't be able to do so until GH 2.0

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David,

Thanks for the answer. I will override the MovedBetweenDocuments method as well. 

Alan 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service