' Written by Timothy S. Thomas. ' Programming began 12/23/99. ' Last update was 12/23/99. ' *********************************************************************** ' This script subtracts one theme from another ' *********************************************************************** ' Beginning program theProject = av.GetProject theView = av.GetActiveDoc 'Choose themes theThemeList = theView.GetThemes mainTheme = MsgBox.ChoiceAsString(theThemeList,"Select theme to subtract from", "Select theme") subTheme = MsgBox.ChoiceAsString(theThemeList,"Select theme to use for subtracting", "Select theme") mainFTab = mainTheme.GetFTab subFTab = subTheme.GetFTab mainFTab.SetEditable(True) subFTab.SetEditable(True) mainshapeField = mainFTab.FindField("Shape") subshapeField = subFTab.FindField("Shape") ' Get filename for new shapefile, and make new shapefile FTabFN = mainFTab.GetSrcName.GetFileName baseString = FTabFN.GetBaseName extString = FTabFN.GetExtension newString = FTabFN.AsString.Substitute(baseString,"") new2String = baseString.Substitute(extString,"dbf") outFN = FileDialog.Put((newString + "Subtracted " + new2String).AsFileName, "*.shp","Output Shapefile") outFN.SetExtension("shp") outFTab = FTab.MakeNew(outFN,POLYGON) outshapeField = outFTab.FindField("Shape") ' Clone fields for outFTab inFields = mainFTab.GetFields newFields = List.Make for each f in inFields if (f.GetName <> "shape") then newFields.Add(f.Clone) end end outFTab.AddFields(newfields) 'Merging subtraction features into one feature tempfield={Field.Make ("c", #FIELD_SHORT, 2, 0)} subFTab.AddFields( tempfield ) cField=subFTab.findfield("c") sub2FN = "$HOME".AsFileName.MakeTmp("tmp","shp") sub2FTab = subFTab.Summarize(sub2FN,SHAPE,cField,{subshapeField},{#VTAB_SUMMARY_AVG}) sub2shapeField = sub2FTab.FindField("Shape") ' Create a spatial index to increase speed sub2FTab.CreateIndex(sub2shapeField) ' Add theme to table sub2Theme = FTheme.Make(sub2FTab) theView.AddTheme(sub2Theme) sub2FTab.SetEditable(True) ' Get poly shape for subtraction subPoly = sub2FTab.ReturnValue(sub2shapeField,0) ' Show message av.ShowMsg("Do subtraction") av.ShowStopButton numrecs = mainFTab.GetNumRecords ' Do subtraction, and write output for each r in mainFTab mainPoly = mainFTab.ReturnValue(mainshapeField,r).ReturnDifference(subPoly) ' Output result newrec = outFTab.AddRecord outFTab.SetValue(outshapeField,newrec,mainPoly) for each fld in mainFTab.GetFields fldName = fld.GetName if (fldName <> "shape") then outfld = outFTab.FindField(fldName) val = mainFTab.ReturnValue(fld,r) outFTab.SetValue(outfld,newrec,val) end end av.SetStatus((r+1) * 100 / numrecs) end ' Clears message av.ClearMsg ' Add to view if (MsgBox.YesNo("Add shapefile to view?","", true)) then outTheme = FTheme.Make(outFTab) theView.AddTheme(outTheme) theView.GetWin.Activate end