Like this:
Those first lines of the main function that asks for user input, feel free to change the order of those lines (per 2 or 3 lines)
You get an extra option: "X" is from left to right, "-X" is from right to left. Dito for Y.
Command INC
and follow the instructions in the command line.
Test dwg as attachment
;; @FILE increment a selected attribute on multiple blocks, sorted by X or Y coordinate of the block insert.
(vl-load-com)
;; http://www.lee-mac.com/attributefunctions.html#alsetattributevaluerc
;; Set Attribute Value - Lee Mac
;; Sets the value of the first attribute with the given tag found within the block, if present.
;; blk - [ent] Block (Insert) Entity Name
;; tag - [str] Attribute TagString
;; val - [str] Attribute Value
;; Returns: [str] Attribute value if successful, else nil.
(defun LM:setattributevalue ( blk tag val / enx )
(if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(if (entmod (subst (cons 1 val) (assoc 1 (reverse enx)) enx))
(progn
(entupd blk)
val
)
)
(LM:setattributevalue blk tag val)
)
)
)
(defun c:inc ( / sel i blkobj blockname start_inc val increase_by sort_by a sel_att x_coords y_coords sortedi blk ip)
;; FEEL FREE TO CHANGE THE ORDER OF THESE COMMANDS.
;; start increment by number:
(setq start_inc (getint "\nStart increment by number: "))
;; increase by how much:
(setq increase_by (getint "\nIncrease by how much: "))
;; sort by X or Y?
(setq sort_by (strcase (getstring "\nSort by X (left to right), Y (bottom to top) -X (right to left) or -Y (top to bottom) : ")))
;; select attribute
(setq a (nentsel "\nSelect attribute: "))
(setq sel_att (cdr (assoc 2 (entget (car a)))))
(setq blkobj (cdr (assoc 330 (entget (car a))))) ;; get the block
(setq blockname (cdr (assoc 2 (entget blkobj)))) ;; get the blockname
(princ "\nBlock: ")
(princ blockname)
(princ " - attribute: ")
(princ sel_att)
;; Do this last. We only know the blockname after we pick the attribute.
;; select blocks
(princ "\nSelect blocks: ")
(setq sel (ssget (list (cons 0 "INSERT") (cons 2 blockname) )))
;;
;; read coordinates of the blocks
(setq i 0)
(setq x_coords (list))
(setq y_coords (list))
(repeat (sslength sel)
(setq blk (ssname sel i))
(setq ip (cdr (assoc 10 (entget blk)))) ;; insert point
(setq x_coords (append x_coords (list (nth 0 ip))))
(setq y_coords (append y_coords (list (nth 1 ip))))
(setq i (+ i 1))
)
(setq sortedi (list))
;; sortedi will be the order of the blocks.
(if (= sort_by "X")
(setq sortedi (vl-sort-i x_coords '<))
)
(if (= sort_by "-X")
(setq sortedi (vl-sort-i x_coords '>))
)
(if (= sort_by "Y")
(setq sortedi (vl-sort-i y_coords '<))
)
(if (= sort_by "-Y")
(setq sortedi (vl-sort-i y_coords '>))
)
;; now loop over the blocks, in order of sortedi
(setq val start_inc)
(foreach i sortedi
(setq blk (ssname sel i))
(LM:setattributevalue blk sel_att (itoa val))
;; increase
(setq val (+ val increase_by))
)
(princ)
)
(Edited the code 13-12-2023)
increment_xy.dwg