changing levels visibility on multiple models in a DGN file

Dear VBA Experts,

I am trying to write some codes which will help me batch process levels visibility on multiple models in several DGN files. The codes I have written is posted below. My codes work perfectly fine when I open the DGN and activate each models at least once. But as a part of the batch process this code only works on the active model but throws error when I refer to other models in the DGN file. I come to realize that in non-active models the references are not loaded. How can I edit this code so I can tell MicroStation to load all references so that the codes will find whats it looking for. In this case the levels in the referenced file.

I tried activating models before it moves on to next model but with no luck. It breaks with an error message saying the reference index were not found.  Any help or thoughts would be great.

Sincerely,

Rabi

Option Explicit
Private Declare Function mdlModelRef_loadReferenceModels Lib "stdmdlbltin.dll" (ByVal modelRef As Long, _
ByVal loadCache As Long, ByVal loadRasterRefs As Long, ByVal loadUndisplayedRefs As Long) As Long
Sub ControlLayerVisibility()

'On Error Resume Next

Dim myDGN As DesignFile
Set myDGN = Application.ActiveDesignFile

Dim myActiveModel As ModelReference
Dim oAtt As Attachment

'Determine the sheet model with msdModelTypeSheet property
Dim modelCount As Integer
Dim myModel As String
Dim tempString As String
Dim myView As View

For modelCount = 1 To myDGN.Models.Count
If myDGN.Models(modelCount).Type = msdModelTypeSheet Then
tempString = myDGN.Models(modelCount).Name
Dim myLevel As String
Dim myModelLogicalName As String
Dim myAttachmentIndex As Integer
Dim i As Integer

If InStr(1, tempString, "RECORD") > 0 Then
myModel = myDGN.Models(modelCount).Name
'Determine the Attachment Index and run the levels.
' For i = 1 To myDGN.Models(myModel).Attachments.Count
' 'Debug.Print myDGN.Models(myModel).Attachments(i).AttachName
' If myDGN.Models(myModel).Attachments(i).AttachName = "50014746_TB 30x42.dgn" Then
' myAttachmentIndex = i
' myModelLogicalName = myDGN.Models(myModel).Attachments(myAttachmentIndex).AttachName
' End If
' Next
Set myActiveModel = myDGN.Models(myModel)
'myActiveModel.Activate
Set myActiveModel = myActiveModel.DesignFile.Models("50014746_TB 30x42.dgn")
mdlModelRef_loadReferenceModels myActiveModel.MdlModelRefP, 1, 1, 1

Set oAtt = myActiveModel.Attachments("default")
'If myActiveModel.IsReadOnly = True Then
'myDGN.Models(myModel).Activate
For Each myView In myDGN.Views
oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = False
oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = False
' myDGN.Models(myModel).Attachments(myAttachmentIndex).Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
'
'myLevel = "LEVEL SET DISPLAY ON file:" & myModelLogicalName & " """ & Row.Value & """"
myView.Redraw
Next myView
'End If
'Close the file appropriately
myDGN.Models(myModel).Levels.Rewrite

CadInputQueue.SendCommand "FILEDESIGN"
CadInputQueue.SendCommand "SAVE DESIGN"

End If

End If

Next

End Sub

Parents
  • Rabi:
    
    Set myActiveModel = myDGN.Models(myModel)
    'myActiveModel.Activate
    Set myActiveModel = myActiveModel.DesignFile.Models("50014746_TB 30x42.dgn")
    
    

    Code like the above is confusing. You first set myActiveModel, but you don't activate it (.Activate is commented). You then attempt to set it to another value, which is probably invalid — do you really have a model named "50014746_TB 30x42.dgn"? Finally, you don't attempt to activate the new assignment, which explains why you cannot find any attachments.

    Put a few Debug statements in there to let you trace the progress of your code. Also, test for valid assignments:

    Set myActiveModel = myActiveModel.DesignFile.Models("50014746_TB 30x42.dgn")

    will almost certainly result in a NULL model.

    
    Dim strModelName As String
    strModelName = "abc"
    Dim oModel1 As ModelReference
    Dim oModel2 As ModelReference
    Set oModel1 = myDGN.Models(strModelName)
    Debug.Assert (Not oModel1 Is Nothing)
    oModel1.Activate
    Debug.Print "Activated model '" &  oModel1.Name & "'"
    Set oModel2 = oModel1.DesignFile.Models("model name")
    Debug.Assert (Not oModel2 Is Nothing)
    oModel2.Activate 
    Debug.Print "Iterating model '" &  oModel2.Name & "' Attachments"
     …
    
    

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Dear Jon,

    Thank you for your help. The VBA formatting to html site requires the style sheet defined which I am not sure how to get it into this site. If you know how I would be very happy to edit it so that it reads just like it does in VBA editor.

    For MBVA I did make this to work. I think my confusion was in understanding the attachments and how they were handled. I was confused with referencing models. Anyways now I have cleared the issues and the codes work just fine. For those people who wants to automate levels visibility can utilize the code below.

    Option Explicit
    Sub ControlLayerVisibility()

    Dim oAtt As Attachment

    Dim tempString As String
    Dim myView As View
    Dim modelCase As String
    Dim i As Integer
    Dim myAttachmentIndex As Integer

    tempString = ActiveModelReference.Name

    If InStr(1, tempString, "RECORD") > 0 Then
    modelCase = "RECORD"
    End If

    If InStr(1, tempString, "BUILT") > 0 Then
    modelCase = "BUILT"
    End If

    For i = 1 To ActiveModelReference.Attachments.Count
    If ActiveModelReference.Attachments(i).AttachName = "50014746_TB 30x42.dgn" Then
    myAttachmentIndex = i
    End If
    Next

    Set oAtt = ActiveModelReference.Attachments(myAttachmentIndex)

    Select Case modelCase
    Case "RECORD"
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
    myView.Redraw
    Next myView
    Case "BUILT"
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = False
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = True
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = True
    myView.Redraw
    Next myView
    Case Else
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = False
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
    myView.Redraw
    Next myView
    End Select

    ActiveModelReference.Levels.Rewrite
    CadInputQueue.SendCommand "FILEDESIGN"
    CadInputQueue.SendCommand "SAVE DESIGN"

    End Sub

    Sincerely,

    Rabi

Reply
  • Dear Jon,

    Thank you for your help. The VBA formatting to html site requires the style sheet defined which I am not sure how to get it into this site. If you know how I would be very happy to edit it so that it reads just like it does in VBA editor.

    For MBVA I did make this to work. I think my confusion was in understanding the attachments and how they were handled. I was confused with referencing models. Anyways now I have cleared the issues and the codes work just fine. For those people who wants to automate levels visibility can utilize the code below.

    Option Explicit
    Sub ControlLayerVisibility()

    Dim oAtt As Attachment

    Dim tempString As String
    Dim myView As View
    Dim modelCase As String
    Dim i As Integer
    Dim myAttachmentIndex As Integer

    tempString = ActiveModelReference.Name

    If InStr(1, tempString, "RECORD") > 0 Then
    modelCase = "RECORD"
    End If

    If InStr(1, tempString, "BUILT") > 0 Then
    modelCase = "BUILT"
    End If

    For i = 1 To ActiveModelReference.Attachments.Count
    If ActiveModelReference.Attachments(i).AttachName = "50014746_TB 30x42.dgn" Then
    myAttachmentIndex = i
    End If
    Next

    Set oAtt = ActiveModelReference.Attachments(myAttachmentIndex)

    Select Case modelCase
    Case "RECORD"
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayed = True
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = True
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
    myView.Redraw
    Next myView
    Case "BUILT"
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = False
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayed = True
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = True
    myView.Redraw
    Next myView
    Case Else
    For Each myView In ActiveDesignFile.Views
    oAtt.Levels("G-ANNO-TTLB-NO AS BUILT").IsDisplayedInView(myView) = False
    oAtt.Levels("G-ANNO-TTLB-AS BUILT").IsDisplayedInView(myView) = False
    myView.Redraw
    Next myView
    End Select

    ActiveModelReference.Levels.Rewrite
    CadInputQueue.SendCommand "FILEDESIGN"
    CadInputQueue.SendCommand "SAVE DESIGN"

    End Sub

    Sincerely,

    Rabi

Children
No Data