GoSubGraph to fit grid

When I create a GoSubGraph from a group og objects, the size is determined by the different objects. And the SubGraph rectangle does not fit the grid corners.
How can I make the subgraph fit the nearest grid points, when I create a new SubGraph. What I wish is that the Subgraph inflate itself until it matches a grid point for all 4 corners of its rectangle.
I Hope anyone can help me here, since I just cant find a solution on this one.

That’s an interesting request. I always like hearing about new features that I hadn’t imagined before.
Well, I’m not sure this will do what you want, but you could try:
public override RectangleF ComputeInsideMargins(GoObject ignore) {
RectangleF r = base.ComputeInsideMargins(ignore);
// compute border
RectangleF b = r;
SizeF tlmargin;
SizeF brmargin;
if (this.IsExpanded) {
tlmargin = this.TopLeftMargin;
brmargin = this.BottomRightMargin;
} else {
tlmargin = this.CollapsedTopLeftMargin;
brmargin = this.CollapsedBottomRightMargin;
b.X -= tlmargin.Width;
b.Y -= tlmargin.Height;
b.Width += tlmargin.Width + brmargin.Width;
b.Height += tlmargin.Height + brmargin.Height;
// figure border to fit grid
// NOTE: this computation hard-codes the grid cell size to 50x50 and grid origin to 0,0
float dl = b.Left % 50;
float dt = b.Top % 50;
float dr = b.Right % 50;
if (dr > 0) dr = 50 - dr;
float db = b.Bottom % 50;
if (db > 0) db = 50 - db;
// adjust the inside-margins rectangle so the border will fit grid
r.X -= dl;
r.Y -= dt;
r.Width += dl + dr;
r.Height += dt + db;
return r;
public override RectangleF ExpandPaintBounds(RectangleF rect, GoView view) {
RectangleF r = base.ExpandPaintBounds(rect, view);
r.Inflate(50, 50); // could be smarter about calculating minimal area, but why bother?
return r;
You’ll need to adapt or generalize this code to deal with different grid cell sizes and grid origins. Note that grids are typically part of the GoView, not part of the GoDocument, so there isn’t any one grid that you can access from your subgraph to find the grid cell size and origin.

Thank you so much This works perfect!