' Name: View.AddXYCoordToFTab ' ' Title: Adds X and Y coordinates of features to Attribute Table ' ' Topics: GeoData ' ' Description: Adds two new fields, named X-coord and Y-coord, to the table ' of the first active theme in the TOC and fills the respective fields with ' the X,Y coordinates of the selected points (or all points if no selection ' is defined) in a point theme. If instead the active theme is a polygon ' theme, then the X,Y coordinates of the polygon centroid are calculated. If ' the theme is projected, the output coordinates will also be projected. ' ' Requires: An active point or polygon theme. This script does minimal ' error checking and assumes that there is an active theme. ' ' Self: ' ' Returns: theView = av.GetActiveDoc 'must be global to work in Calc exp below _theProjection = theView.GetProjection project_flag = _theProjection.IsNull.Not 'true if projected theTheme = theView.GetActiveThemes.Get(0) 'Check if point or polygon theme if (((theTheme.GetSrcName.GetSubName = "point") or (theTheme.GetSrcName.GetSubName = "polygon")).Not) then MsgBox.Info("Active theme must be polygon or point theme","") exit end 'get the theme table and current edit state theFTab = theTheme.GetFTab theFields = theFTab.GetFields edit_state = theFTab.IsEditable 'make sure table is editable and that fields can be added if (theFtab.CanEdit) then theFTab.SetEditable(true) if ((theFTab.CanAddFields).Not) then MsgBox.Info("Can't add fields to the table."+NL+"Check write permission.", "Can't add X,Y coordinates") exit end else MsgBox.Info("Can't modify the feature table."+NL+ "Check write permission.","Can't add X,Y coordinates") exit end 'Check if fields named "X-coord" and Y-coord" exist x_exists = (theFTab.FindField("X-coord") = NIL).Not y_exists = (theFtab.FindField("Y-coord") = NIL).Not if (x_exists or y_exists) then if (MsgBox.YesNo("Overwrite existing fields?", "X-coord, Y-coord fields already exist", false)) then 'if ok to overwrite, delete the fields as they may not be defined 'as required by this script (eg., created from another script). if (x_exists) then theFTab.RemoveFields({theFTab.FindField("X-coord")}) end if (y_exists) then theFTab.RemoveFields({theFTab.FindField("Y-coord")}) end else exit end 'if (MsgBox...) end 'if x = Field.Make ("X-coord",#FIELD_DECIMAL,18,5) y = Field.Make ("Y-coord",#FIELD_DECIMAL,18,5) theFTab.AddFields({x,y}) 'Get point coordinates or polygon centroid coordinates if (theTheme.GetSrcName.GetSubName = "point") then if (project_flag) then 'Projection defined theFTab.Calculate("[Shape].ReturnProjected(_theProjection).GetX", x) theFTab.Calculate("[Shape].ReturnProjected(_theProjection).GetY", y) else 'No projection defined theFTab.Calculate("[Shape].GetX", x) theFTab.Calculate("[Shape].GetY", y) end 'if else 'polygon case if (project_flag) then theFTab.Calculate("[Shape].ReturnCenter.ReturnProjected(_theProjection).GetX", x) theFTab.Calculate("[Shape].ReturnCenter.ReturnProjected(_theProjection).GetY", y) else theFTab.Calculate("[Shape].ReturnCenter.GetX", x) theFTab.Calculate("[Shape].ReturnCenter.GetY", y) end ' if end 'Return editing state to pre-script running state theFTab.SetEditable(edit_state)