Jump to content

AutoLISP: Change Block Spacing in X-Direction (Row-Wise, No Y-Shift)


Recommended Posts

Posted

I need help with adjusting the X-direction spacing of multiple blocks (row-wise) in AutoCAD. The Y-position should remain unchanged. For example, the current spacing between blocks is 27 ft, but once I select all the blocks and input a new value like 23 or 24, the blocks should automatically update to the new spacing as per my requirement.

BLK Sample.dwg

Posted

Hi @Tamim,

 

Try with this:

 

(prompt "\nTo run a LISP type: reara")
(princ)

(defun c:reara ( / old_osmode base_blk spacing ss base_blk_pt dist_blk_lst len i ins_pt dist n dist_n x_cord y_cord new_pt)

  (setq old_osmode (getvar 'osmode))
  (setvar 'osmode 0)
  
  (setq base_blk (car (entsel "\nPick the base block:\n"))
	spacing (getreal "\nEnter the spacing:\n")
	)

  (prompt "\nSelect BLOCK's:")
  
  (setq ss (ssget (list (cons 0 "INSERT")))
	base_blk_pt (cdr (assoc 10 (entget base_blk)))
	dist_blk_lst (list)
	)
  
  (if (ssmemb base_blk ss)
    (ssdel base_blk ss)
    )
  
  (setq len (sslength ss)
	i 0
	)
  
  (while (< i len)
    
    (setq ins_pt (cdr (assoc 10 (entget (ssname ss i))))
	  dist (distance base_blk_pt ins_pt)
	  dist_blk_lst (cons (list dist (ssname ss i)) dist_blk_lst)
	  i (1+ i)
	  )
    )
  
  (setq dist_blk_lst (vl-sort dist_blk_lst (function (lambda (x1 x2) (< (car x1) (car x2)))))
	n 0
	)
  
  (repeat (length dist_blk_lst)
    
    (setq dist_n (- (car (nth n dist_blk_lst)) spacing (* spacing n))
	  x_cord (- (cadr (assoc 10 (entget (cadr (nth n dist_blk_lst))))) dist_n)
	  y_cord (caddr (assoc 10 (entget (cadr (nth n dist_blk_lst)))))
	  new_pt (list x_cord y_cord)
	  )
    
    (command-s "_move" (cadr (nth n dist_blk_lst)) "" (cdr (assoc 10 (entget (cadr (nth n dist_blk_lst))))) new_pt)
    
    (setq n (1+ n))
    
    )

  (setvar 'osmode old_osmode)
  
  (prompt (strcat "\nThe " (itoa (length dist_blk_lst)) " are rearanged!"))
  
  (princ)
  
  )

 

See the following video how it works.

 

 

Best regards.

Posted
6 minutes ago, Saxlle said:

Hi @Tamim,

 

Try with this:

 

(prompt "\nTo run a LISP type: reara")
(princ)

(defun c:reara ( / old_osmode base_blk spacing ss base_blk_pt dist_blk_lst len i ins_pt dist n dist_n x_cord y_cord new_pt)

  (setq old_osmode (getvar 'osmode))
  (setvar 'osmode 0)
  
  (setq base_blk (car (entsel "\nPick the base block:\n"))
	spacing (getreal "\nEnter the spacing:\n")
	)

  (prompt "\nSelect BLOCK's:")
  
  (setq ss (ssget (list (cons 0 "INSERT")))
	base_blk_pt (cdr (assoc 10 (entget base_blk)))
	dist_blk_lst (list)
	)
  
  (if (ssmemb base_blk ss)
    (ssdel base_blk ss)
    )
  
  (setq len (sslength ss)
	i 0
	)
  
  (while (< i len)
    
    (setq ins_pt (cdr (assoc 10 (entget (ssname ss i))))
	  dist (distance base_blk_pt ins_pt)
	  dist_blk_lst (cons (list dist (ssname ss i)) dist_blk_lst)
	  i (1+ i)
	  )
    )
  
  (setq dist_blk_lst (vl-sort dist_blk_lst (function (lambda (x1 x2) (< (car x1) (car x2)))))
	n 0
	)
  
  (repeat (length dist_blk_lst)
    
    (setq dist_n (- (car (nth n dist_blk_lst)) spacing (* spacing n))
	  x_cord (- (cadr (assoc 10 (entget (cadr (nth n dist_blk_lst))))) dist_n)
	  y_cord (caddr (assoc 10 (entget (cadr (nth n dist_blk_lst)))))
	  new_pt (list x_cord y_cord)
	  )
    
    (command-s "_move" (cadr (nth n dist_blk_lst)) "" (cdr (assoc 10 (entget (cadr (nth n dist_blk_lst))))) new_pt)
    
    (setq n (1+ n))
    
    )

  (setvar 'osmode old_osmode)
  
  (prompt (strcat "\nThe " (itoa (length dist_blk_lst)) " are rearanged!"))
  
  (princ)
  
  )

 

See the following video how it works.

 

 

Best regards.

Thanks for the prog. However, this only works for a single X row. I need to select both top and bottom rows (like Y1 and Y2), and show the result below the image.

image.thumb.png.c51bb55a72d673c9e7709e6f4c756729.png

Posted

Do it row by row, not selecting everything.

Posted (edited)
3 hours ago, Saxlle said:

Do it row by row, not selecting everything.

understood, but i have more than 10000 numbers for each CAD file 

Edited by Tamim
Posted (edited)

Upload another example file to see what it looks like. But I'm afraid it can't be done easily, because you have to choose a reference point for the base block, and then rearrange the other blocks to be at equal distances. If you want to select "n" blocks, what can be a reference point from that selection set, which block? Also, they have different insertation points.

Edited by Saxlle
Posted (edited)
(vl-load-com)

(defun c:FOO (/ *error* acDoc ss pt y item data blocks)

  (defun *error* (msg)
    (if ss (vla-delete ss))
    (if acDoc (vla-endundomark acDoc))
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )
    
  (if
    (and
      (ssget "_:L" '((0 . "INSERT")))
      (setq d (getreal "\nEnter Block distance: "))
    )
     (progn
       (vla-startundomark
         (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
       )
       (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
         (setq pt (vlax-get x 'insertionpoint))
         (if (setq item (assoc (setq y (cadr pt)) data))
           (setq data
                  (subst (cons (car item) (append (cdr item) (list x))) item data)
           )
           (setq data (cons (cons y (list x)) data))
         )
       )
       (if data
         (foreach item data
           (setq blocks
                  (vl-sort
                    (cdr item)
                    (function
                      (lambda (a b)
                        (< (car (vlax-get a 'insertionpoint))
                           (car (vlax-get b 'insertionpoint))
                        )
                      )
                    )
                  )
           )
           (setq pt (vlax-get (car blocks) 'insertionpoint))
           (foreach block (cdr blocks)
             (vla-move
               block
               (vlax-3d-point (vlax-get block 'insertionpoint))
               (vlax-3d-point (setq pt (polar pt 0.0 d)))
             )
           )
         )
       )
     )
  )

  (*error* nil)
)

 

This works for each group of Blocks at a given Y level, based on the lowest X position in a given row as starting point.

 

2025-09-11_11-53-04.png

2025-09-11_11-55-52.png

Edited by BlackBox

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...