How to use Revit api "Documentedchange Event" in Python?

I use Revit api “PostableCommand.DetailLine” to draw detailine,I want to retrieve the newly created element, it described here:

I try to convert it to python, but it doesn’t work,I got empty list, I think that I’m doing it wrong here
Any help would be much appreciated

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import TaskDialog
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Events import * 
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB.Events import *
from Autodesk.Revit.DB import * 
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Collections.Generic import List
import System
from System.Windows.Forms import *
from System.Drawing import *

def OnDocumentChanged (sender, e): 
	collection = e.GetAddedElementIds()
	#listId.Add(collection.ElementAt(0))
	listId.AddRange(e.GetAddedElementIds())
	

uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application       
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
listId = List[ElementId]()

TransactionManager.Instance.EnsureInTransaction(doc) 

uiapp.Application.DocumentChanged += OnDocumentChanged
uiapp.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.DetailLine))
uiapp.Application.DocumentChanged -= OnDocumentChanged

TransactionManager.Instance.TransactionTaskDone()

OUT = listId

Hi @urdiafa, I know its an old post but were you able to find a solution to this?
Thanks

@Shambhavee @urdiafa ,

is this a “event” for you to see that transaction happend?

Hi @Draxl_Andreas ,

Thanks for replying, I should have made myself clear :sweat_smile:
I was trying to look for a way to use Application Events in IronPython, which in this case is DocumentChange Event so I can implement it on other events. Let me know if you have a solution to this. I see a lot of examples in C# but as I’m fairly new to Revit API as well as coding so I’m currently able to execute Revit API through python.

I’m basically facing the same issue as in this article:

and I’m trying to use DocumentOpening and DocumentOpened Events and see if suppressing these popups is possible.

I’m currently refering to this article to try and convert it to python:
https://knowledge.autodesk.com/support/revit/learn-explore/caas/CloudHelp/cloudhelp/2014/ENU/Revit/files/GUID-8DF6F94A-1460-42AD-AC00-CF92B9A135BA-htm.html

Thanks in Advance!
Shambhavee

Dynamo generally doesn’t work in a way which supports events to my knowledge.

It would probably be possible using an addin to load dynamo and run a script when an event is detected, but unless it’s an infrequent event it might cause issues or just slow the overall experience. At that point you’d likely just code the addin to run code after the event anyway.

Maybe look into pyRevit and its ‘hook’ system which enables scripts written in Python whilst detecting events.