Zoom to selected parts

I am showing number of parts using a tree based diagram. Along with keyboard based zooming options I need to provide zooming from menu. My requirement is that when user clicks on menu for zoom, it should zoom to those parts that are selected by user. User may have selected one or more parts. User may select parts side by side or he may select parts from different areas of diagram. So based on the parts that user have selected, Panel.Scale can have different values. Panel.SetScaleAndPosition() could work for me, but I am not sure what should be the scale value. Could you please help out me with this?

myDiagram.Panel.ComputeBounds(myDiagram.SelectedParts) will return a Rect describing the area occupied by the selection.

And you already know about setting the Diagram.Panel.Scale and .Position to change the viewport.

if (myDiagram.SelectedParts.Count > 0) { var panel = myDiagram.Panel; var rect = panel.ComputeBounds(myDiagram.SelectedParts); rect.Inflate(10, 10); // leave a margin, and make sure non-zero width and height panel.Scale = Math.Min(panel.ActualWidth/rect.Width, panel.ActualHeight/rect.Height); panel.Position = new Point(rect.X, rect.Y); }

Thanks a lot Walter. It worked for me! This is what exactly I was looking for!

Hi,

I have a similar requirement what manojattal has but with slight change. I have a Zoom button, I click on it and I drag over some nodes in the diagram. and the selected nodes are getting scaled. Next time I want to zoom I have to click on the zoom button again which enables the rubber band zoom. I am using the above code snippet for my requirement and I have put the code in SelectionChanged event of diagram. The issue that I am facing is myDiagram.SelectedParts doesn’t have all the selected Parts as its called multiple times depending on number of nodes selected because of which the scaling is incorrect.

I just want to put the above code into an event which is triggered after all the nodes are selected.

I tried to override DoStop() of DragSelectingTool class but in this the selectedParts are zero.

Can u suggest any event that I use ?

I’m sorry, but I do not understand the situation that you have and that you want to have. Are you talking about a rubber-band zoom operation or a rubber-band selection operation? Or something else?

Maybe you should be overriding DragZoomingTool.ZoomToRect or DragSelectingTool.SelectInRect.

I understand the question was confusing. let me try to rephrase it,

In the diagram I have few nodes selected with rubber band selection operation. Now I click on a zoom button(which is in my application). Then I see that the selected nodes are getting zoomed such that all the nodes are visible in the diagram area. which is as expected.

I have used the above code snipped for accomplishing this.

Now the concern is the scaling is depending on the the way the nodes are placed and once its scaled the position is given as rect.X and rect.Y. Most of the cases this works fine. But if I place the nodes in some specific way and try to zoom the nodes are getting zoomed but the alignment is not consistant. Sometimes its aligned to right and some times its aligned left.

I want them to be aligned to center of the diagram view. Is it possible?

Hope I didn’t confuse this time.

Thanks

Maybe something like:

  myDiagram.Panel.CenterRect(myDiagram.Panel.ComputeBounds(myDiagram.SelectedParts));