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

python

#1

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
    or
  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!


#2

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
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference('RevitAPIUI')
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]()
	tempList.Add(elem.Id)
	selList.append(tempList) # List of ICollection of ElementId

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

# Merge Toposurfaces
TransactionManager.Instance.EnsureInTransaction(doc)
command = uiapp.PostCommand(merge)
TransactionManager.Instance.TransactionTaskDone()

# Select first Toposurface
TransactionManager.Instance.EnsureInTransaction(doc)
uidoc.Selection.SetElementIds(selList[0])
TransactionManager.Instance.TransactionTaskDone()

# Select second Toposurface
TransactionManager.Instance.EnsureInTransaction(doc)
uidoc.Selection.SetElementIds(selList[1])
TransactionManager.Instance.TransactionTaskDone()

# Pass Toposurfaces through
OUT = IN[0]

#3

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


#4

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.


#5

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