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.

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]
1 Like
#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.

#6

So subscribing to the Idling Event is not possible in Dynamo so that will not work. I still need a solution.

I’m attempting to use the “Merge surfaces” postable command followed by a selection of the objects for the tool to execute.

Anyone have any ideas on how to solve this issue?

Thank you!

#7

Hey,

So as you have found, I believe the postable command route is a dead end.

I would look for a similar method, unfortunately I think you are quite limited with topos (hence why you were looking at the postable)

This means your solution is likely not ideal.

For example you could extract the topo points from the originals and create a new single topo?

If this is not satisfactory I would suggest uploading a couple of stripped back test files for us to play with.

Apologies for not being more help!

Mark

1 Like
#8

Mark,

Thank you for your response. I was successful at extracting the points and creating a new topo; however, I still ran into the problem of the topo tool generating convex hulls instead of concave.

So the only way to generate concave hulls is through the split surface tool; and the only way to access the split surface tool is through the postable command method. So I’m curious if that method requires some special workflows to use (I attempted a python multi-thread w/ time modules “sleep” method to delay until the postable command ran but was unable to get working). Otherwise I think we will have to wait for an update in the API to use these tools unfortunately.

Does that make sense to you? Do you have any additional thoughts?

I could put together a test file for this thread but it would have to wait until later today. I really would like to get this figured out I’ve been stuck on this and would like some closure. Thanks again!

#9

Hey,

I’ve tried to get PCs to work, here’s a thread with me being shot down! :smiley:

In that situation there were other methods we could use to get a similar result.

Just as a point of interest, I noticed that you can extract just the boundary points from the topo, is there perhaps a way of refining the points you’re using with other methods then feeding them back in?

As I say it would be easier to discuss with a simple test file.

Cheers,

Mark

Edit: There is also an Add Points method… can you back the points to make it concave after perhaps? Or move points?

Edit edit:

Literally just seen this… https://thebuildingcoder.typepad.com/blog/2019/04/new-revit-2020-sdk-samples.html concave topography in the api?