AutoCAD .NET Addin Wizard (AcadNetAddinWizard) is far more than just some templates scattered around. It is a real-wizard-sense wizard, i.e. multi paged, configurable, flexible and intelligent. It also includes various project item wizards, coders, and widgets for AutoCAD .NET programming.
In this article, let us see how the ContextMenu Extensioner of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help implement ContextMenuExtension interfaces in VB.NET automatically, shortly, and still very flexibly.
The ContextMenu Extensioner can be found from the Add New Item UI of both Visual C# and VB.NET of Visual Studio both full-fledged and Express in version both 2008 and 2010. Let’s take the VB.NET of Visual Studio 2010 for an example. The AutoCAD Addin category will appear under the Common Items node of the Installed Templates and its various item wizards including the ContextMenu Extensioner appear in turn in the middle pane:
Here are wizard pages and some sample fill-outs of the ContextMenu Extensioner:
The auto-generated class and code may look like the following:
#Region "Namespaces"
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Windows
Imports MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application
Imports MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document
Imports AcWindowsNS = Autodesk.AutoCAD.Windows
#End Region
Namespace AcadNetAddinVB1
Public Class ContextMenuExtensioner1
Inherits ContextMenuExtension
Public Sub New()
Me.Title = "ContextMenuExtensioner1 Context Menu"
Dim assem As Assembly = Assembly.GetExecutingAssembly()
Dim mnItem01 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item1")
mnItem01.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.addin.ico"))
mnItem01.Checked = True
AddHandler mnItem01.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
Me.MenuItems.Add(mnItem01)
Dim mnItem02 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item2")
mnItem02.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.addin.ico"))
mnItem02.Checked = False
AddHandler mnItem02.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
Me.MenuItems.Add(mnItem02)
Dim mnItem02_01 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item2_1")
mnItem02_01.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.a.ico"))
mnItem02_01.Checked = True
AddHandler mnItem02_01.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02.MenuItems.Add(mnItem02_01)
Dim mnItem02_02 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item2_2")
mnItem02_02.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.b.ico"))
mnItem02_02.Checked = False
AddHandler mnItem02_02.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02.MenuItems.Add(mnItem02_02)
Dim mnItem02_02_01 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item2_2_1")
mnItem02_02_01.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.c.ico"))
mnItem02_02_01.Checked = True
AddHandler mnItem02_02_01.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02_02.MenuItems.Add(mnItem02_02_01)
Dim mnItem02_02_02 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item2_2_2")
mnItem02_02_02.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.d.ico"))
mnItem02_02_02.Checked = False
AddHandler mnItem02_02_02.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02_02.MenuItems.Add(mnItem02_02_02)
Dim mnItem02_02_02_01 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item_2_2_2_1")
mnItem02_02_02_01.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.e.ico"))
mnItem02_02_02_01.Checked = True
AddHandler mnItem02_02_02_01.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02_02_02.MenuItems.Add(mnItem02_02_02_01)
Dim mnItem02_02_02_02 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item_2_2_2_2")
mnItem02_02_02_02.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.f.ico"))
mnItem02_02_02_02.Checked = False
AddHandler mnItem02_02_02_02.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
mnItem02_02_02.MenuItems.Add(mnItem02_02_02_02)
Dim mnItem03 As AcWindowsNS.MenuItem = New AcWindowsNS.MenuItem("item3")
mnItem03.Icon = New Icon(assem.GetManifestResourceStream("AcadNetAddinVB1.Resources.g.ico"))
mnItem03.Checked = True
AddHandler mnItem03.Click, AddressOf ContextMenuExtensioner1.MenuItem_OnClick
Me.MenuItems.Add(mnItem03)
End Sub
Private Shared Sub MenuItem_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim mnItem As AcWindowsNS.MenuItem = CType(sender,AcWindowsNS.MenuItem)
If (Not (mnItem) Is Nothing) Then
MessageBox.Show(String.Format("'{0}' was just clicked.", mnItem.Text))
System.Diagnostics.Debug.WriteLine(String.Format("'{0}' was just clicked.", mnItem.Text))
If (mnItem.Text = "item1") Then
End If
If (mnItem.Text = "item2") Then
End If
If (mnItem.Text = "item2_1") Then
End If
If (mnItem.Text = "item2_2") Then
End If
If (mnItem.Text = "item2_2_1") Then
End If
If (mnItem.Text = "item2_2_2") Then
End If
If (mnItem.Text = "item_2_2_2_1") Then
End If
If (mnItem.Text = "item_2_2_2_2") Then
End If
If (mnItem.Text = "item3") Then
End If
End If
End Sub
End Class
End Namespace
A lot of more magic stuffs are behind the friendly UIs actually. Just name one here: the Icon column of the ContextMenu items accept icon file picking when a cell is clicked and the wizard will turn the picked external icon file into an internal icon resource automatically and use it instead in the ContextMenu item creation. It will be far better than using some hard-coded icon paths in the ContextMenuExtension implementations, the wizard thinks.
Ok, now create a test command to exercise the ContextMenuExtension, something like:
<CommandMethod("LocalCommands1", "Cmd1", "LocalCommands1_Cmd1_ID", CommandFlags.Modal Or CommandFlags.Interruptible, GetType(AcadNetAddinVB1.ContextMenuExtensioner1), "AcadNetAddinVB.chm", "LocalCommands1_Cmd1_Index")> _
Public Shared Sub Cmd1_Method()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim ed As Editor = AcadApplication.DocumentManager.MdiActiveDocument.Editor
Try
Using tr As Transaction = db.TransactionManager.StartTransaction()
'TODO: add your code below.
Debug.WriteLine("Cmd1 ran.")
ed.WriteMessage("Cmd1 ran." & vbLf)
ed.GetAngle("right click to see context menu.")
tr.Commit()
End Using
Catch ex As System.Exception
Debug.WriteLine(ex.ToString())
ed.WriteMessage(ex.ToString())
End Try
End Sub
Next is to launch AutoCAD, NetLoad the assembly, and run the test command so as to have a look at the cool context menu. In case the project itself is also created by the AutoCAD .NET Addin Wizard (AcadNetAddinWizard) and so does the test command, a single F5 press will get us all the way there.
Give the ContextMenu Extensioner of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) a try and you will not feel any regret.
Recent Comments