.NET

Reply
Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 1 of 10 (431 Views)
Accepted Solution

eInvalidInput thrown when trying to remove layerfilters

431 Views, 9 Replies
10-10-2013 11:45 AM

I am trying to remove all unprotected layer filters from a DWG and getting the following result:

 

Command: CLEANLF
Deleting layerfilter[0]: 'Test1'...
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
   at Autodesk.AutoCAD.LayerManager.LayerFilterCollection.Remove(LayerFilter value)
   at wauCmds.WauCmd.cleanLF_Method()
Deleting layerfilter[1]: 'Test2'...
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
   at Autodesk.AutoCAD.LayerManager.LayerFilterCollection.Remove(LayerFilter value)
   at wauCmds.WauCmd.cleanLF_Method()
Deleting layerfilter[2]: 'Group Filter1'...
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
   at Autodesk.AutoCAD.LayerManager.LayerFilterCollection.Remove(LayerFilter value)
   at wauCmds.WauCmd.cleanLF_Method()
Layerfilter 'All Used Layers' is protected
Layerfilter 'Unreconciled New Layers' is protected
Layerfilter 'Viewport Overrides' is protected
Layerfilter 'Xref' is protected

 

Here is the code that produced this output.  Any ideas why the Remove throws this exception?

 

    <CommandMethod("cleanLF")> _
    Public Sub cleanLF_Method()
        Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        Using docLock As DocumentLock = acDocMgr.MdiActiveDocument.LockDocument
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                For i As Int32 = 0 To db.LayerFilters.Root.NestedFilters.Count - 1
                    Dim deleteMe As LayerFilter = db.LayerFilters.Root.NestedFilters(i)
                    If deleteMe.AllowDelete Then
                        Try
                            ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'...", i, deleteMe.Name) + vbCrLf)
                            db.LayerFilters.Root.NestedFilters.Remove(deleteMe)
                        Catch ex As Exception
                            ed.WriteMessage(ex.ToString + vbCrLf)
                        End Try
                    Else
                        ed.WriteMessage(String.Format("Layerfilter '{0}' is protected", deleteMe.Name) + vbCrLf)
                    End If
                Next
            End Using
        End Using
    End Sub


 

 ps I am running AutoCAD 2013 SP1.1 PV: G.114.0

 

Hi Steve,

 

Here is a sample code to delete the first deletable layer filter from the Layer filters collection. 

 

 <CommandMethod("Test")> _
Public Sub TestMethod()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim lft As Autodesk.AutoCAD.LayerManager.LayerFilterTree = db.LayerFilters
Dim lfc As Autodesk.AutoCAD.LayerManager.LayerFilterCollection = lft.Root.NestedFilters
For i As Int32 = 0 To db.LayerFilters.Root.NestedFilters.Count - 1
Dim lf As Autodesk.AutoCAD.LayerManager.LayerFilter = lfc(i)
If lf.AllowDelete Then
Try
ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'...", i, lf.Name) + vbCrLf)
lfc.Remove(lf)
Exit For
Catch ex As Exception
ed.WriteMessage(ex.ToString + vbCrLf)
End Try
Else
ed.WriteMessage(String.Format("Layerfilter '{0}' is protected", lf.Name) + vbCrLf)
End If
Next
db.LayerFilters = lft
End Sub

 

ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 2 of 10 (383 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 03:12 AM in reply to: sszabo

Hi,

 

Note these lines of code in this blog post : http://through-the-interface.typepad.com/through_the_interface/2008/07/adding-and-remo.html

 

<<<

lfc.Remove(lf);

db.LayerFilters = lft;

>>>

 

It is a local copy of the "LayerFilterTree" that is modified by removing a layer filter and then set back to database.

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 3 of 10 (371 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 05:37 AM in reply to: Balaji_Ram

I changed the code as such, same result:

 

    <CommandMethod("cleanLF")> _
    Public Sub cleanLF_Method()
        Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        Using docLock As DocumentLock = acDocMgr.MdiActiveDocument.LockDocument
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim lft As LayerFilterTree = db.LayerFilters
                For i As Int32 = 0 To db.LayerFilters.Root.NestedFilters.Count - 1
                    Dim deleteMe As LayerFilter = db.LayerFilters.Root.NestedFilters(i)
                    If deleteMe.AllowDelete Then
                        Try
                            ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'", i, deleteMe.Name) + vbCrLf)
                            db.LayerFilters.Root.NestedFilters.Remove(deleteMe)
                        Catch ex As Exception
                            ed.WriteMessage(ex.ToString + vbCrLf)
                        End Try
                    Else
                        ed.WriteMessage(String.Format("Layerfilter '{0}' is protected", deleteMe.Name) + vbCrLf)
                    End If
                Next
                db.LayerFilters = lft
            End Using
        End Using
    End Sub

 I also tried adding tr.Commit() after the for loop, no change, the filters are still there.  There is an autocad dialog box that kicks in automatically when I try to open a drawing with 20000 layer filters and it cleans it up correctly.  I just need the same functionality through the API to prevent drawings from reaching that point to begin with.

 

ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 4 of 10 (364 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 06:10 AM in reply to: sszabo

Hi Steve,

 

Here is a sample code to delete the first deletable layer filter from the Layer filters collection. 

 

    <CommandMethod("Test")> _
    Public Sub TestMethod()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Dim lft As Autodesk.AutoCAD.LayerManager.LayerFilterTree = db.LayerFilters
        Dim lfc As Autodesk.AutoCAD.LayerManager.LayerFilterCollection = lft.Root.NestedFilters

        For i As Int32 = 0 To db.LayerFilters.Root.NestedFilters.Count - 1
            Dim lf As Autodesk.AutoCAD.LayerManager.LayerFilter = lfc(i)
            If lf.AllowDelete Then
                Try
                    ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'...", i, lf.Name) + vbCrLf)
                    lfc.Remove(lf)
                    Exit For
                Catch ex As Exception
                    ed.WriteMessage(ex.ToString + vbCrLf)
                End Try
            Else
                ed.WriteMessage(String.Format("Layerfilter '{0}' is protected", lf.Name) + vbCrLf)
            End If
        Next

        db.LayerFilters = lft

    End Sub

 



Balaji
Developer Technical Services
Autodesk Developer Network

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 5 of 10 (356 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 08:28 AM in reply to: Balaji_Ram

Ok, thanks Balaji: your code sortof works. 

 

Removing the Exit For still leaves about half of the filters intact but if I nest the for loop in a while loop and do multiple passes it does remove all layerfilters. 

 

What made the difference is the lfc variable.  The original code works by simply assigning and using this variable, the last assignment (db.LayerFilters = lft) was not necessary:

 

        Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim acDocMgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        Using docLock As DocumentLock = acDocMgr.MdiActiveDocument.LockDocument
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim lft As LayerFilterTree = db.LayerFilters
                Dim lfc As LayerFilterCollection = lft.Root.NestedFilters
                For i As Int32 = 0 To lfc.Count - 1
                    Dim deleteMe As LayerFilter = lfc(i)
                    If deleteMe.AllowDelete Then
                        Try
                            ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'", i, deleteMe.Name) + vbCrLf)
                            lfc.Remove(deleteMe)
                        Catch ex As Exception
                            ed.WriteMessage(ex.ToString + vbCrLf)
                        End Try
                    Else
                        ed.WriteMessage(String.Format("Layerfilter '{0}' is protected", deleteMe.Name) + vbCrLf)
                    End If
                Next
            End Using
        End Using

 Thanks again for your help.

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 6 of 10 (347 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 09:05 AM in reply to: Balaji_Ram

One last thing to qualify my previous statement: that last assignment (db.LayerFilters = lft) was necessary for the while loop I mentioned above to clean up left overs in multiple passes.  Just to clarify this here is the solution that seems to be able to remove all layerfilters:

 

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim ed As Editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor
        Dim done As Boolean = False

        While Not done
            Dim lft As LayerFilterTree = db.LayerFilters
            Dim lfc As LayerFilterCollection = lft.Root.NestedFilters
            Dim filtersLeft As Int32 = 0
            For i As Int32 = 0 To lfc.Count - 1
                Dim lf As LayerFilter = lfc(i)
                If lf.AllowDelete Then
                    Try
                        ed.WriteMessage(String.Format("Deleting layerfilter[{0}]: '{1}'...", i, lf.Name) + vbCrLf)
                        lfc.Remove(lf)
                    Catch ex As Exception
                        ed.WriteMessage(ex.ToString + vbCrLf)
                    End Try
                Else
                    ed.WriteMessage(String.Format("Layerfilter '{0}' is protected. AD={1}",
                                                  lf.Name, lf.AutoDelete) + vbCrLf)
                End If
            Next
            db.LayerFilters = lft
            For i As Int32 = 0 To lfc.Count - 1
                Dim lf As LayerFilter = lfc(i)
                If lf.AllowDelete Then
                    filtersLeft += 1
                    ed.WriteMessage(String.Format("LEFT layerfilter[{0}]: '{1}'...", i, lf.Name) + vbCrLf)
                End If
            Next
            If filtersLeft = 0 Then
                ed.WriteMessage(String.Format("DONE! filtersleft={0}", filtersLeft) + vbCrLf)
                done = True
            End If
        End While

 

If there is a less messy more elegant solution that doesn not necessitate multiple passes please let me know.

 

Thanks,

 

Steve

 

ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 7 of 10 (329 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-14-2013 10:30 PM in reply to: sszabo

Hi Steve,

 

Can you try this ?

 

    <CommandMethod("DeleteAllLayerFilters")> _
    Public Sub DeleteAllLayerFilters()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Dim lft As Autodesk.AutoCAD.LayerManager.LayerFilterTree = db.LayerFilters
        Dim lfc As Autodesk.AutoCAD.LayerManager.LayerFilterCollection = lft.Root.NestedFilters

        Dim totalFilters As Integer = lfc.Count
        Dim i As Integer = 0
        While i < totalFilters
            Dim lf As Autodesk.AutoCAD.LayerManager.LayerFilter = lfc.Item(i)
            ed.WriteMessage([String].Format("{0} Checking {1}...", Environment.NewLine, lf.Name))
            If lf.AllowDelete Then
                lfc.Remove(lf)
                totalFilters = lfc.Count
                i -= 1
                ed.WriteMessage([String].Format("{0} Deleted {1}", Environment.NewLine, lf.Name))
            End If
            i += 1
        End While

        db.LayerFilters = lft
    End Sub

 

Regards,



Balaji
Developer Technical Services
Autodesk Developer Network

Valued Contributor
sszabo
Posts: 86
Registered: ‎09-05-2012
Message 8 of 10 (318 Views)

Re: eInvalidInput thrown when trying to remove layerfilters

10-15-2013 06:52 AM in reply to: Balaji_Ram

Balaji,

 

This is a very elegant fast algorithm, kudos!

 

Thanks a bunch,

Steve

Member
geenakv
Posts: 4
Registered: ‎01-27-2014
Message 9 of 10 (251 Views)

Can u Help Me-AutoDesk Group

01-27-2014 02:17 AM in reply to: sszabo

Hi Friends..!!!!

I am trying to do 1 project using vb.net and AutoCAD. In the drawing we have multiple layers and should separately store the Layer blocks with the block names in an Excel Application. I stored the Layer names.But all the entities in the drawing will be stored in the Excel file. I need one by one.

Thanks & Regards
Geena..
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 10 of 10 (241 Views)

Re: Can u Help Me-AutoDesk Group

01-27-2014 02:57 AM in reply to: geenakv
Hi geenakv,
you should create a new message for your Problem. Please create a new thread in this case. Other People will try to help you. you may notice that this thread has already been marked solved so nobody will look into your message. And please try to make clear what do you want actually. I am Little bit confuse what do you really want.
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.