PostCommand of external command (coming from ribbon) not working

PostCommand of external command (coming from ribbon) not working

maisoui
Advocate Advocate
311 Views
6 Replies
Message 1 of 7

PostCommand of external command (coming from ribbon) not working

maisoui
Advocate
Advocate

Hi,

 

I tried to execute an external command coming from an addin with ribbon. It's not working when I used the same code used for external command coming from addin without ribbon:

var commandName = "CustomCtrl_%CustomCtrl_%CustomCtrl_%Add-Ins%Revit Lookup%RevitLookupButton%RevitLookup.Commands.DashboardCommand"; // :RevitLookup.Commands.DashboardCommand
var commandId = RevitCommandId.LookupCommandId(commandName);
if(commandId != null && application.CanPostCommand(commandId)) {
	application.PostCommand(commandId);
}

 

By consulting the journal, I found the correct string defining the external command :

Jrn.RibbonEvent "Execute external command:CustomCtrl_%CustomCtrl_%MyAddin%Commmands%test:MyAddmin.Commands.Test" 

So the code is:

var commandName = "CustomCtrl_%CustomCtrl_%MyAddin%Commands%test"; // :MyAddmin.Commands.Test
var commandId = RevitCommandId.LookupCommandId(commandName);
if(commandId != null && application.CanPostCommand(commandId)) {
	application.PostCommand(commandId);
}

 

The command is not executed and I found the following line in the journal:

'C 22-Jan-2025 14:39:12.602;  DBG_INFO: CustomCtrl_%CustomCtrl_%MyAddin%Commands%testdoes not exist.: line 889 of E:\Ship24.3.1\2024_px64\Source\API\RevitAPIUI\Objects\APIUIApplicationHandwritten.cpp. 

 

I don't undestand why API can find the commandId and returns true to CanPostCommand but the call to PostCommand raises an internal error. I tried to change the Transaction and Regeneration attributes over command class, but it has no effect.

 

Has anyone ever managed to do this?

All suggestions are welcome

 

Jonathan

--
Jonathan
0 Likes
312 Views
6 Replies
Replies (6)
Message 2 of 7

ricaun
Advisor
Advisor

Your command have some IExternalCommandAvailability?

 

Looks like the CanPostCommand does not check if the command is available to execute.

 

If your have a command with IExternalCommandAvailability return always false, the CanPostCommand return true.

 

Knowing that I can assume that CanPostCommand only checks if you can post the command, it doesn't mean the command is available to execute.

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 3 of 7

maisoui
Advocate
Advocate

Thank you for your reply.

This could have been a good idea, but I already tried with or without CommandAvailability but nothing changed.

According to the Revit journal, it seems that CanPostCommand does not have the correct Assembly (dll path). Please, note the following line :

in%Commands%testdoes not ex

There is no space between "test" and "does". Maybe it's just a logger issue... Whatever I didn't find a way to specify/check the CommandId Assembly.

--
Jonathan
0 Likes
Message 4 of 7

ricaun
Advisor
Advisor

This Command%test you are creating in a RibbonPanel?

 

I know you can find the Id of the button using the Autodesk.Windows.RibbonItem

 

var pushButton = ribbonPanel.CreatePushButton<CommandPost>();
Autodesk.Windows.RibbonItem ribbonItem = pushButton.GetRibbonItem();
var commandName = ribbonItem.Id;
Console.WriteLine(commandName);
var commandId = RevitCommandId.LookupCommandId(commandName);

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 5 of 7

maisoui
Advocate
Advocate

Yes my command is created in a RibbonPanel with PushButtonData. I'm not using your code because it seems to be Revit 2025 API and my code needs to be compatible with multiple Revit product years. But regardless, it seems to me that it is not a command ID problem since I found it in the Revit journal file.

--
Jonathan
0 Likes
Message 6 of 7

ricaun
Advisor
Advisor

The code should work in all Revit version. I'm using my library that have some extension for Revit Ribbon Panels, https://github.com/ricaun-io/ricaun.Revit.UI

 

I'm trying to understand the reason your command does not work and replicate your 'does not exist.' in the journal.

 

What Revit version are you using? Version 2024 my guess.

 

And a sample code to replicate the problem would help.

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 7 of 7

maisoui
Advocate
Advocate

Hi,

 

I took some time to create a sample code to reproduce the problem and I found the cause of the issue. I created the ribbon tab, panel and controls in a OnApplicationInitialized callback. I tried to move my code in the OnStartup method and it's working now. 

I don't understand why it causes an issue. Is it a prerequisite of the Revit API to manipulate the ribbon only in this method?

 

Thank you for your time.

I hope it will help somebody else.

 

Best regards

--
Jonathan