Python - use element name to get element ID

Hi all,

I need to get the element’s IDs from the list using python.

What I am doing is not working. Any help?


import clr
import sys
import System
from System import Int16, Int32, Int64, String
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

def getElementByValue(v):
	# if value is ElementId or Reference object
	if isinstance(v, (ElementId, Reference)):
		return doc.GetElement(v)
	# if value is a integer
	elif isinstance(v, (int, Int16, Int32, Int64)):
		return doc.GetElement(ElementId(v))
	# if value is a String 'integer'
	elif isinstance(v, (str, String)) and str(v).isdigit():
		return doc.GetElement(ElementId(int(v)))
	# if value is a String Unique Id
	elif isinstance(v, (str, String)) and '-' in str(v):
		return doc.GetElement(v)

ids = IN[0]
out = []

for id in ids:
	out.append(getElementByValue(id))

OUT = out
1 Like

@aafonsoBYKA8 ,

You just get Ids from the element not from the name…

(here is your code pasted)

import sys
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

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

clr.AddReference('System')
from System.Collections.Generic import List

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def getElementByValue(v):
# if value is ElementId or Reference object
    if isinstance(v, (ElementId, Reference)):
        return doc.GetElement(v)
# if value is a integer
    elif isinstance(v, (int, Int16, Int32, Int64)):
        return doc.GetElement(ElementId(v))
# if value is a String ‘integer’
    elif isinstance(v, (str, String)) and str(v).isdigit():
        return doc.GetElement(ElementId(int(v)))
# if value is a String Unique Id
    elif isinstance(v, (str, String)) and ‘-’ in str(v):
        return doc.GetElement(v)

ids = IN[0]
out = []

for id in ids:
    out.append(getElementByValue(id))

OUT = out

Hello,
an example using FilteredElementCollector with filter

import clr
import System
from System.Collections.Generic import List
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument


def get_ElementIds_from_name(name):
    rule = ParameterFilterRuleFactory.CreateEqualsRule(ElementId(BuiltInParameter.ELEM_TYPE_PARAM), name, True)
    filt = ElementParameterFilter(rule)
    return FilteredElementCollector(doc).WherePasses(filt).WhereElementIsNotElementType().ToElementIds()

OUT = [get_ElementIds_from_name(name) for name in IN[0]] 
3 Likes

I have several walls, with diferent types. I need to get the IDs of all walls with the prefix “NLA_42”.

1 Like

@aafonsoBYKA8 ,

Can you show me the python script for Get ID by name?

1 Like
import clr
import System
from System.Collections.Generic import List
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument


def get_ElementIds_from_name(name):
    rule = ParameterFilterRuleFactory.CreateEqualsRule(ElementId(BuiltInParameter.ELEM_TYPE_PARAM), name, True)
    filt = ElementParameterFilter(rule)
    return FilteredElementCollector(doc).WherePasses(filt).WhereElementIsNotElementType().ToElementIds()

OUT = [get_ElementIds_from_name(name) for name in IN[0]] 

it is the same from @c.poupin