gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:1881回复:0

自动计算polyline的起点和终点坐标放置到字段中

楼主#
更多 发布于:2003-10-22 10:49
自动计算polyline的起点和终点坐标,放置到各自的字段中
It is not possible to create a field that automatically calculates values but it is possible to do this in VBA. I have included the code here

To use this code

First paste the entire code in the Visual Basic Editor

Then before you start adding features just run the StartListeningtoEditEvents Subroutine

I have named the fields that contain the coordinates as FromXPoint, FromYPoint, ToXPoint, and ToYPoint. They are self explanatory. For eg. the field FromXPoint will contain the X Coordinate of the From Point

now when you add a feature those fields will get updated automatically.

if you dont want one of the fields just delete the appropriate lines

cheers

  
  Option Explicit

Private WithEvents Editevents As Editor

Public Sub StartListeningToEditEvents()
  Set Editevents = Application.FindExtensionByName("ESRI Object Editor"
End Sub



Public Sub StopListeningToEditEvents()
  Set Editevents = Nothing
End Sub

Private Sub EditEvents_OnCreateFeature(ByVal pObj As IObject)
  Dim fxFieldIndex As Long
  Dim fyFieldIndex As Long
  Dim txFieldIndex As Long
  Dim tyFieldIndex As Long
  
   'Change "FromXPoint" to the appropriate field name this is the field that should contain the
  'X Coordinate of the from point and the other fieldnames have to modified as needed
  fxFieldIndex = pObj.Fields.FindField("FromXPoint"
  fyFieldIndex = pObj.Fields.FindField("FromYPoint"
  txFieldIndex = pObj.Fields.FindField("ToXPoint"
  tyFieldIndex = pObj.Fields.FindField("ToYPoint"
  
  Dim pObjectClass As IObjectClass
  Set pObjectClass = pObj.Class
  
  Dim pOID As Long
  pOID = pObj.OID
  
  Dim pFeatureClass As IFeatureClass
  Set pFeatureClass = pObjectClass
  
  Dim pFeature As IFeature
  Set pFeature = pFeatureClass.GetFeature(pOID)

     Dim pCurve As ICurve
     Set pCurve = pFeature.Shape
    
     pObj.Value(fxFieldIndex) = pCurve.FromPoint.x
     pObj.Value(fyFieldIndex) = pCurve.FromPoint.y
     pObj.Value(txFieldIndex) = pCurve.ToPoint.x
     pObj.Value(tyFieldIndex) = pCurve.ToPoint.y
      
  
  
End Sub

Private Sub EditEvents_OnChangeFeature(ByVal pObj As IObject)
  Dim fxFieldIndex As Long
  Dim fyFieldIndex As Long
  Dim txFieldIndex As Long
  Dim tyFieldIndex As Long
  
  'Change "FromXPoint" to the appropriate field name this is the field that should contain the
  'X Coordinate of the from point and the other fieldnames have to modified as needed
  
  fxFieldIndex = pObj.Fields.FindField("FromXPoint"
  fyFieldIndex = pObj.Fields.FindField("FromYPoint"
  txFieldIndex = pObj.Fields.FindField("ToXPoint"
  tyFieldIndex = pObj.Fields.FindField("ToYPoint"
  
  Dim pObjectClass As IObjectClass
  Set pObjectClass = pObj.Class
  
  Dim pOID As Long
  pOID = pObj.OID
  
  Dim pFeatureClass As IFeatureClass
  Set pFeatureClass = pObjectClass
  
  Dim pFeature As IFeature
  Set pFeature = pFeatureClass.GetFeature(pOID)

     Dim pCurve As ICurve
     Set pCurve = pFeature.Shape
    
     pObj.Value(fxFieldIndex) = pCurve.FromPoint.x
     pObj.Value(fyFieldIndex) = pCurve.FromPoint.y
     pObj.Value(txFieldIndex) = pCurve.ToPoint.x
     pObj.Value(tyFieldIndex) = pCurve.ToPoint.yEnd Sub
End Sub
喜欢0 评分0
GIS麦田守望者,期待与您交流。
游客

返回顶部