Snap to grid in center of an icon

I have a goView where I draw simplenodes with snapping to grid enabled. I want the simplenodes to snap to the grid in the center of the icon instead of the top-left corner.
I have overridden the Location property of the simplenode to achive this when I create the node and add it to the view:
public override PointF Location
{
get
{
return base.Location;
}
set
{
base.Location = value;
SetSpotLocation(Middle,value);
}
}
But when I move the nodes afterwards, the spotlocation seem to have moved back to the top-left corner, because nodes now snap to the grid in the top-left corner.
How do I keep the spotlocation in the center of the icons of the simplenodes when I move them after they have been added to the goView ?
Regards
Stig Nielsson

That’s very odd, because the behavior I believe you want is actually the default behavior. I just tried running Demo1 to make sure–I used the PropertyGrid to change the view’s values for GridStyle and GridSnapDrag.
So if you are using GoSimpleNodes, the centers of the icons should snap to the grid points, without having to override any methods.
If you do want to change the “Location” of an object, you need to do so consistently between the getter and the setter:
public override PointF Location {
get { return this.Icon.Center; }
set { this.Icon.Center = value; }
}
This is actually what GoSimpleNode implements, except that the setter is implemented more efficiently, to avoid unnecessary calls to LayoutChildren:
set {
SizeF off = GoTool.SubtractPoints(this.SelectionObject.Center, this.Position);
this.Position = new PointF(value.X - off.Width, value.Y - off.Height);
}
(The SelectionObject is the same as the GoSimpleNode.Icon)

Yes, you are right. I must have had something cluttering up my code and understanding of what was happening. Thanks for the explanation and sorry for wasting your time.
Regards
Stig Nielsson

Walter,

Is there any way I can get Nodes to automatically snap to grid after the document has been loaded and all of the nodes initialized? I have been having a lot of trouble getting my nodes to position themselves correctly but I figured that if I could at least force them all to neatly snap to grid once they've all been created then regardless of how incorrectly I managed to screw up the original layout algorithm you guys wrote I could at least get everything to look somewhat normal by having them snap to grid automatically using each node's center port.
Thanks in advance.

[code]GoSelection sel = new GoSelection(null);

sel.AddRange(goView1.Document);
goView1.MoveSelection(sel, new SizeF(0, 1), true);[/code]
This assumes that your GoView has a Grid whose GridSnapStyle has been set.

Nothing seems to happen when I implement this code.

And goView1.GridSnapStyle isn’t None, right?

So interactive dragging does snap?
And the programmatically created nodes are indeed not snapped correctly?

Walter,

I have tried everything under the sun to get my tree working correctly.
You replied to my last post and mentioned something about creating a generation_id column in my node table to keep track of what level in the tree each node was at. I have done this although I have not really implemented any logic to perform the layout in the family tree b/c it seems to require a lot more work than I think either of us first anticipated. It becomes real "hairy" when dealing with multiple spouses and then trying to show the geneology of a spouses family tree once they have married into a family.
I suppose that if I could just get 3 major issues taken care of then everything could pretty much work itself out.
1. Have the nodes snap to grid automatically upon loading of the tree.
2. Have the nodes re-snap to grid and re-center the tree after each new node is added to the tree.
3. Be able to add grandparents, great-grandparents etc. to a tree and not have the tree get all screwed up.
I have tried implementing some of the code snippets you gave me and I am running into a few problems. It seems that when I try to implement the "auto snap to grid" code it causes the whole tree to shift to the top left corner of the document leaving about 1/10 of the nodes at the very top of the tree "cut off" and forcing the user to have to scroll to the top each time. Another unwanted side-effect of the "auto snap to grid" is that instead of having all the nodes snap to the nearest point the entire tree just jumps to the top left corner which obviously doesn't look right to the user. This also happens when a user adds new nodes. If I right-click on a node and select "add brother", it adds the link and node and then the whole tree snaps to grid at the top left corner of the screen instead of the individual nodes snapping to the closest gridpoint.
I also save the X,Y coordinates for each node. This gives the user the ability to override the auto placement of the nodes if they so choose. Ideally I would like to have the auto snap to grid only affect the newly added node and not reposition the whole tree. Any chance you could show me how to do this?
The code below doesn't do the job. It just repositions EVERYTHING.
GoSelection sel = new GoSelection(null);
sel.AddRange(myView.Document);
myView.MoveSelection(sel, new SizeF(0, 1), true);
Thanks!

What’s the value of your GoView.GridSnapStyle?

Do you need to use a different value for GoView.GridOrigin, so that nodes at the top or the left side don't get positioned with their centers on the grid points straddling the zero coordinate? Or is that OK, and you just need to resize the document and rescroll the view?
I just tried this, and everything behaves the way I expected: objects whose location was not aligned to the grid were moved. It was not the case that everything got moved to (0,0) or something like that.

I don’t have a property called GridSnapStyle, just GridStyle and GridSnapDrag.

Oops, my typo – I meant: GoView.GridSnapDrag and GoView.GridOrigin. And GoView.GridStyle and .GridCellSize too, I suppose.

I have GridSnapDrag and GridStyle set. Do I need to also set GridCellSize and GridOrigin? If so, what do you recommend I set the values to?

You still haven't told me what grid-related property values you are using. Also, have you tried setting them to different values?
If GridCellSize is huge relative to the size of the nodes, I could see why a lot of nodes would be shifted to (0,0), because that would be closest grid point.
The nodes cannot move more than half a grid cell width/height. I would have assumed that you had already set the GridCellSize to something like 10x10.
You can try playing with the GridOrigin to make sure there's some room at the top and at the left sides.
Also, you can do something like:
goView1.Document.Bounds = goView1.ComputeDocumentBounds();
goView1.RescaleToFit();
to make sure everything is visible (within reason).