Sub ExportFc()
Dim pDoc As IMxDocument
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
'Get the first layer in the map
Set pDoc = ThisDocument
Set pFLayer = pDoc.FocusMap.Layer(0)
Set pFc = pFLayer.FeatureClass
'Get the Featureclassname from the featureclass
Set pDataset = pFc
Set pInFeatureClassName = pDataset.FullName
Set pInDsName = pInFeatureClassName
'Get the selected features
Set pFSel = pFLayer
Set pSelSet = pFSel.SelectionSet
'Define the output featureclass
'give it the name of the input feature class + exp
Set pFeatureClassName = New FeatureClassName
Set pOutDatasetName = pFeatureClassName
pOutDatasetName.Name = pFc.AliasName & "exp"
Set pWorkspaceName = New WorkspaceName
pWorkspaceName.PathName = "c:\temp"
pWorkspaceName.WorkspaceFactoryProgID = "esriCore.shapefileworkspacefactory.1"
Set pOutDatasetName.WorkspaceName = pWorkspaceName
'Give the output shapefile the same props as the input dataset
pFeatureClassName.FeatureType = pFc.FeatureType
pFeatureClassName.ShapeType = pFc.ShapeType
pFeatureClassName.ShapeFieldName = pFc.ShapeFieldName
'Export selected features
Set pExportOp = New ExportOperation
pExportOp.ExportFeatureClass pInDsName, Nothing, pSelSet, Nothing, pOutDatasetName, 0
End Sub
/// <summary> /// selects a feature from a specific featureclass /// </summary> /// <param name="myMap">map control</param> /// <param name="player">layer to search on in the map control</param> /// <param name="searchString">search criteria</param> public void selectFeature(AxMapControl myMap, ILayer player, string searchString) { pFeatureLayer = (IFeatureLayer)player; if (pFeatureLayer != null) { //select the feature IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = searchString; IFeatureSelection featureSelection = (IFeatureSelection)pFeatureLayer; myMap.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, pFeatureLayer, null); featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } return; }
/// <summary> /// Exports the selected features of a layer in the Map Control to a Shapefile /// </summary> /// <param name="pLayer">mapcontrol layer</param> /// <param name="inputPath">input shapefile full path</param> /// <param name="outputPath">output shapefile full path</param> /// <param name="qry">query to filter out features</param> public void ExportMapLyrToShapefile(ILayer pInLayer, string inputName, string outputPath, string qry) { // Set the GeoFeatureLayer to the layer in the map so that we can use it as the input feature later IGeoFeatureLayer pInGFLayer = (IGeoFeatureLayer)pInLayer; IFeatureLayer pInFeatureLayer = (IFeatureLayer)pInGFLayer; IFeatureClass inputFeatureClass = pInFeatureLayer.FeatureClass; IDataset inputDataset = (IDataset)inputFeatureClass; IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName; // Get the layer's selection set (the selected/highlighted features of the layer in the mapcontrol) IFeatureSelection featureSelection = (IFeatureSelection)pInFeatureLayer; ISelectionSet selectionSet = featureSelection.SelectionSet; // Create a feature class name for the output shapefile and open the output's workspace IWorkspaceFactory shpWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace shpWorkspace = shpWorkspaceFactory.OpenFromFile(outputPath, 0); IDataset shpWorkspaceDataset = (IDataset)shpWorkspace; IWorkspaceName workspaceName = (IWorkspaceName)shpWorkspaceDataset.FullName; IFeatureClassName shpFeatureClassName = new FeatureClassNameClass(); IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName; shpDatasetName.WorkspaceName = workspaceName; shpDatasetName.Name = qry.Replace(" = ", ""); // Use the IFieldChecker interface to make sure all of the field names are valid for the output shapefile. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields shapefileFields = null; IEnumFieldError enumFieldError = null; fieldChecker.InputWorkspace = inputDataset.Workspace; fieldChecker.ValidateWorkspace = shpWorkspace; fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields); // We also need to retrieve the GeometryDef from the input feature class. int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName); IFields inputFields = inputFeatureClass.Fields; IField shapeField = inputFields.get_Field(shapeFieldPosition); IGeometryDef geometryDef = shapeField.GeometryDef; //// Create a query filter to select specific features in the input featureclass //IQueryFilter queryFilter = new QueryFilterClass(); //queryFilter.WhereClause = qry; // Now we create a feature data converter to "Export" the selected features to a shapefile IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null, selectionSet, null, shpFeatureClassName, geometryDef, shapefileFields, "", 1000, 0); }
Now, the only issue i'm running into is that I have a table joined to the map layer and those joined fields are not being "exported"! So far no luck on this particular issue.
If anyone has any info on how I can include my joined table's fields in the "Export"... please help!
Derek
Take a look at using IDisplayTable for your source.
public void PerformTest5() { // Get the selection in TOC. object item = ArcMap.Document.SelectedItem; // Make sure it is a feature layer if (item is IFeatureLayer) { IFeatureLayer featureLayer = item as IFeatureLayer; IDisplayTable displayTable = featureLayer as IDisplayTable; ISelectionSet selectionSet = displayTable.DisplaySelectionSet; IDataset dataset = displayTable.DisplayTable as IDataset; IDatasetName datasetName = dataset.FullName as IDatasetName; IWorkspaceFactory outShapeWSF = new ShapefileWorkspaceFactoryClass(); IDataset outShapeWS = outShapeWSF.OpenFromFile("C:\\GIS", 0) as IDataset; IWorkspaceName outShapeWSName = outShapeWS.FullName as IWorkspaceName; IDatasetName outShapeName = new FeatureClassNameClass(); outShapeName.Name = "Export_test1"; outShapeName.WorkspaceName = outShapeWSName; IExportOperation exportOperation = new ExportOperationClass(); exportOperation.ExportFeatureClass(datasetName, null, selectionSet, null, outShapeName as IFeatureClassName, 0); } else { MessageBox.Show("Please select a feature layer."); } }
This works well when I export to a file geodatabase.
What part of code do I have to modify if I wish to export it to a featuredataset within a file geodatabase?