Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

renumber cogo points

cilate4044
Observer

renumber cogo points

cilate4044
Observer
Observer

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

0 Likes
Reply
Accepted solutions (3)
335 Views
10 Replies
Replies (10)

Jeff_M
Consultant
Consultant

@cilate4044 the following was tested with the X sort option. The Y should work, not sure about the Line option...n more time for testing. Removed ....found a bug

 

 

Jeff_M, also a frequent Swamper
EESignature
0 Likes

cilate4044
Observer
Observer

sorry I am not programmer 

the error appear : syntax error

0 Likes

Jeff_M
Consultant
Consultant
Accepted solution

@cilate4044 This one works...mostly. A check should be added to be sure the starting number will be greater than the highest number in the dwg.

;;------------=={ SelectionSet -> VLA Objects }==-------------;;
;;                                                            ;;
;;  Converts a SelectionSet to a list of VLA Objects          ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  ss - Valid SelectionSet (Pickset)                         ;;
;;------------------------------------------------------------;;
;;  Returns:  List of VLA Objects, else nil                   ;;
;;------------------------------------------------------------;;

(defun LM:ss->vla (ss / i l)
  (if ss
    (repeat (setq i (sslength ss))
      (setq
	l (cons	(vlax-ename->vla-object (ssname ss (setq i (1- i))))
		l
	  )
      )
    )
  )
)
(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)
		  (list e (vlax-get e 'Number))
		)
	       (LM:ss->vla 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 10000))
      ;;the program will fail when a duplicate number is encountered so set to number higher than any in the drawing
      ;; 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 (car a) 'Easting)
			(vlax-get (car b) 'Easting)
		     )
		   )
		)
	 )
	)

	;; Sort points by Y coordinate
	((equal sortOption "Y")
	 (setq sortedPoints
		(vl-sort
		  sortedPoints
		  '(lambda (a b)
		     (<	(vlax-get (car a) 'Northing)
			(vlax-get (car b) 'Northing)
		     )
		   )
		)
	 )
	)

					; Sort points by a specific line
	((equal sortOption "L")
	 ;; 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 (car item))
			  (setq coords (vlax-get obj 'Location))
			  (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.")
	   ;;if this is hit should abort the renumbering else it will just use the existing order
	 )
	)
					; 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 (car item))
	(vlax-put obj 'Number newNum)
	(setq newNum (1+ newNum))
      )
      (princ "\nRenumbering completed.")
    )
    (princ "\nNo points selected.")
  )
  (princ)
)

 

Jeff_M, also a frequent Swamper
EESignature

cilate4044
Observer
Observer

the code is working fine but sometimes the ordinary is not correct and can you add regen to update screen after use lisp

thank you

 

0 Likes

Jeff_M
Consultant
Consultant
Accepted solution

@cilate4044 wrote:

the code is working fine but sometimes the ordinary is not correct

 


Please describe what you are seeing vs what you are expecting. A screen capture would help.

 

For the Regen:

      )
      (command "REGEN") ;;<<<< Insert this line
      (princ "\nRenumbering completed.")
Jeff_M, also a frequent Swamper
EESignature

cilate4044
Observer
Observer

the ordinary not correct (I test lisp for x and polyline )

0 Likes

cilate4044
Observer
Observer

when I asked chatgpt answer:

 

 

I believe the reason the code is sorting the points based on the point number rather than the selected options is that the points are initially extracted and sorted by their number before any sorting option is applied.

Note the code:

lisp
(setq sortedPoints
       (mapcar '(lambda (e) (list e (vlax-get e 'Number)))
               (LM:ss->vla pts)))

The above code extracts the points and sorts them based on the point number only. Therefore, when applying any sorting option, the appropriate sorting has not been applied because the points have already been sorted by their numbers.

To fix this, you can modify the code to directly extract the coordinates and apply the sorting based on the chosen options before sorting the points.

0 Likes

Jeff_M
Consultant
Consultant
Accepted solution

@cilate4044 chatGPT is wrong. That bit of code is not doing any sorting, it is merely collecting the point numbers of the selected points so the tool can get the lowest and highest point numbers. One issue was with getting the sort option. Using getstring would fail if you entered x expecting the X option. Use getkword instead to avoid that problem:

      (initget 1 "X Y Line")
					; Choose sorting method
      (setq sortOption
	     (getkword "\nChoose sorting method: [X/Y/Line]: ")
      )

which also will require the cond statement for Line to actually say Line (like you originally had).

 

ANother issue comes up when you use the line option, select a poyline which runs through the points. The vlax-curve-getclosestpointto function fails to work as expected. If  you select a line it works just fine.

 

before renumber.png

2024-12-30_15-00-34.png

Numbers 1-5 where sorted by X, 6-10 sorted by a straight line.

Jeff_M, also a frequent Swamper
EESignature

cilate4044
Observer
Observer

thank you very much

0 Likes

cilate4044
Observer
Observer

"This Lisp can handle polyline, but it changes all the numbers in the drawing instead of just the selected points. It doesn't allow choosing the starting number or selecting the direction of the polyline to renumber (beginning or end)."

 

0 Likes