Follow

How can I incrementally number my objects using MapViewer automation?

This article contains a sample script for creating IDs for all selected objects in an open MapViewer plot. Names are auto generated in incremented numeric order and can include a custom prefix or suffix.

To run this script:

  1. Copy the script below, or click here to download the BAS file: ID_Generate.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\MapViewer 8\Scripter.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Click Script | Run to run the script.
     

'=======================================================================
' ID_Generate.bas
' Generates values to store in the ID or attribute fields of selected objects.
' The value is an incremented number with optional prefix and suffix strings. ' ' Open your GSM file in MapViewer and select one or more objects before running the script.
'======================================================================= Sub Main Dim mvApp, lyr As Object ' Find MapViewer and create a pointer to the application object Set mvApp = GetObject(,"MapViewer.Application") ' Make sure that at least one object is selected Set lyr = mvApp.ActiveDocument.ActiveLayer If lyr.Selection.Count = 0 Then MsgBox "Generating IDs requires that" & vbCrLf & "one or more objects be selected first." End End If ' Create and display the dialog to get the prefix, ' starting number, suffix, and ID to write. Dim ID_List(4) As String ID_List(0) = "PID" ID_List(1) = "SID" ID_List(2) = "ID3" ID_List(3) = "ID4" ID_List(4) = "Hyperlink" Begin Dialog UserDialog 370,119,"Generate IDs" ' %GRID:10,7,1,1 DropListBox 140,7,180,112,ID_List(),.ID1 Text 30,42,60,14,"Prefix",.Text2,2 OKButton 90,91,90,21 CancelButton 210,91,90,21 Text 20,7,110,14,"ID to generate",.Text1,1 TextBox 10,56,110,21,.Prefix TextBox 140,56,90,21,.Num TextBox 250,56,110,21,.Suffix Text 280,42,50,14,"Suffix",.Text3,2 Text 130,42,110,14,"Starting Number",.Text4 End Dialog Dim dlg As UserDialog dlg.ID1 = 0 Dim sts As Integer sts = Dialog(dlg) ' The Cancel button returns 0. ' The OK button returns -1. If sts = 0 Then ' cancel button pressed End End If ' Make sure the Starting Number is a number Dim num As Long If IsNumeric(dlg.Num) Then num = CLng(dlg.Num) Else MsgBox "The Starting Number must be numeric." End End If ' For each selected object, generate the next value and store it. Dim i As Integer Select Case dlg.ID1 Case 0 'PID For i = 1 To lyr.Selection.Count lyr.Selection.Item(i).PIDName = dlg.Prefix & CStr(num+i-1) & dlg.Suffix Next ' Refresh display lyr.Shapes.InvertSelection() lyr.Shapes.InvertSelection() Case 1 'SID For i = 1 To lyr.Selection.Count lyr.Selection.Item(i).SIDName = dlg.Prefix & CStr(num+i-1) & dlg.Suffix Next ' Refresh display lyr.Shapes.InvertSelection() lyr.Shapes.InvertSelection() Case 2 'ID3 For i = 1 To lyr.Selection.Count lyr.Selection.Item(i).Attrib1 = dlg.Prefix & CStr(num+i-1) & dlg.Suffix Next ' Refresh display lyr.Shapes.InvertSelection() lyr.Shapes.InvertSelection() Case 3 'ID4 For i = 1 To lyr.Selection.Count lyr.Selection.Item(i).Attrib2 = dlg.Prefix & CStr(num+i-1) & dlg.Suffix Next ' Refresh display lyr.Shapes.InvertSelection() lyr.Shapes.InvertSelection() Case 4 'Hyperlink For i = 1 To lyr.Selection.Count lyr.Selection.Item(i).HyperlinkStr = dlg.Prefix & CStr(num+i-1) & dlg.Suffix Next ' Refresh display lyr.Shapes.InvertSelection() lyr.Shapes.InvertSelection() End Select End Sub

 

Updated May 1, 2018

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.