Creating A Custom Layer

Description:

This sample demonstrates how to create a custom layer. The layer has as its data source, a text file containing a list of comma-delimited XY coordinates of points.

How to use:
  1. Resiter the XYPointLayer.dll on your machine.
  2. Start a new blank ArcMac document.
  3. In the Visual Basic Editor, go to Tools > References. If this sample's dll does not appear in the Available References box, Browse to the dll and add it. Check the box next to the dll's name and dismiss the References dialog.
  4. Copy-paste the following procedure into VBA Make sure you have xyPoints.txt file in the location specified.
    Sub Test_PointLayer()
      Dim pLyr As New XYPointLayer.PointLayer
      pLyr.XYFileName = "c:\arcgis\arcexe83\ArcObjects Developer Kit\Samples\ArcMap\Layers\Custom Layer\xypoints.txt"
      
      Dim pL As ILayer
      Set pL = pLyr
      pL.Name = "MyXYPointLayer"
    
      Dim pDoc As IMxDocument
      Dim pMap As IMap
      Set pDoc = ThisDocument
      Set pMap = pDoc.FocusMap
      pMap.AddLayer pLyr
      pDoc.ActiveView.Refresh
    End Sub
    If you want to create a file with comma-delimited XY coordinates of all the points in your point featureclass, you can use the procedure below.
    Private Sub WriteXYPointsFile()
      Dim pWSFact As IWorkspaceFactory
      Dim pFeatWS As IFeatureWorkspace
      Dim pFeatCls As IFeatureClass
      Dim pFeatCur As IFeatureCursor
      Dim pFeat As IFeature
      Dim pPt As IPoint
      Dim lFreeFile As Long
      Dim sXY As String
      Dim x As Double, y As Double
      
      Set pWSFact = New AccessWorkspaceFactory
      Set pFeatWS = pWSFact.OpenFromFile("E:\data\access\usa\usa.mdb", 0)
      Set pFeatCls = pFeatWS.OpenFeatureClass("Capitals")
      Set pFeatCur = pFeatCls.Search(Nothing, True)
      
      lFreeFile = FreeFile
      Open "C:\Temp\xyPoints.txt" For Output As #lFreeFile
      
      Do
        Set pFeat = pFeatCur.NextFeature
        If Not pFeat Is Nothing Then
          Set pPt = pFeat.Shape
          pPt.QueryCoords x, y
          sXY = CStr(x) + ", " + CStr(y)
          Print #lFreeFile, sXY
        End If
      Loop Until pFeat Is Nothing
      
      Close #lFreeFile
    End Sub
  5. Run the Test_PointLayer macro. You should see the points from your text file displayed in the map. Save your document as "customlayer.mxd". Open this document and see that the layer has been persisted.

Application:
ArcMap

Difficulty: Advanced


Visual Basic
File Description
PointLayer.cls Main form containing the mapping application.
xypoints.txt Text file.
XYPointLayer.vbp Visual Basic Project.
XYPointLayer.dll This sample's dll.


Key Interfaces: ILayer, IPersistVariant, IPoint, IEnvelope