Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created November 8, 2024 12:04
Show Gist options
  • Save zeffii/3bf885cc3a519c48c867501fc44add41 to your computer and use it in GitHub Desktop.
Save zeffii/3bf885cc3a519c48c867501fc44add41 to your computer and use it in GitHub Desktop.
point picker and setter
(defun c:FindNearestText ()
;; Prompt the user to select a point
(setq pickPoint (getpoint "\nPick a point: "))
;; Check if the point is valid
(if pickPoint
(progn
;; Set initial variables
(setq minDist nil ;; Store minimum distance
nearestTextContent nil) ;; Store content of the nearest text
(setq selectionSet (ssget "X" '((0 . "TEXT")))) ;; Select all TEXT objects
;; Check if there are any Text objects
(if selectionSet
(progn
(setq totalObjects (sslength selectionSet) ;; Get total number of TEXT objects
i 0) ;; Start loop index
;; Loop through each Text object in the selection set
(while (< i totalObjects)
;; Get entity at index i
(setq textEntity (ssname selectionSet i))
(setq textData (entget textEntity)) ;; Get entity data
;; Get the position of the Text object (10 is for the insertion point)
(setq textPos (cdr (assoc 10 textData)))
;; Ensure the Text object has a valid position
(if textPos
(progn
;; Calculate distance between the picked point and the Text object (ignore Z)
(setq dist (sqrt (+ (expt (- (car textPos) (car pickPoint)) 2) ;; X difference squared
(expt (- (cadr textPos) (cadr pickPoint)) 2)))) ;; Y difference squared
;; If it's the closest Text object so far, store its data
(if (or (not minDist) (< dist minDist))
(progn
(setq minDist dist)
(setq nearestTextContent (cdr (assoc 1 textData))) ;; Store the Text content
)
)
)
)
;; Increment the index
(setq i (1+ i))
)
;; If a nearest Text object was found, print its content
(if nearestTextContent
(princ (strcat "\nNearest Text Content: " nearestTextContent))
(princ "\nNo Text objects found.")
)
)
(princ "\nNo Text objects found.")
)
)
(princ "\nInvalid point selected.")
)
;; End of function
(princ)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment