Command Unavailable error

Erik Missal

Command Unavailable error

I get a "Command Unavailable: Update All" error sometimes when I try to use CATIA.StartCommand using C#. The command is not always active because of the state of the workpackage. Is there a way to check if the command is available before running StartCommand? Running it inside a Try/Catch does not work because the "Power input message" window freezes everything until you click OK. I was trying to avoid using a backgroundworker to watch for the error window.

Thanks,

Erik

Emanuel Hristea

RE: Command Unavailable error
(in response to Erik Missal)

I also faced this issue and I am interested if there is a solution.

What I usually do is check if the Active Document is the right kind or check the workbench.

Still I needed to click ok for save all for instance without blocking the main thread of the application. You can achieve this without wasting a background worker like this:

public sealed class CatiaService : ICatiaService, IDisposable
{

const string SaveAllCommand = "Save All";

public void SaveAll()
{
    // After 300 ms we will hit ok on SaveAll. SaveAll is blocking.
    Task.Delay(TimeSpan.FromMilliseconds(300)).ContinueWith(_ => ValidateSaveAll());
    StartCommand(SaveAllCommand);
}

 

public static void ValidateSaveAll()
{
    var wnd = User32.FindWindow("#32770", "Save All");
    if (wnd != IntPtr.Zero) {
         ClickButton(wnd, "&Yes");
    }
}

 

//For Win32Interop I am using Varana.PInvoke 

private static void ClickButton(HWND window, string buttonName)
{
     var button = User32.FindWindowEx(window, IntPtr.Zero, null, buttonName);
     if (button == IntPtr.Zero) {
           return;
      }

     _ = User32.SendMessage(button,User32.WindowMessage.WM_LBUTTONDOWN, MouseButtonState.MK_LBUTTON);
     _ = User32.SendMessage(button, User32.WindowMessage.WM_LBUTTONUP, MouseButtonState.MK_LBUTTON);

}

}

In order to avoid blocking your main thread call the code that needs the button click using a Task.Run. For example I usesd Save All like this:

 

// Trick, I am using the progress report to mark that Catia is busy

var progressReport = new Progress<bool>(status => CatiaState.IsBusy = status);

await Task.Run(() => SaveInCatia(fileName,progressReport, path), tokenSource.Token)
       .ContinueWith(async _ => await progressDialog.CloseAsync(), tokenSource.Token)
       .ConfigureAwait(false);

private void SaveInCatia(string fileName, IProgress<bool> progressReport, params string[] path)
{
      progressReport.Report(true);
      CatiaService.Navigate(path).InsertAssemblies(fileName); 
      CatiaService.SaveAll();
      progressReport.Report(false);
}

Erik Missal

RE: Command Unavailable error
(in response to Emanuel Hristea)

Thanks

I will give that a try.

Erik