Get grasshopper canvas size with c#


Is it possible to get the height and width dimensions of the whole canvas size in grasshopper?

this is what I tried so far:

 if (run){
      //var viewport = new Grasshopper.GUI.Canvas.GH_Viewport();
      var view = new Grasshopper.GUI.Canvas.GH_Canvas();
      height = view.Height;
      width = view.Width; (6.5 KB)

many thanks!

I think this should do it:

    if (run){
      var canvas = Grasshopper.Instances.ActiveCanvas;
      var rec = GH_Convert.ToRectangle(canvas.Document.BoundingBox(false));
      height = rec.Height;
      width = rec.Width;

No point making a new canvas, it will be different from the one used in the editor.

Grasshopper.Instances.ActiveCanvas gives you access to the instance used in the main editor. Careful, it may be null. You can access the width and height properties on that instance.

1 Like

thanks, sorry for posting a bit early :grinning: