Hey guys I would really appreciate any help on this. I'm trying to just get to the point where I have a .NET MDL Form application I can launch from a toolbar or Icon. I am pretty good with .NET, but I am a bit confused by what is going on here. I just want a starting point template project for now that I can compile and add my own methods as I go. Im using an excellent tutorial from Yongan Fu to start.
I am good understanding how everything works so far except for the Command Table. When I added in the Embedded Resource Commands.XML below I started getting the error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Bentley.MicroStation.Exceptions.CommandTableException: Keyin "ImportTraffic CreateElement LineAndLineString" not mapped to method in AddIn ImportTraffic.ImportTraffic.MyAddin
at Bentley.Internal.MicroStation.CommandTables.RegisterCommandNumbers(KeyinTree tree, AddIn app)
at Bentley.Internal.MicroStation.CommandTables.LoadKeyinTables(String xmlFileName, AddIn app)
at Bentley.MicroStation.AddIn.Initialize(IntPtr mdlDescriptor, String appXmlFileFqn)
at Bentley.MicroStation.AddIn..ctor(IntPtr mdlDescriptor)
at ImportTraffic.ImportTraffic.MyAddin..ctor(IntPtr mdlDesc) in C:\Users\spen6722\Documents\SharpDevelop Projects\ImportTraffic\ImportTraffic\ImportTraffic.vb:line 48
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Bentley.MicroStation.AddIn.Load(String[] commandTail, IntPtr mdlDesc)
Here is where I am at so far:
ImportTraffic.vb
Imports System
Imports System.Windows.Forms
Imports Bentley.MicroStation.WinForms
Imports Bentley.MicroStation.InteropServices
Imports System.Runtime.InteropServices
Imports Bentley.MicroStation
Namespace ImportTraffic
''' <summary>''' Import Traffic Microstation Plugin''' By Matt Spencer 2017.09.11''' Maatspencer@gmail.com''' Written in VB.Net using Sharp Develop'''''' This is the primary startup file that is called by Microstation via the MDL Load ImportTraffic Key-in'''''' Setup:''' 1. Create a new class Library''' 2. Change the Build to Target .NET 3.5 Which is a bastardised version based on .NET 2.0 to pull XP users along I believe.''' 3. Add References to Bentley.General.1.0.dll , ustation.dll, Bentley.Microstation.Interfaces.1.0.dll System.Windows.Forms''' 4. Set the output path'''''' Rules For Compilation:''' If a .NET assembly can be executed as an Addin under Mstn, it must meet three conditions:''' a) Includes one class derived from Bentley.MicroStation.Addin;''' b) This derived class must supply a single argument constructor that take an MDL descriptor as an int;''' That constructor must chain to the base class constructor and pass the argument along;''' c) This derived class must override the virtual Run() method of the base Addin class'''''' How to run:''' 1. Build the Dll file to your output Path''' 2. In microstation Append the directory path to MS_ADDINPATH''' 3. Run the Add In With MDL Load ImportTraffic,,MyDomain''' 4. CLR Dialog will show you the run status''' 5. CLR UNLOAD DOMAIN MyDomain - Will unload the project for debugging'''''' </summary>' Commands for the Key-in' Derive the base class from Bentley.Microstaton.Addin<Bentley.MicroStation.AddInAttribute(KeyinTree := "ImportTraffic.Commands.xml", MdlTaskID := "ImportTraffic")> _
Friend NotInheritable Class MyAddin
Inherits Bentley.MicroStation.AddIn' MDL Load Description requires a sinle integer argument to run
Private Sub New(mdlDesc As System.IntPtr)
MyBase.New(mdlDesc)
End Sub' Override the Run() Function in the AddIn Class' This is where we call the program via Key-In
Protected Overrides Function Run(commandLine As String()) As Integer
Return 0
End Function
End Class
End Namespace
Create Element.vbImports System
Imports Bentley.MicroStation.InteropServices
Imports Bentley.Interop.MicroStationDGN
Namespace ImportTraffic
Class CreateElement
Public Shared Sub LineAndLineString(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim startPnt As Point3d = app.Point3dZero()
Dim endPnt As Point3d = startPnt
startPnt.X = 10
Dim oLine As LineElement = app.CreateLineElement2(Nothing, startPnt, endPnt)
oLine.Color = 0
oLine.LineWeight = 2
app.ActiveModelReference.AddElement(oLine)
Dim pntArray As Point3d() = New Point3d(4) {}
pntArray(0) = app.Point3dZero()
pntArray(1) = app.Point3dFromXY(1, 2)
pntArray(2) = app.Point3dFromXY(3, -2)
pntArray(3) = app.Point3dFromXY(5, 2)
pntArray(4) = app.Point3dFromXY(6, 0)
oLine = app.CreateLineElement1(Nothing, pntArray)
oLine.Color = 1
oLine.LineWeight = 2
app.ActiveModelReference.AddElement(oLine)
End Sub
Public Shared Sub ShapeAndComplexShape(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim pntArray As Point3d() = New Point3d(5) {}
pntArray(0) = app.Point3dFromXY(0, -6)
pntArray(1) = app.Point3dFromXY(0, -2)
pntArray(2) = app.Point3dFromXY(2, -2)
pntArray(3) = app.Point3dFromXY(2, -4)
pntArray(4) = app.Point3dFromXY(4, -4)
pntArray(5) = app.Point3dFromXY(4, -6)
Dim oShape As ShapeElement = app.CreateShapeElement1(Nothing, pntArray)
oShape.Color = 0
oShape.LineWeight = 2
app.ActiveModelReference.AddElement(oShape)
Dim elmArray As ChainableElement() = New ChainableElement(1) {}
For i As Integer = 0 To 5
pntArray(i).X += 5
Next
elmArray(0) = app.CreateLineElement1(Nothing, pntArray)
pntArray(2).Y = -8
elmArray(1) = app.CreateArcElement3(Nothing, pntArray(5), pntArray(2), pntArray(0))
Dim oComplexShape As ComplexShapeElement = app.CreateComplexShapeElement1(elmArray)
oComplexShape.Color = 1
oComplexShape.LineWeight = 2
app.ActiveModelReference.AddElement(oComplexShape)
End Sub
Public Shared Sub TextAndTextNode(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim savedTextHeight As Double = app.ActiveSettings.TextStyle.Height
Dim savedTextWidth As Double = app.ActiveSettings.TextStyle.Width
Dim savedFont As Font = app.ActiveSettings.TextStyle.Font
Dim savedAnnotationScaleEnabled As Boolean = app.ActiveSettings.AnnotationScaleEnabled
app.ActiveSettings.TextStyle.Height = 0.7
app.ActiveSettings.TextStyle.Width = 0.6
'app.ActiveSettings.TextStyle.Font = app.ActiveDesignFile.Fonts.Find(MsdFontType.WindowsTrueType, "Arial Unicode MS")
app.ActiveSettings.AnnotationScaleEnabled = False
Dim origin As Point3d = app.Point3dFromXY(0, -7.5)
Dim rMatrix As Matrix3d = app.Matrix3dIdentity()
Dim oText As TextElement = app.CreateTextElement1(Nothing, "Text String", origin, rMatrix)
oText.Color = 0
app.ActiveModelReference.AddElement(oText)
origin = app.Point3dFromXY(2, -9)
Dim oTN As TextNodeElement = app.CreateTextNodeElement1(Nothing, origin, rMatrix)
oTN.AddTextLine("Text Node Line 1")
oTN.AddTextLine("Text Node Line 2")
oTN.Color = 1
app.ActiveModelReference.AddElement(oTN)
app.ActiveSettings.TextStyle.Height = savedTextHeight
app.ActiveSettings.TextStyle.Width = savedTextWidth
app.ActiveSettings.TextStyle.Font = savedFont
app.ActiveSettings.AnnotationScaleEnabled = savedAnnotationScaleEnabled
End Sub
Public Shared Sub CellAndSharedCell(unparsed As String)
Dim app As Application = Utilities.ComApp
app.AttachCellLibrary("sample2.cel")
Dim origin As Point3d = app.Point3dFromXY(1, -13)
Dim xScale As Double = 0.1 * app.ActiveModelReference.UORsPerMasterUnit / 1000.0
Dim scale As Point3d = app.Point3dFromXYZ(xScale, xScale, xScale)
Dim rMatrix As Matrix3d = app.Matrix3dIdentity()
Dim oCell As CellElement = app.CreateCellElement2("DECID", origin, scale, True, rMatrix)
oCell.Color = 0
app.ActiveModelReference.AddElement(oCell)
Dim oSC As SharedCellElement
xScale = 0.02 * app.ActiveModelReference.UORsPerMasterUnit / 1000.0
scale = app.Point3dFromXYZ(xScale, xScale, xScale)
For x As Integer = 4 To 8 Step 2
origin = app.Point3dFromXY(x, -14)
oSC = app.CreateSharedCellElement2("NORTH", origin, scale, True, rMatrix)
oSC.OverridesComponentColor = True
oSC.Color = 1
app.ActiveModelReference.AddElement(oSC)
Next
End Sub
Public Shared Sub LinearAndAngularDimension(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim ds As DimensionStyle = app.ActiveSettings.DimensionStyle
ds.OverrideAnnotationScale = True
ds.AnnotationScale = 1
ds.OverrideTextHeight = True
ds.TextHeight = 0.5
ds.OverrideTextWidth = True
ds.TextWidth = 0.4
ds.MinLeader = 0.01
ds.TerminatorArrowhead = MsdDimTerminatorArrowhead.Filled
Dim rMatrix As Matrix3d = app.Matrix3dIdentity()
Dim pnts As Point3d() = New Point3d(2) {}
pnts(0) = app.Point3dFromXY(0, -17)
pnts(1) = app.Point3dFromXY(3, -17)
Dim oDim As DimensionElement = app.CreateDimensionElement1(Nothing, rMatrix, MsdDimType.SizeArrow)
oDim.AddReferencePoint(app.ActiveModelReference, pnts(0))
oDim.AddReferencePoint(app.ActiveModelReference, pnts(1))
oDim.Color = 0
oDim.DimHeight = 1
app.ActiveModelReference.AddElement(oDim)
ds.AnglePrecision = MsdDimValueAnglePrecision.DimValueAnglePrecision1Place
oDim = app.CreateDimensionElement1(Nothing, rMatrix, MsdDimType.AngleSize)
pnts(0) = app.Point3dFromXY(7, -13)
pnts(1) = app.Point3dFromXY(5, -15)
pnts(2) = app.Point3dFromXY(9, -15)
oDim.AddReferencePoint(app.ActiveModelReference, pnts(0))
oDim.AddReferencePoint(app.ActiveModelReference, pnts(1))
oDim.AddReferencePoint(app.ActiveModelReference, pnts(2))
oDim.Color = 1
oDim.DimHeight = 1
app.ActiveModelReference.AddElement(oDim)
End Sub
Public Shared Sub CurveAndBsplineCurve(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim pntArray As Point3d() = New Point3d(4) {}
pntArray(0) = app.Point3dFromXY(0, -19)
pntArray(1) = app.Point3dFromXY(1, -17)
pntArray(2) = app.Point3dFromXY(2, -19)
pntArray(3) = app.Point3dFromXY(3, -17)
pntArray(4) = app.Point3dFromXY(4, -19)
Dim oCurve As CurveElement = app.CreateCurveElement1(Nothing, pntArray)
oCurve.Color = 0
oCurve.LineWeight = 2
app.ActiveModelReference.AddElement(oCurve)
For i As Integer = 0 To 4
pntArray(i).X += 5
Next
Dim oInterpolationCurve As InterpolationCurve = New InterpolationCurveClass()
oInterpolationCurve.SetFitPoints(pntArray)
Dim oBsplineCurve As BsplineCurveElement = app.CreateBsplineCurveElement2(Nothing, oInterpolationCurve)
oBsplineCurve.Color = 1
oBsplineCurve.LineWeight = 2
app.ActiveModelReference.AddElement(oBsplineCurve)
End Sub
Public Shared Sub ConeAndBsplineSurface(unparsed As String)
Dim app As Application = Utilities.ComApp
Dim basePt As Point3d = app.Point3dFromXYZ(2, -23, 0)
Dim topPt As Point3d = app.Point3dFromXYZ(2, -20, 0)
Dim rMatrix As Matrix3d = app.Matrix3dFromAxisAndRotationAngle(0, app.Pi() / 6)
Dim oCone As ConeElement = app.CreateConeElement1(Nothing, 2, basePt, 1, topPt, rMatrix)
oCone.Color = 0
app.ActiveModelReference.AddElement(oCone)
Dim aFitPnts As Point3d() = New Point3d(3) {}
Dim oFitCurve As InterpolationCurve = New InterpolationCurveClass()
Dim aCurves As BsplineCurve() = New BsplineCurve(2) {}
aFitPnts(0) = app.Point3dFromXYZ(5.9, -21, -0.5)
aFitPnts(1) = app.Point3dFromXYZ(6.9, -20, 1)
aFitPnts(2) = app.Point3dFromXYZ(7.9, -20.3, 1.3)
aFitPnts(3) = app.Point3dFromXYZ(8.9, -20.8, 0.3)
oFitCurve.SetFitPoints(aFitPnts)
oFitCurve.BesselTangents = True
aCurves(0) = New BsplineCurveClass()
aCurves(0).FromInterpolationCurve(oFitCurve)
aFitPnts(0) = app.Point3dFromXYZ(6.4, -22, 0)
aFitPnts(1) = app.Point3dFromXYZ(7.1, -21.2, 0.7)
aFitPnts(2) = app.Point3dFromXYZ(7.7, -21, 1)
aFitPnts(3) = app.Point3dFromXYZ(8.4, -21.7, -0.2)
oFitCurve.SetFitPoints(aFitPnts)
oFitCurve.BesselTangents = True
aCurves(1) = New BsplineCurveClass()
aCurves(1).FromInterpolationCurve(oFitCurve)
aFitPnts(0) = app.Point3dFromXYZ(5.9, -23, 0)
aFitPnts(1) = app.Point3dFromXYZ(7.2, -23.1, 1.2)
aFitPnts(2) = app.Point3dFromXYZ(7.8, -23.3, 0.8)
aFitPnts(3) = app.Point3dFromXYZ(8.7, -22.8, 0.2)
oFitCurve.SetFitPoints(aFitPnts)
oFitCurve.BesselTangents = True
aCurves(2) = New BsplineCurveClass()
aCurves(2).FromInterpolationCurve(oFitCurve)
Dim oBsplineSurface As BsplineSurface = New BsplineSurfaceClass()
oBsplineSurface.FromCrossSections(aCurves, MsdBsplineSurfaceDirection.V, 4, True, True)
Dim oSurfaceElm As BsplineSurfaceElement = app.CreateBsplineSurfaceElement1(Nothing, oBsplineSurface)
oSurfaceElm.Color = 1
app.ActiveModelReference.AddElement(oSurfaceElm)
End Sub
End Class
End Namespace
Comannds.xml (Embedded Resource)
<?xml version="1.0" encoding="utf-8" ?><KeyinTree xmlns="http://www.bentley.com/schemas/1.0/MicroStation/AddIn/KeyinTree.xsd"><RootKeyinTable ID="root"><Keyword SubtableRef="CreateElement"
CommandClass="MacroCommand" CommandWord="ImportTraffic" ><Options Required ="true"/></Keyword></RootKeyinTable><SubKeyinTables><KeyinTable ID="CreateElement"><Keyword SubtableRef="Commands" CommandWord="CreateElement"><Options Required ="true"/></Keyword></KeyinTable><KeyinTable ID="Commands"><Keyword CommandWord="LineAndLineString"> </Keyword><Keyword CommandWord="ShapeAndComplexShape"> </Keyword><Keyword CommandWord="TextAndTextNode"> </Keyword><Keyword CommandWord="CellAndSharedCell"> </Keyword><Keyword CommandWord="LinearAndAngularDimension"> </Keyword><Keyword CommandWord="CurveAndBsplineCurve"> </Keyword><Keyword CommandWord="ConeAndBsplineSurface"> </Keyword></KeyinTable></SubKeyinTables><KeyinHandlers><KeyinHandler Keyin="ImportTraffic CreateElement LineAndLineString"
Function="ImportTraffic.CreateElement.LineAndLineString"/><KeyinHandler Keyin="ImportTraffic CreateElement ShapeAndComplexShape"
Function="ImportTraffic.CreateElement.ShapeAndComplexShape"/><KeyinHandler Keyin="ImportTraffic CreateElement TextAndTextNode"
Function="ImportTraffic.CreateElement.TextAndTextNode"/><KeyinHandler Keyin="ImportTraffic CreateElement CellAndSharedCell"
Function="ImportTraffic.CreateElement.CellAndSharedCell"/><KeyinHandler Keyin="ImportTraffic CreateElement LinearAndAngularDimension"
Function="ImportTraffic.CreateElement.LinearAndAngularDimension"/><KeyinHandler Keyin="ImportTraffic CreateElement CurveAndBsplineCurve"
Function="ImportTraffic.CreateElement.CurveAndBsplineCurve"/><KeyinHandler Keyin="ImportTraffic CreateElement ConeAndBsplineSurface"
Function="ImportTraffic.CreateElement.ConeAndBsplineSurface"/></KeyinHandlers></KeyinTree>