Jump to content

Multiple ASSOC values


Emmanuel Delay
 Share

Recommended Posts

An example, this is the result of (setq ent (entget (car (eltsel "\nSelect object: "))))

 

((-1 . <Entity name: 1770492fe00>) (0 . "POLYLINE") (330 . <Entity name: 176be833f00>) (5 . "BBD8") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDb3dPolyline") (66 . 1) (10 0.0 0.0 0.0) ...

 

(assoc 100 ent) will return  (100 . "AcDbEntity").

How do I get that second one, the  (100 . "AcDb3dPolyline") ?

Link to comment
Share on other sites

reverse the list, do the assoc on that? So long as there are only two '100's that should do it

Link to comment
Share on other sites

Ah yes, that's an idea.

 

Is there also any obvious solution in case there's more than 2?

Link to comment
Share on other sites

This will return the entity list from the second one on: 

(member (assoc 100 (cdr (member (assoc 100 ent) ent))) ent)

You could save and repeat until none remain.

  • Like 2
Link to comment
Share on other sites

Here is my utility function for multiple associations, credit Tony Tanzillo 1999.

 

;;;=========================================================================
;;; massoc
;;;
;;; get multiple items from an association list (instead of just 1st one)
;;;
;;; From: Tony Tanzillo (tony.tanzillo@worldnet.att.net)
;;; Subject: Re: extracting multiple assoc from list
;;; Newsgroups: autodesk.autocad.customization
;;; Date: 1999/09/29
;;;
;;; revised by Dan, 2017
;;; to add option for key to be a list of assoc codes, '(10 11), or just a single
;;;=========================================================================
(defun massoc (key alist / x nlist)
  (if (not (= 'LIST (type key)))
    (setq key (list key))
  )
  (foreach y key
    (foreach x alist
      (if (eq y (car x))
        (setq nlist (cons (cdr x) nlist))
      )
    )
  )
  (reverse nlist)
) ;end defun

 

  • Like 1
Link to comment
Share on other sites

Basically the same thing just condensed down a little more.

 

;(cdrs 10 (entget (car (entsel "\nSelect a polyline: "))))
;returns something like this:
;((259.943 -252.219) (214.182 -140.305) (254.223 -92.925) (215.0 -21.0386) 
; (253.406 41.8621) (215.817 112.115))
;Michal Puckett
(defun cdrs (key lst / pair rtn)
  (while (setq pair (assoc key lst))
    (setq rtn (cons (cdr pair) rtn)
          lst (cdr (member pair lst))
    )
  )
  (reverse rtn)
)

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

Another one commonly used to pull point lists .. modified for 100 code:

(mapcar 'cdr (vl-remove-if '(lambda (x) (/= 100 (car x))) elist))

 

  • Like 2
  • Agree 1
Link to comment
Share on other sites

Right.
So, depending on what I need I will make dedicated functions.

 

For example: selecting all 2D polylines:


 

...

(if (assocm (entget ent) 100 "AcDb2dPolyline" ) (selectAll2DPolylines) )

;; 

(defun assocm ( lst key val / pair rtn)
  (while (setq pair (assoc key lst))
    (if (= val (cdr pair))
      (setq rtn T)
    )
    (setq lst (cdr (member pair lst)))
  )
  rtn
)

 

To find all endpoints of a LWPOLYLINE I might use Michal Puckett's cdrs

...

Link to comment
Share on other sites

2 hours ago, Emmanuel Delay said:

 

To find all endpoints of a LWPOLYLINE I might use Michal Puckett's cdrs

...

(vlax-curve-getendpoint ename)

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...