2

I use ArcObjects to produce a map document.

I have a rotated rectangle that I want to be the exact extent of my data frame.

enter image description here

So, I want to rotate the data frame with the same orientation than the rectangle, then fill the map space with the rectangle.

What I want:

enter image description here

I tried this code:

IPageLayout pageLayout = (IPageLayout)mapDocument.getActiveView();
IMap dataFrame = mapDocument.getMap(0);

IPolygon polygon = ...

// data frame rotation
double dataFrameRotation = -44.46035122650748; // (the polygon orientation in negative)
IActiveView activeView = (IActiveView)dataFrame;
activeView.getScreenDisplay().getDisplayTransformation().setRotation(dataFrameRotation);

// setting the extent
IEnvelope zoomToExtent = polygon.getEnvelope();
activeView.setExtent(zoomToExtent);

The problem I get is that zoomToExtent means the polygon extent in the map coordinate system (xMin, xMax, yMin, yMax), so the data frame is not enough zoomed.

Any suggestion?

2
  • Is your blue box polygon a graphic or actually a polygon in a FeatureClass?
    – Hornbydd
    Nov 27 '14 at 18:02
  • It's a IPolygon object that I construct programmatically and I save in a feature class.
    – superrache
    Nov 28 '14 at 8:15
1

The following code zooms to the extent of the polygon in the map then fits the whole page in page layout. This code gets the polygon and rotates it back to the horizontal (approximately 35° in my example) and then zooms to the extent of that rather than the original polygon extent which is much larger if the rectangle is rotated.

Public Sub Zoom()
    Dim pMXDocument As IMxDocument
    Set pMXDocument = ThisDocument

    Dim pPageLayout As IPageLayout
    Set pPageLayout = pMXDocument.PageLayout

    Dim pMap As IMap
    Set pMap = pMXDocument.FocusMap

    Dim pActiveView As IActiveView
    Set pActiveView = pMap

    ' Get layer with single polygon
    Dim pLayer As ILayer
    Set pLayer = pMap.Layer(0)
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pLayer

    ' Get polygon and then its Envelope
    Dim pFeature As IFeature
    Set pFeature = pFeatureLayer.FeatureClass.GetFeature(1)
    Dim pPolygon As IPolygon
    Set pPolygon = pFeature.Shape

    ' Get centroid
    Dim pArea As IArea
    Set pArea = pPolygon
    Dim pPoint As IPoint
    Set pPoint = pArea.Centroid

    ' Rotate polygon back to the horizontal (note units are in radians)
    Dim pTransform As ITransform2D
    Set pTransform = pPolygon
    pTransform.Rotate pPoint, 90

    ' Set Extent of Map to polygon Extent then
    ' Zoom to PageLayout
    pActiveView.Extent = pPolygon.Envelope
    pPageLayout.ZoomToWhole
End Sub

Rotated rectangle with graphic showing extent:

Rotated rectangle with graphic showing extent

Map rotated 35° to back rectangle horizontal:

Map rotated 35° to back rectangle horizontal

Correct Page Layout:

Correct Page Layout

3
  • 1
    Thanks for your response. This method works well if there is no rotations of the map. If the rectangle is oriented, its extent will be greater, then the map will not be enough zoomed.
    – superrache
    Nov 28 '14 at 11:30
  • I have edited my answer with new code. I think the issue is that you don't want the envelope you want the rectangle rotated back to the horizontal.
    – Hornbydd
    Nov 28 '14 at 12:23
  • that's exactly what I need: to get the rectangle rotated back then zoomed in it. Thanks a lot.
    – superrache
    Dec 15 '14 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.