This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
What's the easiest way to implement node snapping? Consider we have an imaginary (or visible, either way) - and we want to make sure the nodes snap to a certain position in the grid. Thanx, Gilad |
|
|
Hi Gilad, The DiagramSurface includes a basic way to snap nodes to a grid. This can be enabled by setting these properties on DiagramSurface:
The numbers represent the number of pixels between snap lines at 100% zoom. These lines can also be displayed by setting the SnapLineDrawing property. Here's an example of a resource that you can set to the SnapLineDrawing property:
Custom snap logic beyond a grid - such as snapping to the alignment of nearby nodes - will need to be implement at your end. One way of doing this could be to listen to node movements using the following code (where ds is a DiagramSurface instance):
And the handler:
In the handler, you'd have custom logic to determine any snap lines for the moving node. Then you can set the Bounds property of the node to snap it if needed. Visualizing such custom snap lines could be difficult - probably best to be done using a control that overlays the DiagramSurface. Hope that helps, let me know if you have further questions. -Jason Fauchelle |
|
|
This looks great and works perfectly, I do have a more tricky requirement though. I should have snap lines as show in the image below. There are basically boxes in which the user can drop a node, but the boxes are are designed such that there would be a minimal distance between each node (see image). What would be the best approach to implement something like that? Gilad |
|
|
Here's another image that show how a node should be positioned in that grid. |
|
|
Hi Gilad, Since this requirement is a grid that follows some kind of pattern independent of the nodes, you'll be able to implement this snapping grid in your application. Create a class that implements the ISnapSpecifier interface. A single snap specifier deals with a single dimension - either horizontal or vertical. This has two methods to implement. GetSnappedValue takes in a value, and returns the closest snap value. GetGuidelines take a logical range, and returns an enumeration of snap values - this is to generate the visual. Once you've implemented this, you can create a couple of instances, and set them to the HorizontalSnapSpecifier and VerticalSnapSpecifier properties of the DiagramSurface. Let me know if you have further questions about this. -Jason Fauchelle |
|
|
Thank you so much! I'll give it a try. |
|