Follow

How can I combine two attributes and store as a new attribute in MapViewer automation?

This article contains a sample script for combining two ID fields together and creating new ID field. Uses selected objects in an already open MapViewer plot.

To run this script:

  1. Copy the script below, or click here to download the BAS file: ID_Concatenate.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_Concatenate.bas
' Copyright (C) Golden Software Inc. 1992-2015
'
' This script operates on the ID and attribute fields of selected objects.
' It concatenates two fields together and stores the results to the field of choice.
'
' To run this script open your MapViewer GSM file in MapViewer and select one or more objects.
' Then run the script in Scripter.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 "Concatenating IDs requires that" & vbCrLf & "one or more objects be selected first."
      End
    End If

    ' Create and display the dialog to find out which
    ' IDs should be concatenated and where the result
    ' should be stored.
    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 300,126,"Concatenate IDs" ' %GRID:10,7,1,1
        DropListBox 110,7,180,119,ID_List(),.ID1
        DropListBox 110,28,180,119,ID_List(),.ID2
        Text 10,63,90,14,"and assign to",.Text2,1
        DropListBox 110,63,180,119,ID_List(),.ID3
        OKButton 70,98,90,21
        CancelButton 180,98,90,21
        Text 10,7,90,14,"Concatenate",.Text1,1
        Text 70,28,30,14,"with",.Text3,1
    End Dialog
    Dim dlg As UserDialog
    dlg.ID1 = 0
    dlg.ID2 = 0
    dlg.ID3 = 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

    ' For each selected object, get the first attribute,
    ' concatenate the second, and store in the third.
    Dim newID As String
    Dim i As Integer
    For i = 1 To lyr.Selection.Count

    Select Case dlg.ID1
    Case 0 'PID
        newID = lyr.Selection.Item(i).PIDName
    Case 1 'SID
        newID = lyr.Selection.Item(i).SIDName
    Case 2 'ID3
        newID = lyr.Selection.Item(i).Attrib1
    Case 3 'ID4
        newID = lyr.Selection.Item(i).Attrib2
    Case 4 'Hyperlink
        newID = lyr.Selection.Item(i).HyperlinkStr
    End Select

    Select Case dlg.ID2
    Case 0 'PID
        newID = newID & lyr.Selection.Item(i).PIDName
    Case 1 'SID
        newID = newID & lyr.Selection.Item(i).SIDName
    Case 2 'ID3
        newID = newID & lyr.Selection.Item(i).Attrib1
    Case 3 'ID4
        newID = newID & lyr.Selection.Item(i).Attrib2
    Case 4 'Hyperlink
        newID = newID & lyr.Selection.Item(i).HyperlinkStr
    End Select

    Select Case dlg.ID3
    Case 0 'PID
        lyr.Selection.Item(i).PIDName = newID
    Case 1 'SID
        lyr.Selection.Item(i).SIDName = newID
    Case 2 'ID3
        lyr.Selection.Item(i).Attrib1 = newID
    Case 3 'ID4
        lyr.Selection.Item(i).Attrib2 = newID
    Case 4 'Hyperlink
        lyr.Selection.Item(i).HyperlinkStr = newID
    End Select
    Next

    If dlg.ID3 > 1 Then
        ' refresh the OM
        lyr.Shapes.InvertSelection()
        lyr.Shapes.InvertSelection()
    End If

End Sub

 

Updated November 2, 2016

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.