Hi I was hoping someone could help me with code below. I would like to delete all layer filters in a given drawing. The code I use below does not seem to work and I'm sure it's something simple. I know that I could possibly delete the entire dictionary but I am not sure how to do that, so was taking this approach. I am open to any and all suggestions.
Public Sub DeleteLayerFilters() Dim acCurDb As Database = Active.Database Dim acCurDoc As Document = Active.Document Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() Dim acFilTre As LayerFilterTree = acCurDb.LayerFilters Dim acFiltrs As LayerFilterCollection = acFilTre.Root.NestedFilters For Each acFilterName As LayerFilter In acFiltrs acFiltrs.Remove(acFilterName) Next acTrans.Commit() End Using End Sub
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
You do not need to start a transaction.
You cannot use a foreach statement to edit the iterated collection, use for or while instead.
You should check the LayerFilter.AllowDelete property before removing it.
You have to update the layer filter tree after having edited the filter collection
public static void RemoveAllFilters() { var db = HostApplicationServices.WorkingDatabase; var filterTree = db.LayerFilters; var filterCollection = filterTree.Root.NestedFilters; for (int i = 0; i < filterCollection.Count;) { var filter = filterCollection[i]; if (filter.AllowDelete) filterCollection.Remove(filter); else i++; } db.LayerFilters = filterTree; }
Gilles,
As always, thank you. That worked like a dream. I really appreciate it.
What kind of app would the code be for? Is it a script? or one that you netload? Sorry for the somewhat basic I am sure coding-ish question.
Thanks,
Andrea
I know this is an older post but I am running to an issue where all layer filters "allowdelete" are showing False unless I manually edit it then the code runs. any thoughts, how can I force the unlock?
Can't find what you're looking for? Ask the community or share your knowledge.