ArcGIS Engine常用开发代码整理(1)
1. 创建工作空间工厂——EDN
View Code
public void IWorkspaceFactory_Create_Example_Access() {
// create a new Access workspace factory
IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
// Create a workspacename with the workspace factory
IWorkspaceName workspaceName = workspaceFactory.Create("C:\\temp\\", "MyNewpGDB.mdb", null, 0);
// Cast for IName
ESRI.ArcGIS.esriSystem.IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
//Open a reference to the access workspace through the name object
IWorkspace pGDB_workspace = (IWorkspace)name.Open();
Console.WriteLine("Current path of the {0} is {1}", pGDB_workspace.Type, pGDB_workspace.PathName);
}
2. 遍历所有图层
注意帮助文档中“Loop Through Layers of Specific UID Snippet”主题中详细列出了不同的UIDClass过滤选项,替换get_layers()中的null:
View Code
1 /// The different layer GUID's and Interface's are:
2 /// "{AD88322D-533D-4E36-A5C9-1B109AF7A346}" = IACFeatureLayer
3 /// "{74E45211-DFE6-11D3-9FF7-00C04F6BC6A5}" = IACLayer
4 /// "{495C0E2C-D51D-4ED4-9FC1-FA04AB93568D}" = IACImageLayer
5 /// "{65BD02AC-1CAD-462A-A524-3F17E9D85432}" = IACAcetateLayer
6 /// "{4AEDC069-B599-424B-A374-49602ABAD308}" = IAnnotationLayer
7 /// "{DBCA59AC-6771-4408-8F48-C7D53389440C}" = IAnnotationSublayer
8 /// "{E299ADBC-A5C3-11D2-9B10-00C04FA33299}" = ICadLayer
9 /// "{7F1AB670-5CA9-44D1-B42D-12AA868FC757}" = ICadastralFabricLayer
10 /// "{BA119BC4-939A-11D2-A2F4-080009B6F22B}" = ICompositeLayer
11 /// "{9646BB82-9512-11D2-A2F6-080009B6F22B}" = ICompositeGraphicsLayer
12 /// "{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E}" = ICoverageAnnotationLayer
13 /// "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}" = IDataLayer
14 /// "{0737082E-958E-11D4-80ED-00C04F601565}" = IDimensionLayer
15 /// "{48E56B3F-EC3A-11D2-9F5C-00C04F6BC6A5}" = IFDOGraphicsLayer
16 /// "{40A9E885-5533-11D0-98BE-00805F7CED21}" = IFeatureLayer
17 /// "{605BC37A-15E9-40A0-90FB-DE4CC376838C}" = IGdbRasterCatalogLayer
18 /// "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}" = IGeoFeatureLayer
19 /// "{34B2EF81-F4AC-11D1-A245-080009B6F22B}" = IGraphicsLayer
20 /// "{EDAD6644-1810-11D1-86AE-0000F8751720}" = IGroupLayer
21 /// "{D090AA89-C2F1-11D3-9FEF-00C04F6BC6A5}" = IIMSSubLayer
22 /// "{DC8505FF-D521-11D3-9FF4-00C04F6BC6A5}" = IIMAMapLayer
23 /// "{34C20002-4D3C-11D0-92D8-00805F7C28B0}" = ILayer
24 /// "{E9B56157-7EB7-4DB3-9958-AFBF3B5E1470}" = IMapServerLayer
25 /// "{B059B902-5C7A-4287-982E-EF0BC77C6AAB}" = IMapServerSublayer
26 /// "{82870538-E09E-42C0-9228-CBCB244B91BA}" = INetworkLayer
27 /// "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}" = IRasterLayer
28 /// "{AF9930F0-F61E-11D3-8D6C-00C04F5B87B2}" = IRasterCatalogLayer
29 /// "{FCEFF094-8E6A-4972-9BB4-429C71B07289}" = ITemporaryLayer
30 /// "{5A0F220D-614F-4C72-AFF2-7EA0BE2C8513}" = ITerrainLayer
31 /// "{FE308F36-BDCA-11D1-A523-0000F8774F0F}" = ITinLayer
32 /// "{FB6337E3-610A-4BC2-9142-760D954C22EB}" = ITopologyLayer
33 /// "{005F592A-327B-44A4-AEEB-409D2F866F47}" = IWMSLayer
34 /// "{D43D9A73-FF6C-4A19-B36A-D7ECBE61962A}" = IWMSGroupLayer
35 /// "{8C19B114-1168-41A3-9E14-FC30CA5A4E9D}" = IWMSMapLayer
示例:
View Code
IMap pMap = axMapControl1.Map;
IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
pEnumLayer.Reset();
ILayer pLayer = pEnumLayer.Next();
while (pLayer != null)
{
// Console.WriteLine(pLayer.Name);
pLayer = pEnumLayer.Next();
}
3. 判断图层类型
http://bbs.esrichina-bj.cn/ESRI/thread-59674-1-1.html
4. 【转】根据图层名称找到当前的图层的两种方法
http://www.gisempire.com/blog/user1/1/58.html
View Code
'功能:根据图层名称找到当前的图层
'返回:当前图层对象
Private Function GetCurLayer() As ILayer
Dim i As Integer
Set GetCurLayer = Nothing
For i = 0 To ff_m_Map.LayerCount - 1
If UCase$(ff_m_Map.Layer(i).Name) = UCase$(ff_m_strCurLayername) Then
Set GetCurLayer = ff_m_Map.Layer(i)
Exit For
End If
Next i
End Function
'功能:找到当前的图层
'返回:当前图层对象
'修改时间:
Private Function GetCurLayer() As ILayer
Set ff_m_ActiveView = ff_m_Map
Dim pEnumLayer As IEnumLayer
Dim pId As IFeatureLayer
Dim pLayer As ILayer
Set ff_m_Map = ff_m_ActiveView.FocusMap
Set pEnumLayer = ff_m_Map.Layers(pId, True)
Set GetCurLayer = Nothing
pEnumLayer.Reset
Set pLayer = pEnumLayer.Next
Do While Not pLayer Is Nothing
If UCase$(pLayer.Name) = UCase$(ff_m_strCurLayername) Then
Set GetCurLayer = pLayer
End If
Set pLayer = pEnumLayer.Next
Loop
End Function
5. 打开TIN数据集
View Code
public ILayer openTinLayer(string fullPath)
{
ITinWorkspace pTinWorkspace;
IWorkspace pWS;
IWorkspaceFactory pWSFact = new TinWorkspaceFactoryClass();
// ITinLayer pTinLayer = new TinLayerClass();
string pathToWorkspace = System.IO.Path.GetDirectoryName(fullPath);
string tinName = System.IO.Path.GetFileName(fullPath);
pWS = pWSFact.OpenFromFile(pathToWorkspace, 0);
pTinWorkspace = pWS as ITinWorkspace;
if (pTinWorkspace.get_IsTin(tinName))
{
pTin = pTinWorkspace.OpenTin(tinName);
pTinLayer.Dataset = pTin;
pTinLayer.ClearRenderers();
return pTinLayer as ILayer;
}
else
{
MessageBox.Show("该目录不包含Tin文件");
return null;
}
}
6. 读取选中的IElement对象
http://bbs.esrichina-bj.cn/ESRI/thread-101968-1-1.html
7. 通过点的集合IPointCollection构建线IPolyline或面要素IPolygon
View Code
IPolyline m_ProfilePolyline =new PolylineClass();
IPointCollection m_PtCol = m_ProfilePolyline as IPointCollection;
IPoint pPoint1 = new PointClass();
pPoint1.X = x ;
pPoint1.Y = y;
m_PtCol.AddPoint(pPoint1, ref missing, ref missing);
IPoint pPoint2 = new PointClass();
pPoint2.X = x ;
pPoint2.Y = y;
m_PtCol.AddPoint(pPoint2, ref missing, ref missing);
8. 【转】平头缓冲
http://www.cnblogs.com/zuiyirenjian/archive/2011/01/13/1934267.html(AE中应该提供了其他的接口,这里主要学习点的操作)思路就是将线向左右两边移动相同的距离,然后将一条线的方向反向,加入另外一条,构造矩形或者矩形面
View Code
private IPolygon FlatBuffer(IPolyline myLine, double bufferDis)
{
object o = System.Type.Missing;
//分别对输入的线平移两次(正方向和负方向)
IConstructCurve mycurve = new PolylineClass();
mycurve.ConstructOffset(myLine, bufferDis, ref o, ref o);
IPointCollection pCol = mycurve as IPointCollection;
IConstructCurve mycurve2 = new PolylineClass();
mycurve2.ConstructOffset(myLine, -1 * bufferDis, ref o, ref o);
//把第二次平移的线的所有节点翻转
IPolyline addline = mycurve2 as IPolyline;
addline.ReverseOrientation();
//把第二条的所有节点放到第一条线的IPointCollection里面
IPointCollection pCol2 = addline as IPointCollection;
pCol.AddPointCollection(pCol2);
//用面去初始化一个IPointCollection
IPointCollection myPCol = new PolygonClass();
myPCol.AddPointCollection(pCol);
//把IPointCollection转换为面
IPolygon myPolygon = myPCol as IPolygon;
//简化节点次序
myPolygon.SimplifyPreserveFromTo();
return myPolygon;
}
9. 遍历要素类中的所有字段
View Code
IField pField = null;
IFields pFields = pFeatureClass.Fields;
for (int i = 0; i < pFields.FieldCount - 1;i++ )
{
pField = pFields.get_Field(i);
if (pField.Type!=esriFieldType.esriFieldTypeGeometry)
{
dgvCombo.Items.Add(pField.Name);
}
}
10. 获取图层的3DProperties
View Code
/// <summary>
/// 获取图层三维属性
/// </summary>
/// <param name="pTinLayer">pFeatLayer图层</param>
/// <returns></returns>
public I3DProperties get3DProps(IFeatureLayer pFeatLayer)
{
I3DProperties p3DProps = null;
ILayer pLayer = pFeatLayer as ILayer;
ILayerExtensions lyrExt = pLayer as ILayerExtensions;
for (int i = 0; i < lyrExt.ExtensionCount; i++)
{
if (lyrExt.get_Extension(i) is I3DProperties)
{
p3DProps = lyrExt.get_Extension(i) as I3DProperties;
}
}
return p3DProps;
}
pSceneControl.Scene.AddLayer(pLyr,false);
//必须先添加图层到Scene中
I3DProperties p3DProps = get3DProps(pFeatLyr);
p3DProps.ExtrusionType = esriExtrusionType.esriExtrusionAbsolute;
p3DProps.ExtrusionExpressionString = "["+combHeight.Text+"]";
p3DProps.Apply3DProperties(pFeatLyr);