Deleting Titleblocks

Deleting Titleblocks

will.wydock
Advocate Advocate
559 Views
3 Replies
Message 1 of 4

Deleting Titleblocks

will.wydock
Advocate
Advocate

We often want to delete all titleblocks from sheets for various reasons. I am trying to create a routine that deletes titleblocks from the model. What I have put together seems to only work if the model only contains 1 family to delete otherwise the model just spins. Any idea what would be causing this?

 

		public void deleteTB()
		{
			UIDocument uidoc = this.ActiveUIDocument;
			Document doc= uidoc.Document;
			bool deleteStartViewTB = true;
			int categoryid = (int)BuiltInCategory.OST_TitleBlocks;
			IEnumerable<Family> collector = new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>().Where(q=>q.FamilyCategoryId.IntegerValue==categoryid);
			foreach (Family family in collector)
			{
				using(Transaction T = new Transaction(doc, "Delete titlblock"))
				{
					try
					{
						T.Start();
						if(deleteStartViewTB)
						{
							doc.Delete(family.Id);
						}
						else
						{
							if(!family.Name.StartsWith("Starting View"))
							{
							   	doc.Delete(family.Id);
							}
						}
						T.Commit();
					}
					catch (Exception ex) 
					{
						TaskDialog.Show("Error", ex.Message);
					}
				}
			}
		}

 

0 Likes
Accepted solutions (2)
560 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

You have wrapped each individual call to Delete an element one by one inside its own transaction, inside a loop over elements.

  

I would suggest restructuring that by first collecting all the elements you wish to delete, storing their element ids in a list, and then, right at the end, starting a transaction with one single call to Delete, passing in the entire list at once.

  

https://www.revitapidocs.com/2020/f4ce9113-b164-954e-5025-7b4edbdcc07d.htm

 

That will be cleaner and faster.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

TripleM-Dev.net
Advisor
Advisor
Accepted solution

The "spinning"/hanging of the model occurs because the collection is changed during the loop.

The deletion of the element causes the Collection to change, don't change a collection while going thruw it's collection, or convert it first.

(I actually expected a crash with this...)

 

In the Foreach loop collect all id's in one list, and use delete after the loop has ended!

As @jeremytammik also suggested.

 

I also see you actually delete the family from the Project, deleting the familyInstances not enough?

 

Message 4 of 4

so-chong
Advocate
Advocate

Hi,

 

In addition to Jeremy's and TripleM-Dev.net's replies, did you forgot to use the ToList() Method in your code?

 

And would this solve your problem?

 

IEnumerable<Family> collector = new FilteredElementCollector(doc)
	.OfClass(typeof(Family))
	.Cast<Family>()
	.Where(q=>q.FamilyCategoryId.IntegerValue==categoryid)
	.ToList();

 

 

 

0 Likes