' Table.Modify ' Renames or modifies fields in a Table document ' Assumes that the base table is a shapefile or dBase (.DBF) file ' Saves the old table with a .BAK extension ' At this point, only DECIMAL and CHAR fields are supported '**** see if directory and dBase file write-protected theTable = av.GetActiveDoc tabName = theTable.GetName tabExt = theTable.GetWin.ReturnExtent tabOrigin = theTable.GetWin.ReturnOrigin theVTab = theTable.GetVTab theFName = theVTab.GetBaseTableFileName theDir = theFName.ReturnDir theTitle = "Modify" if (File.IsWritable(theDir).Not) then MsgBox.Error("Cannot write to directory.",theTitle) return nil end if (File.IsWritable(theFName).Not) then MsgBox.Error("File is not writable.",theTitle) return nil end '**** get list of field names fl = List.Make for each f in theVTab.GetFields fnm = f.GetName ft = f.GetType.AsString.AsTokens("_").Get(1) if ((ft = "CHAR") or (ft = "DECIMAL")) then fl.Add(fnm.AsString) end end '**** get modifications f_list = List.Make modlist = List.Make modfield = true theMsg = "Enter modifications" theLabels = {"Name","Type","Width","Precision"} while (modfield) f = MsgBox.ChoiceAsString(fl,"Select field to modify",theTitle) if (f = nil) then return nil elseif (f_list.FindByValue(f) > -1) then MsgBox.Info("Field already selected",theTitle) else theField = theVTab.FindField(f) theType = theField.GetType.AsString.AsTokens("_").Get(1) theWidth = theField.GetWidth.AsString thePrecision = theField.GetPrecision.AsString theDefaults = {f,theType,theWidth,thePrecision} result = MsgBox.MultiInput(theMsg,theTitle,theLabels,theDefaults) if (result = nil) then return nil end newName = result.Get(0) nt = result.Get(1) if ((nt = "CHAR") or (nt = "DECIMAL")) then newType = ("#FIELD_"+nt).AsEnum else MsgBox.Error("Unsupported type",theTitle) return nil end newWidth = result.Get(2).AsNumber newPrecision = result.Get(3).AsNumber f_list.Add(f) modlist.Add({newName,newType,newWidth,newPrecision}) end modfield = MsgBox.MiniYesNo("Modify another field?",false) end '**** generate field list oldFields = List.Make newFields = List.Make for each f in theVTab.GetFields fnm = f.GetName if (fnm <> "Shape") then i = f_list.FindByValue(fnm) if (i > -1) then nm = modlist.Get(i).Get(0) nt = modlist.Get(i).Get(1) nw = modlist.Get(i).Get(2) np = modlist.Get(i).Get(3) nf = Field.Make(nm,nt,nw,np) newFields.Add(nf) else newFields.Add(f.Clone) end oldFields.Add(f) end end '**** generate scratch file tmpFName = theDir.MakeTmp("tmp","dbf") tmpVTab = VTab.MakeNew(tmpFName,dBase) tmpVTab.AddFields(newFields) '**** populate records i_last = newFields.Count - 1 numdone = 0 numrec = theVTab.GetNumRecords for each r in theVTab r2 = tmpVTab.AddRecord for each i in 0..i_last of = oldFields.Get(i) nf = newFields.Get(i) v = theVTab.ReturnValue(of,r) if (nf.IsTypeString) then if (v.Is(Number)) then theVal = v.AsString else theVal = v end else if (v.Is(String)) then theVal = v.AsNumber else theVal = v end end tmpVTab.SetValue(nf,r2,theVal) end numdone = numdone + 1 av.SetStatus(numdone / numrec * 100) end tmpVTab.Flush tmpVTab.DeActivate tmpVTab = nil theVTab.DeActivate theVTab = nil av.GetProject.RemoveDoc(theTable) theTable = nil av.PurgeObjects av.ClearStatus '**** rename files bakFName = FileName.Make(theFName.AsString) bakFName.SetExtension("bak") File.Copy(theFName,bakFName) File.Copy(tmpFName,theFName) File.Delete(tmpFName) '**** create new table doc theVTab = VTab.Make(theFName,false,false) theTable = Table.Make(theVTab) av.GetProject.AddDoc(theTable) theTable.SetName(tabName) theTable.GetWin.MoveTo(tabOrigin.GetX,tabOrigin.GetY) theTable.GetWin.Resize(tabExt.GetX,tabExt.GetY) theTable.GetWin.Activate return nil