Multiple PostCommand actions

Hi all,

Kind of a beginner in Iron Python scripting within Dynamo here.

I’m having this situation in which I have to execute multiple PostCommand actions in sequence. I know this is far from ideal and you should simply stick to API actions, but in this scenario I need to automate a third party plugin and this is the only way possible…

For executing one command, there isn’t much of a problem. But as soon as you execute multiple commands in sequence, it resolves into the following error:


Of course this makes sense, but I cannot find a working method to wait with execution of the code until Revit turns idle after a command has been posted.

Earlier topics like this: Python - How to get a Revit API method to run after the Post Command method? - Revit - Dynamo (dynamobim.com) don’t offer much cause for hope, but don’t exclude the possibility either. I haven’t been able to find a succesful example anywhere, but that could be because there isn’t much to find on this subject at all.

This is the code I’m using:

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.Transactions import TransactionManager
from RevitServices.Persistence import DocumentManager

uiapp = DocumentManager.Instance.CurrentUIApplication
doc = DocumentManager.Instance.CurrentDBDocument

test = IN[0]


CmndID = RevitCommandId.LookupCommandId(IN[0])

try:
	CmndID = RevitCommandId.LookupCommandId(IN[0])
	TransactionManager.Instance.EnsureInTransaction(doc)
	uiapp.PostCommand(CmndID)
	TransactionManager.Instance.TransactionTaskDone()
	errorReport = 'Success'
except OSError as exc:
	errorReport = exc.errno
	
try:
	CmndID = RevitCommandId.LookupCommandId(IN[1])
	TransactionManager.Instance.EnsureInTransaction(doc)
	uiapp.PostCommand(CmndID)
	TransactionManager.Instance.TransactionTaskDone()
	errorReport = 'Success'
except OSError as exc:
	errorReport = exc.errno

OUT = errorReport

#Assign your output to the OUT variable

And this is my (simplified for this purpose) dyn:
Postcommands.dyn (6.2 KB)

Any help would be much appreciated!

Try separating the postable commands with a transaction node and their own python block. That would be the only option I can think of.

1 Like

Of @Nick_Boyts solution doesn’t work you may be out just about out of options here. Some background on why:

  1. Revit will only allow one posted command at a time, with the posted command not running until the transaction is over and the focus returns to the UI.
  2. Dynamo will not return focus to the UI until after it completes execution, no matter how many times you tell it to work.

Without knowing more about the intended commands I can’t think of another way to execute these in sequence.

1 Like

Thanks Jacob, very helpful!

So, does that mean that the first command is actually posted as soon as ALL other nodes, and thereby the complete script, was run?

Well, about the inteded use: I use AGACAD Wood Framing. It is a very neat plugin, but unfortunetaly it requires to press tens of buttons in the exact same sequence in order to work. So my task is to automate this button pressing. Another method could be to simply use the API. I may do that instead. Or can you think of another solution?

Assuming the two transactions didn’t work?

Can you map the command to a keyboard shortcut perhaps? Search the same command ID in the menu which pops up when you type KS and try to assign something there.

You could also try PyRevit or another automation tool which doesn’t automatically open a transaction once ‘run’ starts.

Naah, shortcuts are not really what I’m looking for, I need to find a way that eliminates user input. Also because this project is part of a bigger plan (full automated detailing on a cloud server).

Will look into other solutions. Thanks for the suggestion, wasn’t aware of PyRevit’s existence :slight_smile:

If you’re running it in the context of a larger effort that may be blocking again as you’re stuck in limbo with action, but definitely check it out. By the sounds of it you’ll likely want to move over to Forge and the Design Automation API though.

Depending on how complex the function you are running is you might consider building a code base which performs the task outside of the context of the add-in. I’m not familiar with the tool though so that might be the bigger lift.

Dynamo is not the problem here
DynamoBim or other solution, in all cases it will be necessary to subscribe to an event to ensure that the first postable command is finished (DialogBoxShowingEventArgs ?)

here is a simple test with an IronPython macro (with same problem)
test_multi_command

1 Like

I believe that PyRevit has such an event listener, but I’m not directly familiar with it.

1 Like

It’s not easy but Dynamo can too

example of advanced usage of Python

1 Like

Yeah Forge would have been the best solution by far, but unfortunetaly this plugin isn’t working with Forge. It isn’t made for automation either, it’s designed for user interaction, so I have to make due with it.

There isn’t much of an alternative either, only solutions are:

  1. Create own software from scratch ← Far too expensive
  2. Competing plugins, which are designed for user interaction too.

Thanks for your interaction though, really appreciate this!

Wow this could really be the key!! :smiley:

Unfortunetaly I haven’t much time at the moment to investigate, but I certainly will dive into this article in the coming weeks.

I’ll keep you guys posted!

2 Likes