PostCommand Method?

Good morning,

Can someone please explain the PostCommand Method within the UIApplication Class? I can’t find any resources talking about how to apply this method.

I’m trying to write a code to use the Split Surface tool on a toposurface. From what I understand it is a built-in command. The only place I found the Split Surface tool in the Revit API was in the PostableCommand Enumeration; so, I’m trying to figure out if I can use the Split Surface command with the PostCommand Method.

Here is a code where using the PostCommand method is not giving me any errors, but I’m not sure what is happening when I run it.

image

Any help is greatly appreciated. Thank you in advance!

1 Like

@erfajo is one of the few I know who has delved into black magic that is postable commands in the Revit API via Dynamo and lives to tell the tale. Perhaps he can provide some insight?

3 Likes

Thanks for the reply. I’m trying to read some articles from The Builder Coder about the subject but since I am new I’m having a hard time following.

It seems like I may need to do something with the Idling Event to successfully use postable commands. Also, does tagging @erfajo notify them or should I send them a message?

Hi,

Here is a working script :

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.UI import RevitCommandId
from Autodesk.Revit.UI import UIApplication
from Autodesk.Revit.UI import ExternalCommandData
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

uiapp = DocumentManager.Instance.CurrentUIApplication

RunIt = IN[0]

if RunIt ==  True:
	CmndID = RevitCommandId.LookupCommandId('ID_SPLIT_SURFACE')
	CmId = CmndID.Id
	uiapp.PostCommand(CmndID)
	errorReport = 'Success'
else:
	errorReport = 'Set IN[0] to true'

#Assign your output to the OUT variable
OUT = errorReport 

Split%20surface

4 Likes

Alban,

Thank you for your reply. Your code is another way to access the commandId, my code above also was able to retrieve the commandId and post successfully. I just didn’t realize because the SplitSurface command doesn’t seem to have any results. So although I have not accomplished what I set out to do (use the split surface tool as I would through Revit), the topic of this post is resolved all for except one problem

Running the PostCommand method only works once. From my research it seems as though an Idling Event must be registered in order to run the script again successfully. An easy way test is through the ThinLines command. If you PostCommand the ThinLines commandId it will toggle once, but won’t work again. This is what I’m attempting to solve right now through registering an idling event.

Please see relevant links below. Please let me know if you have any questions or solutions. Thanks!

Link 1
Link 2
Link 3

Once the script is executed, you must select the topography in Revit before splitting it.

I’m not aware of a method to execute several times the PostCommand in one run.
I hope you will find a way to do it. Good luck !

1 Like

You can’t use postable commands more than once. And they don’t return anything as they are void methods so don’t have a return type. All you are doing is sending a command to revit (same as clicking a button). Generally speaking they are not used in code unless you really need them and even then, it’s not good practice.

3 Likes

Thank you Daniel, great information! It makes sense that it would be bad practice to use them in code. When I say use them more than once, I mean if I PostCommand(ThinLine) and then manually turn Thin Lines off running the script again doesn’t work a second time. Is that what you are referring to?

And in the specific case of using the Split Surface tool, since the only place in the API documents I see anything about the tool is in the postable commands enumeration does that mean I just have to recreate that tool with nodes/code if I want a script for it?

My first impression when discovering using dynamo & Revit API with python was that anything you can do within the program you can create a script for, but I’m finding not everything is accessible through classes/methods/properties. Maybe everything is still possible but I just have to create more advanced scripts.

No, I mean run them back to back. You can put an empty port on the python node and plug in a bool node to refresh the node for execution as an input change should mark the node as dirty. I don’t think you can use 2 postable commands in the same dynamo session either. Don’t forget that these are the commands bound to the buttons, so they usually require input from the user, therefore require yours and revits attention so dynamo will not run at the same time when a command is running.

Sometimes you can achieve something close to or emulate entirely what a command does, but in my experience they can be quite tedious to recreate as there’s gaps in the public API. Sometimes there is just no alternative to using postable commands as there is limited API access to an element types members or they don’t exist in the public API. It’s quite frustrating. When I bump into these things I usually see what others have tried (usually on the building coder! Haha!) and if no luck then add an item to the revit wish list on autodesks website and hope they include it in a mid year update or next version.

However, I’m not saying not to use them (I use them albeit very rarely, so I’d be a hypocrite to say an absolute no), just be aware that they have a downside and not to use them as a go to for coding, they are a last, last resort.

Cheers,
Dan

1 Like

Nicely done @erfajo. I’ll have to check that out. But in your case @ericabbott the command you are using requires you to interact with revit, therefore not sure you can employ the same hack.

2 Likes

Thank you for the information @Daniel_Woodcock1 @erfajo! I’m going to move forward and try to recreate the split surface tool without the post command. I will make a follow up post once I figure it out.

I’m excited to be a part of the forums and appreciate the active responses on my posts. Hopefully as I learn I can help others with problems they encounter.

Have a great day!

2 Likes

You’re welcome @ericabbott! Good luck with the split surface script. As always, if you get stuck, the forum will be more than happy to help!

1 Like

Agree with the experts above :slight_smile:

Something else which I don’t think has been mentioned, is that there can be a class which has a similar method to the postable command… So you have to be careful if you see that inital response in your search. Your example with split surface doesn’t, but your example with thin lines does…

http://www.revitapidocs.com/2018/cc12cf65-dbb1-8e89-5136-c8e7f087bd5e.htm

Hope that’s of interest…

Mark

Edit: Wall by face is a better example :slight_smile: http://www.revitapidocs.com/2018.1/751ee1bb-878d-da68-9a70-57f051f4d13e.htm
vs

1 Like