ArcObjects Library Reference  

MultipleGlobeViewers

About the Multiple globe viewers Sample

[C#]

MultipleGlobeViewers.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.Geometry;


namespace MultipleGlobeViewers
{
    public class MultipleGlobeViewers : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        #region Member Variables

        private ESRI.ArcGIS.GlobeCore.IGlobe globe;
        private ESRI.ArcGIS.GlobeCore.IGlobeDisplay globeDisplay;
        private ESRI.ArcGIS.GlobeCore.IGlobeCamera globeCamera;
        private ESRI.ArcGIS.GlobeCore.IGlobeDisplayEvents_Event globeDispEvent;
        private SecondaryViewerForm theForm;
        private ESRI.ArcGIS.Analyst3D.ISceneViewer viewer;
        private ESRI.ArcGIS.GlobeCore.IGlobeCamera viewerGlobeCamera;
        private bool topDownView = false;
        private string viewerCaption = "";

        #endregion

        #region DLLImportFunction

        [DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);

        [DllImport("user32.dll")]
        static extern int ShowWindow(int hwnd, int nCmdShow);


        #endregion

        public MultipleGlobeViewers()
        {
            globe = ArcGlobe.Globe;
            globeDisplay = globe.GlobeDisplay;
            globeCamera = (ESRI.ArcGIS.GlobeCore.IGlobeCamera)globeDisplay.ActiveViewer.Camera;
            globeDispEvent = (ESRI.ArcGIS.GlobeCore.IGlobeDisplayEvents_Event)globeDisplay;
        }

        protected override void OnClick()
        {
            theForm = new SecondaryViewerForm();
            //register form's events
            theForm.topDownButton.Click += new EventHandler(topDownButton_Click);
            theForm.normalButton.Click += new EventHandler(normalButton_Click);
            theForm.FormClosing += new FormClosingEventHandler(theForm_FormClosing);
            //get viewer list
            getListOfSecondaryViewers();
            //register the ArcGlobe globe's display afterdraw event
            globeDispEvent.AfterDraw += new IGlobeDisplayEvents_AfterDrawEventHandler(globeDispEvent_AfterDraw);

            theForm.Show();
        }

        protected override void OnUpdate()
        {
            Enabled = ArcGlobe.Application != null;
        }

        #region Custom Methods

        void globeDispEvent_AfterDraw(ISceneViewer pViewer)
        {
            double obsLat;
            double obsLon;
            double obsAlt;
            double tarLat;
            double tarLon;
            double tarAlt;
            globeCamera.GetObserverLatLonAlt(out obsLat, out obsLon, out obsAlt);
            globeCamera.GetTargetLatLonAlt(out tarLat, out tarLon, out tarAlt);
            //set the observer and target of the secondary viewer to be the same as main viewer if top-down view = false
            if (topDownView == false || globeCamera.OrientationMode == esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal)
            {
                viewerGlobeCamera.OrientationMode = globeCamera.OrientationMode;
                viewerGlobeCamera.SetObserverLatLonAlt(obsLat, obsLon, obsAlt);
                viewerGlobeCamera.SetTargetLatLonAlt(tarLat, tarLon, tarAlt);
            }
            //set the observer top down view for the secondary viewer
            else if (topDownView == true && globeCamera.OrientationMode == esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal)
            {
                viewerGlobeCamera.OrientationMode = esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal;
                tarLat = obsLat + 0.0000001;
                tarLon = obsLon + 0.0000001;
                viewerGlobeCamera.SetTargetLatLonAlt(tarLat, tarLon, tarAlt);
            }
        }

        void getListOfSecondaryViewers()
        {
            ESRI.ArcGIS.esriSystem.IArray viewers = globeDisplay.GetAllViewers();
            if (viewers.Count < 2) return;
            for (int i = 0; i < viewers.Count; i++)
            {
                ESRI.ArcGIS.Analyst3D.ISceneViewer viewerElement = (ESRI.ArcGIS.Analyst3D.ISceneViewer)viewers.get_Element(i);
                if (viewerElement.Caption != "Globe view")
                {
                    theForm.viewerListBox.Items.Add(viewerElement.Caption);
                }
            }

        }

        void normalButton_Click(object sender, EventArgs e)
        {
            topDownView = false;
            viewerCaption = theForm.viewerListBox.SelectedItem.ToString();
            viewer = (ESRI.ArcGIS.Analyst3D.ISceneViewer)globeDisplay.FindViewer(viewerCaption);
            viewerGlobeCamera = (ESRI.ArcGIS.GlobeCore.IGlobeCamera)viewer.Camera;
        }

        void topDownButton_Click(object sender, EventArgs e)
        {
            topDownView = true;
            viewerCaption = theForm.viewerListBox.SelectedItem.ToString();
            viewer = (ESRI.ArcGIS.Analyst3D.ISceneViewer)globeDisplay.FindViewer(viewerCaption);
            viewerGlobeCamera = (ESRI.ArcGIS.GlobeCore.IGlobeCamera)viewer.Camera;
        }

        void theForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //unregister the ArcGlobe globe's display afterdraw event
            globeDispEvent.AfterDraw -= new IGlobeDisplayEvents_AfterDrawEventHandler(globeDispEvent_AfterDraw);
        }
        #endregion
    }
}

[Visual Basic .NET]

MultipleGlobeViewers.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.GlobeCore
Imports ESRI.ArcGIS.Analyst3D
Imports ESRI.ArcGIS.Geometry


Namespace MultipleGlobeViewers
	Public Class MultipleGlobeViewers
		Inherits ESRI.ArcGIS.Desktop.AddIns.Button
		#Region "Member Variables"

		Private globe As ESRI.ArcGIS.GlobeCore.IGlobe
		Private globeDisplay As ESRI.ArcGIS.GlobeCore.IGlobeDisplay
		Private globeCamera As ESRI.ArcGIS.GlobeCore.IGlobeCamera
		Private globeDispEvent As ESRI.ArcGIS.GlobeCore.IGlobeDisplayEvents_Event
		Private theForm As SecondaryViewerForm
		Private viewer As ESRI.ArcGIS.Analyst3D.ISceneViewer
		Private viewerGlobeCamera As ESRI.ArcGIS.GlobeCore.IGlobeCamera
		Private topDownView As Boolean = False
		Private viewerCaption As String = ""

		#End Region

		#Region "DLLImportFunction"

		<DllImport("gdi32.dll")> _
		Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
		End Function

		<DllImport("user32.dll")> _
		Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
		End Function


		#End Region

		Public Sub New()
			globe = ArcGlobe.Globe
			globeDisplay = globe.GlobeDisplay
			globeCamera = CType(globeDisplay.ActiveViewer.Camera, ESRI.ArcGIS.GlobeCore.IGlobeCamera)
			globeDispEvent = CType(globeDisplay, ESRI.ArcGIS.GlobeCore.IGlobeDisplayEvents_Event)
		End Sub

		Protected Overrides Sub OnClick()
			theForm = New SecondaryViewerForm()
			'register form's events
			AddHandler theForm.topDownButton.Click, AddressOf topDownButton_Click
			AddHandler theForm.normalButton.Click, AddressOf normalButton_Click
			AddHandler theForm.FormClosing, AddressOf theForm_FormClosing
			'get viewer list
			getListOfSecondaryViewers()
			'register the ArcGlobe globe's display afterdraw event
			AddHandler globeDispEvent.AfterDraw, AddressOf globeDispEvent_AfterDraw

			theForm.Show()
		End Sub

		Protected Overrides Sub OnUpdate()
			Enabled = ArcGlobe.Application IsNot Nothing
		End Sub

		#Region "Custom Methods"

		Private Sub globeDispEvent_AfterDraw(ByVal pViewer As ISceneViewer)
			Dim obsLat As Double
			Dim obsLon As Double
			Dim obsAlt As Double
			Dim tarLat As Double
			Dim tarLon As Double
			Dim tarAlt As Double
			globeCamera.GetObserverLatLonAlt(obsLat, obsLon, obsAlt)
			globeCamera.GetTargetLatLonAlt(tarLat, tarLon, tarAlt)
			'set the observer and target of the secondary viewer to be the same as main viewer if top-down view = false
			If topDownView = False OrElse globeCamera.OrientationMode = esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal Then
				viewerGlobeCamera.OrientationMode = globeCamera.OrientationMode
				viewerGlobeCamera.SetObserverLatLonAlt(obsLat, obsLon, obsAlt)
				viewerGlobeCamera.SetTargetLatLonAlt(tarLat, tarLon, tarAlt)
			'set the observer top down view for the secondary viewer
			ElseIf topDownView = True AndAlso globeCamera.OrientationMode = esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal Then
				viewerGlobeCamera.OrientationMode = esriGlobeCameraOrientationMode.esriGlobeCameraOrientationLocal
				tarLat = obsLat + 0.0000001
				tarLon = obsLon + 0.0000001
				viewerGlobeCamera.SetTargetLatLonAlt(tarLat, tarLon, tarAlt)
			End If
		End Sub

		Private Sub getListOfSecondaryViewers()
			Dim viewers As ESRI.ArcGIS.esriSystem.IArray = globeDisplay.GetAllViewers()
			If viewers.Count < 2 Then
				Return
			End If
			For i As Integer = 0 To viewers.Count - 1
				Dim viewerElement As ESRI.ArcGIS.Analyst3D.ISceneViewer = CType(viewers.Element(i), ESRI.ArcGIS.Analyst3D.ISceneViewer)
				If viewerElement.Caption <> "Globe view" Then
					theForm.viewerListBox.Items.Add(viewerElement.Caption)
				End If
			Next i

		End Sub

		Private Sub normalButton_Click(ByVal sender As Object, ByVal e As EventArgs)
			topDownView = False
			viewerCaption = theForm.viewerListBox.SelectedItem.ToString()
			viewer = CType(globeDisplay.FindViewer(viewerCaption), ESRI.ArcGIS.Analyst3D.ISceneViewer)
			viewerGlobeCamera = CType(viewer.Camera, ESRI.ArcGIS.GlobeCore.IGlobeCamera)
		End Sub

		Private Sub topDownButton_Click(ByVal sender As Object, ByVal e As EventArgs)
			topDownView = True
			viewerCaption = theForm.viewerListBox.SelectedItem.ToString()
			viewer = CType(globeDisplay.FindViewer(viewerCaption), ESRI.ArcGIS.Analyst3D.ISceneViewer)
			viewerGlobeCamera = CType(viewer.Camera, ESRI.ArcGIS.GlobeCore.IGlobeCamera)
		End Sub

		Private Sub theForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
			'unregister the ArcGlobe globe's display afterdraw event
			RemoveHandler globeDispEvent.AfterDraw, AddressOf globeDispEvent_AfterDraw
		End Sub
		#End Region
	End Class
End Namespace