Can't delete element because of ID issue

#1

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

#2

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

#3

@koclukasz
First loop is a bit funny.

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

#4

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?

#5

@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.

#6

Just try getting Titleblocks directly from the sheet. Maybe see this post for information.

#7

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:

#8

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()

`

#9

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.

#10

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())
#11

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: