Jump to content

Please help me understand this autolisp code


Brick_top

Recommended Posts

Hi there, I have this code that I asked someone to write me a couple of years ago, and I don't even remember where I got in touch with him over the net

 

I know how to use it and how some individual parts work.

 

It makes a list of the vertices of a polyline.

 

Could someone give me a detailed explantion of what it does exactly?

 

(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) )

 

thanks a lot!

Link to comment
Share on other sites

 
;;; its a sub-function for the bigger program
;;; when it is called you need to pass it 2 variables example: (cdrs 10 and the polyline list from the autolisp entget function)
;;; the variable KEY  is the first item in a dotted pair like (10 100.500 200.600)
;;; the variable LST is the list of entity information like if you use (entget(entlast))
(defun cdrs (key lst / pair rtn)
  ;_while pair is not nil loop.
     (while (setq pair (assoc key lst)); pair = the first occurrence of the key item in the list
     (setq rtn (cons (cdr pair) rtn);_a list of the items are being made
             lst (cdr (member pair lst));_remaking the list to be passed back on the next loop
             ;;;with out the previous (assoc key lst) so it will get thew next item in the list
       );_setq
     );_while
  (reverse rtn);_flip the list so the fitst item the while loop retrives is now the first in the list
 );_defun
;;; this sub-function can be tested by the following call
;;; first draw a polyline then make the call
(cdrs 10 (entget(entlast)))

Link to comment
Share on other sites

I still don't understand how does the loop goes from the onde code 10 to the next

 

Sorry

;;; its a sub-function for the bigger program
;;; when it is called you need to pass it 2 variables example: (cdrs 10 and the polyline list from the autolisp entget function)
;;; the variable KEY  is the first item in a dotted pair like (10 100.500 200.600)
;;; the variable LST is the list of entity information like if you use (entget(entlast))
(defun cdrs (key lst / pair rtn)
  ;_while pair is not nil loop.
     (while (setq pair (assoc key lst)); pair = the first occurrence of the key item in the list
     (setq rtn (cons (cdr pair) rtn);_a list of the items are being made
             lst (cdr                     ;_[color="Red"]2. Cdr find in 1. And save in lst variable
                                            ;_Next pass member find next code 10[/color]
                    (member pair lst) ;[color="Red"]_1. Find first code 10[/color]
                  )
;_remaking the list to be passed back on the next loop
             ;;;with out the previous (assoc key lst) so it will get thew next item in the list
       );_setq
     );_while
  (reverse rtn);_flip the list so the fitst item the while loop retrives is now the first in the list
 );_defun
;;; this sub-function can be tested by the following call
;;; first draw a polyline then make the call
(cdrs 10 (entget(entlast)))

Another variant this function

(defun pl-list-massoc (key alist) 
(mapcar 'cdr (vl-remove-if-not (function (lambda (x) (= key (car x)))) alist))) ;_ end of defun 

Use

(vl-load-com)(pl-list-massoc 10 (entget(car(entsel "\nSelect a polyline:"))))

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

×
×
  • Create New...