Assuming I have two SDEs or other datasources with the same datamodel. One contains features the other one is empty. In ArcMap I have layers from both datasources in my table of contents. Now I would like to copy some features that are already selected in the map view from one sde to the other using C# and ArcObjects. What would be the best way to do this?
Assuming that your features are non-complex, you will want to use an Insert Cursor (method 1). If your features are complex, you must use IFeature.Store() (method 2).
See the following code from this link (It's in VBA, but shouldn't take long to translate over)
Public Sub LoadFeatures() Dim pMxDoc As IMxDocument Dim pFeatureLayer As IFeatureLayer Dim pInFeatureClass As IFeatureClass Dim pOutFeatureClass As IFeatureClass Dim pQueryFilter As IQueryFilter Dim pFeatureCursor As IFeatureCursor Dim pFeature As IFeature 'For simplicity sake, load features into the feature class 'selected in the table of contents Set pMxDoc = Application.Document If pMxDoc.SelectedItem Is Nothing Then Exit Sub If Not TypeOf pMxDoc.SelectedItem Is IFeatureLayer Then Exit Sub Set pFeatureLayer = pMxDoc.SelectedItem Set pOutFeatureClass = pFeatureLayer.FeatureClass 'Open a shapefile containing the features to load into the geodatabase Set pInFeatureClass = OpenFeatureClass("d:\data\usa", "counties") If pInFeatureClass Is Nothing Then Exit Sub Set pQueryFilter = New QueryFilter Set pFeatureCursor = pInFeatureClass.Search(pQueryFilter, True) Set pFeature = pFeatureCursor.NextFeature Do While Not pFeature Is Nothing 'The next two lines control which loading method is used 'To use an insert cursor, comment out the second line 'To use CreateFeature/Store, comment out the first line InsertFeature pOutFeatureClass, pFeature.Shape 'CreateFeature pOutFeatureClass, pFeature.Shape Set pFeature = pFeatureCursor.NextFeature Loop 'Refresh the display pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, Nothing End Sub
Method 1: an insert cursor
Private Sub InsertFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry) Dim pFeatureBuffer As IFeatureBuffer Dim pFeatureCursor As IFeatureCursor Set pFeatureCursor = pFeatureClass.Insert(True) Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer Set pFeatureBuffer.Shape = pGeometry pFeatureCursor.InsertFeature pFeatureBuffer End Sub
Method 2: CreateFeature/Store
Private Sub CreateFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry) Dim pFeature As IFeature Set pFeature = pFeatureClass.CreateFeature Set pFeature.Shape = pGeometry pFeature.Store End Sub
Opening the feature class:
Public Function OpenFeatureClass(strWorkspace As String, strFeatureClass As String) As IFeatureClass On Error GoTo ErrorHandler Dim pShpWorkspaceName As IWorkspaceName Dim pDatasetName As IDatasetName Dim pName As IName 'Create the workspace name object Set pShpWorkspaceName = New WorkspaceName pShpWorkspaceName.PathName = strWorkspace pShpWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory.1" 'Create the feature class name object Set pDatasetName = New FeatureClassName pDatasetName.Name = strFeatureClass Set pDatasetName.WorkspaceName = pShpWorkspaceName 'Open the feature class Set pName = pDatasetName Set OpenFeatureClass = pName.Open Exit Function ErrorHandler: Set OpenFeatureClass = Nothing End Function
If you really want to do with using C#, you could create an add-in or GP tool. You could use search and insert cursors to actually copy the features. Or, you could call the Copy Features GP tool from C# to do the work.