Python code, GetElementByID

Hi,
Would need your help.

I don’t usually write python code and I’m not that comfortable with it, at some point you have to challenge yourself.

When I run the script in the python script module in dynamo, I just get PythonEvaluator.Evaluate: invalid syntax

Could someone who has the habit help me?

Would like to retrieve elements that hold an ID.

Thanks in advance

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
ElementsMatch = []
ElementIDn = IN[0]

if isinstance(ElementIDn, str):
    ElementsMatch = doc.GetElement(ElementIDn)
    elif isinstance(ElementIDn, float):
    integerValue = int(ElementIDn)
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(integerValue))
    elif isinstance(ElementIDn, int):
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(int(ElementIDn)))
else:
ElementsMatch = doc.GetElement(ElementIDn)
OUT = ElementsMatch

You’re missing a lot of indents. Anything under a loop (if / elif / else) needs to be indented.

1 Like

Hello,
here is a possibility
with the indents and if you have a list

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

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

doc = DocumentManager.Instance.CurrentDBDocument
#Inputs
ElementsMatch = []
Elem = IN[0]
for i in Elem:
    if isinstance(i,int) or isinstance(i,str):
        a=doc.GetElement(ElementId(int(i)))
        ElementsMatch.append(a)
    else:
        a=doc.GetElement(i)
        ElementsMatch.append(a)
OUT =ElementsMatch

cordially
christian.stan

1 Like

Hi @Nick_Boyts

I’ve tried different options for indents, if I have too many it says that there are too many and if I have too few it says.

This setup is where it doesn’t say anything, but just gives an error message

You have to show us what it is you’re doing, otherwise we have no context. @christian.stan showed you the correct indenting. What is that getting you?

1 Like

Hi @christian.stan,

Thanks, I’ll givit a try tomorrow when back at the Office, I’ll get back to you.

@Nick_Boyts and @christian.stan

What program do you use when you write code, I’ve tried various online tools but it still gets wrong with formatting.

Can you recommend any free software

1 Like

If I’m writing for Dynamo I just use the Dynamo Python editor. If it’s a lot of code and I’m dealing with something I’m unfamiliar with I might use Visual Studio Code for some additional context.

However, none of these are a replacement for knowing the formatting and syntax. That’s up to you.

3 Likes

given my level, I’m doing with dynamo’s python editor, I can’t wait to go under 2024 to be able to zoom in the editor
Sincerely
christian.stan

1 Like

I recommend looking into pyCharm, it’s a free and solid IDE. Generally I switch to it when writing Python even if in Dynamo these days, although you lose some context that Dynamo provides when querying methods, properties etc.

1 Like

Hi @Gavin, @christian.stan and @Nick_Boyts

Thanks for the information about software.

Unfortunately, I have big problems with IT restrictions that set group policies to not be able to run scripts on the local computer.

But that’s my problem…

I’ll look into PyCharm and Visual Studio Code.

Which version of Ironpython, for example, works with Revit intelligence, is it 2.7 or should you always run on the latest version.

If I remember correctly, it is something that is not supported if you install the wrong version.

I’d recommend that one too.
Or regular Visual Studio.

1 Like

Well, there are some logical error with your code, you can try below code it may resolve your error.

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB 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
ElementsMatch = []
ElementIDn = IN[0]

if isinstance(ElementIDn, str):
    ElementsMatch = doc.GetElement(ElementIDn)
elif isinstance(ElementIDn, float):
    integerValue = int(ElementIDn)
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(integerValue))
elif isinstance(ElementIDn, int):
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(int(ElementIDn)))
else:
    ElementsMatch = doc.GetElement(ElementIDn)

OUT = ElementsMatch

Thanks

1 Like

there are quite a few problems with your script.
first is about indentation, you should learn about if else structure to solve this problem
The next thing I’m understanding is that you want to retrieve elementID from a list of input data to ElementIDn.
There are 2 cases
Case 1, IN[0] is a list, then your loop is invalid, because you only check it with data types string, float and int so it won’t return any value, you need to use a loop to iterate through the item in the list
Case 2, IN[0] has only 1 element then maybe it will match 1 of the datatypes you gave but the methods you gave don’t exist in Revit API document, Autodesk.Revit.DB.ElementId(int32) is an constructor, not a method to retrieve element from ID

1 Like

@negigulshan903

Thanks, still have som trouble and that @thanhdatp297GRTHP mension.

Your program has complications that I don’t think are necessary, According to your purpose, I created a program that can do that with simple logic,

  1. You just need to check if the input is a list, if true: contunute with this one, if false: throw it in a list
  2. Used Try Except Syntax to remove exceptions such as float or string and get results
    You can try this.
    GetElementById.dyn (8.7 KB)
import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import ElementId
from Autodesk.Revit.DB import Document
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
clr.AddReference("System")
from System.Collections.Generic import List


doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
ids = IN[0]

# if it's not a list, make it a list
if not isinstance(ids, list):
	ids = [ids]

# convert to element ids

elements = []
for id in ids:
    try:
        eleid = ElementId(id)
        element = doc.GetElement(eleid)
        elements.append(element)
    except:
        pass

OUT = elements
1 Like

Friendly reminder that for the Dynamo python interpreter indents have to be tabs or 4 spaces you can’t have both in the one script

2 Likes

Thank you so much, you are a rock, it works perfectly.

Really appreciate this, now I can compare my code against yours and check the API at the same time.