IRubberBand接口以及用法代码实例

我们经常需要在项目中画地理要素(Feature)时或者画元素(Element),其实IRUbberBand接口就实现了绘制几何形体(Geometry)的方法TrackNew,以及移动一个一个几何形体的方法TrackExisting。

IRUbberBand接口有两个方法:1,TrackExisting方法;2,TrackNew方法;

下面是代码程序例子,请大家参考(VBA):
(1)   Dim pmxd As IMxDocument
    Set pmxd = ThisDocument
    Dim pRudder As IRubberBand
    Set pRudder = New RubberEnvelope‘’创建一个新对象Envelope对象
    ‘’使用TrackNew方法创建一个Envelope并赋予地图的范围
    pmxd.ActiveView.Extent = pRudder.TrackNew(pmxd.ActiveView.ScreenDisplay, Nothing)''返回的是IGeometry类型
    pmxd.ActiveView.Refresh

(2)在VBA下添加一个Itoolcommand,并添加如下代码
If button = 1 Then ' 左键按下创建新的多边形
    ‘’调用tracknew方法
    Set pPoly = pRubberPoly.TrackNew(pMXDoc.ActiveView.ScreenDisplay, Nothing)
    If Not pPoly Is Nothing Then
      '创建新的元素,并把tracknew所绘制的多边形赋予这个元素的几何对象
      Set pElem = New PolygonElement
      pElem.Geometry = pPoly
     
      '添加到地图中
      pGraCont.AddElement pElem, 0
    End If
  Elseif button =2 then‘’如何用户按下右键,那么就调用trackexisting方法,拖拽选用的元素
  
    Set pGraContSel = pGraCont
    '确定选中了一个元素
    If pGraContSel.ElementSelectionCount > 0 Then
      If pGraContSel.ElementSelectionCount = 1 Then‘’选中一个
        Set pElem = pGraContSel.SelectedElement(0)  ‘’获取选中的元素
      ElseIf pGraContSel.ElementSelectionCount > 1 Then‘’选中多个
        Set pElem = pGraContSel.DominantElement     ‘’获取选中元素集合中的一个
      End If
      ' 判断是多边形元素
      If TypeOf pElem Is IPolygonElement Then
        ' 创建一个rubberpolygon对象
        Set pRubberPoly = New RubberPolygon
        ‘’获取元素的几何形体
        Set pPoly = pElem.Geometry
        '使用TrackExisting方法移动选中的多边形元素(引用传递)
        pRubberPoly.TrackExisting pMXDoc.ActiveView.ScreenDisplay, Nothing, pPoly
        ' 将移动过的多边形再次赋给元素的几何对象
        pElem.Geometry = pPoly
        '更新
        pGraCont.UpdateElement pElem
      End If
    End If
  End If

需要注意:本接口在移动几何对应TrackExisting方法,必须是使用本接口的TrackNew方法创建的对象才能生效,它不能识别其它方法创建的IGeometry。这是一个值得注意的地方。具体为什么还不太清楚,可能是识别上的问题。