Monday, May 18, 2009

Add a Shape file Programatically


Dear all

find this steps to understand the arcobject code for Adding shape file programatically

This sample, which can easily be changed to support different data types, opens a shapefile on your local disk and adds the contents to the map as a feature layer. In this sample, you will be adding a control button and writing the code for it.


Start ArcMap.
Open an existing map document(.mxd) or add layers to the empty (Untitled) map document.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIButtonControl as the UIControl Type.
Click Create.
Click and drag the new Project.UIButtonControl1 in the Commands list and drop it on any toolbar.
Click Close.
Right-click the newly placed control and click View Source. This opens the Visual Basic Editor.
In the ThisDocument (Code) window, click the Procedure Box drop-down arrow (the one on the right of the window) and choose Click. This adds the wrapper code for the procedure you are creating.
Copy and paste the following code between the two wrapper code lines (between Private Sub UIButtonControl1_Click() and End Sub). Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pWorkSpace As IFeatureWorkspace
'Change C:\Source to the source location of the shapefile you wish to add
Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\GISQuery\", 0)
Dim pClass As IFeatureClass
'Change USStates to the name of the shapefile you wish to add
Set pClass = pWorkSpace.OpenFeatureClass("GISQuery")
Dim pLayer As IFeatureLayer
Set pLayer = New FeatureLayer
Set pLayer.FeatureClass = pClass
pLayer.Name = pClass.AliasName
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.AddLayer pLayer
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing
Go to line 5, Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Source", 0), and change C:\Source to the source location of the shapefile you want to add.
Go to line 8, Set pClass = pWorkSpace.OpenFeatureClass("GISQuery"), and change GISQuery to the name of the shapefile you want to add.
Close the Visual Basic Editor.
Click the new button in ArcMap to add the feature class to the map.



No comments:

Post a Comment