AE C#与C++接口调用差异总结

目前AE C++的开发资料比较少,即使是C#的资料,也不尽如人意。考虑到国内目前做C++ ArcGIS engine开发甚至Qt + AE的人比较少,对于C#+AE出身的人,刚开始尝试QT +AE可能会困难重重,因此总结一下两者之间的接口调用差异,希望能对大家有所帮助。

获取接口
[C#]

            IGlobeControl pGlobeCtr = axGlobeControl1.Object as IGlobeControl;
            pGlobeCtr.Load3dFile(Application.StartupPath + "..\\..\\..\\Test3d.3dd");
[C++ Qt]
    IGlobeControl *m_ipGlobeControl;
    HRESULT hr = this->queryInterface(IID_IGlobeControl,reinterpret_cast<void**>(&m_ipGlobeControl));
    m_ipGlobeControl->Load3dFile(L"Test3d.3dd");

可以看到C#下,从控件对象到接口,直接转换即可,但是在Qt C++ API中,需要通过调用com接口queryInterface来获取;

接口转换
[C#]
                IGlobeCamera pGlobeCamera = axGlobeControl1.GlobeCamera;
                ICamera pCamera = pGlobeCamera as ICamera;        //具有继承关系的接口,直接转换
                IGlobeDisplay pGlobeDisplay = axGlobeControl1.GlobeDisplay;
                IGlobe = axGlobeControl1.Globe;

                if (pGlobeCamera.OrientationMode == esriGlobeCameraOrientationMode.esriGlobeCameraOrientationGlobal)
                {
                    //...
                    double zt1 = zt * (UnitSacleToMeter(axGlobeControl1.Globe.GlobeUnits));
                    //...
                }

[C++ Qt]
    IGlobeCamera *pGlobeCamera;
    HRESULT hr = m_ipGlobeControl->get_GlobeCamera(&pGlobeCamera);

    ICamera *pCamera = reinterpret_cast<ICamera*>(pGlobeCamera);     //需要强制类型转换

    IGlobeDisplay *pGlobeDispaly;                    //通过指针,获取对GlobeDisplay的引用
    hr = m_ipGlobeControl->get_GlobeDisplay(&pGlobeDispaly);

    IGlobe *pGlobe;
    hr = m_ipGlobeControl->get_Globe(&pGlobe);

    //IGlobePtr pGlobe;  //也可以用智能指针来实现
    //hr = m_ipGlobeControl->get_Globe(&pGlobe);

    esriGlobeCameraOrientationMode pMode;                //而这个地方需要传入一个已经分配好地址
    hr= pGlobeCamera->get_OrientationMode(&pMode);

    if(pMode == esriGlobeCameraOrientationMode::esriGlobeCameraOrientationGlobal)
    {
        //...
        esriUnits pUnits;
        pGlobe->get_GlobeUnits(&pUnits);
        qreal zt1 = zt * unitScaleToMeter(pUnits);
        //...
    }

创建接口对象
[C#]
            ICommand command = new ControlsGlobeNavigateToolClass(); // new ControlsGlobeZoomInOutToolClass();
            command.OnCreate(axGlobeControl1.Object); 
            if (command.Enabled == true) { axGlobeControl1.CurrentTool = (ITool)command; }

[C++ Qt]
    ICommandPtr cmd(CLSID_ControlsGlobeNavigateTool);   //CLSID_ControlsGlobeZoomInOutTool
    IDispatchPtr disp;
    m_ipGlobeControl->get_Object(&disp);
    cmd->OnCreate(disp);
    VARIANT_BOOL bEnable;
    hr = cmd->get_Enabled(&bEnable);
    if(bEnable)
    {
        m_ipGlobeControl->putref_CurrentTool(IToolPtr(cmd));
    }

C#下,通过new出实现了相应接口的类来生成一个接口实例,但是在C++中,这个过程需要通过智能指针和CLSID来创建;同样是ICommand接口指针,ICommand和ICommandPtr的区别是ICommand是抽象类,就是c#里所谓的接口,而ICommandPtr是可以通过CLSID实例化的;


你可能感兴趣的:(C++,cmd,C#,command,qt)