ArcObjects Library Reference  

SunPositionTool

About the Set sun position tool Sample

[C#]

SunPositionTool.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.Analyst3D;

namespace SetSunPosition
{
    public class SunPositionTool : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        #region Member Variables

        private IGlobe m_globe = null;
        private IGlobeCamera m_globeCamera = null;
        private IGlobeViewUtil m_globeViewUtil = null;
        private IGlobeDisplay3 m_globeDisplay = null;
        private IGlobeDisplayRendering m_globeDisplayRendering = null;
        private ISceneViewer m_sceneViewer = null;
        private bool m_bDrawPoint = false;

        #endregion

        public SunPositionTool()
        {
            //get the different members
            m_globe = ArcGlobe.Globe;
            m_globeDisplay = m_globe.GlobeDisplay as IGlobeDisplay3;
            m_globeDisplayRendering = m_globeDisplay as IGlobeDisplayRendering;
            m_globeCamera = m_globeDisplay.ActiveViewer.Camera as IGlobeCamera;
            m_globeViewUtil = m_globeCamera as IGlobeViewUtil;
            m_sceneViewer = m_globeDisplay.ActiveViewer;
        }

        #region Tool overrides

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

        protected override void OnActivate()
        {
            //Enable the light source
            m_globeDisplayRendering.IsSunEnabled = true;
            //set an ambient light
            m_globeDisplayRendering.AmbientLight = 0.1f;
            m_globeDisplayRendering.SunContrast = 30;
        }

        protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            m_bDrawPoint = true;

            //move the light-source according to the mouse coordinate
            double lat, lon, alt;
            lat = lon = alt = 0;

            m_globeViewUtil.WindowToGeographic(m_globeDisplay, m_sceneViewer, arg.X, arg.Y, false, out lon, out lat, out alt);

            m_globeDisplayRendering.SetSunPosition(lat, lon);

            //Refresh the display so that the AfterDraw will get called
            m_sceneViewer.Redraw(false);
        }

        protected override void OnMouseMove(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            
        }

        protected override void OnMouseUp(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            m_bDrawPoint = false;
        }

        protected override bool OnDeactivate()
        {
            //disable the light source
            m_globeDisplayRendering.IsSunEnabled = false;
            return base.OnDeactivate();
        }

        #endregion

    }

}

[Visual Basic .NET]

SunPositionTool.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.GlobeCore
Imports ESRI.ArcGIS.Analyst3D

Namespace SetSunPosition
	Public Class SunPositionTool
		Inherits ESRI.ArcGIS.Desktop.AddIns.Tool
		#Region "Member Variables"

		Private m_globe As IGlobe = Nothing
		Private m_globeCamera As IGlobeCamera = Nothing
		Private m_globeViewUtil As IGlobeViewUtil = Nothing
		Private m_globeDisplay As IGlobeDisplay3 = Nothing
		Private m_globeDisplayRendering As IGlobeDisplayRendering = Nothing
		Private m_sceneViewer As ISceneViewer = Nothing
		Private m_bDrawPoint As Boolean = False

		#End Region

		Public Sub New()
			'get the different members
			m_globe = ArcGlobe.Globe
			m_globeDisplay = TryCast(m_globe.GlobeDisplay, IGlobeDisplay3)
			m_globeDisplayRendering = TryCast(m_globeDisplay, IGlobeDisplayRendering)
			m_globeCamera = TryCast(m_globeDisplay.ActiveViewer.Camera, IGlobeCamera)
			m_globeViewUtil = TryCast(m_globeCamera, IGlobeViewUtil)
			m_sceneViewer = m_globeDisplay.ActiveViewer
		End Sub

		#Region "Tool overrides"

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

		Protected Overrides Sub OnActivate()
			'Enable the light source
			m_globeDisplayRendering.IsSunEnabled = True
			'set an ambient light
			m_globeDisplayRendering.AmbientLight = 0.1f
			m_globeDisplayRendering.SunContrast = 30
		End Sub

		Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
			m_bDrawPoint = True

			'move the light-source according to the mouse coordinate
			Dim lat, lon, alt As Double
			alt = 0
			lon = alt
			lat = lon

			m_globeViewUtil.WindowToGeographic(m_globeDisplay, m_sceneViewer, arg.X, arg.Y, False, lon, lat, alt)

			m_globeDisplayRendering.SetSunPosition(lat, lon)

			'Refresh the display so that the AfterDraw will get called
			m_sceneViewer.Redraw(False)
		End Sub

		Protected Overrides Sub OnMouseMove(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)

		End Sub

		Protected Overrides Sub OnMouseUp(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
			m_bDrawPoint = False
		End Sub

		Protected Overrides Function OnDeactivate() As Boolean
			'disable the light source
			m_globeDisplayRendering.IsSunEnabled = False
			Return MyBase.OnDeactivate()
		End Function

		#End Region

	End Class

End Namespace