This is a migrated thread and some comments may be shown as answers.

SnappingEngine and Bounds.Rect.IntersectWith

4 Answers 105 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Dario Concilio
Top achievements
Rank 2
Dario Concilio asked on 29 May 2017, 03:29 PM

Hi to all,

I would avoid ovelap between RadDiagramShapes (all rectangle items).

When I set IsSnapToGridEnabled="False" IsSnapToItemsEnabled="true" and move RadDiagramShapes, SnappingEngine uses snap on myshape aligned to other ractangle.

If I try to user MyMovedShaped.Bounds.Rect.IntersectWith(OtherRectangleShape) returns me ever True.

I'm using this code:

01.private void topRadDiagram_Drag(object sender, DragRoutedEventArgs e)
02.{
03.    var diagram = (RadDiagram)sender;
04. 
05.    foreach (var item in e.Items)
06.    {
07.        var currentItem = (RadDiagramShape)item;
08. 
09.        var shapeItem = (GruppoPianoDiCarico)currentItem.Tag;
10. 
11.        foreach (RadDiagramShape otherItems in diagram.Items)
12.        {
13.            if (otherItems.Tag.GetType() == typeof(GruppoPianoDiCarico))
14.            {
15.                var shapeOther = (GruppoPianoDiCarico)otherItems.Tag;
16. 
17.                if (shapeItem != shapeOther)
18.                {
19.                    if (currentItem.Bounds.IntersectsWith(otherItems.Bounds))
20.                    {
21.                        //Da ripristinare le coordinate originarie
22.                        currentItem.Position = new Point(currentItem.Position.X - (e.MousePosition.X - e.StartMousePosition.X),
23.                            currentItem.Position.Y - (e.MousePosition.Y - e.StartMousePosition.Y));
24.                    }
25.                }
26.            }
27.        }
28.    }
29.}

 

4 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 01 Jun 2017, 09:07 AM
Hello Dario,

First, keep in mind that the Drag event fires when you release the mouse. If you need event while you still move the mouse before mouse up, you can use DraggingService.Dragging event:

DraggingService dragService = this.diagram.ServiceLocator.GetService<IDraggingService>() as DraggingService;
           if (dragService != null)
           {
               dragService.Dragging += DragService_Dragging;
           }

In your scenario Snap to Grid is disabled but snap to items is enabled. By default SnapX and SnapY properties of RadDiagram are set to 20. This means that if you move one shape closer to other shape (closer than 20 pixels), the snapping engine will move the dragged shape 20 pixels and distance between them will be 0, Bounds will be immediately updated and IntersectsWith will return true (again I am not sure in which moment you test).  I guess in your scenario you move shapes that are closer than 20 px. If this is the case - limit the SnapX, SnapY to 10, 5.

I hope this helps you move forward.


Regards,
Petar Mladenov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Dario Concilio
Top achievements
Rank 2
answered on 01 Jun 2017, 09:34 AM

I see, but....

The sequence of actions is this:

  • I have two shapes (RectangleShape) (ShapeBefore.PNG)
  • I try to move first one near other one (ShapeTryToMove.PNG)
  • When I release button mouse, my code returns original positions (by Drag event) with di formula (ShapeAfterReleaseMouse.PNG)
  • If I change SmapX to 10 and SnapX to 5, it takes not effects
01.private void topRadDiagram_Drag(object sender, DragRoutedEventArgs e)
02.{
03.    var diagram = (RadDiagram)sender;
04. 
05.    foreach (var item in e.Items)
06.    {
07.        var currentItem = (RadDiagramShape)item;
08. 
09.        var shapeItem = (GruppoPianoDiCarico)currentItem.Tag;
10. 
11.        foreach (RadDiagramShape otherItems in diagram.Items)
12.        {
13.            if (otherItems.Tag.GetType() == typeof(GruppoPianoDiCarico))
14.            {
15.                var shapeOther = (GruppoPianoDiCarico)otherItems.Tag;
16. 
17.                if (shapeItem != shapeOther)
18.                    if (currentItem.Bounds.IntersectsWith(otherItems.Bounds))
19.                        currentItem.Position = new Point(currentItem.Position.X - (e.MousePosition.X - e.StartMousePosition.X),
20.                            currentItem.Position.Y - (e.MousePosition.Y - e.StartMousePosition.Y));
21.            }
22.        }
23.    }
24.}
0
Dario Concilio
Top achievements
Rank 2
answered on 01 Jun 2017, 09:41 AM

Ok, I added a "Tollerance" of 1 px, do you like it? :-)

1.[....]
2.if (currentItem.Bounds.IntersectsWith(GetRectWithTollerance(otherItems.Bounds)))
3.[....]
4. 
5.private Rect GetRectWithTollerance(Rect pBounds)
6.{
7.    return new Rect(pBounds.X + 1, pBounds.Y + 1, pBounds.Width - 2, pBounds.Height - 2);
8.}
0
Petar Mladenov
Telerik team
answered on 02 Jun 2017, 12:26 PM
Hi Dario,

Yes tolerance trick seems a good option. Let us know if you need further assistance on this topic.

Regards,
Petar Mladenov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
Diagram
Asked by
Dario Concilio
Top achievements
Rank 2
Answers by
Petar Mladenov
Telerik team
Dario Concilio
Top achievements
Rank 2
Share this question
or