VBA Show all Levels (Including ones in Library)

Hello,

I am trying to list all the levels present in a dgnlib file using VBA. However, all library levels do not show.

Below, is the simple sub (test) to output the name of each level. However it only outputs ones that are imported in the dgnlib.

Sub main(standard As String)
    Dim oFile As DesignFile
    Dim path As String
    Dim lvlLib As Levels
    Dim lvl As Level

    
    path = "C:\ProgramData\Bentley\MicroStation CONNECT Edition\Configuration\Standards\Dgnlib" & "\" & standard & ".DGNLIB"
    Set oFile = Application.OpenDesignFileForProgram(path)
    
    
    For Each lvl In oFile.Levels
        Debug.Print "Level" & lvl.name
    Next
    

    oFile.Close
End Sub

Parents
  • However, all library levels do not show

    Levels are complex, especially when you use OpenDesignFileForProgram.  Search VBA help for CopyElement to see this comment: When copying to a design file opened with OpenDesignFileForProgram it may be necessary to explicitly call DesignFile.Levels.Rewrite. MicroStation uses 3 pieces of data to represent a level. It uses the Level Manager's level tables as the working data. It uses level-definition elements in the element cache as a persistent representation of a level. Finally, there is the disk image of the elements. When CopyElement sees that it must copy a level to the target design file, it creates the definition in the level table. However, it does not immediately write the updated element table to the element cache. Instead it transfers the data from the level table to the element cache when the program destroys the CopyContext. This can produce some surprising results when copying to a design file opened with OpenDesignFileForProgram.

    Prefer to use the active DGN file, which presumably can already see your level library (it's in the standard location for DGNLibs).  Do something like this...

    Dim oLevel As Level
    For Each oLevel In ActiveDesignFile.Levels
      If oLevel.IsFromLevelLibrary Then
        Debug.Print "Level " & oLevel.Name & " was found in the level library"
      Else
        Debug.Print "Level " & oLevel.Name & " is present in the active DGN file"
      End If
    Next oLevel

     
    Regards, Jon Summers
    LA Solutions

Reply Children
No Data