Select/Delete all the circles with a specified radius using VBA

Select/Delete all the circles with a specified radius using VBA

niranjan_thyagaraja
Explorer Explorer
666 Views
3 Replies
Message 1 of 4

Select/Delete all the circles with a specified radius using VBA

niranjan_thyagaraja
Explorer
Explorer

Hi All,

 

Here is my vba code to select all the circles with a radius of 1.9433 and delete. Unfortunately, it deletes all the circles, even the ones which don't match the specified radius. Any help to resolve this is very much appreciated. Thank you.

 

Sub DeleteAllSelectedCircles()
    ' Declare variables
    Dim acadApp As Object
    Dim acadDoc As Object
    Dim selectionSet As Object
    Dim filterType(1) As Integer
    Dim filterData(1) As Variant

    ' Get the AutoCAD application and active document
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument

    ' Create a new selection set
    On Error Resume Next
    Set selectionSet = acadDoc.SelectionSets.Add("CircleSelectionSet")
    If Err.Number <> 0 Then
        Set selectionSet = acadDoc.SelectionSets.Item("CircleSelectionSet")
        Err.Clear
    End If
    On Error GoTo 0

    ' Define the filter for circles
    filterType(0) = 0
    filterData(0) = "CIRCLE"
    filterType(1) = 40 ' Radius property
    filterData(1) = 1.9433

    ' Select all circles in the drawing
    selectionSet.Select acSelectionSetAll, , , filterType, filterData

    ' Delete the selected circles
    Dim entity As Object
    For Each entity In selectionSet
        entity.Delete
    Next entity

    ' Inform the user
    MsgBox selectionSet.Count & " circles deleted."
End Sub


-NT

0 Likes
Accepted solutions (1)
667 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Well, since the circle's radius is a Double type, the value (1.9433 in your case) could be either exactly 1.9433, or could be 1.943333333333..... Using radius property in the filter, the value must be matched exactly. You can try to create a circle with radius of exact 1.9 and set the filter value to 1.9, and your code would be able to select it.

 

So, it might not be a good idea to use properties with floating value. Since you are writing code, it would be simple to just do the fine filtering in the code:

 

Dim c As AcadCircle

'' target radius

Dim r As Double

r=1.9433

'' choose a suitable tolerance

Dim tolerance as Double

tolerance=0.000001 

'' loop through the selected circles and test its radius

For Each ent In selectionSet

  Set c = ent

  If Abs(c.Radius - r)<=tolerance Then

    '' This circle is the target one.

  End If

Next

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

niranjan_thyagaraja
Explorer
Explorer

Thanks, Norman! I was able to confirm that the code works for a circle with precise radius of 1.9. I'll try your workaround now 🤞

0 Likes
Message 4 of 4

niranjan_thyagaraja
Explorer
Explorer

This update worked! Thank you once again..

 

' Define the filter for circles
filterType(0) = 0
filterData(0) = "CIRCLE"
filterType(1) = -4
filterData(1) = ">"
filterType(2) = 40 ' Radius property
filterData(2) = 1.9432
filterType(3) = -4
filterData(3) = "<"
filterType(4) = 40 ' Radius property
filterData(4) = 1.9433

0 Likes