Grasshopper

algorithmic modeling for Rhino

public class MyComponent:GH_Component

{

  myObject obj;

 

  void MyPreview(object sender, DrawEventArg e)

  {

    //draw my object in the display pipeline

  }

 

  public override void AddedToDocument(GH_Document document) 

  {            

    DisplayPipeline.PostDrawObjects += this.MyPreview;

    base.AddedToDocument(document);

  }

  public override void RemovedFromDocument(GH_Document document)

  {

    DisplayPipeline.PostDrawObjects -= this.MyPreview; 

    RhinoDoc.ActiveDoc.Views.Redraw();   

    base.RemovedFromDocument(document);  

   }

}

 

My problem is that when the GH document is closed, if the component instance was on the canvas, the custom object stays in the display pipeline.  The object will not disappear untill Rhino closed.

Views: 915

Replies to This Discussion

Hi Ian,

you should override the methods on GH_Component to draw your geometry:

IsPreviewCapable should be overridden to always return True.

ClippingBox should be overridden to make sure the geometry you want to draw is not clipped (be sure to also call the base class method and union your boxes).

DrawViewportMeshes and/or DrawViewportWires should be overriden to draw your stuff. Again, be sure to call the base class methods so that peview geometry in your input/output parameters is also drawn.

Is this enough to get you going?

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks, David!

It's greatly appreciated!

I override the Locked as well, so that when the component is set locked, my objects won't stay on the screen

public override bool Locked

         {

             get

             {

                 return base.Locked;

             }

             set

             {

                 base.Locked = value;

                    if(Locked)

                    {

                      //code that cleans my objects

                     }

             }

         }

You might want to rely on the Hidden property instead.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

In this component, all the objects that DrawViewportwire draws are not parameters. They're in the component's field, assigned by solverInstance. I had tried Hidden property before. But when the component is disabled, "ctrl+q" still can change the Hidden property.  

You're right.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

A component creates a windows.form. Then if the active GH_document is closed, the form can not be disposed. Is there anything I can do when the document is closing?

Grasshopper.GH_InstanceServer has a member called DocumentServer. This class maintains all the loaded documents that Grasshopper is aware of. DocumentServer has events for DocumentAdded and DocumentRemoved. If you handle DocumentRemoved from within your Form and close/dispose yourself at that point. Or was that not what you were asking?

--

David Rutten

david@mcneel.com

Poprad, Slovakia

DocumentRemoved event is raised when a document, any of the opened documents, is closed.  So the form is closed not only when its parent document is closed. But, problem solved.  Thanks, David!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service