Originally Posted by
BlackBox
FWIW -
This could be implemented via .NET plug-in, where NEW/OPEN/SAVE Command is
Veto()-ed when FILEDIA == 0, and a custom
CommandMethod Method is called, which then provides the subsequent user prompt, and should the user select the 'dialog'
PromptResult option, FILEDIA is programmatically set to 1, and then the initial Command is invoked again via
SendStringToExecute().
After trying out a few different ways of going about this, I decided that a simpler, more practical solution is to instead implement a CommandMethod Method aptly named FILEDIAAUTO as a working substitute for a would-be system variable of the same name.
I've added some comments in the code to help explain each step, but this is admittedly less than a full description, mainly because there's additional features intentionally lacking here, such as storing the ON/OFF setting as a static field in lieu of registry key, etc.... Again, this is only working code for others' consideration.
In any event... Doing this (adding a psuedo-system variable scenario rather than actually Veto()-ing the native command when FILEDIA=0), reduced the implementation's code-behind significantly, from the Switch Cases for each native command being monitored (i.e. Netload, New, Open, Saveas, etc.), to the small contingent of CommandCancelled, CommandEnded, and CommandFailed event handlers (as one has to account for the first command invoked by user which is Veto()-ed, in addition to the second, asynchronous command call [if SendStringToExecute() is used], based upon the user's input via command call at FILEDIA=0, etc.).
Attached at the bottom of this post is an assembly compiled to .NET 4.0 framework for AutoCAD 2013 & 2014 (just unblock, unpack the .ZIP file, and NETLOAD the assembly). Should anyone be interested in having this available as an Autoloader .bundle, let me know, and I'll gladly compile an additional assembly to .NET 3.5 framework (for R18 Database), and post the .bundle, etc.
... Enough with the chatter, post the code!
Source-code:
Code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: ExtensionApplication(typeof(BlackBox.AutoCAD.FilediaAuto.Commands))]
[assembly: CommandClass(typeof(BlackBox.AutoCAD.FilediaAuto.Commands))]
namespace BlackBox.AutoCAD.FilediaAuto
{
public class Commands : IExtensionApplication
{
private static DocumentCollection acDocs;
private static int filedia;
// replace these fields with registry keys for published app:
private static bool on;
private static string commandsToMatch = "*NETLOAD,*NEW,*OPEN,*SAVEAS";
public void Initialize()
{
acDocs = acApp.DocumentManager;
acDocs.MdiActiveDocument.Editor.WriteMessage(
"\nFILEDIAAUTO loaded, another creative innovation by BlackBox " +
"\n\t\tEnter \"FILEDIAAUTO\" at the command line to invoke \n"
);
}
public void Terminate()
{
if (on)
acApp.SetSystemVariable("FILEDIA", filedia);
}
[CommandMethod("FileDiaAuto")]
public static void FileDiaAuto()
{
if (on)
{
// unregister event handler
acDocs.DocumentLockModeChanged -= onDocumentLockModeChanged;
// set field to false
on = false;
// restore original system variable value
acApp.SetSystemVariable("FILEDIA", filedia);
}
else
{
// store current system variable value
filedia = System.Convert.ToInt32(acApp.GetSystemVariable("FILEDIA"));
// register event handler
acDocs.DocumentLockModeChanged += onDocumentLockModeChanged;
// set field to true
on = true;
}
// prompt user
acDocs.MdiActiveDocument.Editor.WriteMessage(
"\nFILEDIAAUTO = {0} ", on == true ? "ON" : "OFF");
}
private static void onDocumentLockModeChanged(object sender,
DocumentLockModeChangedEventArgs e)
{
if (Utils.WcMatch(e.GlobalCommandName.ToUpper(), commandsToMatch))
acApp.SetSystemVariable("FILEDIA", 1);
}
}
}