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.

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