GoLabeledLink MidLabel align to grid

I'm using the following code to align the center of the MidLabel of a GoLabeledLink to the grid:
protected override void PositionMidLabel(GoObject lab, PointF a, PointF b)
{
base.PositionMidLabel(lab, a, b);

if (View == null)
return;

PointF alignedCenter = View.FindNearestGridPoint(lab.Center);
lab.SetSpotLocation(Middle, alignedCenter);
}
It turns out that the View of the link is null when it matters, so the alignment cannot be performed this way.
The objects are in a LinksLayer that indeed is only added to the document (this code is from Demo1).
How do I go about aligning these labels to the grid ?
(The MidLabel actually is a GoPort subclass in my case).

No GoObject, nor any GoDocument, should have a reference to any GoView.

I suggest you create your own GoGrid and initialize it the way you want (you could copy the GoView.Grid, perhaps), create a background layer in your GoDocument, and add it to that new layer. You'll want that new background layer to have AllowSelect set to false.
That way your document objects will always have access to the grid.
It's probably pointless to call the base.PositionMidLabel method when you are able to find the grid and call a method on it. But it's a good idea if you're not able to find the grid.

Is overriding base.PositionMidLabel pointless because it would have aligned to the grid on its own if it had access to it ?

Also, are you implying that the GoObject.View property is not for regular use ?
I have a grid in the GraphView:GoView.
Should it be added to the document instead ?
Or could the LinksLayer be added to the View ?

The override is necessary; I was just saying that calling the base method isn’t useful when you’re about to reposition the label anyway. But it doesn’t do any harm.

No, it's just that if you add a reference to a GoView or GoSelection or anything like that in a GoObject or in a GoDocument, it's not going to be serializable, so you won't be able to do a copy-and-paste of the object or of the document. I suppose you could attribute it [NonSerialized] to get around that problem, but it's still against the philosophy of having the document and its objects not be dependent on any views.

Well, the idea of the override was to let the base method figure out the approximate position based on the stroke, and then just ‘nudge’ it onto the grid.

Could you explain how to make the grid available to all objects ?
Does it mean I have to move the existing grid into the document instead of the view ?
Or would it be possible to add the LinksLayer to the view ?

I see what you mean, but you can just use the point that is midway between A and B, the argument points. Well, no matter, either method should be OK.

The grid, and thus the new background layer, needs to be part of the GoDocument. You don't want to put "meaningful" things into the GoView's layers.
GoLayer back = goView1.Document.Layers.CreateLayerBefore(null);
back.AllowSelect = false;
GoGrid grid = goView1.Grid;
goView1.Grid = null; // remove from view
back.Add(grid); // add to document
If you do this, you won't be able to refer to any GoView.Grid... properties.
I have a similar issue to Willem. When I drop my GoGeneralNode onto the document from a TreeView, it is not aligned to the grid. It first must be manually Moved. I tried to correct the problem by overriding the OnLayerChanged method, but the view (and thus the grid) was not available.
protected override void OnLayerChanged(GoLayer oldlayer, GoLayer newlayer, GoObject mainObj)
{
base.OnLayerChanged(oldlayer, newlayer, mainObj);
if (newlayer.View != null) {
PointF snapPoint = newlayer.View.FindNearestGridPoint(Location);
Location = snapPoint;
}
UpdateInputTextPositions();
}

When something is being dropped from a different kind of Control (i.e. not a GoView such as a GoPalette), GoDiagram doesn’t know anything about what you want to create and add to the GoView.Document. That’s why you need to override GoView.DoExternalDrop (and perhaps other methods, such as GoView.GetExternalDragImage) when handling a drop from an unknown kind of Control.

I assume you do have an override of GoView.DoExternalDrop, where you look at the drag-and-drop data and figure out what to add to the GoDocument. It's at that time that you need to align to the grid, if that's what you want to do. Obviously since you are overriding a GoView method, you will have access to the GoView and its GoView.Grid.

Yes, I am using the GoView.DoExternalDrop and aligning the new GoObject to the grid there works. Thanks!