IFC Guid, Guid(DwfID), Element ID and Unique ID extraction by using Python and Revit API

Here is to share my successful result on extracting the above mentioned IDs, including IFC Guid, Guid(DwfID), Element ID and Unique ID. This python code is originated from [Bakery Archives » What Revit Wants](Bakery All Ids), while there is no mentioning in dynamo forum. Though the following code may have some unnecessary imports or clumsy codes, I hope you find it useful and get ready to continue your programme. Here is the perfect match of IDs between Navisworks Export file and Dynamo reading on Revit file.

import clr
clr.AddReference("RevitAPI")
import Autodesk 
from Autodesk.Revit.DB import *
clr.AddReference('RevitAPIIFC')
from Autodesk.Revit.DB.IFC import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

from System.Collections.Generic import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

inputdoc = UnwrapElement(IN[1])
if inputdoc == None:
	doc = DocumentManager.Instance.CurrentDBDocument
elif inputdoc.GetType().ToString() == "Autodesk.Revit.DB.RevitLinkInstance":
	doc = inputdoc.GetLinkDocument()
elif inputdoc.GetType().ToString() == "Autodesk.Revit.DB.Document":
	doc = inputdoc
else: doc = None

elementIds, uniqueIds, DwfGuids, IfcGuids, successlist = [], [], [], [], []
listIn = IN[0]
for i in listIn:
    try:
        elementIds.append(i.Id)
        uniqueIds.append(i.UniqueId)
        DwfGuids.append(str(ExportUtils.GetExportId(doc, ElementId(i.Id))))
        IfcGuids.append(ExporterIFCUtils.CreateSubElementGUID(UnwrapElement(i),0))
        successlist.append("Success")
    except:
        successlist.append("Failure")
OUT = elementIds, uniqueIds, DwfGuids, IfcGuids, successlist

I would like to ask if there is further improvement to make this code to a level-based code, maintaining the list level structure.

1 Like