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.

Posted

Hi @Saxlle started to do something found the plines are drawn in a CCW or CW direction for left right so I am going to set that to one direction I look at the length of 1st and 3rd section to work out which end to change, so no need for Left or right. 

 

The start point is (/ offset 2) left or right. So don't need user enter base_point.

 

I just worked out the new X values for the pline and use (vlax-put obj 'coordinates pts) to redo the pline no need to actually draw a new pline. use (vlax-get obj 'coordinates) for the XY values of the pline. Yes need a ssget but looking at a drag over the plines for offset order. So yes would do twice for sample.

 

NOTE my code is based on the sample dwg provided, got about 1/2 way when posting this, hopefully later today will post my attempt.

 

Posted
14 hours ago, Saxlle said:

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.

@Saxlle Thanks for the code. It’s working based on the concept. Please check the DWG file. I’ve worked on Option 1 and Option 2.
In Option 1, I planned the left-side spacing as per your video input, all settings are done, but the output line looks different.
Please advise on this.
Option 2 is another concept where the top side moves up and down, similar to the left and right adjustmentsimage.thumb.png.469a9deff287328640b98fa0f7635903.png

line shifting.dwg

Posted

Hey @BIGAL,

 

I used a "base point" to avoid determinanting is the polyline drawn CCW or CW, easier, and then calculated new position of the polylines based on choosing "Left or Right", which means the "X" coord will change in that side. Also, I supose that the polyline can have many vertexes and drawed randomly (not only straight lines).

Bassicly, this is the one of the concept how it can be done.

 

8 hours ago, BIGAL said:

Yes need a ssget but looking at a drag over the plines for offset order

 

I figured out the easiest way is sorting by the Lengths of the polyline, from min to max, to get a proper order.

 

8 hours ago, BIGAL said:

NOTE my code is based on the sample dwg provided, got about 1/2 way when posting this, hopefully later today will post my attempt.

 

Can't wait to see your solution, maybe find something interesting inside the code 👍.

 

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