C++CLI调用ArcObjects.docx

上传人:b****5 文档编号:7888963 上传时间:2023-01-27 格式:DOCX 页数:17 大小:1,003.47KB
下载 相关 举报
C++CLI调用ArcObjects.docx_第1页
第1页 / 共17页
C++CLI调用ArcObjects.docx_第2页
第2页 / 共17页
C++CLI调用ArcObjects.docx_第3页
第3页 / 共17页
C++CLI调用ArcObjects.docx_第4页
第4页 / 共17页
C++CLI调用ArcObjects.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C++CLI调用ArcObjects.docx

《C++CLI调用ArcObjects.docx》由会员分享,可在线阅读,更多相关《C++CLI调用ArcObjects.docx(17页珍藏版)》请在冰豆网上搜索。

C++CLI调用ArcObjects.docx

C++CLI调用ArcObjects

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

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

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

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

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

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

usingnamespaceSystem;

usingnamespaceSystem:

:

Diagnostics;

usingnamespaceESRI:

:

ArcGIS:

:

Geometry;

usingnamespaceESRI:

:

ArcGIS:

:

esriSystem;

usingnamespaceESRI:

:

ArcGIS;

namespaceAoWrapper

{

publicrefclassGeometryWrapper

{

public:

GeometryWrapper()

{

ESRI:

:

ArcGIS:

:

RuntimeManager:

:

Bind(ProductCode:

:

Engine);

IAoInitialize^pAoInitialize=gcnewAoInitializeClass();

pAoInitialize->Initialize(esriLicenseProductCode:

:

esriLicenseProductCodeEngineGeoDB);

}

~GeometryWrapper()

{

}

public:

intline_test()

{

IPoint^ipPt1=gcnewPointClass();

IPoint^ipPt2=gcnewPointClass();

ipPt1->PutCoords(1,2);

ipPt2->PutCoords(2,3);

ILine^ipLine=gcnewLineClass();

Stopwatch^watch=gcnewStopwatch();

watch->Start();

for(longi=0;i<=10000000;i++)

{

ipLine->PutCoords(ipPt1,ipPt2);

//Console:

:

WriteLine(i);

}

watch->Stop();

Console:

:

WriteLine(watch->ElapsedMilliseconds);

return0;

}

};

}

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

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

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

//stdafx.h:

includefileforstandardsystemincludefiles,

//orprojectspecificincludefilesthatareusedfrequently,but

//arechangedinfrequently

//

#pragmaonce

#include"targetver.h"

#defineWIN32_LEAN_AND_MEAN//Excluderarely-usedstufffromWindowsheaders

//WindowsHeaderFiles:

#include

 

//TODO:

referenceadditionalheadersyourprogramrequireshere

#pragmawarning(push)

#pragmawarning(disable:

4192)/*Ignorewarningsfortypesthatareduplicatedinwin32headerfiles*/

#pragmawarning(disable:

4146)/*Ignorewarningsforuseofminusonunsignedtypes*/

#import"C:

\ProgramFiles(x86)\ArcGIS\Desktop10.1\com\esriSystem.olb"raw_interfaces_only,raw_native_types,no_namespace,named_guids,exclude("OLE_COLOR","OLE_HANDLE","VARTYPE")

#import"C:

\ProgramFiles(x86)\ArcGIS\Desktop10.1\com\esriGeometry.olb"raw_interfaces_only,raw_native_types,no_namespace,named_guids,exclude("OLE_COLOR")

//LoadtheArcGISVersionlibrary.

//ThiscodeiscommonlyplacedintheStdAfx.hheaderfile.

#import"libid:

6FCCEDE0-179D-4D12-B586-58C88D26CA78"raw_interfaces_only,no_implementation

添加一个头文件NAoHelper.h

修改后的头文件如下:

#ifndefAOHELPER_H_

#defineAOHELPER_H_

#ifdefNAOHELPER_EXPORTS

#defineDAPI__declspec(dllexport)

#else

#defineDAPI__declspec(dllimport)

#endif

 

//导出这个类

classDAPIAoHelper

{

public:

AoHelper();

~AoHelper();

intline_test();

private:

intInitializeAo();

};

#endif

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

//NAoHelper.cpp:

DefinestheexportedfunctionsfortheDLLapplication.

//

#include"stdafx.h"

#include"NAoHelper.h"

AoHelper:

:

AoHelper()

{

:

:

CoInitialize(NULL);

//不初始化也可以

//InitializeAo();

}

AoHelper:

:

~AoHelper()

{

:

:

CoUninitialize();

}

intAoHelper:

:

InitializeAo()

{

ArcGISVersionLib:

:

IArcGISVersionPtripVer(__uuidof(ArcGISVersionLib:

:

VersionManager));

VARIANT_BOOLsucceeded;

if(FAILED(ipVer->LoadVersion(ArcGISVersionLib:

:

esriArcGISDesktop,L"10.1",&succeeded)))

return0;

IAoInitializePtrm_AoInit;//(CLSID_AoInitialize);

m_AoInit.CreateInstance(CLSID_AoInitialize);

esriLicenseStatusls;

HRESULTh=m_AoInit->Initialize(esriLicenseProductCode:

:

esriLicenseProductCodeEngineGeoDB,&ls);

return0;

}

intAoHelper:

:

line_test()

{

IPointPtripPt1(CLSID_Point);

IPointPtripPt2(CLSID_Point);

ipPt1->PutCoords(1,2);

ipPt2->PutCoords(2,3);

ILinePtripLine(CLSID_Line);

for(longi=0;i<=10000000;i++)

{

ipLine->PutCoords(ipPt1,ipPt2);

}

return0;

}

 

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

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

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

修改头文件NAoHelper.h

//NAoWrapper.h

#pragmaonce

#include"NAoHelper.h"

usingnamespaceSystem;

namespaceNAoWrapper{

publicrefclassRefAoWrapper

{

//TODO:

Addyourmethodsforthisclasshere.

private:

AoHelper*ao;

public:

intline_test();

RefAoWrapper();

~RefAoWrapper();

};

}

修改NAoWrapper.cpp

//ThisisthemainDLLfile.

#include"stdafx.h"

#include"NAoWrapper.h"

usingnamespaceSystem:

:

Diagnostics;

namespaceNAoWrapper{

RefAoWrapper:

:

RefAoWrapper()

{

ao=newAoHelper();

}

RefAoWrapper:

:

~RefAoWrapper()

{

deleteao;

}

intRefAoWrapper:

:

line_test()

{

Stopwatch^watch=gcnewStopwatch();

watch->Start();

ao->line_test();

watch->Stop();

Console:

:

WriteLine(watch->ElapsedMilliseconds);

return0;

}

}

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

添加ESRI.ArcGIS.相关引用

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingESRI.ArcGIS.Geometry;

usingSystem.Diagnostics;

namespaceCSharpAoHeleper

{

publicclassCSharpAoHeleper

{

publicintline_test()

{

IPointipPt1=newPointClass();

IPointipPt2=newPointClass();

ipPt1.PutCoords(1,2);

ipPt2.PutCoords(2,3);

ILineipLine=newLineClass();

Stopwatchwatch=newStopwatch();

watch.Start();

for(longi=0;i<=10000000;i++)

{

ipLine.PutCoords(ipPt1,ipPt2);

//Console:

:

WriteLine(i);

}

watch.Stop();

Console.WriteLine(watch.ElapsedMilliseconds);

return0;

}

}

}

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

6添加新项目,命名为Test

右键项目属性,修改输出路径(Outputpath):

即AoWrapperDemo\Debug目录

添加ESRI.AcrGIS.相关引用

添加项目引用

修改Program.cs

usingSystem;

usingESRI.ArcGIS;

usingESRI.ArcGIS.esriSystem;

namespaceTest

{

staticclassProgram

{

///

///Themainentrypointfortheapplication.

///

[STAThread]

staticvoidMain()

{

if(!

RuntimeManager.Bind(ProductCode.Engine))

{

if(!

RuntimeManager.Bind(ProductCode.Desktop))

{

Console.WriteLine("UnabletobindtoArcGISruntime.Applicationwillbeshutdown.");

return;

}

}

IAoInitializepAoInitialize=newAoInitializeClass();

esriLicenseStatuslicenseStatus=pAoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

if(licenseStatus!

=esriLicenseStatus.esriLicenseCheckedOut)

return;

Console.WriteLine("进行10000000次运算测试\r\n");

Console.WriteLine("===C++/CLI测试开始====");

AoWrapper.GeometryWrappergeo=newAoWrapper.GeometryWrapper();

geo.line_test();

Console.WriteLine("===C++/CLI封装本地Api测试开始====");

NAoWrapper.RefAoWrapperrefAoWrapper=newNAoWrapper.RefAoWrapper();

refAoWrapper.line_test();

Console.WriteLine("===C#测试开始====");

CSharpAoHeleper.CSharpAoHelepersharpAo=newCSharpAoHeleper.CSharpAoHeleper();

sharpAo.line_test();

Console.WriteLine("\r\n====测试结束====");

Console.ReadKey();

}

}

}

编译,Debug模式运行结果如下

Release模式运行结果如下

可以发现C++/CLI封装本地Api性能有明显提高!

C++/CLI直接调用ArcObjects和C#直接操作ArcObjects并无明显变化。

源码下载:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 理化生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1