- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This AutoLISP function is designed for use in AutoCAD, specifically with Civil 3D. It allows the user to renumber a set of COGO (Coordinate Geometry) points in the drawing. The points can be sorted according to different criteria before renumbering
I use chat gpt but it is not working
(defun c:RenumberPoints ( / c3dApp c3dDoc c3dPoints pts startNum newNum sortedPoints obj sortOption lineObj distList minNum maxNum defaultStartNum)
;
; Load Visual LISP library
(vl-load-com)
; Call AutoCAD application
(setq c3dApp (vlax-get-acad-object)) ;; Get AutoCAD application
; (setq c3dDoc (vla-get-ActiveDocument c3dApp)) ;; Get the active document
; Access points within Civil 3D using vla-get-CogoPoints from the AutoCAD document
; (setq c3dPoints (vla-get-CogoPoints c3dDoc)) ;; Access points in the active document
; Ensure points are selected
(setq pts (ssget "_:L" '((0 . "AECC_COGO_POINT")))) ;; Select only COGO points
(if pts
(progn
; Extract point numbers
(setq sortedPoints
(mapcar
(lambda (e)
(setq obj (vlax-ename->vla-object e))
(list e (vla-get-PointNumber obj)))
(vl-remove-if 'null (mapcar 'ssname (list pts)))
)
)
; Calculate the smallest and largest point numbers
(setq minNum (apply 'min (mapcar 'cadr sortedPoints)))
(setq maxNum (apply 'max (mapcar 'cadr sortedPoints)))
(setq defaultStartNum (+ maxNum 1)) ;; Suggested starting number
; Display information window
(princ (strcat
"\nSelected Points Info:\n"
" Smallest Point Number: " (itoa minNum) "\n"
" Largest Point Number: " (itoa maxNum) "\n"
" Suggested Starting Number: " (itoa defaultStartNum) "\n"
))
; Input starting number
(setq startNum (getint (strcat "\nEnter the starting number for renumbering [Suggested: " (itoa defaultStartNum) "]: ")))
(if (null startNum) (setq startNum defaultStartNum))
; Choose sorting method
(setq sortOption (getstring "\nChoose sorting method: [X/Y/Line]: "))
(cond
; Sort points by X coordinate
((equal sortOption "X")
(setq sortedPoints
(vl-sort
sortedPoints
'(lambda (a b) (< (vlax-get (vlax-ename->vla-object (car a)) 'Easting)
(vlax-get (vlax-ename->vla-object (car b)) 'Easting)))
)
)
)
;; Sort points by Y coordinate
((equal sortOption "Y")
(setq sortedPoints
(vl-sort
sortedPoints
'(lambda (a b) (< (vlax-get (vlax-ename->vla-object (car a)) 'Northing)
(vlax-get (vlax-ename->vla-object (car b)) 'Northing)))
)
)
)
; Sort points by a specific line
((equal sortOption "Line")
;; Select line
(setq lineObj (vlax-ename->vla-object (car (entsel "\nSelect the line or polyline to sort points by: ")))))
(if lineObj
(setq sortedPoints
(vl-sort
(mapcar
(lambda (item)
(setq obj (vlax-ename->vla-object (car item)))
(setq coords (vlax-get obj 'Coordinates))
(list (car item)
(vlax-curve-getDistAtPoint
lineObj
(vlax-curve-getClosestPointTo lineObj coords))))
sortedPoints
)
'(lambda (a b) (< (cadr a) (cadr b)))
)
)
(princ "\nNo line or polyline selected.")
)
)
; Handle invalid sorting option
(T (princ "\nInvalid sorting option. Please choose X, Y, or Line."))
)
; Renumber the points
(setq newNum startNum)
(foreach item sortedPoints
(setq obj (vlax-ename->vla-object (car item)))
(vla-put-PointNumber obj newNum)
(setq newNum (1+ newNum))
)
(princ "\nRenumbering completed.")
)
(princ "\nNo points selected.")
)
(princ)
)
Solved! Go to Solution.