Subscribe to this thread
Home - General / All posts - Scripting column name change and map components
philw
96 post(s)
#06-Nov-18 08:38

I have been working on a script that will do the following:

1) Select pixels in the active surface from a drawing polygon (UI script - transfer selection)

2) Crop the surface to the selection (Analyzer script)

3) Transform the cropped surface (UI script - Transform)

4) Transfer heights from the transformed surface to points in a drawing.

A couple of issues; a) Can anyone help with renaming a column in the points drawing within the script for (4) above? Essentially the issue is that the UI dialogue for Transfer Heights sends the heights to a "Heights" column, which gets overwritten each time. I would like to rename the column from "Heights" to the name of the surface from which the heights came, so that on the next iteration of the script the "Heights" column can receive the new data and then get renamed etc.

b) as the UI scripting involves some dialogues that are only available within the Map component, can anyone suggest some code to cycle through all the surfaces in the Map with the above script/workflow?

The script for 4) is below:

Sub Main

Dim UI, Dlg

Set UI = Application.UserInterface

UI.InvokeCommand("MapTransferHeights")

' Wait for dialog to show (to avoid timing issues)

Do ' Nothing

Loop Until UI.DisplaysModalDialog

Do ' Nothing

Loop Until UI.ModalDialog.Caption = "Transfer Heights"

Set Dlg = UI.ModalDialog

With Dlg.ControlSet

.Item("ComboBoxSource").Text = "[Application.ActiveWindow.ActiveComponent.Name]"

.Item("ComboBoxTarget").Text = "POINTS"

.Item("ComboBoxTargetColumn").Text = "Application.ActiveWindow.ActiveComponent.Name"

End With

Dlg.Accept

End Sub

adamw


8,259 post(s)
#06-Nov-18 16:11

For (b), this code loops through all map layers and reports surfaces:

'VBScript

Sub Main

  Set map = Document.ComponentSet("Map")

  Set mapLayers = map.LayerSet

  For Each layer In mapLayers

    If layer.Component.Type = ComponentSurface Then

      Application.MessageBox layer.Component.Name, "Script"

    End If

  Next

End Sub

For (a), the Transfer Heights dialog does not allow specifying the name for a new column. You can either create a column with the desired name before invoking the dialog and then select it in the dialog using UI scripting. Or you can accept the default Height column and rename it after the dialog completes. Which one do you want?

philw
96 post(s)
#08-Nov-18 01:31

Thanks Adam. I did not seem possible in the UI for the Transfer Heights dialogue to name the column so my original thought would be to rename the [Height] column after the dialogue completes, preferably to the name of the surface that the heights came from. I just am not sure how to do that by code and could not find any similar scripts to alter in the forum.

adamw


8,259 post(s)
#08-Nov-18 14:22

Renaming an existing column is pretty easy:

'VBScript

Sub Main

  Set table = Document.ComponentSet("Table")

  Set columns = table.ColumnSet

  If columns.ItemByName("Height") < 0 Then

    Exit Sub ' no column with this name

  End If

  Set column = table.ColumnSet("Height")

  column.Name = "Renamed"

End Sub

This locates the table named 'Table', checks if it contains the column named 'Height', and if so, renames that column to 'Renamed'.

Hope this helps.

philw
96 post(s)
#12-Nov-18 06:48

Great, I'll have a go at making the "Renamed" be the active surface name in the script. Thanks for your help.

philw
96 post(s)
#22-Nov-18 01:12

I almost have it working. The script works without the looping part but when I try to add the code to get it to loop through all surface components in the map it breaks the part that renames the POINTS Table columns to the name of the surface from which the Heights were transferred. Is there a way to get set the current surface in the loop to be the Application.ActiveWindow.ActiveComponent in the section of code "column.Name = Application.ActiveWindow.ActiveComponent.Name". At the moment it names all columns to the name of the initially selected Component in the Map (i.e. the column names are all the same with the usual integer increment added):

code

'VBScript

Sub Main

Set map = Document.ComponentSet("Map")

  Set mapLayers = map.LayerSet

  For Each layer In mapLayers

    If layer.Component.Type = ComponentSurface Then

 Dim UI, Dlg

 Set UI = Application.UserInterface

 UI.InvokeCommand("MapTransferHeights")

 ' Wait for dialog to show (to avoid timing issues)

 Do ' Nothing

 Loop Until UI.DisplaysModalDialog

 Do ' Nothing

 Loop Until UI.ModalDialog.Caption = "Transfer Heights"

 Set Dlg = UI.ModalDialog

 With Dlg.ControlSet

  .Item("ComboBoxSource").Text = "[Application.ActiveWindow.ActiveComponent.Name]"

  .Item("ComboBoxTarget").Text = "POINTS"

  .Item("ComboBoxTargetColumn").Text = "[New Column]"

 

 End With

 Dlg.Accept

  Set table = Document.ComponentSet("POINTS Table")

  Set columns = table.ColumnSet

  If columns.ItemByName("Height") < 0 Then

  Exit Sub ' no column with this name

  End If

  Set column = table.ColumnSet("Height")

  column.Name = Application.ActiveWindow.ActiveComponent.Name

 End If

 Next

End Sub

adamw


8,259 post(s)
#22-Nov-18 08:33

Why not just set the name of the produced height column to layer.Component.Name? (Same for setting the name of the source component in the dialog.)

Manifold User Community Use Agreement Copyright (C) 2007-2017 Manifold Software Limited. All rights reserved.