Skip to content

Instantly share code, notes, and snippets.

@sgrodnik
Created July 14, 2025 11:56
Show Gist options
  • Select an option

  • Save sgrodnik/7984b43c10b679d31c214f9f52da7bfb to your computer and use it in GitHub Desktop.

Select an option

Save sgrodnik/7984b43c10b679d31c214f9f52da7bfb to your computer and use it in GitHub Desktop.
This AutoLISP routine, DIVPL, divides a polyline, including arc segments, into points at a specified interval. It then exports the X, Y, Z coordinates of these points to a temporary tab-delimited text file and automatically opens the file.
(defun c:DIVPL ( / ss ent obj len interval counter param pt output_file file_path temp_path )
(vl-load-com) ; Load Visual LISP functions
;; Get the path to the user's temporary directory
(setq temp_path (getenv "TEMP"))
;; Use MILLISECS for a unique temporary file name
(setq file_path (strcat temp_path "\\polyline_points_" (itoa (getvar "MILLISECS")) ".txt"))
;; --- Check for existing selection ---
(setq ss (ssget "_I")) ; Get implicit selection set (already selected objects)
(if (and ss (= (sslength ss) 1)) ; If exactly one object is pre-selected
(setq ent (ssname ss 0)) ; Get the ename of the selected object
(setq ent (car (entsel "\nSelect a polyline to divide (or press Enter if already selected): "))) ; Else, prompt user to select
)
(if (and ent (eq "LWPOLYLINE" (cdr (assoc 0 (entget ent)))))
(progn
(setq obj (vlax-ename->vla-object ent))
;; --- Set default interval to 50 ---
(setq interval (getreal (strcat "\nEnter division interval (default: 50): <50>")))
(if (null interval)
(setq interval 50.0) ; If user presses Enter, use default 50
)
(if (and (<= 0.0 interval) (/= 0.0 interval))
(progn
;; Open the file for writing
(setq output_file (open file_path "w"))
(if output_file
(progn
(setq len (vlax-curve-getDistAtParam obj (vlax-curve-getEndParam obj)))
(setq counter 0)
;; Loop through points and write to file
(while (<= (* counter interval) len)
(setq param (vlax-curve-getParamAtDist obj (* counter interval)))
(setq pt (vlax-curve-getPointAtParam obj param))
;; Write coordinates to the file, separated by tabs
(write-line (strcat (rtos (car pt) 2 8) "\t" (rtos (cadr pt) 2 8) "\t" (rtos (caddr pt) 2 8)) output_file)
;; Create a point object in AutoCAD (optional, can be removed)
(entmake (list '(0 . "POINT") (cons 10 pt)))
(setq counter (1+ counter))
)
;; Close the file
(close output_file)
(princ (strcat "\nCoordinates saved to temporary file: " file_path))
;; Open the file using the default application
(startapp "EXPLORER" file_path)
)
(princ (strcat "\nError: Could not open file for writing: " file_path))
)
)
(princ "\nInvalid interval. Please enter a positive number.")
)
)
(princ "\nNo polyline selected or selected object is not a polyline.")
)
(princ) ; Suppress last value
)
@sgrodnik
Copy link
Author

AutoCAD LISP: Polyline Division & Coordinate Export

This AutoLISP routine, DIVPL, divides a polyline, including arc segments, into points at a specified interval. It then exports the X, Y, Z coordinates of these points to a temporary tab-delimited text file and automatically opens the file.

Features

  • Polyline Selection: Uses a pre-selected polyline if one is present. Otherwise, it prompts the user to select.
  • Default Interval: Sets a default division interval of 50 units. Users can accept the default or input a custom value.
  • Coordinate Export: Writes X, Y, Z coordinates to a text file.
  • Tab Delimited: Coordinates are separated by tabs.
  • Temporary File: Creates a uniquely named file in the user's temporary directory.
  • Auto-Open: Opens the generated text file using the system's default application.
  • Optional Point Creation: Creates AutoCAD point entities at each division point (can be removed from the code).

How to Use

  1. Save: Save the code to a .lsp file (e.g., divpl_smart.lsp).
  2. Load: In AutoCAD, type APPLOAD, navigate to and load the .lsp file.
  3. Run: Type DIVPL in the command line.
    • Select a polyline or ensure one is pre-selected.
    • Enter the division interval, or press Enter for the default (50).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment