Can't delete element because of ID issue

Hi,
I have some issues with deleting sheets with python code. Code is similar with the archi-lab node but i have added some additional functionality to this. Error message is shown in the bottom.

TransactionManager.Instance.EnsureInTransaction(doc)

# Obtaining titleblocks
for sheet in shts:
	trans = SubTransaction(doc)
	trans.Start()
	elements_on_sheet_ids.append(doc.Delete(sheet.Id))
	trans.RollBack()
TransactionManager.Instance.TransactionTaskDone()

for i in elements_on_sheet_ids:
    for j in i:
        elements_on_sheet.append(doc.GetElement(j))

for i in elements_on_sheet:
    if i.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM).AsValueString() == "Title Blocks":
        titleblocks.append(i)

TransactionManager.Instance.EnsureInTransaction(doc)

This error seems to disappear when I restart my computer. Do you have any solution for this to work every time?

Error in Dynamo:
image

Hi,
why do you use sub transaction at all? First loop is quite strange

@koclukasz
First loop is a bit funny.

Where is the rest of your code. Is line 37 that loop?

Are you just trying to get all the items on the sheet? If so, I think there would be a better way to do this then pseudo deleting the sheet like this. What is your goal here?

@SeanP
Yes I am just trying to get elements on sheet because I want to get titleblock located on sheet.
@Tomasz_Puchala
Sub transaction is just to delete elements and restore them. This is the approach that was used in archi-lab node “Elements on Sheet”
Rest of my code is not important because this is very long and does not affect first few lines.

Just try getting Titleblocks directly from the sheet. Maybe see this post for information.
https://forum.dynamobim.com/t/get-sheet-instances-from-titleblock/28189/2?u=seanp

Yes I know this solution because I already used this. However right now I want to use python code for this not just a custom node. I am trying to learn python a bit and I don’t know why it does not work properly.
Thanks for your input :slight_smile:

Have you tried opening up the custom node to see how it works? Often times it is python inside as you require. Just double click and look around.

Also, since Sheets are really views, you can use a Filtered Collector like this
TB = FilteredElementCollector (doc,Sheet.Id).Of Category(BuiltInCategory.OST_TitleBlocks).ToElements()

`

Yes but right now my input is just a few sheets from which I want to grab titleblocks. I dont need to collect all titleblocks available in the project.
So to sum up:
I have list of sheets -> I want to have list of titleblocks located on these sheets ( there might be different sizes of titleblocks)

I want to do this because I am writing a code to copy selected sheets with views and legends for next stage of the project and keep the old set present in the Revit file.

Correct. The code I supplied will only get the Titleblock(s) on the sheet supplied. That is why the Sheet.Id is used in the collector. That limits the collector results to only the view (in this case sheet) supplied.

for sheet in shts:
    titleblocks.append(FilteredElementCollector(doc,sheet.Id).OfCategory(BuiltInCateogry.OST_TitleBlocks).ToElements())

Oh great! i didnt notice this. Thanks for your help. Very appreciated!

This solves my problem but still doesnt solve the issue stated in the first post :smiley: