About the Custom scene navigation commands Sample
[C#]
ZoomIn.cs
using System; using System.Drawing; using ESRI.ArcGIS.Analyst3D; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.ADF.CATIDs; using System.Runtime.InteropServices; namespace sceneTools { [ClassInterface(ClassInterfaceType.None)] [Guid("6BA62F0E-5337-47fe-8B45-E5359B0D85D8")] public sealed class ZoomIn : BaseTool { [DllImport("user32")] public static extern int ReleaseCapture(int hwnd); [DllImport("user32")] public static extern int SetCapture(int hwnd); [DllImport("user32")] public static extern int GetCapture(int fuFlags); [DllImport("user32")] public static extern int GetWindowRect(int hwnd, ref Rectangle lpRect); #region COM Registration Function(s) [ComRegisterFunction()] [ComVisible(false)] static void RegisterFunction(Type registerType) { // Required for ArcGIS Component Category Registrar support ArcGISCategoryRegistration(registerType); // // TODO: Add any COM registration code here // } [ComUnregisterFunction()] [ComVisible(false)] static void UnregisterFunction(Type registerType) { // Required for ArcGIS Component Category Registrar support ArcGISCategoryUnregistration(registerType); // // TODO: Add any COM unregistration code here // } #region ArcGIS Component Category Registrar generated code /// <summary> /// Required method for ArcGIS Component Category registration - /// Do not modify the contents of this method with the code editor. /// </summary> private static void ArcGISCategoryRegistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); ControlsCommands.Register(regKey); } /// <summary> /// Required method for ArcGIS Component Category unregistration - /// Do not modify the contents of this method with the code editor. /// </summary> private static void ArcGISCategoryUnregistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); ControlsCommands.Unregister(regKey); } #endregion #endregion private System.Windows.Forms.Cursor m_pCursor; private ISceneHookHelper m_pSceneHookHelper; private long m_lMouseX, m_lMouseY; private bool m_bInUse; public ZoomIn() { base.m_category = "Sample_SceneControl(C#)"; base.m_caption = "Zoom In"; base.m_toolTip = "Zoom In"; base.m_name = "Sample_SceneControl(C#)/Zoom In"; base.m_message = "Zooms in on the scene"; //Load resources string[] res = GetType().Assembly.GetManifestResourceNames(); if(res.GetLength(0) > 0) { base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("sceneTools.ZoomIn.bmp")); } m_pCursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("sceneTools.ZoomIn.cur")); m_pSceneHookHelper = new SceneHookHelperClass (); } public override bool Enabled { get { if(m_pSceneHookHelper.Scene == null) return false; else return true; } } public override void OnCreate(object hook) { m_pSceneHookHelper.Hook = hook; } public override int Cursor { get { return m_pCursor.Handle.ToInt32(); } } public override bool Deactivate() { return true; } public override void OnKeyDown(int keyCode, int Shift) { if(m_bInUse == true) { if(keyCode == 27) //If ESC was pressed { //Redraw the scene viewer ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer; pSceneViewer.Redraw(true); ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd); m_bInUse = false; } } } public override void OnMouseDown(int Button, int Shift, int X, int Y) { //Initialize mouse coordinates m_bInUse = true; m_lMouseX = X; m_lMouseY = Y; //Get the scene viewer ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer; SetCapture(m_pSceneHookHelper.ActiveViewer.hWnd); } public override void OnMouseMove(int Button, int Shift, int X, int Y) { if(!m_bInUse) return; IEnvelope pEnvelope; //Draw rectangle on the device CreateEnvelope(X, Y, out pEnvelope); DrawRectangle(pEnvelope); } public override void OnMouseUp(int Button, int Shift, int X, int Y) { if(!m_bInUse) return; try { if (GetCapture(m_pSceneHookHelper.ActiveViewer.hWnd) != 0) ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd); //Get the scene viewer's camera ICamera pCamera = (ICamera)m_pSceneHookHelper.Camera; //Get the scene graph ISceneGraph pSceneGraph = (ISceneGraph)m_pSceneHookHelper.SceneGraph; //Create envelope IEnvelope pEnvelope; CreateEnvelope(X, Y, out pEnvelope); IPoint pPoint; object pOwner, pObject; if (pEnvelope.Width == 0 || pEnvelope.Height == 0) { //Translate screen coordinates into a 3D point pSceneGraph.Locate(pSceneGraph.ActiveViewer, X, Y, esriScenePickMode.esriScenePickAll, true, out pPoint, out pOwner, out pObject); //Set camera target and zoom in pCamera.Target = pPoint; pCamera.Zoom(0.75); } else { //if perspective (3D) view if (pCamera.ProjectionType == esri3DProjectionType.esriPerspectiveProjection) { //Zoom camera to the envelope pCamera.ZoomToRect(pEnvelope); } else { //Translate screen coordinates into a 3D point pSceneGraph.Locate(pSceneGraph.ActiveViewer, (int)(pEnvelope.XMin + (pEnvelope.Width / 2)), (int)(pEnvelope.YMin + (pEnvelope.Height / 2)), esriScenePickMode.esriScenePickAll, true, out pPoint, out pOwner, out pObject); //Set camera target pCamera.Target = pPoint; //Get dimension of the scene viewer window Rectangle rect; rect = new Rectangle(); if (GetWindowRect(m_pSceneHookHelper.ActiveViewer.hWnd, ref rect) == 0) return; double dx, dy; dx = pEnvelope.Width; dy = pEnvelope.Height; //Determine zoom factor if (dx > 0 && dy > 0) { dx = dx / Math.Abs(rect.Right - rect.Left); dy = dy / Math.Abs(rect.Top - rect.Bottom); if (dx > dy) pCamera.Zoom(dx); else pCamera.Zoom(dy); } else pCamera.Zoom(0.75); } } } finally { //Redraw the scene viewer ISceneViewer pSceneViewer = (ISceneViewer)m_pSceneHookHelper.ActiveViewer; pSceneViewer.Redraw(true); m_bInUse = false; } } public void CreateEnvelope(int X, int Y, out IEnvelope pEnvelope) { //Create envelope based upon the initial //and current mouse coordinates pEnvelope = new EnvelopeClass(); if((double)m_lMouseX <= (double)X) { pEnvelope.XMin = (double) m_lMouseX; pEnvelope.XMax = (double) X; } else { pEnvelope.XMin = (double) X; pEnvelope.XMax = (double) m_lMouseX; } if((double) m_lMouseY <= (double) Y) { pEnvelope.YMin = (double) m_lMouseY; pEnvelope.YMax = (double) Y; } else { pEnvelope.YMin = (double) Y; pEnvelope.YMax = (double) m_lMouseY; } } public void DrawRectangle(IEnvelope pEnvelope) { //Get the scene viewer ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer; using (Graphics myGraphics = Graphics.FromHdc((IntPtr)m_pSceneHookHelper.ActiveViewer.hDC)) { using (Brush brush = new SolidBrush(Color.Transparent))//hollow brush { //GDI+ call to fill a rectangle with a hollow brush myGraphics.FillRectangle(brush, (int)pEnvelope.XMin, (int)pEnvelope.YMin, (int)pEnvelope.Width, (int)pEnvelope.Height); } using (Pen pen = new System.Drawing.Pen(Color.Black, 2)) //A solid, width of 2 black pen { //GDI+ call to draw a rectangle with a specified pen myGraphics.DrawRectangle(pen, (int)pEnvelope.XMin, (int)pEnvelope.YMin, (int)pEnvelope.Width, (int)pEnvelope.Height); } } } } }
[Visual Basic .NET]
ZoomIn.vb
Imports System.Drawing Imports ESRI.ArcGIS.Analyst3D Imports ESRI.ArcGIS.GeomeTry Imports ESRI.ArcGIS.Controls Imports ESRI.ArcGIS.ADF.BaseClasses Imports ESRI.ArcGIS.ADF.CATIDs Imports System.Runtime.InteropServices <ComClass(ZoomIn.ClassId, ZoomIn.InterfaceId, ZoomIn.EventsId)> _ Public NotInheritable Class ZoomIn Inherits BaseTool #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = "2E281E85-C9F7-4846-B40B-BC865EBCEC26" Public Const InterfaceId As String = "3A6847EC-AA6C-40C2-8F4A-3C60D5D3FC9D" Public Const EventsId As String = "D3A50432-2789-49E5-9262-5C750875405A" #End Region #Region "COM Registration Function(s)" <ComRegisterFunction(), ComVisibleAttribute(False)> _ Public Shared Sub RegisterFunction(ByVal registerType As Type) ' Required for ArcGIS Component Category Registrar support ArcGISCategoryRegistration(registerType) 'Add any COM registration code after the ArcGISCategoryRegistration() call End Sub <ComUnregisterFunction(), ComVisibleAttribute(False)> _ Public Shared Sub UnregisterFunction(ByVal registerType As Type) ' Required for ArcGIS Component Category Registrar support ArcGISCategoryUnregistration(registerType) 'Add any COM unregistration code after the ArcGISCategoryUnregistration() call End Sub #Region "ArcGIS Component Category Registrar generated code" ''' <summary> ''' Required method for ArcGIS Component Category registration - ''' Do not modify the contents of this method with the code editor. ''' </summary> Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type) Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID) ControlsCommands.Register(regKey) End Sub ''' <summary> ''' Required method for ArcGIS Component Category unregistration - ''' Do not modify the contents of this method with the code editor. ''' </summary> Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type) Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID) ControlsCommands.Unregister(regKey) End Sub #End Region #End Region Declare Function GetCapture Lib "user32" (ByVal fuFlags As Integer) As Integer Declare Function SetCapture Lib "user32" (ByVal hwnd As Integer) As Integer Declare Function ReleaseCapture Lib "user32" (ByVal hwnd As Integer) As Integer Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As Rectangle) As Integer Private m_pCursor As System.Windows.Forms.Cursor Private m_pSceneHookHelper As ISceneHookHelper Private m_lMouseX, m_lMouseY As Long Private m_bInUse As Boolean Private m_pen As Pen Private m_brush As Brush Private myGraphics As Graphics ' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() MyBase.m_category = "Sample_SceneControl(VB.NET)" MyBase.m_caption = "Zoom In" MyBase.m_toolTip = "Zoom In" MyBase.m_name = "Sample_SceneControl(VB.NET)/Zoom In" MyBase.m_message = "Zooms in on the scene" 'Load resources Dim res() As String = GetType(ZoomIn).Assembly.GetManifestResourceNames() If res.GetLength(0) > 0 Then MyBase.m_bitmap = New System.Drawing.Bitmap(GetType(ZoomIn).Assembly.GetManifestResourceStream("SceneToolsVB.ZoomIn.bmp")) End If m_pCursor = New System.Windows.Forms.Cursor(GetType(ZoomIn).Assembly.GetManifestResourceStream("SceneToolsVB.ZoomIn.cur")) m_pSceneHookHelper = New SceneHookHelperClass End Sub Public Overrides Sub OnCreate(ByVal hook As Object) m_pSceneHookHelper.Hook = hook Dim ptrHDC As IntPtr ptrHDC = New IntPtr(m_pSceneHookHelper.ActiveViewer.hDC) myGraphics = Graphics.FromHdc(ptrHDC) m_brush = New SolidBrush(Color.Transparent) 'hollow brush m_pen = New System.Drawing.Pen(Color.Black, 2) 'A solid, width of 2 black pen End Sub Public Overrides ReadOnly Property Enabled() As Boolean Get If (m_pSceneHookHelper.Scene Is Nothing) Then Return False Else Return True End If End Get End Property Public Overrides ReadOnly Property Cursor() As Integer Get Return m_pCursor.Handle.ToInt32() End Get End Property Public Overrides Function Deactivate() As Boolean Return True End Function Public Overrides Sub OnKeyDown(ByVal keyCode As Integer, ByVal Shift As Integer) If (m_bInUse = True) Then If (keyCode = 27) Then 'if ESC was pressed 'Redraw the scene viewer Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer) pSceneViewer.Redraw(True) ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd) m_bInUse = False End If End If End Sub Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer) 'Initialize mouse coordinates m_bInUse = True m_lMouseX = X m_lMouseY = Y Dim pEnvelope As IEnvelope = Nothing 'Initialize(Envelope) CreateEnvelope(X, Y, pEnvelope) 'Get the scene viewer Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer) SetCapture(m_pSceneHookHelper.ActiveViewer.hWnd) End Sub Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer) If Not m_bInUse Then Return End If Dim pEnvelope As IEnvelope = Nothing 'Draw rectangle on the device CreateEnvelope(X, Y, pEnvelope) DrawRectangle(pEnvelope) End Sub Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer) If Not m_bInUse Then Return End If If (GetCapture(m_pSceneHookHelper.ActiveViewer.hWnd) <> 0) Then ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd) End If 'Get the scene viewer's camera Dim pCamera As ICamera = CType(m_pSceneHookHelper.Camera, ICamera) 'Get the scene graph Dim pSceneGraph As ISceneGraph = CType(m_pSceneHookHelper.SceneGraph, ISceneGraph) 'Create(Envelope) Dim pEnvelope As IEnvelope = Nothing CreateEnvelope(X, Y, pEnvelope) Dim pPoint As IPoint = Nothing Dim pOwner As Object = Nothing, pObject As Object = Nothing If (pEnvelope.Width = 0 Or pEnvelope.Height = 0) Then 'Translate screen coordinates into a 3D point pSceneGraph.Locate(pSceneGraph.ActiveViewer, X, Y, esriScenePickMode.esriScenePickAll, True, pPoint, pOwner, pObject) 'Set camera target and zoom in pCamera.Target = pPoint pCamera.Zoom(0.75) Else 'If perspective (3D) view If (pCamera.ProjectionType = esri3DProjectionType.esriPerspectiveProjection) Then 'Zoom camera to the envelope pCamera.ZoomToRect(pEnvelope) Else 'Translate screen coordinates into a 3D point pSceneGraph.Locate(pSceneGraph.ActiveViewer, pEnvelope.XMin + (pEnvelope.Width / 2), _ pEnvelope.YMin + (pEnvelope.Height / 2), esriScenePickMode.esriScenePickAll, True, _ pPoint, pOwner, pObject) 'Set camera target pCamera.Target = pPoint 'Get dimension of the scene viewer window Dim rect As Rectangle rect = New Rectangle If (GetWindowRect(m_pSceneHookHelper.ActiveViewer.hWnd, rect) = 0) Then Return End If Dim dx, dy As Double dx = pEnvelope.Width dy = pEnvelope.Height 'Determine zoom factor If (dx > 0 And dy > 0) Then dx = dx / Math.Abs(rect.Right - rect.Left) dy = dy / Math.Abs(rect.Top - rect.Bottom) If (dx > dy) Then pCamera.Zoom(dx) Else pCamera.Zoom(dy) End If Else pCamera.Zoom(0.75) End If End If End If 'Redraw the scene viewer Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer) pSceneViewer.Redraw(True) m_bInUse = False End Sub Public Sub CreateEnvelope(ByVal X As Integer, ByVal Y As Integer, ByRef pEnvelope As IEnvelope) 'Create envelope based upon the initial 'and current mouse coordinates pEnvelope = New EnvelopeClass If (CType(m_lMouseX, Double) <= CType(X, Double)) Then pEnvelope.XMin = CType(m_lMouseX, Double) pEnvelope.XMax = CType(X, Double) Else pEnvelope.XMin = CType(X, Double) pEnvelope.XMax = CType(m_lMouseX, Double) End If If (CType(m_lMouseY, Double) <= CType(Y, Double)) Then pEnvelope.YMin = CType(m_lMouseY, Double) pEnvelope.YMax = CType(Y, Double) Else pEnvelope.YMin = CType(Y, Double) pEnvelope.YMax = CType(m_lMouseY, Double) End If End Sub Public Sub DrawRectangle(ByRef pEnvelope As IEnvelope) 'Get the scene viewer Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer) 'Redraw the rectangle pSceneViewer.Redraw(True) 'GDI+ call to fill a rectangle with a hollow brush myGraphics.FillRectangle(m_brush, CType(pEnvelope.XMin, Integer), CType(pEnvelope.YMin, Integer), _ CType(pEnvelope.Width, Integer), CType(pEnvelope.Height, Integer)) 'GDI+ call to draw a rectangle with a specified pen myGraphics.DrawRectangle(m_pen, CType(pEnvelope.XMin, Integer), CType(pEnvelope.YMin, Integer), _ CType(pEnvelope.Width, Integer), CType(pEnvelope.Height, Integer)) End Sub End Class