This sample will save a spatial adjustment limit adjust area to a text file and read a text file back in.
This sample takes a limit adjust polygon and saves the coordinates out as a text file, and also reads a text file with the x and y coordinates of a polygon and create a limit adjust polygon.
How to use
- Paste the code into VBA
- Start Editing
- Create a limit adjust area, and run the sub routine LapTxt() to save to text file
- Run the sub routine TxtLap() to create a limit adjust area from a text file
Public Sub TxtLap()
'Create limit adjust polygon from text file
Dim pGonColl As IPointCollection
Dim pPoint As IPoint
Dim pGxDialog As IGxDialog
Dim pGxObjFilter As IGxObjectFilter
Dim pGxObject As IGxObject
Dim pFileSel As IEnumGxObject
Dim pAdjust As IAdjustment
Dim pMXDoc As IMxDocument
Dim pFirstPoint As IPoint
Dim iCount As Integer
Dim dX As Double
Dim dY As Double
'Select text file
Set pGxDialog = New GxDialog
pGxDialog.Title = "Select File"
Set pGxObjFilter = New GxFilterTextFiles
Set pGxDialog.ObjectFilter = pGxObjFilter
If (pGxDialog.DoModalOpen(Application.hWnd, pFileSel) = True) Then
'Open text file
Set pMXDoc = ThisDocument
Set pGonColl = New Polygon
Set pGxObject = pFileSel.Next
Open pGxObject.FullName For Input As #1
Do Until EOF(1)
'Read each line and get X, Y of polygon vertices
Input #1, dX, dY
Set pPoint = New Point
pPoint.PutCoords dX, dY
If iCount = 0 Then
Set pFirstPoint = pPoint
End If
'Add each vertex to a polygon
pGonColl.AddPoint pPoint
iCount = iCount + 1
Loop
Close #1
'Exit if there are less than 3 points in the text file
If pGonColl.PointCount < 3 Then
MsgBox "You need at least three points to create limit adjust polygon"
Exit Sub
End If
pGonColl.AddPoint pFirstPoint
'Get adjustment extension
Set pAdjust = Application.FindExtensionByName("ESRI Adjustment Tools")
'Create limit adjust polygon using current polygon
Set pAdjust.LimitedAdjustmentArea = pGonColl
pMXDoc.ActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
End If
End Sub
Public Sub LapTxt()
'Read the vertices from limit adjust polygon to text file
Dim pUid As New UID
Dim pAdjust As IAdjustment
Dim pGxDialog As IGxDialog
Dim pObjFilter As IGxObjectFilter
Dim pPointColl As IPointCollection
Dim pEnumVertex As IEnumVertex
Dim pPoint As IPoint
Dim sFileName As String
Dim lPart As Long
Dim lVertex As Long
Dim i As Integer
'Get Adjustment extension
pUid = "esriEditorExt.Adjustment"
Set pAdjust = Application.FindExtensionByCLSID(pUid)
'Exit if no limit adjust area
If pAdjust.LimitedAdjustmentArea Is Nothing Then
MsgBox "Limit adjust area is not defined", vbOKOnly, "LAP to text file"
Exit Sub
End If
'Create or select a text file to save limit adjust polygon
Set pGxDialog = New GxDialog
pGxDialog.Title = "Save LAP"
Set pObjFilter = New GxFilterTextFiles
Set pGxDialog.ObjectFilter = pObjFilter
If (pGxDialog.DoModalSave(Application.hWnd) = True) Then
'Get vertices of limit adjust area
Set pPointColl = pAdjust.LimitedAdjustmentArea
Set pEnumVertex = pPointColl.EnumVertices
pEnumVertex.Reset
'Save the file as .txt file
sFileName = pGxDialog.FinalLocation.FullName + "\" + pGxDialog.Name
If StrComp(Right(pGxDialog.Name, 4), ".txt") <> 0 Then
sFileName = sFileName + ".txt"
End If
'Save each point in each line of the text file
Open sFileName For Output As #1
For i = 1 To pPointColl.PointCount - 1
pEnumVertex.Next pPoint, lPart, lVertex
Print #1, pPoint.X; vbTab; pPoint.Y
Next i
Close #1
End If
End Sub