Thursday, August 19, 2010

Deleting a bunch of Elements By ElementId in RevitPythonShell

I know I promised examples for RevitPythonShell and never delivered. Well, here is an example I came across this morning:

I had to delete a bunch of elements from a document. I new the ids of the elements and was about to manually delete them using the "Select by ID" tool in Revit and deleting them one by one. I know this works, because I did the exact same thing yesterday on the exact same document, but forgot to save the changes. I'd just have to do all that tedious work again.

But then I thought: Wait a minute... aren't we supposed to be scripting this stuff?

So, here is a session in the shell part of RevitPythonShell

>>>ids = [136116, 136119, 136120, 136121, 136122, 136131, 136132, 136133, 136134, 136135, 136136, 136137, 136138, 136139, 136140, 136141, 136142, 136143, 136144, 136145, 136155, 136165, 136206, 136306, 136309, 136419, 136422]

>>>import clr

>>>clr.AddReference("RevitAPI")

>>>from Autodesk.Revit.DB import *

>>>transaction = Transaction(doc, "find all the mutants!")

>>>transaction.Start()

>>>for id in ids:
...    doc.Delete(doc.get_Element(ElementId(id)))
...
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002D [136116]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002E [136119]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002F [136120]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000030 [136121]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000031 [136122]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000032 [136131]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000033 [170627]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000034 [136132]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000035 [136133]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000036 [136134]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000037 [170628]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000038 [136135]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000039 [136136]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003A [136137]>, <Autodesk.Revit.DB.ElementId object at 0x000000000000003B [170629]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003C [136138]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003D [136139]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003E [136140]>, <Autodesk.Revit.DB.ElementId object at 0x000000000000003F [170630]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000040 [136141]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000041 [136142]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000042 [136143]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000043 [170631]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000044 [136144]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000045 [136145]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000046 [136155]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000047 [136165]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000048 [136206]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000049 [136306]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004A [136309]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004B [136419]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004C [136422]>])
>>>transaction.Commit()

<Autodesk.Revit.DB.TransactionStatus object at 0x000000000000004D [Committed]>
>>>

Here a few notes that might illuminate some parts of working with RevitPythonShell:

  • access to the Revit API is added by the following lines
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
  • the document is provided via the __revit__ variable
doc = __revit__.ActiveUIDocument.Document
  • you need to start a transaction before changing the document
transaction = Transaction(doc, "call this whatever you like")
transaction.Start()
# do stuff
transaction.Commit()
  • to get an element given an integer, use this technique
element = doc.get_Element(ElementId(myinteger))
  • Document.Delete() returns a list of deleted elements on each call. That is what the clutter (List[ElementId]([Autodesk.Revit.DB.ElementID object at...) is all about in the session transcript above.

2 comments:

  1. Hi @Daren@Work, As we know we have method ViewSheet.DeleteViewPort() method to remove ViewPort from sheet, but i can not find method to remove ScheduleInstance from Sheet, I also try doc.Delete(elementId) method, but it’s not work . So how i can delete it from sheet ???

    ReplyDelete
  2. Hi Tran, huh... I have no idea. You could try to ask on StackOverflow.com - be sure to use the "revitapi" tag.

    ReplyDelete