@Keith.Brown
It seems to me, you just want to make sure the Props window is closed prior to the processing's beginning, and really not care much to re-open the Props window, if it was open before the processing begins. In this case, you can use CommandEnded event handler to handle "PROPERTIESCLOSE" command to actually kick off the processing.
I quickly put together some code to demo what I mean:
1. A WinForm user control, used as Palette in the PalettSet, which only has a button in it. It code behind as following
using System;
using System.Windows.Forms;
namespace PaletteSetWithCommandHandling
{
public partial class PaletteView : UserControl
{
public PaletteView()
{
InitializeComponent();
DwgProcessTool.ProcessStarted += () => { DoWorkButton.Enabled = false; };
DwgProcessTool.ProcessEnded += () => { DoWorkButton.Enabled = true; };
}
public static Action DoWorkButtonClick;
private void DoWorkButton_Click(object sender, EventArgs e)
{
DoWorkButtonClick?.Invoke();
}
}
}
2. The PaletteSet:
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System;
namespace PaletteSetWithCommandHandling
{
public class MyPaletteSet : PaletteSet
{
public static bool DwgProcessingStarted = false;
public const string PROCESSING_TRIGGER_COMMAD = "PROPERTIESCLOSE";
private Document _currentDwg = null;
private readonly PaletteView _palette;
public MyPaletteSet() :
base("","", new Guid("A775612C-B26A-4B91-8483-A4ED2B69B7E4"))
{
this.Style = PaletteSetStyles.ShowCloseButton |
PaletteSetStyles.ShowAutoHideButton |
PaletteSetStyles.UsePaletteNameAsTitleForSingle;
_palette = new PaletteView();
Add("DWG Processing", _palette);
PaletteView.DoWorkButtonClick += StartDwgProcessing;
}
private void StartDwgProcessing()
{
var dwg = CadApp.DocumentManager.MdiActiveDocument;
DwgProcessingStarted = true;
dwg.CommandEnded += Dwg_CommandEnded;
dwg.SendStringToExecute($"{PROCESSING_TRIGGER_COMMAD}\n", true, false, false);
}
private void Dwg_CommandEnded(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.ToUpper() == PROCESSING_TRIGGER_COMMAD && DwgProcessingStarted)
{
var dwg = CadApp.DocumentManager.MdiActiveDocument;
dwg.CommandEnded -= Dwg_CommandEnded;
_currentDwg = dwg;
CadApp.MainWindow.Focus();
DwgProcessTool.ProcessEnded += ProcessEnded;
var tool = new DwgProcessTool();
tool.ProcessDrawings();
}
}
private void ProcessEnded()
{
CadApp.Idle += CadApp_Idle;
}
private void CadApp_Idle(object sender, EventArgs e)
{
if (DwgProcessingStarted)
{
DwgProcessingStarted = false;
CadApp.Idle -= CadApp_Idle;
// Do something that is necessary after the processing if needed
// for example, update the PaletteSet UI components
CadApp.DocumentManager.MdiActiveDocument = _currentDwg;
this.Focus();
}
}
}
}
3. The drawing processing tool. For the sake of simplicity, it only loop through each opened drawing, set it as MdiActiveDocument (that is why you want to prevent Props window from being kept open, right?) retrieve layer's count in the drawing"
using Autodesk.AutoCAD.ApplicationServices;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using System.Windows.Forms;
namespace PaletteSetWithCommandHandling
{
public class DwgProcessTool
{
public static Action ProcessStarted;
public static Action ProcessEnded;
public void ProcessDrawings()
{
ProcessStarted?.Invoke();
foreach (Document dwg in CadApp.DocumentManager)
{
CadApp.DocumentManager.MdiActiveDocument = dwg;
ProcessDrawing(dwg);
}
MessageBox.Show($"Drawings processed: {CadApp.DocumentManager.Count}");
ProcessEnded?.Invoke();
}
private void ProcessDrawing(Document dwg)
{
using (dwg.LockDocument())
{
using (var tran = dwg.TransactionManager.StartTransaction())
{
var lt = (LayerTable)tran.GetObject(dwg.Database.LayerTableId, OpenMode.ForRead);
var count=lt.Cast<ObjectId>().Count();
var msg=$"Drawing Name: {dwg.Name}\nLayer Count: {count}";
MessageBox.Show(msg);
tran.Commit();
}
}
}
}
}
4. Finally the commandmethod showing the PaletteSet:
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(PaletteSetWithCommandHandling.MyCommands))]
namespace PaletteSetWithCommandHandling
{
public class MyCommands
{
private static MyPaletteSet _processingUI = null;
[CommandMethod("ShowTool", CommandFlags.Session)]
public static void RunMyCommand()
{
if (_processingUI == null)
{
_processingUI = new MyPaletteSet();
}
_processingUI.Visible = true;
}
}
}
Here is the video clip showing how it works:
As you can see, clicking the button on the PaletteSet send command "PROPERTIESCLOSE" to command line (regardless the Props window being open or not. And when the "PROPERTIESCLOSE" command ends, the processing gets started. You would notices there are 2 static Actions (events) - ProcessStarted/Ended can be hooked to the UI to disable/enable the button; and the processing is done in the ApplicationContext (thus the locking document).
Edit: in the video, I forgot leave the Props window open prior to clicking the button. But it surely close the Props window, if it is open.