Finding and Modifying Objects

This example shows how to traverse through all the objects in the project, using the IInformationTree5, IobjectManager51 and ITerrainLabel5 interfaces, find all the text labels and set their altitude to 10 meters above ground.

 

Note: This example code is part of an ActiveX project. To use this example you should create a new project and place the following code in the correct place.

 

// TraverseTree.h : Declaration of the CTraverseTree

 

#ifndef __TRAVERSETREE_H_

#define __TRAVERSETREE_H_

 

#include "resource.h"       // main symbols

#include "COMDEF.H" // For _bstr_t

 

#import "C:\Program Files\Skyline\TerraExplorer Pro\TerraExplorerX.dll" no_namespace, named_guids

 

// CTraverseTree

class ATL_NO_VTABLE CTraverseTree :

public CComObjectRootEx<CComSingleThreadModel>,

public CComCoClass<CTraverseTree, &CLSID_TraverseTree>,

public IDispatchImpl<ITraverseTree, &IID_ITraverseTree, &LIBID_TRAVERSEINFOTREELib>

{

public:

CTraverseTree()

{

   m_pIObjectManager51 = NULL;

   m_pIInformationTree5 = NULL;

}

 

DECLARE_REGISTRY_RESOURCEID(IDR_TRAVERSETREE)

 

DECLARE_PROTECT_FINAL_CONSTRUCT()

 

BEGIN_COM_MAP(CTraverseTree)

COM_INTERFACE_ENTRY(ITraverseTree)

COM_INTERFACE_ENTRY(IDispatch)

END_COM_MAP()

 

// ITraverseTree

public:

STDMETHOD(SetLabelsHeights)()

{

   m_pIInformationTree5.CreateInstance(CLSID_TerraExplorer);

   m_pIInformationTree5.QueryInterface(IID_IObjectManager51, (IObjectManager51Ptr**)&m_pIObjectManager51);

 

   TraverseBranch(0);

 

   return S_OK;

}

 

void TraverseBranch(long ItemID)

{

   try

   {

    //Get the first child item of this group.

    long SiblingItemID;

    SiblingItemID = m_pIInformationTree5->GetNextItem(ItemID, CHILD);

    if (SiblingItemID == 0)

      return;

 

    //Start with the first child item and traverse

    //all of the sibling items in that group.

 

    while (SiblingItemID != 0)

    {

      if (m_pIInformationTree5->IsGroup(SiblingItemID))

      {

       //If we are here, this tree item is a group.

       //A recursive call - traverse all of the tree items under this group.

       TraverseBranch(SiblingItemID);

      }

      else

      {

       //If we are here, this tree item is an object.

 

       //Get an interface to the object that this is its ID.

       ITerraExplorerObject5Ptr pITEObject = m_pIInformationTree5->GetObjectEx(SiblingItemID, “”);

       if (pITEObject != NULL)

       {

          // Check that this is a label

          ITerrainLabel5Ptr pITerrainLabel5 = NULL;

          HRESULT hr = pITEObject.QueryInterface(IID_ITerrainLabel5, (ITerrainLabel5Ptr**)&pITerrainLabel5);

          if (SUCCEEDED(hr))

           //This is a label. Set its height to 10 meters above the ground.

           //The last parameter is a combination of the following values: IGNORE_X |

           //IGNORE_Y | IGNORE_ORIENTATION. See ITerrainLocation5::SetPosition

           //method for a detail list of possible values.

           pITerrainLabel5->SetPosition(0,0,10,0,0,0, 61);

       }

      }

      //Continue to the next sibling tree item.

      SiblingItemID  = m_pIInformationTree5->GetNextItem(SiblingItemID, NEXT);

    }

   }

   catch(_com_error const &e)

   {

    ATLTRACE((char*)e.Description());

    return;

   }

}

IObjectManager51Ptr m_pIObjectManager51;

IInformationTree5Ptr m_pIInformationTree53;

};

#endif //__TRAVERSETREE_H_