阅读:1881回复:0
自动计算polyline的起点和终点坐标放置到字段中
自动计算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 |
|
|