Created
July 14, 2025 11:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
How to Use
.lspfile (e.g.,divpl_smart.lsp).APPLOAD, navigate to and load the.lspfile.DIVPLin the command line.