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

    Default VBA - information on selection

    Registered forum members do not see this ad.

    Hello,


    Q1: How do I get the number of entities in a selection using VBA?
    Q2: What is the command/method to find the type of each entity in a selection using VBA?


    Thank you

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

    Default

    A1: use .Count method of SelectionSet object
    A2: you could use typeOf() like:
    Code:
    If typeOf acEnt Is AcadLine then
    ...
    and the likes where acEnt is some AcadEntity type variable you're looping through SelectionSet with

  3. #3
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,124

    Default

    Heres my example-attempt:

    Code:
    Sub test() 'define a named function
      
      'Filter for circles - I don't know how to implement this type of filtering:
      'Dim gpCode(0) As Integer
      'Dim dataValue(0) As Variant
      'gpCode(0) = 0
      'dataValue(0) = "Circle"  
      
      Dim SS As AcadSelectionSet 'Declare "SS" variable as local
      Set SS = ThisDrawing.SelectionSets.Add("NewSS") 'create a new selection set object [include it in the "SelectionSets" collection]
     
      SS.SelectOnScreen 'select the objects to change (invoke the "SelectOnScreen" method)
     
      For Each Entity In SS 'iterate over the selection set
       
        If Entity.ObjectName = "AcDbCircle" Then 'Filter for circles by objectname
          Entity.color = 1 'change entity's colour
        End If
          
      Next 'process the next entity
      MsgBox SS.Count 'alert the total selected amount of objects
      SS.Delete 'delete the selection set object [exclude it from the "SelectionSets" collection]
    End Sub
    I'm just starting to learn about VBA, so any inputs are appreciated.

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

    Default

    Grr this may be usefull

    Code:
    Dim FilterDXFCode(0) As Integer
    Dim FilterDXFVal(0) As Variant
    
    FilterDXFCode(0) = 0
    FilterDXFVal(0) = "INSERT"
    'FilterDXFCode(1) = 2
    'FilterDXFVal(1) = "SCHEDTEXT"
    
    Set SS = ThisDrawing.SelectionSets.Add("pit1sel")
    SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
    
    for circle
    FilterDXFVal(0) = "CIRCLE"
    
    lisp (0 . "Insert")
    A man who never made mistakes never made anything

  5. #5
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,124

    Default

    Registered forum members do not see this ad.

    Thanks BIGAL!

    This second shot worked:
    Code:
    Sub test() 'define a named function
      
      Dim FilterDXFCode(0) As Integer
      Dim FilterDXFVal(0) As Variant
    
      FilterDXFCode(0) = 0
      FilterDXFVal(0) = "CIRCLE"
    
      Dim SS As AcadSelectionSet 'Declare "SS" variable as local
      Set SS = ThisDrawing.SelectionSets.Add("NewSS") 'create a new selection set object [include it in the "SelectionSets" collection]
     
      'SS.Select acSelectionSetWindow , , , FilterDXFCode, FilterDXFVal '<<< BIGAL's example 
      SS.SelectOnScreen FilterDXFCode, FilterDXFVal 'select the objects to change (invoke the "SelectOnScreen" method) and include filters
       
      For Each Entity In SS 'iterate over the selection set
    
          Entity.color = 1 'change entity's colour
          
      Next 'process the next entity
      MsgBox SS.Count 'alert the total selected amount of objects
      SS.Delete 'delete the selection set object [exclude it from the "SelectionSets" collection]
    End Sub
    It seems hard for me to get use with VBA, from lisp (since the lack of syntaxes).
    First Impressions are that VBA and Visual Lisp (not Vanilla) are related, you have to, create a Selection Set Object, invoke a method and iterate thru it - instead of a single ssget function, that would be enough in vanilla lisp (altho still requires iteration).

    With Visual Lisp it would wook like this:
    Code:
    (
      (lambda ( / SS )
        (vla-SelectOnScreen
          (setq SS (vla-Add (vla-get-SelectionSets (vla-get-ActiveDocument (vlax-get-acad-object))) "NewSS"))
          (vlax-safearray-fill (vlax-make-safearray vlax-vbinteger '(0 . 0)) '(0))
          (vlax-safearray-fill (vlax-make-safearray vlax-vbvariant '(0 . 0)) '("CIRCLE"))
        )
        (vlax-for o SS (vla-put-Color o 1))
        (vla-Delete SS)
      )
    )
    P.S. Sorry for late reply - I'm busy with other kind of work (computerless).

Similar Threads

  1. Break Down Selection Set into smaller selection sets
    By broncos15 in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 13th Apr 2017, 03:33 am
  2. Create secondary Selection Sets from first Selection Set...
    By lamensterms in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 23rd Aug 2012, 10:39 am
  3. Trim selection & Object selection problem
    By Kurt Crim in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 2
    Last Post: 12th Aug 2011, 12:19 pm
  4. Replies: 1
    Last Post: 18th Nov 2010, 01:56 pm
  5. delay in selection of objects/selection windows
    By RedRobMol in forum AutoCAD Drawing Management & Output
    Replies: 2
    Last Post: 31st May 2005, 01:34 pm

Tags for this Thread

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