C++CLI调用ArcObjects

更新时间:2023-09-16 08:18:01 阅读量: 高中教育 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1新建一个空的工程命名为AoWrapperDemo

2 添加一个新项目,命名为AoWrapper

右键项目-》属性,添加ESRI.ArcGIS相关引用

将配置类型改为动态链接库

添加一个c++文件,命名为GeometryWrapper

由于该类功能简单,没必要添加头文件

using namespace System;

using namespace System::Diagnostics; using namespace ESRI::ArcGIS::Geometry; using namespace ESRI::ArcGIS::esriSystem; using namespace ESRI::ArcGIS; namespace AoWrapper {

}

~GeometryWrapper() { }

public ref class GeometryWrapper { public:

pAoInitialize->Initialize(esriLicenseProductCode::esriLicenseProductC

GeometryWrapper() {

ESRI::ArcGIS::RuntimeManager::Bind(ProductCode::Engine); IAoInitialize^ pAoInitialize=gcnew AoInitializeClass();

odeEngineGeoDB);

}

public: };

int line_test() { }

IPoint ^ ipPt1 =gcnew PointClass(); IPoint ^ ipPt2 =gcnew PointClass(); ipPt1->PutCoords(1, 2); ipPt2->PutCoords(2, 3);

ILine^ ipLine=gcnew LineClass(); Stopwatch^ watch=gcnew Stopwatch(); watch->Start();

for(long i=0;i<=10000000; i++) { }

watch->Stop();

Console::WriteLine(watch->ElapsedMilliseconds); return 0;

ipLine->PutCoords(ipPt1,ipPt2); //Console::WriteLine(i);

完成C++/CLI对ArcObjects的调用封装类库

3 添加一个新项目,命名为NAoHelper,利用本地c++直接操作AO

修改头文件stdafx.h,导入ArcObjects相关库

// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently //

#pragma once

#include \

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

// Windows Header Files: #include

// TODO: reference additional headers your program requires here #pragma warning(push)

#pragma warning(disable : 4192) /* Ignore warnings for types that are duplicated in win32 header files */

#pragma warning(disable : 4146) /* Ignore warnings for use of minus on unsigned types */

#import \ raw_interfaces_only, raw_native_types, no_namespace, named_guids, exclude(\, \, \)

#import \ raw_interfaces_only, raw_native_types, no_namespace, named_guids,exclude(\) // Load the ArcGISVersion library.

// This code is commonly placed in the StdAfx.h header file. #import \ raw_interfaces_only,no_implementation

添加一个头文件NAoHelper.h

修改后的头文件如下:

#ifndef AOHELPER_H_ #define AOHELPER_H_ #ifdef NAOHELPER_EXPORTS

#define DAPI __declspec(dllexport) #else

#define DAPI __declspec(dllimport) #endif

//导出这个类

class DAPI AoHelper { public: }; #endif

AoHelper(); ~AoHelper(); int line_test(); int InitializeAo();

private:

打开NAoHelper.cpp,修改后如下:

// NAoHelper.cpp : Defines the exported functions for the DLL application. //

#include \ #include \

AoHelper::AoHelper() { }

AoHelper::~AoHelper() { }

int AoHelper::InitializeAo() {

esriLicenseStatus ls; HRESULT h=

IAoInitializePtr m_AoInit;//(CLSID_AoInitialize); m_AoInit.CreateInstance (CLSID_AoInitialize); ArcGISVersionLib::IArcGISVersionPtr VARIANT_BOOL succeeded;

if (FAILED(ipVer->LoadVersion(ArcGISVersionLib::esriArcGISDesktop ,

return 0;

ipVer(__uuidof(ArcGISVersionLib::VersionManager));

::CoUninitialize(); ::CoInitialize(NULL); //不初始化也可以 //InitializeAo();

L\,&succeeded)))

m_AoInit->Initialize(esriLicenseProductCode::esriLicenseProductCodeEngineGeoDB ,&ls); }

int AoHelper::line_test() {

IPointPtr ipPt1(CLSID_Point); IPointPtr ipPt2(CLSID_Point); ipPt1->PutCoords(1, 2); ipPt2->PutCoords(2, 3); return 0;

}

ILinePtr ipLine(CLSID_Line);

for(long i = 0; i <= 10000000; i++) { }

return 0;

ipLine->PutCoords(ipPt1,ipPt2);

编译,完成本地c++调用AO封装类库

4 添加新项目,命名为NAoWrapper,利用C++/CLI对NAoHelper进行封装的库,以便于C#调用

编辑包含文件(头文件)目录

修改头文件NAoHelper.h

// NAoWrapper.h

#pragma once

#include \ using namespace System; namespace NAoWrapper { }

public ref class RefAoWrapper { };

// TODO: Add your methods for this class here. AoHelper *ao; int line_test(); RefAoWrapper(); ~RefAoWrapper(); private: public:

修改NAoWrapper.cpp

// This is the main DLL file.

#include \ #include \

using namespace System::Diagnostics;

namespace NAoWrapper {

int RefAoWrapper ::line_test() {

Stopwatch^ watch=gcnew Stopwatch(); watch->Start();

RefAoWrapper::~RefAoWrapper() { }

delete ao;

RefAoWrapper::RefAoWrapper() { }

ao=new AoHelper();

}

}

ao->line_test(); watch->Stop();

Console::WriteLine(watch->ElapsedMilliseconds); return 0;

5 添加新项目,命名为CSharpAoHeleper,C#直接调用ArcObjects

添加ESRI.ArcGIS.相关引用

将Class1.cs重命名为CSharpAoHeleper,代码如下

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using ESRI.ArcGIS.Geometry; using System.Diagnostics;

namespace CSharpAoHeleper {

public class CSharpAoHeleper {

public int line_test() {

IPoint ipPt1 = new PointClass(); IPoint ipPt2 = new PointClass(); ipPt1.PutCoords(1, 2); ipPt2.PutCoords(2, 3);

ILine ipLine = new LineClass();

Stopwatch watch = new Stopwatch(); watch.Start();

for (long i = 0; i <= 10000000; i++) {

ipLine.PutCoords(ipPt1, ipPt2); //Console::WriteLine(i); }

watch.Stop();

Console.WriteLine(watch.ElapsedMilliseconds);

return 0; } } }

编译,完成C#直接操作AO封装库

6 添加新项目,命名为Test

右键项目属性,修改输出路径(Output path):即AoWrapperDemo\\Debug目录

添加ESRI.AcrGIS.相关引用 添加项目引用

修改Program.cs

using System; using ESRI.ArcGIS;

using ESRI.ArcGIS.esriSystem;

namespace Test {

static class Program {

///

/// The main entry point for the application. ///

[STAThread]

static void Main() {

if (!RuntimeManager.Bind(ProductCode.Engine))

本文来源:https://www.bwwdw.com/article/455h.html

Top