Comparing Element ID from 2 linked models

Hi all, I’m creating a model comparison script that compares the items in 2 revisions of linked models to identify new, moved or deleted elements.

I’ve generated a list of the element IDs of the relevant items from each model, but I’m after a simple way of comparing. I’m sure it must be simple, but I’m overcomplicating it.

How can I go about creating lists that compare list A & B:

  • Items in A but not in B (deleted items)
  • Items in B but not in A (new items)

I’d say you should go about this differently if you’re in Revit 2023 or higher. This document method will get you the changes between two version of a model: GetChangedElements Method

The result of it will return a DocumentDifference class, which can then be used to query new, deleted, and changed elements. No need to do any set comparisons.

If you are in 2022 or older the ‘big list’ process is the only real option though. For that use the following list nodes:

  • List.SetIntersection to return the items which are in both lists. Any of these could have been changed between the two versions, so you’ll have to check for geometry and parameter updates on the elements in both models. Note that this is a BIG ram task so I recommend C# if you can, python if not that. Don’t bother with nodes as there will be too much stuff in memory if your project is sufficiently sized.
  • List.SetDiffrence to return items which are in list A but not list B (reverse the inputs to get the in list B but not in list A).
2 Likes

Awesome thanks, I’m in Revit 2023 so I’ll look into the GetChangedElements method

1 Like

This might help. Pulled it out of my archives and cleaned it up a bit:

########################################
############## Properties ##############
########################################
__author__ = 'Jacob Small'
__version__ = '0.1.0'
__description__ = """Gets the modified elements between the current document and a path to a previous version of the same document."""
__RevitBuilds__ = "2023.1"
__DynamoBuilds__ = "2.15"
__ReleaseNotes__ = """-"""
__Dependancies__ = "Revit API 2023 or later"
__Copyright__ = "2024, Autodesk Inc."
__License__ = """MIT"""


########################################
################ Import ################
########################################
import sys #add the sys class to the Python environment so we can work with the sys objects
import clr #add the CLR (common language runtime) class to the Python environment so we can work with .net libraries
clr.AddReference("RevitNodes") #add Dynamo's Revit nodes library to the clr
import Revit #import Dynamo's Revit node class
clr.ImportExtensions(Revit.Elements) #add the element conversion methods to the CLR
clr.AddReference("RevitServices") #add the Revit services library to the CLR
import RevitServices #import the Revit services class to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment 
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #add the Autodesk class to the Python environment 
from Autodesk.Revit.DB import * #import every class of the Revit API to the Python environment
from System import Guid #import the .net GUID class into the Python environment

########################################
################# Code #################
########################################
results = {} #empty dictionary to hold the results

# get data from the current document and append the GUID as a string to the reuslts 
doc = DocumentManager.Instance.CurrentDBDocument #the current Revit document
currentVersion = Document.GetDocumentVersion(doc) #the current document version
currentVersionGuid = currentVersion.VersionGUID #the current document version GUID
currentVersionGuidString = str(currentVersionGuid) #the current document version GUID as a string
results["Document Version Guid String"] = currentVersionGuidString #add the current document verison GUID as a string to the results dictionary
currventVersionSaveNumber = currentVersion.NumberOfSaves #get the current version number
results["Current Save Number"] = currventVersionSaveNumber  #add the current save number ot the results dictionary

#get the old document information
oldDocPath = IN[0] #the old document version GUID as a string from the Dynamo environment
basicFileInfo = BasicFileInfo.Extract(oldDocPath) #get the basic file info from the old path
oldDocVersion = basicFileInfo.GetDocumentVersion() #get the document version from the basic file info
oldDocVersionGuid = oldDocVersion.VersionGUID #get the old document version guid
oldDocVersionGuidString = str(oldDocVersionGuid) #convert the old document version GUID to a string
results["Old document version GUID"] = oldDocVersionGuidString #add the old document version GUID to the results dictionary
oldDocVersionSaveCount = oldDocVersion.NumberOfSaves #get the number of saves at the old document version
results["Old Document Save Number"] = oldDocVersionSaveCount #add the old document save number to the results dictionary
saveDif = currventVersionSaveNumber-oldDocVersionSaveCount #get the number of saves between the two documents
results["Document Deltas"] = saveDif #Add the number of saves between the two document versions to the results dictionary

#get the document difference
docDif = doc.GetChangedElements(oldDocVersionGuid) #the document difference
#Get the new elements and append to the results
newIds = docDif.GetCreatedElementIds() #get the new element ids from the document difference
newElements = [doc.GetElement(i) for i in newIds] #get the new elements from the document
results["New Elements"] = newElements #add the new elemetns to the results dictionary
#get the modified elements and append to the results
modifiedIds = docDif.GetModifiedElementIds() #get the the modified element ids from the document difference 
modifiedElements = [doc.GetElement(i) for i in modifiedIds] #get the modified elements from the document 
results["Modified Elements"] = modifiedElements #add the modified elements to the reuslts dictionary 
#get the deleted element ids and append to the results
deletedIds = docDif.GetDeletedElementIds() #get the deletd element ids from the document difference 
results["Deleted Element Ids"] = deletedIds #append the element IDs to the results dictionary (not fetching the elements from the document as they no longer exist anyway

########################################
################ Result ################
########################################
OUT = results #return the results to the Dynamo environment
3 Likes