Advertisement
maphew

Canvec/NTDB Symbology arcmap 9.3 vba macro

Mar 9th, 2011
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '********************************************
  2. 'This Module is part of the Sofware Enhancement for Emergency Mapping (SEEM) Project
  3. 'this Module is used to generate a symbology that fits a field in the data and the name of a style galery item
  4. 'As to be in ArcMap to use not in a stand alone vb6 application but as a vba macro
  5. '********************************************
  6.  
  7.  
  8. Option Explicit
  9.  
  10. Dim m_pGeoFLayer As Variant     'modular variable use to pass on the feature layer
  11. Dim m_pMxDoc As IMxDocument     'modular variable, active document
  12. Dim m_pMap As IMap              'modular variable, active Map
  13.  
  14.  
  15. Public Sub MapSymbology()
  16. '*** this is the start of the program. The goal is to change the symbology to fit with the style gallery
  17.  
  18.  
  19. Dim strMes As String    ' use for debug with mesagebox
  20. Dim i As Integer        ' simple counter
  21.  
  22. ' get the document
  23.  
  24. Set m_pMxDoc = ThisDocument
  25.  
  26. ' get the map
  27.  
  28. Set m_pMap = m_pMxDoc.FocusMap
  29.  
  30.  
  31. ' get the layers in the map
  32. Dim pLayer As ILayer
  33. Dim pstrLayerName As String
  34.  
  35.  
  36. ' cycle through each layer to change Symbology
  37. For i = 0 To m_pMap.LayerCount - 1
  38.    
  39.     'layer in this map
  40.    Set pLayer = m_pMap.Layer(i)
  41.     pstrLayerName = pLayer.Name
  42.     'MsgBox (" layer name = " & pstrLayerName)
  43.    
  44.    
  45.    
  46.     If TypeOf pLayer Is IFeatureLayer Then
  47.         'feature layer
  48.        Dim pFLayer As IFeatureLayer2
  49.         Set pFLayer = pLayer            'QI not really use here but always usefull
  50. 'MsgBox ("shapetype of layer" & pFLayer.ShapeType)
  51.        'Dim pGeoFLayer As IGeoFeatureLayer
  52.        If TypeOf pLayer Is IGeoFeatureLayer Then
  53.          Set m_pGeoFLayer = pLayer
  54.         End If
  55.    
  56.         ApplyUniqueRenderer (m_pGeoFLayer)
  57.         'If frmSymbology.Tag = "Error" Then Exit Sub
  58.        
  59.      End If
  60. Next i
  61.  
  62. 'Update and refresh the view to see changes
  63. m_pMxDoc.UpdateContents
  64. m_pMxDoc.ActiveView.Refresh
  65.  
  66.  
  67. End Sub
  68.  
  69. Private Sub ApplyUniqueRenderer(SomeLayer As IGeoFeatureLayer)
  70.  
  71. '*****************************************************
  72. '   this sub generate the uniquevaluerenderer to use
  73. '   need a featurelayer as intrant
  74. '******************************************************
  75.  
  76. ' Create and initiate renderer
  77. Dim pUVRenderer As IUniqueValueRenderer
  78. Set pUVRenderer = New UniqueValueRenderer
  79. Dim strField As String
  80. strField = frmSymbology.txtFieldName
  81.  
  82. pUVRenderer.FieldCount = 1   'set how many field for to use to symbolize
  83. pUVRenderer.Field(0) = strField 'name of the field to use
  84.  
  85.  
  86. Dim pFLayer As IFeatureLayer
  87. Dim pFClass As IFeatureClass
  88. Set pFLayer = SomeLayer     'QI
  89. Set pFClass = pFLayer.FeatureClass
  90.  
  91. 'Need a cursor to cycle the featureclass
  92.  
  93. Dim pFCursor As IFeatureCursor
  94. Set pFCursor = pFClass.Search(Nothing, True)
  95.  
  96. Dim pFeature As IFeature
  97. Dim pSym As IUnknown            ' IUnknown because it can be a point, line or fill symbol
  98. Dim pSymDefault As IUnknown     ' IUnknown because it can be a point, line or fill symbol
  99.  
  100. 'get the first element
  101. Set pFeature = pFCursor.NextFeature
  102.  
  103. Dim strValue As String
  104.  
  105. 'Dinamic array to get all possible element values
  106.  
  107. Dim MyArray() As String
  108. Dim i As Integer        'counter
  109. Dim j As Integer        'counter
  110. Dim lngField As Long
  111. i = 0
  112.  
  113. Dim bolFound As Boolean
  114.  
  115. ReDim MyArray(0)        'dimention the array to 1 element
  116.  
  117. Dim strSymCategory As String  ' use to get the category of element
  118.  
  119. 'Loop to all feature
  120. Do Until pFeature Is Nothing
  121. strSymCategory = ""
  122.  
  123. lngField = pFClass.FindField(strField)
  124.  
  125. If lngField = -1 Then
  126.     MsgBox "field not found!!"
  127.     'frmSymbology.Tag = "Error"
  128.    Exit Sub
  129.    
  130. Else
  131.     strValue = pFeature.Value(pFClass.FindField(strField)) ' get the value of the field ATC for this element (row)
  132. '    If strValue = "0" Then
  133. '        strValue = "Generic/Default"
  134. '    End If
  135.    If Not strValue = "" Or Not strValue = " " Then
  136.         If MyArray(0) = "" Then
  137.             MyArray(i) = strValue
  138.             i = i + 1
  139.             ReDim Preserve MyArray(i)  'make space to received a other value
  140.        Else
  141.             For j = 0 To i - 1
  142.                 If MyArray(j) = strValue Then
  143.                     bolFound = True     ' value is already in the array
  144.                    Exit For
  145.                 Else
  146.                     bolFound = False    ' value is not in this array(j)
  147.                End If
  148.             Next j
  149.             If Not bolFound Then
  150.                 MyArray(i) = strValue
  151.                 i = i + 1
  152.                 ReDim Preserve MyArray(i) 'make space to received a other value
  153.            End If
  154.         End If
  155.     End If
  156.    
  157.     Set pFeature = pFCursor.NextFeature   ' go to the next feature
  158. End If
  159. Loop
  160.  
  161. 'get the default value
  162. Select Case pFClass.ShapeType
  163.     Case 1  'point
  164.     Set pSymDefault = GetStyleItem("Marker Symbols", "Default", strSymCategory)    '** Get the Default Symbol for Marker
  165.    
  166.     Case 3  'Line
  167.     Set pSymDefault = GetStyleItem("Line Symbols", "Default", strSymCategory)      '** Get the Default Symbol for Line
  168.    
  169.     Case 4  'Polygon
  170.     Set pSymDefault = GetStyleItem("Fill Symbols", "Default", strSymCategory)      '** Get the Default Symbol for Fill
  171.    
  172.     End Select
  173. If frmSymbology.Tag = "Error" Then Exit Sub
  174.  
  175. ' find the corresponding symbol for each element present in the array
  176. For j = 0 To UBound(MyArray) - 1
  177.   '** Choose the right group of Shape (point, line, fill)
  178.    If Not MyArray(j) = "0" Then
  179.    
  180.         Select Case pFClass.ShapeType
  181.             Case 1  'point
  182.                Set pSym = GetStyleItem("Marker Symbols", MyArray(j), strSymCategory)           '** Get the Symbol
  183.              '**************** go to the angle field and see if the angle is -1 or not, that tell us is we have to apply a angle to the symbology
  184.  
  185.                 Dim pFCursor2 As IFeatureCursor
  186.                 Set pFCursor2 = pFClass.Search(Nothing, True)
  187.                 Dim pFeature2 As IFeature
  188.                 Set pFeature2 = pFCursor2.NextFeature
  189.                
  190.                 Dim pBolAngle As Boolean
  191.                 Dim numValue As Integer
  192.                 Dim intLoop As Integer
  193.                 pBolAngle = False
  194.                 lngField = pFClass.FindField("ANGLE")
  195.                 If Not lngField = -1 Then
  196.                
  197.                     numValue = pFeature2.Value(pFClass.FindField("ANGLE")) ' get the value of the field ANGLE for this element (row)
  198.    
  199.                     If Not numValue = -1 Then
  200.                         pBolAngle = True
  201.                     End If
  202.                 End If
  203.  
  204.             Case 3  'Line
  205.                Set pSym = GetStyleItem("Line Symbols", MyArray(j), strSymCategory)              '** Get the Symbol
  206.            
  207.             Case 4  'Polygon
  208.                 Set pSym = GetStyleItem("Fill Symbols", MyArray(j), strSymCategory)              '** Get the Symbol
  209.        End Select
  210.    
  211.    
  212.         If Not pSym Is Nothing Then
  213.             pUVRenderer.AddValue MyArray(j), "", pSym    ' "pFLayer.Name"Symbol is in the style gallery
  214.            pUVRenderer.Label(MyArray(j)) = strSymCategory ' label in the TOC for the corresponding value
  215.            
  216.         Else
  217.             If strSymCategory = "Default" Or strSymCategory = "" Then
  218.                 strSymCategory = "No style found, Default Symbol"   ' symbol is not in the style gallery
  219.            End If
  220.            
  221.             pUVRenderer.AddValue MyArray(j), "", pSymDefault    ' "pFLayer.Name"symbol is not in the style gallery
  222.                 ' label in the TOC for the corresponding default value
  223.            strSymCategory = ""
  224.         End If
  225.     ElseIf Not pSym Is Nothing Then
  226.         pUVRenderer.AddValue "0", "", pSym    'pFLayer.Name there is nothing in the ATC field so this will put the last symbol use as a default
  227.        pUVRenderer.Label("0") = "Generic/Default" ' label in the TOC for the corresponding value
  228.     Else
  229.         pUVRenderer.AddValue "0", "", pSymDefault    'pFLayer.Name there is nothing in the ATC field so this will put the default symbol
  230.        pUVRenderer.Label("0") = "Generic/Default" ' label in the TOC for the corresponding value
  231.   End If
  232.    
  233. Next j
  234. Dim strStyleGaleryName As String
  235. strStyleGaleryName = frmSymbology.txtStyle & ".style"
  236.    
  237. pUVRenderer.LookupStyleset = strStyleGaleryName  ' set up the style name
  238. pUVRenderer.UseDefaultSymbol = False            ' don't show default symbol
  239. pUVRenderer.DefaultSymbol = pSymDefault
  240.  
  241.  
  242. Set SomeLayer.Renderer = pUVRenderer  ' apply the renderer to the layer
  243. Dim pRotation As IRotationRenderer
  244.  
  245. '**** apply a rotation if the angle field is found, this apply only for point/marker feature
  246. If pBolAngle Then
  247. Set pRotation = SomeLayer.Renderer
  248.     pRotation.RotationField = "ANGLE"
  249.     pRotation.RotationType = esriRotateSymbolArithmetic
  250. End If
  251.  
  252.  
  253. End Sub
  254.  
  255.  
  256.  
  257.  
  258.  
  259. Public Function GetStyleItem(StyleCategory As String, StyleName As String, ByRef Category As String) As IUnknown
  260. '**********************
  261. ' this function go in the style library and return the matching Symbol
  262. ' return a symbol (point, line or fill)
  263. '**********************
  264.  
  265. ' get the style gallery
  266. Dim pStyleGallery As IStyleGallery
  267. Dim pstyleStorage As IStyleGalleryStorage
  268. Static blstyle As Boolean
  269. Set pStyleGallery = m_pMxDoc.StyleGallery
  270. Set pstyleStorage = pStyleGallery
  271.  
  272. 'get the list for the specific ntdb style
  273. Dim pEnumStyleItems As IEnumStyleGalleryItem
  274. 'Set pEnumStyleItems = pStyleGallery.Items(StyleCategory, "BAntdb2.style", "")
  275. Dim strStyleGaleryName As String
  276. strStyleGaleryName = frmSymbology.txtStyle & ".style"   'get the name given by the user
  277.  
  278. If Not blstyle Then
  279.     pstyleStorage.AddFile (strStyleGaleryName)
  280.     blstyle = True
  281. End If
  282.  
  283. Set pEnumStyleItems = pStyleGallery.Items(StyleCategory, strStyleGaleryName, "")
  284.  
  285. pEnumStyleItems.Reset
  286.  
  287. 'If pEnumStyleItems Is Empty Then Exit Function
  288. Dim pStyleGalleryItems As IStyleGalleryItem
  289. On Error GoTo ErrorStyle
  290. Set pStyleGalleryItems = pEnumStyleItems.Next
  291.  
  292. ' find out if the style existe for this element
  293. Do Until pStyleGalleryItems Is Nothing
  294.     If pStyleGalleryItems.Name = StyleName Then Exit Do
  295.     Set pStyleGalleryItems = pEnumStyleItems.Next
  296. Loop
  297.  
  298. If Not pStyleGalleryItems Is Nothing Then
  299.     Category = pStyleGalleryItems.Category  'get the category name for display
  300.    Set GetStyleItem = pStyleGalleryItems.Item
  301. Else
  302.     Set GetStyleItem = Nothing
  303.     Category = "Default"        'there is no symbology for the feature so use the default
  304. End If
  305.  
  306. Exit Function
  307. ErrorStyle:
  308.  
  309. MsgBox "Can't find the Style galery!!"
  310. frmSymbology.Tag = "Error"
  311. Exit Function
  312.  
  313. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement