@Anonymous wrote:
I am trying to create a list of blocks in the drawing.
The multiple blocks can be on multiple layers or 1 layer.
I can't get past updating the quantity of the blocks if it already exists in the list.
Example list where I run into problems -
(setq List_Blocks (list (list "Block1" "LayerA" 1) (list "Block1" "LayerB" 1) (list "Block2" "LayerA" 1)))
How do I find and update the 3rd value (quantity) for "Block1" "LayerB" without having to step thru the list each time?
As a list is only accessible via a pointer to its first item, you'll always have to step through (part) of the list; either explicitly or in the internals of ASSOC, MEMBER etc.
As AutoLISP lacks the tools to directly modify list items (unlike other Lisps, where you could use SETF, RPLACD etc. for this), you can only modify a list by either copying it, changing something during the copy, or by adding new stuff in front.
The copying variant is done for example by SUBST.
The idea with adding new stuff in front is to just find the first occurrence of a key, ignoring the older items with the same key. The idea is known as an association list, so the operator is ASSOC.
One way of doing this:
(defun reset-blockcount ()
(setq *blocks* nil
*keys* nil))
(defun increment-blockcount (name layer / key count)
(setq key (cons name layer))
(cond ((member key *keys*)
;; Already on the list, increment count
(setq count (cadr (assoc key *blocks*)))
(setq *blocks*
(cons (list key (1+ count)) *blocks*)))
(T ;; not previously seen, create item
(setq *keys* (cons key *keys*))
(setq *blocks*
(cons (list key 1) *blocks*)))))
(defun show-blockcount()
(mapcar (function
(lambda (key)
(list (car key)
(cdr key)
(cadr (assoc key *blocks*)))))
*keys*))
_$ (reset-blockcount)
nil
_$ (increment-blockcount "Block1" "LayerA")
((("Block1" . "LayerA") 1))
_$ (increment-blockcount "Block1" "LayerB")
((("Block1" . "LayerB") 1) (("Block1" . "LayerA") 1))
_$ (increment-blockcount "Block2" "LayerA")
((("Block2" . "LayerA") 1) (("Block1" . "LayerB") 1) (("Block1" . "LayerA") 1))
_$ (increment-blockcount "Block1" "LayerB")
((("Block1" . "LayerB") 2) (("Block2" . "LayerA") 1) (("Block1" . "LayerB") 1) (("Block1" . "LayerA") 1))
_$ (increment-blockcount "Block1" "LayerA")
((("Block1" . "LayerA") 2) (("Block1" . "LayerB") 2) (("Block2" . "LayerA") 1) (("Block1" . "LayerB") 1) (("Block1" . "LayerA") 1))
_$ (show-blockcount)
(("Block2" "LayerA" 1) ("Block1" "LayerB" 2) ("Block1" "LayerA" 2))
-