'View.RandomPointTheme ' Generates a point theme containing random points within a region ' defined by a selected polygon feature in the active feature theme ' May be used in a projected View '**** get first selected polygon from first active theme theTitle = "Random Points" theView = av.GetActiveDoc thePrj = theView.GetProjection ThemeList = theView.GetActiveThemes if (ThemeList.Count = 0) then MsgBox.Info("No Active Theme",theTitle) return nil end theTheme = ThemeList.Get(0) if (theTheme.Is(FTheme).Not) then MsgBox.Info("Active Theme is not an FTheme",theTitle) return nil end TheFTab = theTheme.GetFTab TheType = TheFTab.GetShapeClass.GetClassName if (TheType <> "Polygon") then MsgBox.Info("Active Theme is not Polygon",theTitle) return nil end recno = theFTab.GetSelection.GetNextSet(-1) if (recno = -1) then MsgBox.Info("No feature selected",theTitle) return nil end thePolygon = theFTab.ReturnValue(theFTab.FindField("Shape"),recno) if (thePrj.IsNull.Not) then temp = thePolygon.ReturnProjected(thePrj) thePolygon = temp end '**** get number of points temp = MsgBox.Input("Number of Points:",theTitle,"1") if (temp = nil) then return nil end NumPt = temp.AsNumber '**** get tolerances temp = MsgBox.Input("Minimum Distance to Boundary",theTitle,"0") if (temp = nil) then return nil end BndTol = temp.AsNumber temp = MsgBox.Input("Minimum Distance to Another Point",theTitle,"0") if (temp = nil) then return nil end PTol = temp.AsNumber '**** get output shapefile out_name = FileName.GetCWD.MakeTmp("Theme","shp") out_name = FileDialog.Put(out_name, "*.shp", "Output Shapefile") if (out_name = nil) then return nil end '**** let's crank it!!!! PShape = FTab.MakeNew(out_name, Point) fields = List.Make fields.Add(Field.Make("ID",#FIELD_LONG,11,0)) PShape.AddFields(fields) theExtent = thePolygon.ReturnExtent X0 = theExtent.GetLeft Y0 = theExtent.GetBottom X1 = theExtent.GetRight Y1 = theExtent.GetTop numdec = NumPt.Log(10).Ceiling + 2 w = theExtent.GetWidth min theExtent.GetHeight if (w < 1) then numdec = numdec + w.Log(10).Abs.Ceiling end av.ClearMsg av.ClearStatus av.ShowMsg("Percent Done: 0%") av.ShowStopButton recno = -1 for each i in 1..NumPt Done = false iteration = 0 while (Done.Not) if ((iteration mod 100) = 0) then iteration = 0 Cancel = av.SetWorkingStatus.Not if (Cancel) then break end end u = (X1 - X0) * (10 ^ numdec) X = (Number.MakeRandom(0,u) / (10 ^ numdec)) + X0 u = (Y1 - Y0) * (10 ^ numdec) Y = (Number.MakeRandom(0,u) / (10 ^ numdec)) + Y0 thePoint = X@Y Done = true if (thePolygon.Contains(thePoint).Not) then Done = false end if (thePoint.Distance(thePolygon.AsPolyline) < BndTol) then Done = false end PShape.SelectByPoint(thePoint,PTol,#VTAB_SELTYPE_NEW) if (PShape.GetSelection.GetNextSet(-1) <> -1) then Done = false end iteration = iteration + 1 end if (Cancel) then break end recno = PShape.AddRecord PShape.SetValue(PShape.FindField("ID"),recno,i) if (thePrj.IsNull.Not) then temp = thePoint.ReturnUnprojected(thePrj) thePoint = temp end PShape.SetValue(PShape.FindField("Shape"),recno,thePoint) av.ShowMsg("Percentage Done:"++(100*i/NumPt).AsString+"%") end av.ClearMsg av.ClearStatus '**** check for null shapefile if (recno = -1) then PShape.Deactivate PShape = nil av.PurgeObjects File.Delete(out_name) out_name.SetExtension("shx") File.Delete(out_name) out_name.SetExtension("dbf") File.Delete(out_name) return nil end '**** ask if add to View theView = av.GetActiveDoc if (MsgBox.YesNo("Add shapefile as theme to the view?",theTitle,true)) then fthm = FTheme.Make(PShape) theView.AddTheme(fthm) theView.GetWin.Activate end