You can use MDL functions in VBA classes by importing libraries delivered with the MicroStation SDK.
• The stdmdlbltin.dll library provides access to the core mdl functions in MicroStation.
• The stdmdlaccessor.dll library provides some useful Element/Element descriptor functions.
The MicroStation SDK is available for download at
http://www.bentley.com/en‐US/Corporate/Bentley+Partner+Program/
Technology+Partners/MicroStation+SDK.htm.
Some common MDL data types are:
In Visual Basic, an Integer is 16 bits and a Long is 32 bits. Passing an argument by reference generates a pointer to the variable.
Some examples of using these are as follows.
Levels
Option Explicit
Private Declare Function mdlLevel_setActiveByName Lib "stdmdlbltin.dll" _ (ByVal parentLevelIdIn As Long, _
ByVal pLevelNameIn As Long) As Long
Sub SetActiveLevelFromLibraryTest() Dim ParentID As Long
Dim oLevelName As String Dim status As Long Dim oPtr As Integer
' this needs to end up as 0xffffffff ParentID = &HFFFFFFFF
oLevelName = "G-CODE"
status = mdlLevel_setActiveByName(ParentID, StrPtr(oLevelName)) Debug.Print status
End Sub
C DataType VBA DataType
Dpoint3d ByRef pt As Point3d
Transform ByRef transP As Transform3d DgnModelRefP ByVal modelRef As Long MSElementDescr ByRef edPP As Long
Calling C and MDL Functions
Multi‐line placement
Const NULLPtr As Long = 0
Declare Function mdlMline_create Lib "stdmdlbltin.dll" _ (ByVal mline As Long, _
ByVal seed As Long, _ ByRef normal As Point3d, _
ByRef points As Point3d, _ ByVal nPoints As Long) As Long
Declare Function mdlElmdscr_new Lib "stdmdlbltin.dll" _ (ByRef elDescrPP As Long, _
ByVal elemHeader As Long, _ ByVal element As Long) As Long
Function CreateMultiLine() As element Dim bytes(0 To 2000) As Byte Dim vertices(0 To 4) As Point3d Dim elmDescrP As Long
vertices(0) = Point3dFromXY(0,0) vertices(1) = Point3dFromXY(1,1) vertices(2) = Point3dFromXY(2,0) vertices(3) = Point3dFromXY(3,1) vertices(4) = Point3dFromXY(4,0)
mdlMline_create VarPtr(bytes(0)), NULLPtr, Point3dFromXYZ(0,0,1), _ vertices(0), 5
mdlElmdscr_new elmDescrP, NULLPtr, bytes(0)
Set CreateMultiLine = MdlCreateElementFromElementDescrP(elmDescrP)
End Function
Multi‐line information
Declare Sub mdlMline_getInfo Lib "stdmdlbltin.dll" _ (ByRef nPoints As Long, _
ByRef nLines As Long, _ ByRef dist1 As Double, _ ByRef dist2 As Double, _
ByRef zVector As Point3d, _ ByVal mline As Long)
Declare Function ElmdscrAccessor_getMSElement Lib "stdmdlaccessor.dll" _ (ByVal ElementDescr As Long) As Long
Sub DumpMLineInfo() Dim ele As element Dim pElement As Long Dim elmDescr As Long Dim nPoints As Long Dim nLines As Long Dim dDist1 As Double Dim dDist2 As Double Dim zVector As Point3d
Dim oScanCriteria As New ElementScanCriteria Dim ElementEnum As ElementEnumerator
oScanCriteria.ExcludeAllTypes
oScanCriteria.IncludeType msdElementTypeMultiLine
Set ElementEnum = ActiveModelReference.Scan(oScanCriteria)
Do While ElementEnum.MoveNext With ElementEnum.Current
elmDescr = .MdlElementDescrP
pElement = ElmdscrAccessor_getMSElement(elmDescr) mdlMline_getInfo nPoints, nLines, dDist1, dDist2, _
zVector, pElement
Debug.Print "nPoints = " & nPoints;
Debug.Print ", nLines = " & nLines;
Debug.Print ", dDist1 = " & dDist1;
Debug.Print ", dDist2 = " & dDist2 End With
Loop End Sub
Note: In MicroStation V8 XM Edition and later, there exists a PropertyHandler object in the VBA object model that provides read‐write access to the property system used by the Element Information tool. Using PropertyHandler is
Calling C and MDL Functions
therefore a viable alternative not only for getting and setting element properties but also design file, model, and attachment properties. Because
PropertyHandler works on a file's copy of an object, if a macro uses the object to change element properties, those changes are not automatically reflected in the Element object. The macro has to load the object again — perhaps by using the GetElementByID method — to get the changes. There are numerous examples of using PropertyHandler in the MicroStation VBA help document, Objects, PropertyHandler Object.
References
' Reference File Parameters Const REFERENCE_DISPLAY = 1 Const REFERENCE_SNAP = 2 Const REFERENCE_LOCATE = 3 Const REFERENCE_SLOTACTIVE = 4 Const REFERENCE_SCALELINESTYLES = 5 Const REFERENCE_FILENOTFOUND = 6 Const REFERENCE_FILENAME = 7 Const REFERENCE_DESCRIPTION = 8 Const REFERENCE_LOGICAL = 9 Const REFERENCE_SCALE = 10 Const REFERENCE_ROTATION = 11 Const REFERENCE_ATTACHNAME = 13
Const REFERENCE_SCALE_MASTERUNITS = 15 Const REFERENCE_RESERVEDA = 16
Const REFERENCE_HIDDEN_LINE = 18 Const REFERENCE_DISPLAY_HIDDEN = 19 Const REFERENCE_CLIP_ROTATE = 20 Const REFERENCE_WCHAR_DESCRIPTION = 21 Const REFERENCE_WCHAR_LOGICAL = 22 Const REFERENCE_SCALE_STORED = 23 Const REFERENCE_SCALE_BY_UNITS = 24 Const REFERENCE_ANONYMOUS = 25 Const REFERENCE_OWNINGMODELREF = 26 Const REFERENCE_SOURCEMODELID = 27 Const REFERENCE_REFNUM = 28
Const REFERENCE_ELEMENTID = 29
Const REFERENCE_DISPLAYRASTERREFS = 30 Const REFERENCE_WCHAR_MODELNAME = 31 Const REFERENCE_USE_LIGHTS = 32 Const REFERENCE_DONOTNEST = 33 Const REFERENCE_CLIPBACK = 34 Const REFERENCE_CLIPFRONT = 35 Const REFERENCE_NESTDEPTH = 36 Const REFERENCE_RENDERMODE = 37 Const REFERENCE_REDUNDANT = 38 Const REFERENCE_LEVEL_OVERRIDES = 39 Const REFERENCE_DISPLAYFILENAME = 40 Const REFERENCE_DISPLAYATTACHNAME = 41 Const REFERENCE_DISPLAYMODELNAME = 42 Const REFERENCE_DWGBLOCKNAME = 43 Const REFERENCE_DONTDETACHONALL = 44 Const REFERENCE_DISPLAYFLAG = 45 Const REFERENCE_MODELNOTFOUND = 46 Const REFERENCE_CLIP_ROTMATRIX = 47 Const REFERENCE_LEVEL = 48
Const REFERENCE_DWGUNITMODE = 49 Const REFERENCE_HSVVALUEADJUST = 50 Const REFERENCE_HSVSATURATIONADJUST = 51 Const REFERENCE_BASENESTDEPTH = 52 Const REFERENCE_RIGHTNOTGRANTED = 53 Const REFERENCE_PRINTCOLORADJUST = 54 Const REFERENCE_METADATAONLY = 55 Const REFERENCE_EXTENDED = 56 Const REFERENCE_HSVHUESETTING = 57 Const REFERENCE_HSVADJUSTMENTFLAGS = 58 Const REFERENCE_DISPLAYPRIORITY = 59 Const REFERENCE_NAMEDGROUP = 60 Const REFERENCE_REVISION = 61 Const REFERENCE_TRANSPARENCY = 62 Const REFERENCE_PLOT_3D = 63 Const REFERENCE_NESTOVERRIDES = 64 Const REFERENCE_NEWLEVELDISPLAY = 65
Calling C and MDL Functions
Const REFERENCE_GLOBALLINESTYLESCALES = 66 Const REFERENCE_TREAT_AS_ELEMENT = 67 Const REFERENCE_PROVIDERID = 68 Const REFERENCE_RAWREVISION = 69 Const REFERENCE_REVISIONNOTFOUND = 70 Const REFERENCE_USEANNOTATIONSCALE = 71 Const REFERENCE_ATTACHMETHOD = 73 Const REFERENCE_ACTIVATESTATUS = 74 Const REFERENCE_SYNCHWITHSAVEDVIEW = 75 Const REFERENCE_USEVIEWFLAGS = 76 Const REFERENCE_SAVEDVIEWNAME = 77 Const REFERENCE_SAVEDVIEWELEMENTID = 78 Const REFERENCE_LEVELCONTROLSDISPLAY = 79 Const REFATTACH_NEST_NONE = 0
Const REFATTACH_NEST_COPY = 1 Const REFATTACH_NEST_DISPLAY = 2 Const REFCOLORTABLE_USEPREF = 0 Const REFCOLORTABLE_ALWAYS = 1 Const REFCOLORTABLE_NEVER = 2 Const REFERENCE_LOCATEON = 0 Const REFERENCE_LOCATEOFF = 1 Const REFERENCE_PARENTLOCATEOFF = 2 Const REFERENCE_NOLOCATERIGHTS = 3 Const REFERENCE_PARENTNOLOCATERIGHTS = 4 ' Reference File mdl Functions
Declare Function mdlRefFile_attach Lib "stdmdlbltin.dll" _ (ByRef outModelRefP As Long, _
ByVal fileName As String, _ ByVal logical As Long, _ ByVal description As Long, _ ByRef masterOrigin As Point3d, _ ByRef referenceOrigin As Point3d, _ ByVal scale As Double, _
ByRef rotMatrix As Matrix3d, _ ByVal nClipPoints As Long, _ ByRef clipPoints As Point2d, _ ByVal levelDisplayFlag As Long, _ ByVal snapLock As Long, _
ByVal locateLock As Long) As Long
Declare Function mdlRefFile_attachByView Lib "stdmdlbltin.dll" _ (ByRef outModelRefP As Long, _
ByVal fileName As String, _ ByVal logical As Long, _ ByVal description As Long, _ ByRef viewName As Long, _ ByVal scale As Double, _
ByRef centerPoint As Point3d, _ ByVal levelDisplayFlag As Long, _ ByVal snapLock As Long, _
ByVal locateLock As Long) As Long
Declare Function mdlRefFile_attachCoincident Lib "stdmdlbltin.dll" _ (ByRef outModelRefP As Long, _
ByVal fileName As String, _ ByVal logical As Long, _ ByVal description As Long, _ ByVal levelDisplayFlag As Long, _ ByVal snapLock As Long, _
ByVal locateLock As Long) As Long
Declare Function mdlRefFile_attachCoincidentExtended Lib _
"stdmdlbltin.dll" _
(ByRef outModelRefP As Long, _ ByVal fileName As String, _ ByVal logical As Long, _ ByVal description As Long, _ ByVal levelDisplayFlag As Long, _ ByVal snapLock As Long, _
ByVal locateLock As Long, _
ByVal callAsynchs As Long) As Long
Declare Function mdlRefFile_attachmentIdFromModelRef Lib _
"stdmdlbltin.dll" _
(ByVal modelRef As Long) As DLong
Declare Function mdlRefFile_beginAttachment Lib "stdmdlbltin.dll" _ (ByRef outModelRefP As Long, _
ByVal fileName As Long, _ ByVal modelName As Long, _ ByVal logical As Long, _
ByVal description As Long) As Long
Declare Function mdlRefFile_completeAttachment Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByVal nestFlag As Long, _
Calling C and MDL Functions
ByVal nestDepth As Long, _
ByVal initialDisplay As Long) As Long
Declare Function mdlRefFile_detach Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long) As Long
Declare Function mdlRefFile_getFromAttachmentID Lib "stdmdlbltin.dll" _ (ByRef refFilePP As Long, _
ByRef modelRefP As Long, _ ByVal srcModelRef As Long, _
ByVal attachmentID As DLong) As Long
Declare Function mdlRefFile_getInfo Lib "stdmdlbltin.dll" _
(ByVal modelRef As Long) As Long ' Returns a pointer to a structure
Declare Function mdlRefFile_getLocateLock Lib "stdmdlbltin.dll" _ (ByVal refP As Long) As Long
Declare Function mdlRefFile_getParameters Lib "stdmdlbltin.dll" _ (ByVal param As Long, _
ByVal paramName As Long, _ ByVal modelRef As Long) As Long
Declare Function mdlRefFile_getParent Lib "stdmdlbltin.dll" _
(ByVal refP As Long) As Long ' Returns a pointer to a structure
Declare Function mdlRefFile_getRefCount Lib "stdmdlbltin.dll" () As Long
Declare Function mdlRefFile_getSnapLock Lib "stdmdlbltin.dll"
(ByVal refP As Long) As Long
Declare Function mdlRefFile_is3d Lib "stdmdlbltin.dll"
(ByVal refP As Long) As Long
Declare Function mdlRefFile_isPrimary Lib "stdmdlbltin.dll"
(ByVal refP As Long) As Long
Declare Function mdlRefFile_reattach Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByVal outName As String, _ ByVal fileName As String, _
ByVal ModelName As Integer) As Long
Declare Function mdlRefFile_reload Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByVal updateDisplay As Long, _ ByVal forceReload As Long) As Long
Declare Function mdlRefFile_rotate Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByRef pivotP As Point3d, _ ByVal xrotation As Double, _ ByVal yrotation As Double, _ ByVal zrotation As Double, _ ByVal view As Long) As Long
Declare Function mdlRefFile_setClip Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByRef pts As Point2d, _ ByVal nverts As Long) As Long
Declare Function mdlRefFile_setParameters Lib "stdmdlbltin.dll" _ (ByVal param As Long, _
ByVal paramName As Long, _ ByVal modelRef As Long) As Long
Declare Function mdlRefFile_updateReference Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long, _
ByVal displayMode As Long) As Long
Declare Function mdlRefFile_writeAttachment Lib "stdmdlbltin.dll" _ (ByVal modelRef As Long) As Long
Declare Function mdlRefFile_writeAttachmentConditionally Lib _
"stdmdlbltin.dll" _
(ByVal modelRef As Long) As Long
Declare Function mdlMline_create Lib "stdmdlbltin.dll" _ (ByVal mline As Long, _
ByVal seed As Long, _ ByRef normal As Point3d, _ ByRef points As Point3d, _ ByVal nPoints As Long) As Long
Calling C and MDL Functions
Declare Function mdlElmdscr_new Lib "stdmdlbltin.dll" _ (ByRef elDescrPP As Long, ByVal elemHeader As Long, _ ByVal element As Long) As Long
Const NULLPtr As Long = 0
Sub DumpRefData()
Dim activeModel As ModelReference Dim refModel As ModelReference Dim refModel2 As ModelReference Dim refModels As ModelReferences Dim attach As Attachment
Dim attchmnts As Attachments Dim status As Long
Dim refP As Long Dim modrefP As Long
Dim pts(0 To 4) As Point2d Dim nverts As Long
Dim refDesc As String Dim buffer(255) As Byte Dim dgnfile As DesignFile Dim param As Variant Dim scalefactor As Double
scalefactor = ActiveModelReference.UORsPerMasterUnit nverts = 5
pts(0).X = 0 pts(0).Y = 0
pts(1).X = 5 * scalefactor pts(1).Y = 0
pts(2).X = 5 * scalefactor pts(2).Y = 2 * scalefactor pts(3).X = 0
pts(3).Y = 2 * scalefactor pts(4).X = 0
pts(4).Y = 0
Dim ModelName As String Dim LogicalName As String Dim Description As String
ModelName = "Default"
LogicalName = "testing"
Description = "test file"
status = mdlRefFile_beginAttachment(modrefP, ActiveDesignFile.Name, _ 0, StrPtr(LogicalName), StrPtr(Description))
If status = 0 Then
status = mdlRefFile_setClip(modrefP, pts(0), nverts) Debug.Print "Reference Clip Status = " & status
status = mdlRefFile_completeAttachment(modrefP, 2, -1, 0) End If
End Sub
Declare Sub mdlElmdscr_setVisible Lib "stdmdlbltin.dll" _ (ByVal edP As Long, _
ByVal visible As Long)
Sub SetVisible() Dim ele As element Dim eleDescr As Long
Dim oScanCriteria As New ElementScanCriteria Dim ElementEnum As ElementEnumerator
Set ElementEnum = ActiveModelReference.Scan(oScanCriteria)
Do While ElementEnum.MoveNext
Set ele = ElementEnum.Current
eleDescr = ele.MdlElementDescrP mdlElmdscr_setVisible eleDescr, 0 If ele.FilePosition <> 0 Then
ele.Rewrite End If
Calling C and MDL Functions
'now make it visible again ActiveDesignFile.Views(1).Redraw mdlElmdscr_setVisible eleDescr, 1 If ele.FilePosition <> 0 Then
ele.Rewrite End If
ActiveDesignFile.Views(1).Redraw Loop
End Sub
Element descriptors
Declare Function mdlACS_getCurrent Lib "stdmdlbltin.dll" _ (ByRef originP As Point3d, _
ByRef rotMatrixP As Matrix3d, _ ByVal typeP As Long, _
ByVal nameP As Long, _
ByVal descriptionP As Long) As Long
Sub DoACS()
Dim ele As LineElement Dim pntOrigin As Point3d
Dim pntStart As Point3d, pntEnd As Point3d Dim rot As Matrix3d
Dim iType As Integer Dim result As Long Dim tms As Transform3d
result = mdlACS_getCurrent(pntOrigin, rot, iType, 0, 0) rot = Matrix3dFromMatrix3dTimesMatrix3d(rot, _
Matrix3dFromScale(ActiveModelReference.UORsPerMasterUnit)) rot = Matrix3dInverse(rot)
tms = Transform3dFromMatrix3dPoint3d(rot, pntOrigin)
' Create an element using this transform
pntStart = Point3dFromTransform3dTimesPoint3d(tms, Point3dFromXY(0,0)) pntEnd = Point3dFromTransform3dTimesPoint3d(tms, Point3dFromXY(3,3))
Set ele = CreateLineElement2(Nothing, pntStart, pntEnd) ele.Redraw
ActiveModelReference.AddElement ele
End Sub
The method MdlCreateElementFromElementDescr will appear when you enable the Show hidden members option in the Object Browser.
Other methods available are:
• MdlGetModelReferenceFromModelRefP
• MdlGetDesignFileFromModelRefP
These will help bridge the gap between the two languages. In addition to
stdmdlbltin.dll and stdmdlaccessor.dll, there are other libraries available that will expose most functions available to MDL in VBA. These are:
• stdbspline.dll for the Bspline curve and surface functions
• stdcons.dll for the constraint manager functions
• stdimage.dll for the image functions
• stdkisolid.dll for the kernel independent solid functions
• stdmtg.dll for the manifold topology functions
• stdraster.dll for the raster reference functions
• stdrdbms.dll for the database functions
• stdrender.dll for the rendering functions
One note about the functions that are in these libraries; since they are Pascal calling sequence functions, they do not handle variable argument lists. So, any function that uses a variable argument will not be available. The function templates are available from the MDL Function Reference help document (MDLAPIFunctionReference.chm) in the MicroStation SDK.
Excerpt from msdn.microsoft.com:
Traditionally, APIs are written for C and C++ programmers who are building Windows applications, but the functions in a DLL can be called by other programming languages, including VBA. Because most DLLs are written and documented primarily for C and C++ programmers, calling a DLL function
Calling C and MDL Functions
You can use the VarPtr function to obtain a memory reference to a block of information. The drawback is that the type of data is unknown, and so, can be easily corrupted. An advantage can exist if a function can accept NULL as an input value.
To pass strings from VBA to mdl functions that require MSWChar*, you need to use the StrPtr method. This is because VBA tries to automatically convert
String variables to char *.
The scope of these functions should be kept Private to the module so that the parameters can be adjusted for the specific case.
Exercise: Displaying a file selection dialog box using the function
mdlDialog_fileOpen
1 If you have not already done so, download and install the MicroStation SDK from
http://www.bentley.com/en‐US/Corporate/Bentley+Partner+Program/
Technology+Partners/MicroStation+SDK.htm
2 In the VBA Project Manager dialog, create and load the project Open_File.
3 Switch to the Visual Basic Editor, insert a module, and create the following macro, ShowOpen: