List all User Selected Elements to Log File

Hello all,
I attach the image of my dynamo codes here. I want to extract the IDs of selected elements via dynamo. More specifically, if someone in Revit selects an element to modify it, we can use the code to export its ID in an excel file. (I suppose we need to run it with the automatic run setting). The problem is that “Select Model Elements” node does not work with automatic setting and I should select the elements from dynamo manually.
*This will help me to optimize my search algorithm to compare IFC files more effectively.

Sincerely,
Sobhan

Have you searched the forum for an answer? There are many topics on selecting elements by ID. Such as Use ID to select objects in Revit

Hello Ewan,
Thank you for the quick response. I searched the forum, but I didn’t find anything similar to my problem. it is the opposite of the post you have mentioned; in my problem, whenever the designer manually selects an object, Dynamo should return the element ID. I use this piece of code and run it periodically (every 1000ms) and it works for me:

import clr
clr.AddReference ( "RevitAPIUI" ) 
from Autodesk.Revit.UI import *

clr.AddReference( "RevitServices " ) 
import RevitServices 
from RevitServices.Persistence import DocumentManager 
doc = DocumentManager.Instance.CurrentDBDocument 
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument 

clr.AddReference("RevitNodes") 
import Revit 
clr.ImportExtensions(Revit.Elements)

sel = uidoc.Selection
obType = Selection.ObjectType.Element
ref = sel.PickObject(obType, "Select Element.")
element = doc.GetElement(ref.ElementId)

OUT = element 

However, it causes a problem for the designer; i.e. the revit environment will be always in the selection mode. I attached a picture to exemplify the problem for you (it is always in the selection mode).

So you want to essentially run a background process that makes a list of all the elements in the model that are modified by users? And use that list to highlight elements in between IFC model snapshots? Like what Navisworks is able to do but with Dynamo inside the Revit environment? Just clarifying :grinning:

1 Like

Yesss, that’s exactly it. :smiley:

Have a read of this discussion, it points to some intersesting options as well as considerarions for different workflows. Realtime tracking in revit creates a lot of ‘noise’ transaction data, but filtering out the ids would be possible.
I didn’t end up looking at a macro, but might take a quick look Monday. How to find who deleted elements

1 Like

The problem is solved; every element which is selected (Realtime tracking) will be shown in dynamo with its timestamp. I attached the code here.

x.dyn (2.6 KB)

1 Like

:+1: Nice Solution @sobhan.kouhestani are you going to combine it with a Write.To… node to create a running record? Which format do you think you will use? CSV, TXT, XLS, SQL?

Thanks!
I am trying to write it into XLS or CSV. Previously, I tried to write data into XLS format (using excel writer node in dynamo), but I could not save different data on the same sheet of the same file; it was erasing the former and saving the latter on it (so I was loosing the former). Also, I tried different things but didn’t work with this node.
Now, I want to use panda library of python to do this action, however Python node in dynamo doesn’t recognize this category. What’s your suggestion?

Have a quick read of this post. I can’t prep a test case using your code till tomorrow but this may give you some ideas on appending the new data. CSV and XLS are where I would be starting also. :+1:

The problem is not solved yet. There is no “passthrough” node in Dynamo, anymore; so, I am trying to use “transaction.start” and “transaction.end” to solve the problem.
Once It’s solved, I’ll share the code here.

You can write your own passthrough or wait node in a code block. It’s pretty simple.

{wait,passthrough}[1]

2 Likes

Have a go with this @sobhan.kouhestani :wink:

Element Selection Logger Code

# Original Script by EwanO 2018 (Sastrugi Package) 
# Use as you like :-)
# This code creates and appends items to a CSV file on the Desktop that
# lists elements fed to the input.

import clr
import System
from System.IO import Directory

import sys
# load python
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

import os

## set the output directory and gather log data
main_dir = 'C:\Users'
user = os.environ.get('USERNAME')
dir = main_dir + '\\' + user + '\\Desktop'

# generate the file name for the log file
eid=(IN[0])
filename = (str("Element_Log" + '.csv'))

# createss the log file (csv) and appends the input data.
fd = open(dir + '\\' + filename,'a')
fd.write(str(eid))
fd.close()

OUT = str("Element Logged")

3 Likes

This code works like a charm…
Thank you very much Ewan.

Topic renamed to improve forum search results :grinning:

1 Like