Author Topic: Using ContextMenuExtension for testing from Visual Studio.  (Read 2519 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2133
  • class keyThumper<T>:ILazy<T>
Using ContextMenuExtension for testing from Visual Studio.
« on: June 18, 2017, 09:07:39 PM »
Zip is attached for AutoCAD2017 with VS2017
... Change to suit
References C:\ObjectARX 2017\inc\...
... Change to suit

Included Startup script to Auto-netload the Assembly ( https://www.theswamp.org/index.php?topic=14492.0 )
              [ just realised that link is 10 years old  .... time sure flies when we're having fun. ]

The menu is adapted from work by Jim Awe to the MgdDbg Assembly.

The main benefit from using this system is that the assembly under test will autoload and we don't need to remember the exact command to type in ... it will be in the menu.


I'll add the Project Template to suit VisualStudio startup when I'm happy with it.

If you have difficulty adapting this just yell ....

All care, no responsibility :)



Commands.cs
Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3.  
  4. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  5.  
  6. [assembly: CommandClass(typeof(ACAD2017_TestAppMenu.Commands))]
  7.  
  8. namespace ACAD2017_TestAppMenu
  9. {
  10.     public class Commands
  11.     {
  12.         [CommandMethod("kb_TEST")]
  13.         public void Test01()
  14.         {
  15.             var doc = CadApp.DocumentManager.MdiActiveDocument;
  16.             var db = doc.Database;
  17.             var ed = doc.Editor;
  18.  
  19.             using (var tr = db.TransactionManager.StartTransaction()) {
  20.  
  21.                 // TODO Do stuff here
  22.  
  23.                 tr.Commit();
  24.             }
  25.  
  26.             CadApp.ShowAlertDialog("Wheeeeeee ....");
  27.         }
  28.  
  29.         [CommandMethod("kb_TEST02")]
  30.         public void Test02()
  31.         {
  32.             var doc = CadApp.DocumentManager.MdiActiveDocument;
  33.             var db = doc.Database;
  34.             var ed = doc.Editor;
  35.  
  36.             ed.WriteMessage($"Document.name => {doc.Name}\n");
  37.         }
  38.     }
  39. }
  40.  

Initialization.cs
Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3.  
  4. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  5.  
  6.  
  7. namespace ACAD2017_TestAppMenu
  8. {
  9.     public class Initialization : IExtensionApplication
  10.     {
  11.         public void Initialize()
  12.         {
  13.             CadApp.Idle += OnIdle;
  14.         }
  15.  
  16.         private void OnIdle(object sender, EventArgs e)
  17.         {
  18.             var doc = CadApp.DocumentManager.MdiActiveDocument;
  19.             if (doc != null){
  20.                 CadApp.Idle -= OnIdle;
  21.  
  22.                 doc.Editor.WriteMessage("\nACAD2017 TestAppMenu loaded.");
  23.  
  24.                 AppContextMenu.AddContextMenu(); // add User commands to the right-click menu
  25.                 doc.Editor.WriteMessage("\nRight-Click in drawing for UserMenu.\n");
  26.             }
  27.         }
  28.  
  29.         public void Terminate()
  30.         {
  31.         }
  32.     }
  33. }
  34.  

TemplateContextMenu.cs
Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Windows;
  3.  
  4. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  5.  
  6. namespace ACAD2017_TestAppMenu
  7. {
  8.     public class AppContextMenuItem : Autodesk.AutoCAD.Windows.MenuItem
  9.     {
  10.         public AppContextMenuItem(string title, string cmdName) : base(title)
  11.         {
  12.             CommandName = cmdName;
  13.         }
  14.  
  15.         public string CommandName{ get; private set; }
  16.     }
  17.  
  18.     public class AppContextMenu
  19.     {
  20.         static ContextMenuExtension _appMenu = null;
  21.  
  22.         public static void AddContextMenu()
  23.         {
  24.             _appMenu = new ContextMenuExtension();
  25.             _appMenu.Title = "TEST AppContextMenu";
  26.  
  27.             _appMenu.MenuItems.Add(new AppContextMenuItem("First Test...", "kb_TEST"));
  28.             _appMenu.MenuItems.Add(new MenuItem(""));    // separator
  29.             _appMenu.MenuItems.Add(new AppContextMenuItem("Second Test...", "kb_TEST02"));
  30.  
  31.             _appMenu.MenuItems.Add(new MenuItem(""));    // separator
  32.             foreach (MenuItem mnuItem in _appMenu.MenuItems) {
  33.                 var appContextMnuItem = mnuItem as AppContextMenuItem;
  34.                 if (appContextMnuItem != null)
  35.                     appContextMnuItem.Click += new EventHandler(ExecuteCommand);
  36.             }
  37.  
  38.             CadApp.AddDefaultContextMenuExtension(_appMenu);
  39.         }
  40.         private static void ExecuteCommand(Object o, EventArgs e)
  41.         {
  42.             var mnuItem = (AppContextMenuItem)o;
  43.  
  44.             string fullCmdLine = string.Format($"_{mnuItem.CommandName}\n");
  45.             CadApp.DocumentManager.MdiActiveDocument.SendStringToExecute(fullCmdLine, false, false, true);
  46.         }
  47.  
  48.         public static void RemoveContextMenu()
  49.         {
  50.             CadApp.RemoveDefaultContextMenuExtension(_appMenu);
  51.         }
  52.     }
  53. }
  54.  




« Last Edit: June 18, 2017, 09:25:53 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.