Q Similar questions to those below crop up now and then on the Bentley Discussion Groups.
MicroStation DGN models may have other models included as attachments. A DGN attachment enables a CAD user to view graphic objects (elements) in other models. A user may modify the visibility of elements in DGN models, including elements in attached models. In fact, a user may modify the visibility of all elements in an attachment by turning the attachment's display on or off.
When MicroStation first opens a file having attachments, it loads only those attachment models that are displayed. MicroStation defers loading non-displayed attachments until such time as the user modifies its display in such as way as to show at least one element from that attachment model.
That some attachment models may not be loaded when MicroStation first opens a DGN model can cause problems for VBA developers. Your VBA macro may want to exercise its control over reference attachments, and is surprised to find are not available. Attempting to debug your code, you are equally surprised when the code works. What you may have not observed is, that while investigating the problem, you turned the display of non-displayed references back on. Simply turning on the references enabled your code to work as designed.
Once you've realised the problem, you want to fix it. You need to load all references, even though those that are not visible. Unfortunately, VBA provides no method to do that. This article helps you to solve the problem, by calling an MDL function.
A
How do I ensure that all reference models are attached using VBA?
We wrote the example AttachRefs
to illustrate one approach.
The source code is provided below.
The key to this solution is MDL function mdlModelRef_loadReferenceModels
.
Here's its description from MDL help:
Loads the reference models attached to the specified modelRef.
By default, reference models are loaded only for the active modelRef (and its descendants),
and child DgnModelRefP's for other modelRefs are not created.
First, you must declare the MDL function in your VBA code. Copy the following declaration to the beginning of a VBA module …
Declare PtrSafe Function mdlModelRef_loadReferenceModels _ Lib "stdmdlbltin.dll" ( _ ByVal modelRef As LongPtr, _ ByVal loadCache As Long, _ ByVal loadRasterRefs As Long, _ ByVal loadUndisplayedRefs As Long ) As Long
Next, provide a wrapper around the MDL function that makes it easier to use in the context of a VBA macro …
Sub LoadReferenceModels (ByVal oModel As ModelReference)
' The following constant definitions are boolean, taking values of 0 or 1
Const LoadCache As Long = 1
Const LoadRasterRefs As Long = 1
Const LoadUndisplayedRefs As Long = 1
mdlModelRef_loadReferenceModels oModel.MdlModelRefP, _
LoadCache, LoadRasterRefs, LoadUndisplayedRefs
End Sub
Finally, here's how you call that wrapper …
LoadReferenceModels ActiveModelReference