Search code examples
vbaautocad

Delete closed Polylines in AutoCAD


I'm Trying to delete all Plines that are not closed in a certain layer. But my code is not working.

I get an error Type Mistype at If LWobj.Closed = False And LWobj.Layer = "0_String" Then

The Layer "0_String" Exists in drawing and LWobj closed state is derive correctly.

Sub clean()

Dim LWobj As AcadLWPolyline

For Each LWobj In ThisDrawing.ModelSpace
    If LWobj.Closed = False And LWobj.Layer = "0_String" Then
        LWobj.Delete
        ThisDrawing.Regen acActiveViewport
    End If
Next

End Sub

Solution

  • The issue is that you have defined LWobj as an AcadLWPolyline, but not all objects in the Modelspace container will be of type AcadLWPolyline.

    Instead, you should either use a selection set filter to acquire a set of only LWPolyline objects, or define LWobj as an AcadEntity and then check the object type along with your other conditions.

    For example:

    Dim ent As AcadEntity
    For Each ent In ThisDrawing.ModelSpace
        If TypeOf ent Is AcadLWPolyline Then
            If ent.Closed = False And ent.Layer = "0_String" Then
                ent.Delete
            End If
        End If
    Next
    

    You also don't want to be performing a regen with every iteration!