In This Topic
In NOV Diagram you can use the HitTest methods of the drawing page to detect which shapes are hit (overlapped) by a given rectangle or a point.
Hit testing in NOV Diagram works with shape geometry, not with shape bounds. If a shape or group doesn't have a geometry, it won't be subject to hit testing and will never be considered as hit.
Get all shapes in a rectangle or under a point
To get all shapes in a given rectangle area of the drawing page, use the following piece of code:
Get all shapes in a rectangle |
Copy Code
|
// Hit test the page
NList<NDiagramItem> hitItems = drawingView.ActivePage.HitTestItems(rectangle);
|
Similarly you can pass a point instead of a rectangle to the HitTestItems method to get all shapes under a given point (in page coordinates).
Get top most shape in a rectangle or under a point
To quickly check if a given rectangle or point hits any shape on the drawing page, you can use the GetTopMostHitItem method of the page:
Check if a rectangle contains any shapes |
Copy Code
|
// Hit test the page
NDiagramItem hitItem = drawingView.ActivePage.GetTopMostHitItem(rectangle);
if (hitItem != null)
{
// Item hit
}
else
{
// Item not hit
}
|
The method returns the top-most item hit by the passed rectangle or point or null if no item was hit.
Get all shapes overlapped by a given shape
To get all diagram items overlapped by a specific shape, use the following piece of code:
Get all shapes overlapped by a given shape |
Copy Code
|
// Get the shape bounds in page coordinates
NRectangle rect = shape.GetAlignBoxInPage();
// Hit test the page
NList<NDiagramItem> hitItems = drawingView.ActivePage.HitTestItems(rect);
// Remove the shape we are hit testing with from the list
hitShapes.Remove(shape);
|
To quickly check if any shape was hit, use the GetTopMostHitItem method instead of the HitTestItems method:
Check if a shape overlaps any shapes |
Copy Code
|
// Get the shape bounds in page coordinates
NRectangle rect = shape.GetAlignBoxInPage();
// Hit test the page
NDiagramItem hitItem = drawingView.ActivePage.GetTopMostHitItem(rect);
if (hitItem != shape)
{
// The shape overlaps hitItem
}
else
{
// The shape doesn't overlap any shapes
}
|
Get all 2D shapes overlapped by a given shape
You can also use filters. For example, to get all 2D shapes overlapped by a given shape, use the following code:
|
Copy Code
|
// Get the shape bounds in page coordinates
NRectangle rect = shape.GetAlignBoxInPage();
// Hit test the page
NList<NDiagramItem> hitItems = drawingView.ActivePage.HitTestItems(rect, NDiagramFilters.ShapeType2D);
// Remove the shape we are hit testing with from the list
hitShapes.Remove(shape);
|
To quickly check if any shape was hit, use the GetTopMostHitItem method instead of the HitTestItems method:
Check if a shape overlaps any shapes |
Copy Code
|
// Get the shape bounds in page coordinates
NRectangle rect = shape.GetAlignBoxInPage();
// Hit test the page
NDiagramItem hitItem = drawingView.ActivePage.GetTopMostHitItem(rect, NDiagramFilters.ShapeType2D);
if (hitItem != shape)
{
// The shape overlaps hitItem
}
else
{
// The shape doesn't overlap any shapes
}
|