.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete All Layer Filters

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
David_Prontnicki
1225 Views, 4 Replies

Delete All Layer Filters

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
4 REPLIES 4
Message 2 of 5
_gile
in reply to: David_Prontnicki

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 Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5
David_Prontnicki
in reply to: _gile

Gilles,

As always, thank you. That worked like a dream. I really appreciate it. 

Message 4 of 5

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

Message 5 of 5
Ender157
in reply to: David_Prontnicki

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.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report