Selecting objects inside a polygon

Discussion in 'AutoCAD' started by JRogers, Sep 27, 2004.

  1. JRogers

    JRogers Guest

    Does anyone know of any routines that will allow me to select objects within
    a predefined boundary/polygon? I would like to do this within the Autocad
    Map source drawing and not have to query the information into a worksession?
     
    JRogers, Sep 27, 2004
    #1
  2. JRogers

    Dan Allen Guest

    ;;;From: Joe Burke ()
    ;;;Subject: Re: Polygon Selection Boundary
    ;;;Newsgroups: autodesk.autocad.customization
    ;;;Date: 2002-04-17 04:38:49 PST
    ;;;
    ;;;
    ;;;returns list of points in a pline

    (defun C:SelectByPline (/ pline ptlist prev count)
    (princ "\nCrossing or Window selection?<C,W>")
    (setq typ (strcase (getstring)))
    (cond
    ((= typ "W")
    (prompt "\nPick polyline to define Window selection set:")
    )
    ((= typ "C")
    (prompt "\nPick polyline to define Crossing selection set:")
    )
    )
    (setq pline (car (entsel))
    ptlist (massoc 10 (entget pline))
    )
    (cond
    ((= typ "W")
    (setq prev (ssget "WP" ptlist))
    (setq count (sslength prev))
    (princ "\nThe Selection Set consists of ")
    (princ count)
    (princ " entities inside the Window Polygon.")
    )
    ((= typ "C")
    (setq prev (ssget "CP" ptlist))
    (ssdel pline prev) ;remove pline from crossing ss
    (setq count (sslength prev))
    (princ "\nThe Selection Set consists of ")
    (princ count)
    (princ " entities inside the Crossing Polygon.")
    )
    )
    (sssetfirst nil prev) ;select
    (princ)
    );end defun
     
    Dan Allen, Sep 27, 2004
    #2
  3. JRogers

    JRogers Guest

    I must be missing something...I can't seem to get it to work.
     
    JRogers, Sep 27, 2004
    #3
  4. JRogers

    Dan Allen Guest

    Also needs massoc subroutine:

    ;;;============================================
    ;;; massoc
    ;;;
    ;;; get multiple items from an association list (instead of just 1st one)
    ;;;
    ;;; From: Tony Tanzillo ()
    ;;; Subject: Re: extracting multiple assoc from list
    ;;; Newsgroups: autodesk.autocad.customization
    ;;; Date: 1999/09/29
    ;;;============================================
    (defun massoc (key alist / x nlist)
    (foreach x alist
    (if (eq key (car x))
    (setq nlist (cons (cdr x) nlist))
    )
    )
    (reverse nlist)
    ) ;end defun
     
    Dan Allen, Sep 27, 2004
    #4
  5. JRogers

    JRogers Guest

    I've loaded the massoc file and still cannot run this routine. How do I call
    up this routine from the command line? What is the purpose of the C: in the
    defun statement?
     
    JRogers, Sep 27, 2004
    #5
  6. JRogers

    JRogers Guest

    Ok...I put the lisp routine under C: and I was able to call up the routine
    but when I try to pick an object I get an error message. If I define a
    rectangle as the bdry it will work but if I try to pick an existing closed
    polyline or a newly defined polyline it bombs. I have large polygonal
    boundaries that I need to be able to pick.
     
    JRogers, Sep 27, 2004
    #6
  7. JRogers

    BillZ Guest

    BillZ, Sep 27, 2004
    #7
  8. JRogers

    Joe Burke Guest

    JRogers,

    Here's an improved version with error checking. It may help sort out the problem you
    encountered. See my notes.

    The massoc function is folded in here. So you don't need it.

    Joe Burke

    ;; 9/27/2004
    ;; Notes:
    ;; The pline selected must be entirely within the current
    ;; view window to ensure a complete selection set.
    ;; Arcs in the selected pline are treated as two points,
    ;; causing unpredictable results.
    ;; Does not work with old style "heavy" plines or 3D plines.

    (defun c:SelectByPline (/ typ pline ptlist ss)
    (initget 1 "C W ")
    (setq typ
    (getkword "\nSelect by [Crossing polygon/Window polygon] <W>: "))
    (if (= typ "") (setq typ "W"))
    (while
    (or
    (not (setq pline (car (entsel "\nSelect pline: "))))
    (/= "LWPOLYLINE" (cdr (assoc 0 (entget pline))))
    )
    (princ "\nMissed pick or wrong object type. ")
    )
    (foreach x (entget pline)
    (if (eq 10 (car x))
    (setq ptlist (cons (cdr x) ptlist))
    )
    )
    (if (= typ "W")
    (setq ss (ssget "WP" ptlist))
    (progn
    (setq ss (ssget "CP" ptlist))
    (ssdel pline ss) ;remove pline from crossing selection
    )
    )
    (if ss
    (progn
    (sssetfirst nil ss)
    (princ (strcat "\nNumber of objects selected: " (itoa (sslength ss))))
    )
    (princ "\nNo objects found by window or crossing methods. ")
    )
    (princ)
    ) ;end
     
    Joe Burke, Sep 28, 2004
    #8
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.