Problem with geting attachment's model name

Hi
I'm trying to change logical names for certain attachments.
Here is my code

Sub ChangeLogicalNames2Ontwerp()
Dim mdl As ModelReference
Dim oAttachment As Attachment

For Each mdl In ActiveDesignFile.Models
    If mdl.name <> "Default" Then

        For Each oAttachment In mdl.Attachments
        
            If (oAttachment.DesignFile.name = Left(ActiveDesignFile.name, 7) & ".dgn" Or oAttachment.DesignFile.name = ActiveDesignFile.name) And oAttachment.name = "Default" Then
                oAttachment.LogicalName = "ontwerp"
                oAttachment.Rewrite
                oAttachment.Redraw
            End If
        Next
    End If
Next

End Sub

Problem is with "oAttachment.Name="Default"

I'm getting error "This operation cannot be completed. Possible cause: The associated design file is unavailable."
I'm not sure what's the cause of this. 


I've also noticed that sometimes when I do something with logical names, attachments "become invisible".  If I check their Display mode in reference manager everything is ok. I have to turn off display and then turn on. after that attachment is visible.


Any help would be appreciated

  • Hi,

    one reason this message occurs is if you try to retrieve properties from an attachment, which design file is not loaded.
    By default, if display for an attachment is off, the underlying design file is not loaded into the cache, so you can´t read details like model name, level names etc. from that attachment.

    In the main menu Workspace > Preferences, Category "Reference" is the setting "Cache when display is off" will make those properties available with display off.
    Enabling this setting may reduce performance while opening design files with a large number of attachments with display off.

    But this does not help in case the reference file is not found. For such a case you may first want to check the existence of the model with oAttachment.IsMissingFile.

    I hope this helps?

    Best regards,
    Artur

  • Thanks for replay.
    Unfortunately this is not the case. All attachments are visible, loaded, nothing is missing.
    Before checking attachment's model name I check also attachment's file name. If it wasn't loaded I could not get its name, but I can.
    I've read Jon Summers' post communities.bentley.com/.../37473 when I was looking for a solution.
    I've tried to use "Cache when display is off" earlier but that not fixed my issue.
    The strange thing is that sometimes code works without any problem. I 've checked other file, used macro, everything went ok. I've checked that "good file" once again to be sure, and suddenly error appears. No rule for that
    I wondered if something is not omitted in the code.

    When I mentioned about "invisible attachment" I meant it was side effect of "messing" with attachments using vba (for eg. changing caps of logical names). An attachment is loaded, with visibility on, but in Level Display panel it is greyed out and looks like not loaded. It happens occacionally so I can't reproduce it
  • I would suggest to add a check, if the file and model of the attachment exist before trying to read .Designfile.name or oAttachment.name.

    What I mean is something similar to this:

    '.....
              If Not (oAttachment.IsMissingFile Or oAttachment.IsMissingModel) Then
                If (oAttachment.DesignFile.name = Left(ActiveDesignFile.name, 7) & ".dgn" Or oAttachment.DesignFile.name = ActiveDesignFile.name) And oAttachment.name = "Default" Then
                    oAttachment.LogicalName = "ontwerp"
                    oAttachment.Rewrite
                    oAttachment.Redraw
                End If
              End If
    '.....
    
    
    

    At least this should avoid a runtime error with trying to read properties from missing objects.
    This also could help finding the reason behind the issues like renamed models/files , possibly network issues etc.

    Best regards

    Artur

  • I've added line "If Not (oAttachment.IsMissingFile Or oAttachment.IsMissingModel) Then" but I still got the runtime error.

    As I said it has to be some bug I guess.

    BTW - here is how the invisible error looks like

    as You can see reference is visible and invisible at the same time.
    I think it has also some impact on my macro. I've tried to add code to turn display off and then on, but that changed nothing :(

    Very much thank you for your effort
    Best regards
    Slawek 

  • Hi,

    here some further suggestions to try:

    - try not to use .Rewrite for each oAttachment. This rewrite should not be required. Without .Rewrite you will need to reopen the reference dialog to see recent changes.

    - Iterating the models may require to activate the model before reading attachments.
    Please try to use mod.Activate before modifying the attachments.

    - If the steps do not help, could you please upload a testcase to allow us reproducing the issue - thanks

    Best regards,

    Artur 

  • I did as you suggested and now the macro works without any problem.
    I'm pretty sure I've tested earlier activating model and not using rewrite function, but without success. As I noticed this time macro started to work after I restarted microstation, I still had the error before I did it.
    Anyway, thank you very much for your help

    Best regards
    Slawek