Introduction

These articles discuss .NET programming from the viewpoint of a MicroStation V8 developer.

When implementing functionality in .NET, there are Windows environment variables to consider. Questions are posted by developers on the MicroStation Programming Forum.

Q Have you any hints about developing an application for MicroStation using .NET?

Visual Studio Versions

A Can I use Visual Studio to build my .NET AddIn?

A Which version of Visual Studio can I use?

Read this article for information about versions of Visual Studio.

MicroStation delivers a version of the .NET CLR runtime. You need to make sure that the runtime installed with MicroStation is compatible with your code developed with your installed version of Visual Studio.

References in Visual Studio

A Microsoft calls a .NET DLL an assembly. MicroStation delivers a number of assemblies. You can see those assemblies in folder \MicroStation\assemblies. You should reference some, but not all, of those assemblies in your Visual Studio project.

The assemblies you have to always reference in your .NET add-in project are …

Additionally, if you want to make your dialogs dockable, you have to reference also
…\MicroStation\assemblies\ECFramework\bentley.windowing.dll.

.NET Hints

A Have you any hints about developing an application for MicroStation using .NET?

At the end of the build process you should end up with a .NET assembly that either has the command.xml file embedded in the assembly or as an external resource (.xml file) next to the assembly. Now you need to tell MicroStation where the assembly is located by augmenting the MS_ADDINPATH configuration varialble in MicroStation. We typically do this by creating a configuration file in the appl directory under MicroStation/config/appl. In the file we have one line that is MS_ADDINPATH > "location of where my assembly is". Now once that is in place we can load the addin in MicroStation using the command MDL LOAD AssemblyName. Make sure that your MDL Task ID is not greater than 15 characters (legacy limit). To validate the addin before running you can use the USTNXOM program from the developer shell. To use this simply run the executable like this:

USTNXOM ValidateAddin "Your Assembly.dll"

It should then tell you if it finds and resolves the command table and is formatted correctly.

To develop a Tool I would recomend starting with the AddIn class. This will help you to define an application that will run in MicroStation. Here is some sample VB.NET code for implementing the AddIn part of the application …

Imports Bentley.MicroStation.WinForms
Imports Bentley.MicroStation.InteropServices
Imports Bentley.Interop.MicroStationDGN
Imports System.Runtime.InteropServices
Imports Bentley.MicroStation

Namespace ProjectReviewApp

Public Class ProjectReviewAppVB
	Inherits Bentley.MicroStation.AddIn
	Private Shared s_App As ProjectReviewAppVB
	Private Shared pReviewDialog As ProjectReviewForm
	Sub New(ByVal mdlDesc As System.IntPtr)
		MyBase.New(mdlDesc)
		s_App = Me
	End Sub
	Public Shared Function GetApp() As ProjectReviewAppVB
		GetApp = s_App
	End Function
	Public Shared Function GetDialog() As ProjectReviewForm
		If pReviewDialog Is Nothing Then
		pReviewDialog = New ProjectReviewForm
		End If
		GetDialog = pReviewDialog
	End Function

	Protected Overrides Function run(ByVal cmdLine() As String) As Integer
		run = 0
	End Function

	Public Shared Sub ReleaseDialog()
		pReviewDialog = Nothing
	End Sub
End Class

Public Class KeyinCommands
	Public Shared Sub Open(ByVal unparsed As String)
		Dim pDialog As ProjectReviewForm
		pDialog = ProjectReviewAppVB.GetDialog
		If Not pDialog Is Nothing Then
			pDialog.ScanFile()
			pDialog.ShowForm()
		End If
	End Sub

	Public Shared Sub Close(ByVal unparsed As String)
		Dim pDialog As ProjectReviewForm
		pDialog = ProjectReviewAppVB.GetDialog
		If Not pDialog Is Nothing Then
			pDialog.CloseForm()
		End If
	End Sub

	Public Shared Sub Scan(ByVal unparsed As String)
		Dim pDialog As ProjectReviewForm
		pDialog = ProjectReviewAppVB.GetDialog
		If pDialog Is Nothing Then
			pDialog.ShowForm()
			Else
			pDialog.ScanFile()
		End If
	End Sub
End Class
End Namespace

Now for the "Tool" itself I would recommend using the COM Interop Bentley.Interop.MicroStationDGN and the IPrimitiveCommandEvents or ILocateCommandEvents class. For documentation on those classes refer to the MicroStationVBA.chm file.

For the user interface I would use a winform that inherits from the Bentley.MicroStation.Winforms.Adapter class. Note when you inherit from this class you will not be able to use the designer mode of the IDE. You can then design with the various components of the IDE for buttons etc. Here is a sample that has a listbox and a scan of the design file …

Imports Bentley.MicroStation.Winforms
Imports Bentley.Interop.MicroStationDGN
Imports Bentley.MicroStation
Imports Bentley.Windowing
Imports System.Windows.Forms
Namespace ProjectReviewApp

	Public Class ProjectReviewForm
	Inherits Bentley.MicroStation.WinForms.Adapter
	Private Shared s_App As Bentley.Interop.MicroStationDGN.Application
	Private Shared s_Current As ProjectReviewForm
	Dim m_window As Bentley.Windowing.WindowContent

	#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call
		s_Current = Me
	End Sub

	'Form overrides dispose to clean up the component list
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
		s_Current = Nothing
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.
	'Do not modify it using the code editor
	Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
	Private Sub InitializeComponent()
		Me.ListBox1 = New System.Windows.Forms.ListBox
		Me.SuspendLayout()
		'
		'ListBox1
		'
		Me.ListBox1.Location = New System.Drawing.Point(48, 40)
		Me.ListBox1.Name = "ListBox1"
		Me.ListBox1.Size = New System.Drawing.Size(200, 173)
		Me.ListBox1.TabIndex = 0
		'
		'ProjectReviewForm
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(292, 268)
		Me.Controls.Add(Me.ListBox1)
		Me.Name = "ProjectReviewForm"
		Me.Text = "ProjectReviewForm"
		Me.ResumeLayout(False)

	End Sub

	#End Region

	Public Function ScanFile() As Integer
		Dim oEnum As Bentley.Interop.MicroStationDGN.ElementEnumerator
		Dim oScanCriteria As Bentley.Interop.MicroStationDGN.ElementScanCriteriaClass
		Dim iStatus As Integer
		oScanCriteria = New Bentley.Interop.MicroStationDGN.ElementScanCriteriaClass
		oEnum = Bentley.MicroStation.InteropServices.Utilities.ComApp.ActiveModelReference.Scan(oScanCriteria)
		ListBox1.Items.Clear()
		Do While oEnum.MoveNext
		If ListBox1.Items.Contains(oEnum.Current.Type.ToString) = False Then
		iStatus = ListBox1.Items.Add(oEnum.Current.Type.ToString)
		End If
		Loop
		ScanFile = iStatus
	End Function

	Public Sub ShowForm()
		If s_Current Is Nothing Then
			s_Current = New ProjectReviewForm
		End If
		Dim winManager As Bentley.Windowing.WindowManager
		winManager = Bentley.Windowing.WindowManager.GetForMicroStation
		m_window = winManager.DockPanel(s_Current, "ProjectReviewForm", s_Current.Text, Bentley.Windowing.DockLocation.Floating)
	End Sub

	Public Sub CloseForm()
		If s_Current Is Nothing Then
			Exit Sub
		End If
		ProjectReviewAppVB.ReleaseDialog()
		m_window.Close()
		m_window = Nothing
		s_Current = Nothing
	End Sub
	End Class
End Namespace

The Bentley Developer Network (BDN) has a wizard for getting started, and a tutorial on building .NET AddIn applications.

Return to .NET articles index.