Jump to content

Looking for a LISP to evenly space polylines from their end points


Recommended Posts

Posted

HI, 

I’m working on a CAD drawing that has several parallel polylines, and I’d like to space them out evenly by a fixed distance, but only from their start points.

For example, all the lines begin from point No. 1, and I want to distribute only between line 1 and line 2, keeping the rest aligned. The goal is to spread them equally on the opposite side, similar to how the “Distribute” option works in TEXTALIGN, but for polylines instead of text.

It would be great if the spacing value (for example, 0.2 ft or 0.5 ft) could be entered by the user.

Does anyone know if there’s an existing LISP routine for this, or something close?
Any tips or examples would be really helpful.

line shifting.dwg

Posted

Hi @Tamim

 

Try this code and see if it fulfil your needs:

 

; ********************************************************
; Functions     :  ESP (Evenly Spacing the Polylines)
; Description   :  Evenly Spacing Polylines
; Author        :  SAXLLE
; Date          :  October 29, 2025
; ********************************************************

(prompt "\nTo run a LISP type: ESP (Evenly Spacing the Polylines)")
(princ)

(defun c:ESP ( / myerr olderr old_osmode flag ss len lst i spacing side base_point inc ent dist_lst npt answ)

  (setq old_osmode (getvar 'osmode))
  
  (defun myerr (errmsg)
    
    (setq *error* olderr)
    
    (if (not (member errmsg '("console break" "Function Cancelled"))
	     )
      
      (princ (strcat "\nError: " errmsg ".\nThe application has finished working..."))
        
      )
    
    (setvar 'osmode old_osmode)
    (princ)
    
    )
  
  (setq olderr *error*
	*error* myerr
	)

  (setq flag T)
  
  (while (not (equal flag nil))

    (setvar 'osmode old_osmode)
    
    (prompt "\nSelect Polylines:")
    (princ)
    
    (setq ss (ssget (list (cons 0 "*POLYLINE")))
	  len (sslength ss)
	  lst (list)
	  i 0
	  )
    
    (repeat len
      
      (setq lst (cons (list (ssname ss i) (getpropertyvalue (ssname ss i) "Length")) lst)
	    i (1+ i)
	    )
      )

    (initget 1 "Left Right")
    
    (setq lst (vl-sort lst (function (lambda (a b) (< (cadr a) (cadr b)))))
	  side (getkword "\nChoose the side? [Left/Right]")
	  spacing (getreal "\nEnter the spacing value:")
	  base_point (getpoint "\nPick the Base Point for spacing:\n")
	  inc spacing
	  i 0
	  )
    
    (setvar 'osmode 0)

    (command-s "_UNDO" "begin")
    
    (while (< i (length lst))
      
      (setq ent (car (nth i lst))
	    dist_lst (list)
	    dist_lst (mapcar (function (lambda (x) (distance (car x) (cadr x)))) (mapcar 'list (setq pt_list (mapcar 'cdr (vl-remove-if-not (function (lambda (x) (= (car x) 10))) (entget (car (nth i lst)))))) (cdr pt_list)))
	    )
      
      (if (= side "Left")
	
	(progn
	  
	  (setq npt (list (- (car base_point) inc) (cadr base_point) (caddr base_point))) ;; to the Left, using "-" sign
	  
	  (command-s "_pline" npt (strcat "@" (rtos (car dist_lst) 2 2) "<90") (strcat "@" (rtos (- (cadr dist_lst) inc) 2 2) "<180") (strcat "@" (rtos (caddr dist_lst) 2 2) "<270") "")
	  
	  )
	
	(progn
	  
	  (setq npt (list (+ (car base_point) inc) (cadr base_point) (caddr base_point))) ;; to the Right, using "+" sign
	  
	  (command-s "_pline" npt (strcat "@" (rtos (car dist_lst) 2 2) "<90") (strcat "@" (rtos (- (cadr dist_lst) inc) 2 2) "<0") (strcat "@" (rtos (caddr dist_lst) 2 2) "<270") "")
	  
	  )
	
	)
      
      (entdel (car (nth i lst)))
      
      (setq inc (+ inc spacing)
	    i (1+ i)
	    )
      
      )

    (command-s "_UNDO" "end")
    
    (initget 1 "Yes No Undo")
    
    (setq answ (getkword "\Do you want to continue? [Yes/No/Undo]"))
    
    (cond
      
      ((equal answ "No")

       (setvar 'osmode old_osmode)
       
       (setq flag nil)
       
       )
      
      ((equal answ "Undo")
       
       (command-s "_UNDO" "")
       
       )
      
      )
    )  
  
  (prompt "\The polylines are evenly spaced!")
  (princ)
  
  )

 

Also, you can see the short video of how does lisp are working.

 

 

Best regards.

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