ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / MapFrame Class / SetAutoCamera Method
The auto camera to set
Example

In This Topic
    SetAutoCamera Method
    In This Topic
    Sets the auto camera for the map frame display constraints. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Sub SetAutoCamera( _
       ByVal autoCamera As CIMAutoCamera _
    ) 
    public void SetAutoCamera( 
       CIMAutoCamera autoCamera
    )

    Parameters

    autoCamera
    The auto camera to set
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Empty map frame
    Map frame contains a scene
    Map frame is linked to a map series
    Invalid auto camera
    Remarks
    Use the auto camera to set map frame display constraints. Display constraints are used to set limits on the map extent within the map frame. Refer to Map frame constraints in the Pro product help for more information on constraints. Attempting to set a CIMAutoCamera will fail with an exception in the following situations:
    o The map frame is empty or does not contain a 2D map.
    o The map frame is currently linked to a layout map series. Refer to IsMapSeriesMapFrame.
    o The autoCamera is not valid. Use ValidateAutoCamera.
    Example
    SetAutoCameraNone
    {
      //Note: Must be on QueuedTask.Run
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.None;
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraFixedExtent
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.Fixed;
      autoCamera.AutoCameraType = AutoCameraType.Extent;
    
      var mf_extent = mapFrame.GetViewExtent();
    
      var extent = EnvelopeBuilderEx.CreateEnvelope(
        400748.62, 800296.4, 1310669.05, 1424520.74, mapFrame.Map.SpatialReference);
      autoCamera.Extent = extent;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraFixedCenter
    {
      //Note: run within the QueuedTask
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.Fixed;
      autoCamera.AutoCameraType = AutoCameraType.Center;
    
      var camera = mapFrame.GetMapView(LayoutView.Active).Camera;
      var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
      var camera2 = new CIMViewCamera()
      {
        Heading = 0,
        Pitch = -90,
        Roll = 0,
        Scale = 21169571,
        X = 855708,
        Y = 1112409,
        Z = double.NaN
      };
      autoCamera.Camera = camera2;
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraFixedScale
    {
      //Note: run within the QueuedTask
    
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.Fixed;
      autoCamera.AutoCameraType = AutoCameraType.Scale;
    
      var camera = mapFrame.GetMapView(LayoutView.Active).Camera;
      var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
      var camera2 = new CIMViewCamera()
      {
        Heading = 0,
        Pitch = -90,
        Roll = 0,
        Scale = 20000571,
        X = 1310669.0 + ((400748.5 - 1310669.0) / 2.0),
        Y = 800296.4 + ((1424520.74 - 800296.4) / 2.0),
        Z = double.NaN
      };
      autoCamera.Camera = camera2;
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      //autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    
    }
    SetAutoCameraLinkedExtent
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.Extent;
      autoCamera.MapFrameLinkName = "mapFrameLink";
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraLinkedCenter
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.Center;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraLinkedCenterAndScale
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.CenterAndScale;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraLinkedScale
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapFrameLink;
      autoCamera.AutoCameraType = AutoCameraType.Scale;
      autoCamera.MapFrameLinkName = "mapFrameLink";
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraLinkedMapSeriesShape
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapSeriesLink;
      autoCamera.AutoCameraType = AutoCameraType.Extent;
      //autoCamera.MapFrameLinkName = mapFrameLink;
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraLinkedMapSeriesCenter
    {
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.MapSeriesLink;
      autoCamera.AutoCameraType = AutoCameraType.Center;
    
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      autoCamera.IntersectLayerPath = states.URI;
    
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    SetAutoCameraFixedCenterAndScale
    {
      //Note: run within the QueuedTask
      var autoCamera = mapFrame.GetAutoCamera();
      autoCamera.Source = AutoCameraSource.Fixed;
      autoCamera.AutoCameraType = AutoCameraType.CenterAndScale;
    
      var camera = mapFrame.GetMapView(LayoutView.Active).Camera;
      var center = mapFrame.GetViewCenter();
    
      //var extent = EnvelopeBuilderEx.CreateEnvelope(
      //	400748.62, 800296.4, 1310669.05, 1424520.74, mf.Map.SpatialReference);
      //autoCamera.Extent = extent;
    
      var camera2 = new CIMViewCamera()
      {
        Heading = 0,
        Pitch = -90,
        Roll = 0,
        Scale = 21169571,
        X = 1310669.0 + ((400748.5 - 1310669.0) / 2.0),
        Y = 800296.4 + ((1424520.74 - 800296.4) / 2.0),
        Z = double.NaN
      };
      autoCamera.Camera = camera2;
      var states = mapFrame.Map.GetLayersAsFlattenedList().First(l => l.Name == "State_Polygons");
      //autoCamera.IntersectLayerPath = states.URI;
      if (mapFrame.ValidateAutoCamera(autoCamera) &&
        !mapFrame.IsMapSeriesMapFrame())
        mapFrame.SetAutoCamera(autoCamera);
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also