操作方法

操作方法:以编程方式将选定要素导出到 shapefile

Last Published: February 12, 2024

摘要

This article shows how to export a selected set of features in ArcMap to a new shapefile using VBA and ArcObjects.

Note: 
The information in this article applies to ArcGIS versions 9.x only. Visual Basic for Applications (VBA) for ArcMap and ArcCatalog has been deprecated at ArcGIS 10.

过程

  1. Open ArcMap.
  2. Open the Visual Basic Editor.
  • In ArcMap, select Tools > Macros > Visual Basic Editor.
  1. In the Project Explorer window, expand the Project folder and select ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument code module only runs in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module. 
  2. Paste the following code into the code module.
    Code:
    Sub ExportFeatureClass()
    
        Dim pDoc As IMxDocument
        Dim pMap As IMap
        Dim pFLayer As IFeatureLayer
        Dim pFc As IFeatureClass
        Dim pINFeatureClassName As IFeatureClassName
        Dim pDataset As IDataset
        Dim pInDsName As IDatasetName
        Dim pFSel As IFeatureSelection
        Dim pSelSet As ISelectionSet
        Dim pFeatureClassName As IFeatureClassName
        Dim pOutDatasetName As IDatasetName
        Dim pWorkspaceName As IWorkspaceName
        Dim pExportOp As IExportOperation
        
        Set pDoc = ThisDocument
        Set pMap = pDoc.FocusMap
        Set pFLayer = pMap.Layer(0)
        Set pFc = pFLayer.FeatureClass
        
        'Get the FcName from the featureclass
        Set pDataset = pFc
        Set pINFeatureClassName = pDataset.FullName
        Set pInDsName = pINFeatureClassName
        
        'Get the selection set
        Set pFSel = pFLayer
        Set pSelSet = pFSel.SelectionSet
        
        'Define the output feature class name
        Set pFeatureClassName = New FeatureClassName
        Set pOutDatasetName = pFeatureClassName
        pOutDatasetName.Name = "TestExport"
        Set pWorkspaceName = New WorkspaceName
        pWorkspaceName.PathName = "c:\temp"
        pWorkspaceName.WorkspaceFactoryProgID = _
              "esriCore.shapefileworkspacefactory.1"
        Set pOutDatasetName.WorkspaceName = pWorkspaceName
        pFeatureClassName.FeatureType = esriFTSimple
        pFeatureClassName.ShapeType = esriGeometryAny
        pFeatureClassName.ShapeFieldName = "Shape"
        
        'Export
        Set pExportOp = New ExportOperation
        pExportOp.ExportFeatureClass pInDsName, Nothing, _
               pSelSet, Nothing, pOutDatasetName, 0
    
    End Sub
  3. If necessary, modify the pWorkspaceName.PathName statement to point to a folder where the new shapefile should be created.
  4. Close the Visual Basic Editor.
  5. Add a feature class to ArcMap and select some features to export.
    Note:
    The first layer in the map must be a vector layer, otherwise a "type mismatch" error will occur.
  6. Run the code.
    1. Click Tools > Macros > Macros to display the Macros dialog box.
    2. Select a macro and click Run. Look in the folder you specified in the PathName setting to see the shapefiles created. Add the shapefile to the map document to see the features that were created.

文章 ID:000004964

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容