Find all members of a group

Discussion in 'AutoCAD' started by Allen Johnson, Nov 29, 2004.

  1. Does VBA have any method to find all members of a selected objects group?
     
    Allen Johnson, Nov 29, 2004
    #1
  2. Allen Johnson

    Matt W Guest

    Are you talking about a selection set?
    If so, yes. If not, then I have no idea what you're talking about.
     
    Matt W, Nov 29, 2004
    #2
  3. No, I have an object that is part of an AutoCAD Group.
    I want to find all the members that are included in it's Group.
    In lisp, you'd do something like shown below, but how would you do it in
    VBA?:


    ;if ename is a member of a group
    ;returns a list of group member enames, otherwise nil
    ;note: ename may be a member of more than one group
    ;by Joe Burke
    (defun GetGroupMembers (ename / names dct ent grouplst lst)
    (and
    (setq names (GetGroupNames ename))
    (setq dct (dictsearch (namedobjdict) "acad_group"))
    (foreach x names
    (setq ent (cdadr (member (cons 3 x) dct)))
    (setq grouplst (massoc 340 (entget ent)))
    (foreach y grouplst
    (if (not (member y lst))
    (setq lst (cons y lst))
    )
    )
    )
    )
    lst
    )

    ;returns a list of group names the entity is a child of,
    ;innermost first in the list.
    ;by Michael Puckett
    (defun GetGroupNames (ename / key dct rtn)
    (setq key (cons 340 ename)
    dct (dictsearch (namedobjdict) "acad_group")
    )
    (while (setq dct (member (assoc 3 dct) dct))
    (if (member key (entget (cdadr dct)))
    (setq rtn (cons (cdar dct) rtn))
    )
    (setq dct (cddr dct))
    )
    (reverse rtn)
    )

    ;returns a list of information associated with a DXF code
    (defun massoc (key alist / x nlist)
    (foreach x alist
    (if (eq key (car x))
    (setq nlist (cons (cdr x) nlist))
    )
    )
    (reverse nlist)
    )

    ;if ename is a member of a group
    ;returns a list of group member enames, otherwise nil
    ;note: ename may be a member of more than one group
    ;by Joe Burke
    (defun GetGroupMembers (ename / names dct ent grouplst lst)
    (and
    (setq names (GetGroupNames ename))
    (setq dct (dictsearch (namedobjdict) "acad_group"))
    (foreach x names
    (setq ent (cdadr (member (cons 3 x) dct)))
    (setq grouplst (massoc 340 (entget ent)))
    (foreach y grouplst
    (if (not (member y lst))
    (setq lst (cons y lst))
    )
    )
    )
    )
    lst
    )
     
    Allen Johnson, Nov 29, 2004
    #3
  4. Dim objGroup As AcadGroup
    Set objGroup = ThisDrawing.Groups("TEST")

    objGroup will contain all the objects of the
    group TEST...

    gl Paul
     
    Paul Richardson, Nov 29, 2004
    #4
  5. But what if you don't know the name of the group?
     
    Allen Johnson, Nov 29, 2004
    #5
  6. Allen Johnson

    Jeff Mishler Guest

    Here's one way to go about it. It's not pretty, and it will only return the
    first Group that the object is a member of, but it should give you an idea
    of how groups work in VBA. Unlike lisp, you cannot directly get the group
    object that an entity belongs to.

    Option Explicit

    Sub findgroup()
    Dim Ent As AcadEntity
    Dim pick As Variant
    Dim Groups As AcadGroups
    Dim Grp As AcadGroup
    Dim GrpEnt As AcadObject
    Dim found As Boolean
    Dim grpName As String

    found = False
    Set Groups = ThisDrawing.Groups
    ThisDrawing.Utility.GetEntity Ent, pick, "Select object: "

    For Each Grp In Groups
    For Each GrpEnt In Grp
    If GrpEnt.ObjectID = Ent.ObjectID Then
    found = True
    grpName = Grp.Name
    End If
    If found Then Exit For
    Next
    If found Then Exit For
    Next
    If found Then
    Set Grp = Groups.Item(grpName)
    Debug.Print "Group Name = " & grpName
    Debug.Print "ObjectID's of group members: "
    For Each GrpEnt In Grp
    Debug.Print GrpEnt.ObjectID
    Next
    End If
    If Not found Then Debug.Print "Entity not found in any group!"
    End Sub
     
    Jeff Mishler, Nov 29, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.