阅读:1983回复:1
请教:大家是如何用Engine实现节点捕捉功能的?
谁告诉我Engine提供了现成的节点捕捉接口的(Esri公司告诉我说没有)
|
|
1楼#
发布于:2005-11-16 11:49
<P>基本上要自己写代码来实现,下面是我从程序里摘取的一个函数,原理差不多就是空间查询</P>
<P>Public Sub Snap()<BR> Dim i As Long<BR> Dim j As Long<BR> Dim pFeatureLayer As IFeatureLayer<BR> Dim pFilter As ISpatialFilter<BR> Dim pCursor As IFeatureCursor<BR> Dim pFeature As IFeature<BR> Dim pGeometry As IGeometry<BR> Dim pPointcollection As IPointCollection<BR> Dim pCollection As Collection<BR> Dim Dist As Double<BR> Dim pPoint As IPoint<BR> Dim pline As Polyline<BR> Dim pPolyline As ISegmentCollection<BR> Dim penv As IEnvelope</P> <P> Dim everySeg As ISegment<BR> Dim pToPo As ITopologicalOperator<BR> Dim pRelOP As IRelationalOperator</P> <P>'On Error GoTo ErrorHandler<BR> If m_pSnapType = MAP_SNAP_MODE_NONE Then<BR> Set searchedPoint = Nothing<BR> lastSnapType = MAP_SNAP_MODE_NONE<BR> Exit Sub<BR> End If</P> <P> Set searchedPoint = Nothing<BR> Set pPointcollection = pline<BR> Set m_pGeometrySelection = New Collection<BR> Dist = DmpConvertPixelsToRW(m_pTolenrance)<BR> lastSnapType = 0<BR> <BR> Set penv = New envelope</P> <P> penv.XMin = m_pPoint.x - Dist<BR> penv.XMax = m_pPoint.x + Dist<BR> penv.YMin = m_pPoint.y - Dist<BR> penv.YMax = m_pPoint.y + Dist<BR> Set pBoundary = penv</P> <P> Set pPolyline = New Polyline<BR> Dim newLine As ILine</P> <P> Set newLine = New esriGeometry.Line<BR> newLine.PutCoords penv.UpperLeft, penv.UpperRight<BR> pPolyline.AddSegment newLine<BR> <BR> Set newLine = New esriGeometry.Line<BR> newLine.PutCoords penv.UpperRight, penv.LowerRight<BR> pPolyline.AddSegment newLine<BR> <BR> Set newLine = New esriGeometry.Line<BR> newLine.PutCoords penv.LowerRight, penv.LowerLeft<BR> pPolyline.AddSegment newLine<BR> <BR> Set newLine = New esriGeometry.Line<BR> newLine.PutCoords penv.LowerLeft, penv.UpperLeft<BR> pPolyline.AddSegment newLine</P> <P> Set pToPo = m_pPoint<BR> Set pRelOP = pToPo.Buffer(Dist)<BR> Set pPolyline = Nothing</P> <P>'选择实体</P> <P> Set m_pGeometrySelection = SelectFeatureByEnv(penv)<BR>'查询捕捉点<BR>Set searchedPoint = Nothing<BR>lastSnapType = MAP_SNAP_MODE_NONE<BR> For i = 1 To m_pGeometrySelection.Count<BR> Set pGeometry = m_pGeometrySelection.Item(i)<BR> If pGeometry.GeometryType = esriGeometryPolygon Then<BR> Set pToPo = pGeometry<BR> Set pGeometry = pToPo.Boundary<BR> End If<BR> <BR> If pRelOP.Crosses(pGeometry) Or pRelOP.Contains(pGeometry) Then<BR> <BR> If pGeometry.GeometryType = esriGeometryPolyline Or pGeometry.GeometryType = esriGeometryPolygon Then '线状或面状实体<BR> <BR> If pGeometry.GeometryType = esriGeometryPolygon Then<BR> Set pToPo = pGeometry<BR> Set pGeometry = pToPo.Boundary<BR> End If<BR> Set pPolyline = pGeometry<BR> <BR> If m_pSnapType = MAP_SNAP_MODE_NONE Then<BR> Set searchedPoint = Nothing<BR> lastSnapType = MAP_SNAP_MODE_NONE<BR> End If<BR> <BR> '捕捉端点<BR> If (m_pSnapType And 1) = 1 Then<BR> Set pPointcollection = pGeometry<BR> If Not pPointcollection Is Nothing Then<BR> For j = 0 To pPointcollection.PointCount - 1<BR> Set pPoint = pPointcollection.Point(j)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_ENDPOINT<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_ENDPOINT<BR> End If<BR> End If<BR> Next j<BR> End If<BR> End If<BR> <BR> '捕捉中点,圆心,垂点,切点<BR> If (m_pSnapType And 2) = 2 Or (m_pSnapType And 4) = 4 Or (m_pSnapType And 32) = 32 Or (m_pSnapType And 64) = 64 Or (m_pSnapType And 16) = 16 Then<BR> For j = 0 To pPolyline.SegmentCount - 1<BR> Set everySeg = pPolyline.Segment(j)<BR> <BR> Dim qgeo As ISegmentCollection<BR> Set qgeo = New Polyline<BR> qgeo.AddSegment everySeg<BR> <BR> If pRelOP.Crosses(qgeo) Or pRelOP.Contains(qgeo) Then<BR> <BR> '捕捉中点<BR> If (m_pSnapType And 2) = 2 Then<BR> Set pPoint = Nothing<BR> Set pPoint = computeMidPoint(everySeg)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_MIDPOINT<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_MIDPOINT<BR> End If<BR> End If<BR> End If<BR> <BR> '捕捉圆心<BR> If (m_pSnapType And 4) = 4 Then<BR> Set pPoint = Nothing<BR> Set pPoint = computeCenter(everySeg)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) <= m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_CENTER<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_CENTER<BR> End If<BR> End If<BR> End If<BR> '捕捉垂点<BR> If (m_pSnapType And 32) = 32 Then<BR> Set pPoint = Nothing<BR> Set pPoint = computePerpendicular(everySeg, otherPoint)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_PERPENDICULAR<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_PERPENDICULAR<BR> End If<BR> End If<BR> End If<BR> '捕捉切点<BR> If (m_pSnapType And 64) = 64 Then<BR> Set pPoint = Nothing<BR> Set pPoint = computeTangent(everySeg, otherPoint)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_TANGENT<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_TANGENT<BR> End If<BR> End If<BR> End If<BR> '捕捉交叉点<BR> If (m_pSnapType And 16) = 16 Then<BR> Set pPoint = Nothing<BR> Set pPoint = computeIntersection(everySeg)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_INTERSECTION<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_INTERSECTION<BR> End If<BR> End If<BR> End If<BR> If m_pSnapType = MAP_SNAP_MODE_NONE Then<BR> Set pPoint = Nothing<BR> Set pPoint = computePerpendicular(everySeg, m_pPoint)<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_NEAREST<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_NEAREST<BR> End If<BR> End If<BR> End If<BR> <BR> End If<BR> Next j<BR> End If<BR> <BR> <BR> <BR> ElseIf pGeometry.GeometryType = esriGeometryPoint Then '点状实体<BR> If (m_pSnapType And 8) = 8 Then<BR> Set pPoint = pGeometry<BR> If Not pPoint Is Nothing Then<BR> If Not searchedPoint Is Nothing Then<BR> If m_pMap.ComputeDistance(m_pPoint, pPoint) < m_pMap.ComputeDistance(m_pPoint, searchedPoint) Then<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_NODE<BR> End If<BR> Else<BR> Set searchedPoint = pPoint<BR> lastSnapType = MAP_SNAP_MODE_NODE<BR> End If<BR> End If<BR> Else<BR> Set searchedPoint = Nothing<BR> lastSnapType = MAP_SNAP_MODE_NONE<BR> End If<BR> End If<BR> End If<BR> Next i</P> <P> If Not searchedPoint Is Nothing Then<BR> Set lastSnapSymbolShape = SnapSymbolShape(lastSnapType)<BR> End If<BR> Exit Sub<BR>'ErrorHandler:<BR>'MsgBox "执行snap时产生错误" ; vbCrLf ; Err.Number ; ": " ; Err.Description<BR>End Sub</P> |
|
|