Tuesday, August 5, 2025

FREELISP : Write, List, Export selected Coordinates

         If user have many points in drawing and want to

  •  Write Coordinate text at point
  • Sort all selected points in 4 direction : Left to Right , Right to Left, Upwards, Downwards
  • Export sorted list to text file in format x,y,z
        User can use this code below :

(defun insert-by-direction (pt lst dir)
  (cond
    ((null lst) (list pt))
    ((or
       (and (= dir "L") (< (car pt) (car (car lst))))
       (and (= dir "R") (> (car pt) (car (car lst))))
       (and (= dir "U") (< (cadr pt) (cadr (car lst))))
       (and (= dir "D") (> (cadr pt) (cadr (car lst))))
     )
     (cons pt lst)
    )
    (T (cons (car lst) (insert-by-direction pt (cdr lst) dir)))
  )
)
(defun insertion-sort-dir (pt-list dir)
  (if (null pt-list)
      nil
      (insert-by-direction (car pt-list) (insertion-sort-dir (cdr pt-list) dir) dir)
  )
)
(defun c:COMPAREPTSXY ( / ss i ent ed pt pt-list sorted idx dir x y txtpt txtfile)
  (prompt "\n Sort Direction:")
  (prompt "\n  L = Left → Right, R = Right → Left, U = Down → Up, D = Up → Down")
  (initget "L R U D")
  (setq dir (getkword "\nPls specify direction [L/R/U/D] <L>: "))
  (if (not dir) (setq dir "L"))
  ;; เลือก POINT
  (prompt "\nSelect points: ")
  (setq ss (ssget '((0 . "POINT"))))
  (if ss
    (progn
      (setq pt-list '()
            i 0)
      ;; Read objects
      (while (< i (sslength ss))
        (setq ent (ssname ss i)
              ed  (entget ent)
              pt  (cdr (assoc 10 ed)))
        (if pt
          (setq pt-list (cons pt pt-list))
        )
        (setq i (1+ i))
      )
      ;; Sort by direction
      (setq sorted (insertion-sort-dir pt-list dir))
      (setq txtfile (open "d://pointexport.TXT" "w"))   ;;=======
      ;; display 30 degree TEXT Coord at point 
      (prompt (strcat "\n--- List by wanted direction " dir " ---"))
      (setq idx 1)
      (foreach p sorted
        (setq x (rtos (car p) 2 2))
        (setq y (rtos (cadr p) 2 2))
        ;; print in Command Line
        (prompt (strcat "\n" (itoa idx)
                        ". X: " x ", Y: " y
                        ", Z: " (rtos (if (caddr p) (caddr p) 0.0) 2 2)))
        ;; place TEXT on coord
      (command "TEXT" p 0.8 30 (strcat x "," y))  

        ;; write coord to file
        (write-line (strcat  x ","y",0.0") txtfile)
        (setq idx (1+ idx))
      )
      (close txtfile)
      (prompt "\n✅ Coordinate written to file 'pointexport.TXT'")
      (prompt "\n-------------------------------------------")
    )
    (prompt "\n No POINT selection.")
  )
  (princ)
)

;;code ended

noted: 
user must create text file named >>> pointexport.TXT
user can change text file location at this line ;;=======

No comments:

Post a Comment

FREELISP : Write, List, Export selected Coordinates

         If user have many points in drawing and want to   Write Coordinate text at point Sort all selected points in 4 direction : Left to ...