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

Rotated shape doesn't snap to grid

2 Answers 243 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
toschkin
Top achievements
Rank 1
toschkin asked on 31 Oct 2018, 11:27 AM

Hi!

For example I took an DockingIntegration_WPF project from examples provided by Telerik. I set up grid snapping and it has worked fine, until I have rotated the shape on 90 (or 270) degrees.
After rotation, the shape does not snap to grid  neither when I drag it over the diagram nor I drop it on the diagram.
But when I rotate the shape on 180 degrees , it snaps to grid well.
On attached picture you can see this bug.
Is it possible to fix this issue?

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 02 Nov 2018, 02:12 PM
Hi toschkin,

The behavior you've described is expected when rotating the shapes inside the RadDiagram control.

Please let me further clarify.

I want to start with the insight that the RadDiagram control mimics most the behavior of the MS Visio software. The same result can be observed when using this product as well.

The rotation mechanism of the RadDiagram is based on the native Canvas drawing functionality. When you rotate a RadDiagramContainerShape or RadDiagramShape the shape's rotation point is the shape's center by default.

When rotating a shape, the actual position of the shape is not changed, only its RotationAngle. You can observe the same behavior if you place a Rectangle in a native Canvas and rotate it. Then you can check its position by calling Canvas.GetTop(this.rect) and Canvas.GetLeft(this.rect). Thus, when snapping the shapes, they are actually snapped by their original X and Y coordinates which differ from the top left corner of the rotated shape.

What I can suggest in this case is for you to create a custom SnappingService and override the SnapPoint method in a similar fashion.

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
        xDiagram.ServiceLocator.Register<ISnappingService>(new CustomSnappingService(this.xDiagram));
    }
}
 
public class CustomSnappingService : SnappingService
{
    public CustomSnappingService(IGraphInternal graph) : base(graph)
    {
    }
 
    public override Point SnapItems(IEnumerable<IDiagramItem> selectedItems, Point newPosition)
    {
        return base.SnapItems(selectedItems, newPosition);
    }
 
    public override Point SnapPoint(Point point)
    {
        var diagram = this.Graph as RadDiagram;
        var basePoint = base.SnapPoint(point);
        var shape = diagram.SelectedItem as RadDiagramShape;
        if (shape != null && shape.RotationAngle != 0)
        {
            var delta = (shape.ActualBounds.Width / 2) - (shape.ActualBounds.Height / 2);
            basePoint.X += delta;
            basePoint.Y -= delta;
        }
 
        return basePoint;
    }
}

Please note that I've used a simple algorithm to modify the final snap point, but you can further extend this to suit your requirements.

I do hope you find this information helpful.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
toschkin
Top achievements
Rank 1
answered on 06 Nov 2018, 12:50 PM
Thank you very much! That's exactly what I need.
Tags
Diagram
Asked by
toschkin
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
toschkin
Top achievements
Rank 1
Share this question
or