I'm having similar problems, but with the ItopologicalOperator intersect. I have tried the suggestions here about simplification, which helped, but, in the end, the code still errors with an unknown COMException.The basic idea is to find the distance to the nearest line feature in a single direction. This is accomplished by creating a line segment from the starting point to another point 10000000m away along the direction specified and intersecting with the line feature class. The distance to all intersections is calculated, and the minimum is stored.The code will work for about 3000 features then crash, when I restart it works for the feature it crashed on (it only processes features not processed yet). Any suggestions would be welcomed. 'Set these values at the start
Dim azimuthField, distanceField As Long
azimuthField = form.inputField
distanceField = form.OutputField
' Get map object
Dim mxd As IMxDocument
Dim map As IMap
mxd = CType(m_application.Document, IMxDocument)
map = mxd.FocusMap
'Get feature classes, first is points, then borders
Dim ptLayer As IFeatureLayer
Dim borderLayer As IFeatureLayer
Dim ptClass As IFeatureClass
Dim borderClass As IFeatureClass
ptLayer = form.InputLayer
ptClass = ptLayer.FeatureClass
borderLayer = form.BorderLayer
borderClass = borderLayer.FeatureClass
' loop through each point feature
Dim featCur As IFeatureCursor
' This is a search to ensure that only non-processed features are processed
Dim filt As IQueryFilter = New QueryFilter()
Dim f As IField = ptClass.Fields.Field(distanceField)
filt.WhereClause = f.Name + " = 0"
featCur = ptClass.Update(filt, True)
Dim feat As IFeature
feat = featCur.NextFeature()
Dim pt As IPoint
Dim azimuth, dist, i As Double ' This will store the azimuth direction
Dim maxDist As Double
Dim tempSegs As ISegmentCollection = New Polyline()
Dim tempSegsGeog As IGeometry
Dim tempFeat As IFeature
Dim topo As ITopologicalOperator5
Dim prox As IProximityOperator
Dim geomTemp As IGeometry
maxDist = 100000000
Try
While Not feat Is Nothing
maxDist = 100000000
pt = feat.ShapeCopy
azimuth = CDbl(feat.Value(azimuthField))
Dim tempLine As ILine
' This function creates the temporary line feature used to intersect
tempLine = createTempLine(pt, CDbl(azimuth))
'The line needs to be wrapped in a polyline to fit in the spatial filter
If tempSegs.SegmentCount > 0 Then
For i = 0 To tempSegs.SegmentCount - 1
tempSegs.RemoveSegments(i, 1, False)
Next
End If
tempSegs.AddSegment(tempLine)
tempSegsGeog = tempSegs
tempSegsGeog.SpatialReference = feat.Shape.SpatialReference
Dim spatial As ISpatialFilter = New SpatialFilter()
spatial.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
spatial.Geometry = tempSegs
' A spatial query is used to get the intersetion points, I thought this is where the memeory leak was happening, but I don't think so anymore (after examining the call stack
Dim tempFeatCur As IFeatureCursor = borderClass.Search(spatial, True)
tempFeat = tempFeatCur.NextFeature()
While Not tempFeat Is Nothing
topo = tempFeat.Shape
topo.IsKnownSimple_2 = False
topo.Simplify() ' attempt to fix intermitent error following http://forums.arcgis.com/threads/1992-ITopological-Operator-Intermittent-COMException-on-Union
'This is where the error is occuring, at least it is for the first time
geomTemp = topo.Intersect(tempSegsGeog, esriGeometryDimension.esriGeometry0Dimension)
prox = geomTemp
dist = prox.ReturnDistance(pt)
If dist < maxDist Then
maxDist = dist
End If
tempFeat = tempFeatCur.NextFeature()
End While
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempFeatCur)
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempLine)
feat.Value(distanceField) = maxDist
featCur.UpdateFeature(feat)
feat = featCur.NextFeature()
End While
Catch ex As System.Runtime.InteropServices.COMException
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
System.Windows.Forms.MessageBox.Show("Finished Calculating Distances")
End If