1

I am trying to make a python list with the selected elements ID's in Revit API. I've tried to collect ID's of the grids in the sample structural file and then use this list back in Visual Studio Code. I am using Revit 2020 and IronPython 2.7.7 (2.7.7.0) on .NET 4.0.30319.42000 (64-bit).

I get a list of the ID's that I want when I run the code in IronPython, but how to make a list of the printed ID's for further use back in Visual Studio Code?

Attached image of result

Screenshot of code

My code:

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

app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document

transaction = Transaction(doc, "Get grids")
transaction.Start()

new_list = DB.FilteredElementCollector(doc) \
    .OfCategory(DB.BuiltInCategory.OST_Grids) \
    .ToElementIds()

for x in range(len(new_list)):
    new_list[x]
    print(new_list[x])


transaction.Commit()
  • What do you man by "Use this list back in VSCode?". If you have a separate python script in vscode, look into the python `pickle` module. You can write this list into a pickle file and load it in your other script – Ehsan Iran-Nejad Oct 26 '20 at 21:42
  • Thank you for the quick answer @EhsanIran-Nejad :) I have written the code in VSCode and I open the .py file in IronPython inside Revit and run it. I want to use this list of element ID's to do perform some actions to the elements. I thought it would be cool to get this list back to VSCode to continue the work in there. For me it is fine to just work with the list inside IronPython while experimenting with the pickle module. Can you guide me how to do create and use this list in IronPython inside Revit? – ExploringPython1 Oct 26 '20 at 22:14
  • 1
    Ehsan's direction is correct. Once your code is done running all values disappear from memory. To persist them and use it on another python execution environment, you will need to persist the values as a file in your hard drive. You can do that by 'dumping' the list out as a pickle file, or by serializing it using a format like json or csv. Search for how to 'dump python list' to file to any of those formats – gtalarico Oct 27 '20 at 02:51
  • 2
    Unrelated to your question: a transaction is not needed because you are not modifying the document. – gtalarico Oct 27 '20 at 02:52

1 Answers1

2

If I understand correctly, you're getting the list of Element IDs, but need to reference that information in another part of your logic, or a separate script altogether. I'd recommend saving this data to a file at the end of this operation, and then consuming that file in any other logic/script which needs the data.

Personally, I prefer saving/reading data to the JSON format. Here are some resources to help you get started:

  1. Read & Write JSON with Python
  2. How do I write JSON data to a file?
  3. Reading JSON from a file?
Petr Mitev
  • 21
  • 3