I looked into the "DOCINIT" issue. Here are what I found:
1. When start "New" in "Start" tab of AUtoCAD, the AutoCAD may have o or multiple drawings open. and the DocumentLockModeChanged event indeed fires as the trigger command is "DOCINIT";
2. While it seems strange when the command starts from "Start" Tab, not from an active document, the DocumentLockModeChanged event still fires with its argument having a Document property. Originally, I though the e.Document property might be null, but it turns out the Document property is not null, but e.Document.Database is null. So, I assume in order to fire the DocumentLockModeChanged event, the API developer has to create a Document object without database as place holder. This would explain the error prompt after "DOCINIT" is vetoed about "damaged drawing" (I found that if I check the "Always recover the drawing file", then the message would not pop up again when "DOCINIT" is vetoed again).
3. The "DOCINIT" is not a "regular" command that one can run at command line. That would be the reason that vetoes it would cause issues: besides the "Damaged drawing" popup message (which can be gotten rid of after first show by checking the check box, as I just mentioned above), there is no action allowed right after the vetoing. That is, this command is not designed to be "fully veto-able" as other regular command.
4. With what I found, for the business case as you required, there is easy workaround to handle this. See the following code:
private static bool _runMyNewCommand = false;
private static bool _isDocInitCommand = false;
[CommandMethod("ForceMyTemplate")]
public static void HandleDocumentLockChange()
{
CadApp.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
}
private static void DocumentManager_DocumentLockModeChanged(object sender, DocumentLockModeChangedEventArgs e)
{
if (_runMyNewCommand) return;
var cmd = e.GlobalCommandName.ToUpper();
if (cmd == "NEW" || cmd == "QNEW" || cmd == "DOCINIT")
{
var msg = "Open new drawing with project-specific template?";
var res = MessageBox.Show(msg, "Open New Drawing Option",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (res == DialogResult.Yes || res == DialogResult.Cancel)
{
CadApp.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
if (res == DialogResult.Yes)
{
_runMyNewCommand = true;
if (cmd =="DOCINIT")
{
_isDocInitCommand = true;
}
}
e.Veto();
}
}
}
private static void DocumentManager_DocumentLockModeChangeVetoed(object sender, DocumentLockModeChangeVetoedEventArgs e)
{
CadApp.DocumentManager.DocumentLockModeChangeVetoed -= DocumentManager_DocumentLockModeChangeVetoed;
if (_runMyNewCommand)
{
if (!_isDocInitCommand && CadApp.DocumentManager.MdiActiveDocument != null)
{
e.Document.SendStringToExecute("MyNewCommand\n", false, false, false);
}
else
{
CadApp.Idle += CadApp_Idle;
}
}
}
private static void CadApp_Idle(object sender, EventArgs e)
{
if (_runMyNewCommand)
{
CadApp.Idle -= CadApp_Idle;
OpenNewDrawingWithTemplate();
}
}
[CommandMethod("MyNewCommand", CommandFlags.NoHistory)]
public static void OpenNewDrawingWithTemplate()
{
MessageBox.Show("This is UI for opening new drawing with\nproject-specific templates");
CadApp.DocumentManager.Add(@"C:\Temp\acad.dwt");
_runMyNewCommand = false;
_isDocInitCommand = false;
}