' File name:  poly2point.ave       
'
' Author:  Shuai Jiangping
' Modified:Maxim Dubinin (http://gis-lab.info)
'
' Description:  Converts selected Polygon/Polyline to create a new points shapefile.  
' If no features are currently selected all Polygon/Polyline will be processed.
' This script did not check projection.
'
'
theView    = av.GetActiveDoc
thmThemeIn = theView.GetActiveThemes.Get(0)
thmThemeInFTab = thmThemeIn.GetFTab

' Specify the output shapefile...
 
  fnDefault = thmThemeInFTab.GetSrcName.GetFileName.GetFullName.Substitute(".shp","pnt.shp").AsFileName
  fnOutput  = FileDialog.Put( fnDefault,"*.shp","Output Shape File" )
  if (fnOutput = nil) then exit end
  fnOutput.SetExtension("shp")
  ftbOutput = FTab.MakeNew( fnOutput, POINT )
  ftbOutput.AddFields({Field.Make("ID", #FIELD_LONG, 8, 0)})
  ftbOutput.AddFields({Field.Make("New-ID", #FIELD_LONG, 8, 0)})
  ftbOutput.AddFields({Field.Make("X-Coord", #FIELD_DECIMAL, 18, 5)})
  ftbOutput.AddFields({Field.Make("Y-Coord", #FIELD_DECIMAL, 18, 5)})

' Check if having selection
 
  if (thmThemeIn.GetFTab.GetSelection.Count > 0) then
      FtbToProcess = thmThemeIn.GetFTab.GetSelection
      nRecs = FtbToProcess.Count
    else
      FtbToProcess = thmThemeIn.GetFTab
      nRecs = FtbToProcess.GetNumRecords
  end

'Get a List of Fieldnames that can be used

  aFields = {}
   for each f in thmThemeIn.GetFtab.GetFields
    if ( f.IsTypeShape.Not ) then
       aFields.Add(f)
     end
   end

' Confirming selection

  thisfield= MsgBox.ListAsString (aFields, "Available fields in "++thmThemeIn.AsString, 
             "Field List")
  ftbOutput.AddFields({Field.Make(thisfield.AsString, thisfield.gettype, thisfield.getwidth, thisfield.getprecision)})
  
  nCount = 0
  nRecsAdded = 0
  newid1 = 0

  shpFldIn = thmThemeInFTab.FindField("shape")
  shpFldOut = ftbOutput.FindField("shape")
  fldIDOut    = ftbOutput.FindField("id")  
  fldnewidOut   = ftbOutput.FindField("New-ID")  
  fldselectedOut = ftbOutput.FindField(thisfield.AsString)  
  fldselectedIn  = thmThemeInFTab.FindField(thisfield.AsString)
  fldXOut  = ftbOutput.FindField("X-Coord")
  fldyOut  = ftbOutput.FindField("Y-Coord")

' Processing 

for each rec in FtbToProcess

    nCount = nCount + 1
     av.SetStatus(nCount * 100/ nRecs)
     shp = thmThemeInFTab.ReturnValue(shpFldIn, rec)
     selectedvalue = thmThemeInFTab.ReturnValue(fldselectedIn, rec)
     id = thmThemeInFTab.ReturnValue(fldselectedIn, rec)
    
     if (id.Is(String).Not) then
             id = id.AsString
     end        ' if id          
   
  For each part in shp.AsList
    For each p in part
       newid1 = newid1 + 1
       shpNew = Point.Make(p.GetX, p.GetY)
      nRecNew = ftbOutput.AddRecord
      ftbOutput.SetValue(shpFldOut,nRecNew,shpNew)
      ftbOutput.SetValue(fldIDOut,nRecNew,nCount)
      ftbOutput.SetValue(fldnewidOut,nRecNew,newid1)
      ftbOutput.SetValue(fldXout,nRecNew,p.GetX)
      ftbOutput.SetValue(fldYout,nRecNew,p.GetY)
      ftbOutput.SetValue(fldselectedOut,nRecNew,selectedvalue)
      nRecsAdded = nRecsAdded + 1
      
       end   ' for each p
    end      ' for each part
 end         ' for each rec

av.SetStatus(100)

' Report process result

if (nRecsAdded = 0) then
   MsgBox.Info(" Unable to convert shapes ", "Convert Polygon/Polyline to Point")
  exit  
 else
  MsgBox.Info(nRecsAdded.AsString++"points created.",
    "Convert Polygon/Polyline to Point")
end
 
' Get the specified view, make the new them, and add it to the selected view
  thmNew = FTheme.Make(ftbOutput)
  theView.AddTheme(thmNew)
  theView.GetWin.Activate
 
