Message 1 of 10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear All,
I am sharing a working code of the above subject. Hope it will be use full to you all.
(defun strip-formatting (s)
;; Strip out formatting control characters (e.g., special symbols in MTEXT)
(vl-remove-if '(lambda (x) (or (< x 32) (> x 126))) (vl-string->list s))
)
(defun clean-text (s)
;; Clean up the text by trimming spaces, converting to lowercase, and removing special formatting characters
(setq s (vl-list->string (strip-formatting s))) ;; Remove non-printable characters
(vl-string-right-trim " \t\r\n" (vl-string-left-trim " \t\r\n" (strcase s))) ;; Trim spaces and convert to lowercase
)
(defun c:HighlightDuplicates ( / ss lst ent i txt objType colorOverride cleanedTxt textCount)
;; Prompt user to select TEXT and MTEXT objects
(setq ss (ssget '((0 . "TEXT,MTEXT")))) ;; Select TEXT and MTEXT objects
;; Check if the user selected any objects
(if ss
(progn
(setq lst '()) ;; Initialize an empty list to track unique text
(setq textCount '()) ;; Create a list to count occurrences of each text
;; First pass: Count occurrences of each cleaned text
(repeat (setq i (sslength ss))
(setq ent (ssname ss (setq i (- i 1)))) ;; Get the entity name
(setq objType (cdr (assoc 0 (entget ent)))) ;; Get the object type (TEXT or MTEXT)
;; Extract the text depending on the object type
(setq txt (cdr (assoc 1 (entget ent)))) ;; Get the raw text for both TEXT and MTEXT
(setq cleanedTxt (clean-text txt)) ;; Clean the text
;; Count occurrences
(if (assoc cleanedTxt textCount)
(setq textCount (subst (cons cleanedTxt (1+ (cdr (assoc cleanedTxt textCount))))
(assoc cleanedTxt textCount) textCount))
(setq textCount (cons (cons cleanedTxt 1) textCount))
)
)
;; Second pass: Highlight duplicates
(foreach item textCount
(if (> (cdr item) 1) ;; Only highlight if count is greater than 1
(progn
(setq colorOverride '(62 . 1)) ;; Set color override to red (1)
;; Loop through the selected entities again to apply color
(repeat (setq i (sslength ss))
(setq ent (ssname ss (setq i (- i 1)))) ;; Get the entity name
(setq objType (cdr (assoc 0 (entget ent)))) ;; Get the object type (TEXT or MTEXT)
;; Extract the text
(setq txt (cdr (assoc 1 (entget ent)))) ;; Get the raw text for both TEXT and MTEXT
(setq cleanedTxt (clean-text txt)) ;; Clean the text
;; Check if the cleaned text matches the duplicate
(if (equal cleanedTxt (car item))
(progn
;; Apply the color directly
(entmod (append (entget ent) (list colorOverride)))
;; Update the entity
(entupd ent)
)
)
)
)
)
)
(princ "\nDuplicate entities have been highlighted.")
)
(princ "\nNo TEXT or MTEXT selected.")
)
(princ)
)
Solved! Go to Solution.