Python - How to get a Revit API method to run after the Post Command method?



Hello everyone,

Here I am trying to run Selection.SetElementIds() after using the PostCommand() method but it seems as though the selection occurs before the PostCommand when I run the script.

I believe the cause for this can be found in the PostCommand method description:

Posts the command to the Revit message queue to be invoked when control returns from the current API context.

So it seems like there are 2 options:

  1. Add the Selection into the message queue along with the PostCommand
  2. Delay the Selection so the PostCommand runs first.

If you guys have any feedback or solutions I would greatly appreciate it!

Thank you in advance!


So I tried to find a way to do this but wasn’t able to. The logic behind my method was to do the following:

  1. Post the MergeSurfaces command
  2. Select the first Surface
  3. Select the second (adjacent) Surface

This ended up not working, but the thought behind it was to select two surfaces in two separate transactions rather than selecting a collection of surfaces at the same time.

import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7')

import clr
from Autodesk.Revit.DB import *

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

from Autodesk.Revit.UI import *

from System.Collections.Generic import *

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

elements = UnwrapElement(IN[0])
selList = []

for elem in elements:
	tempList = List[ElementId]()
	selList.append(tempList) # List of ICollection of ElementId

command = PostableCommand.MergeSurfaces
merge = RevitCommandId.LookupPostableCommandId(command)

# Merge Toposurfaces
command = uiapp.PostCommand(merge)

# Select first Toposurface

# Select second Toposurface

# Pass Toposurfaces through
OUT = IN[0]


I think I need to subscribe to the Idling Event before selecting the surfaces. Do you know a way to code this?


That’s a little too deep into the “Iron” part of IronPython for me. It sounds like the right direction, but I haven’t interacted enough with other parts of the API to be able to provide any advice.


No worries, thank you for all your help so far! Looking forward to returning the favor.