+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Junior Member
    Using
    AutoCAD 2008
    Join Date
    Mar 2008
    Posts
    14

    Default VBA - How to delete all lines on a layer

    Registered forum members do not see this ad.

    Hello,


    Q1. I am not sure how to do this. I have hundreds of lines on a layer that I need to delete. I need to do this in VBA. I do not want to use a selection. just simply delete all lines on a layer.
    Q2. same question as Q1 but with a condition on color (i.e. if red delete).


    Thanks

  2. #2
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, W8 + W8.1 Vista
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2014
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    1,397

    Default Deleting lines in VBA

    Hi Katto01,

    Here's some sample code to get you going.

    Delete all lines on layer "Layer1":
    Code:
    Sub DelAllOnLayer()
        
        Dim oLine As AcadLine
        Dim oLayer As AcadLayer
            
        For Each oLine In ThisDrawing.ModelSpace
            If oLine.Layer = "Layer1" Then
                oLine.Delete
            End If
            
        Next
        
        ThisDrawing.Regen acActiveViewport
        
    End Sub
    To delete all red lines on layer "Layer1":
    Code:
    Sub DelAllOnLayerColour()
        
        Dim oLine As AcadLine
        Dim oLayer As AcadLayer
            
        For Each oLine In ThisDrawing.ModelSpace
            If oLine.Layer = "Layer1" Then
                If oLine.color = acRed Then
                    oLine.Delete
                End If
            End If
            
        Next
        
        ThisDrawing.Regen acActiveViewport
        
    End Sub
    That should get you going. If you are going to repeat this many times with lots of layers you could have a dialogue box where the layer and colour are entered and using the entered values do the deletions.

    Ben
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  3. #3
    Junior Member
    Using
    AutoCAD 2008
    Join Date
    Mar 2008
    Posts
    14

    Default

    Ben, Thank you for your help


    In both cases the line "For Each oLine In ThisDrawing.ModelSpace" gives me the error "type mismatch". I am in AutoCAD 2011

  4. #4
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,815

    Default

    You could try a selection set method

    Code:
    Dim SS As AcadSelectionSet
    Dim FilterDXFCode(0) As Integer
    Dim FilterDXFVal(0) As Variant
    FilterDXFCode(0) = 8
    FilterDXFVal(0) = "MYLAYER"
    Set SS = ThisDrawing.SelectionSets.Add("sel1")
    SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
    A man who never made mistakes never made anything

  5. #5
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, W8 + W8.1 Vista
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2014
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    1,397

    Default

    Quote Originally Posted by katto01 View Post
    Ben, Thank you for your help


    In both cases the line "For Each oLine In ThisDrawing.ModelSpace" gives me the error "type mismatch". I am in AutoCAD 2011
    katto01,

    I can only go back to version 2012 on my computer, but the code works fine there.

    Instead of copying and pasting the code into your VBA Editor try typing it all in line for line, paying particular attention to what AutoCAD offers when declaring the variable "oLine". You should see this:


    See if that helps.

    Ben
    Attached Images
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  6. #6
    Junior Member
    Using
    AutoCAD 2008
    Join Date
    Mar 2008
    Posts
    14

    Default

    Thanks for the tip. I did what you suggested. That did not change the original code you posted. The problem is still in the "For Each oLine In ThisDrawing.ModelSpace" line. same error

  7. #7
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, W8 + W8.1 Vista
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2014
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    1,397

    Default

    Quote Originally Posted by katto01 View Post
    Thanks for the tip. I did what you suggested. That did not change the original code you posted. The problem is still in the "For Each oLine In ThisDrawing.ModelSpace" line. same error
    Are you drawing lines, polylines, or something else? The code will only work with lines, polylines etc will not be deleted.
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  8. #8
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, W8 + W8.1 Vista
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2014
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    1,397

    Default

    Try using this declaration for the oLine declaration:

    Code:
    Dim oLine As Variant
    But that will pick all entities on the layer.

    I tried the code out with just polylines and arcs (no lines) on the layer and got the same error message as you did. Check the properties of the entities on the layer to see that they are lines.

    Ben
    Last edited by Tyke; 2nd Mar 2017 at 04:18 pm.
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  9. #9
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Oct 2013
    Posts
    78

    Default

    Registered forum members do not see this ad.

    Use SelectionSet and its powerful filtering and Erase methods

    Code:
    Sub DeleteElements()
        Dim delSset As AcadSelectionSet
    
        On Error Resume Next
        Set delSset = ThisDrawing.SelectionSets.Add("Deletion")
        On Error GoTo 0
        If delSset Is Nothing Then Set delSset = ThisDrawing.SelectionSets.Item("Deletion")
    
        Dim gpCode(0 to 2) As Integer
        Dim dataValue(0 to 2) As Variant
        gpCode(0) = 0 : dataValue(0) = "LINE" ' filter on line elements only
        gpCode(1) = 8 : dataValue(1) = "myLayerName" ' filter on given layer
        gpCode(2) = 62 : dataValue(2) = 1 ' filter on color (1 is the red color dataValue)
    
        With delSset
            .Clear
            .Select acSelectionSetAll, , , gpCode, dataValue
            If .Count > 0 Then .Erase
        End With
    End Sub

Similar Threads

  1. Replies: 3
    Last Post: 16th Nov 2015, 11:08 pm
  2. Delete multiple lines
    By gchaplin in forum AutoCAD Drawing Management & Output
    Replies: 3
    Last Post: 2nd Nov 2010, 06:07 am
  3. I can't delete lines using the delete button!!
    By lofty in forum AutoCAD General
    Replies: 6
    Last Post: 10th Dec 2009, 07:36 pm
  4. Duplicate Lines - Can't Delete
    By YZ in forum AutoCAD General
    Replies: 5
    Last Post: 8th Jul 2008, 09:33 pm
  5. delete lines circles with vba
    By comcu in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 17th Jun 2008, 04:12 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts