Detect duplicate Mtext entries

Detect duplicate Mtext entries

mickeforsberg
Contributor Contributor
4,085 Views
7 Replies
Message 1 of 8

Detect duplicate Mtext entries

mickeforsberg
Contributor
Contributor

 Hello,

 

Is there any way to detect Mtext entries, or if not possible with Mtext, just text?

 

Let's say I have hundreds of Mtext entries placed in my drawing, but they have to be unique. For instance:

 

1.100

1.101

1.102

1.103

1.104

 

And so on...

Is there a way to quickly find duplicate entries and highlight them, or perhaps list them? For example if 1.100 is used twice in the drawing.

I know I can use FIND, but as I do not know which string is duplicated, this would take forever as I had to but each string in to see if there is a duplicate, if I'm not mistaken?

 

Any ideas?

 

Thanks!

0 Likes
Accepted solutions (2)
4,086 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Maybe you can get some inspiration from this:

 

(defun c:FINDDUPMTEXT (/ groupBy getDups ssList dups ss)

  (defun groupBy (fun lst / key sub res)
    (setq fun (eval fun))
    (mapcar
      '(lambda (l) (cons (car l) (reverse (cdr l))))
      (foreach l lst
        (setq res
               (if (setq sub (assoc (setq key (fun l)) res))
                 (subst (vl-list* key l (cdr sub)) sub res)
                 (cons (list key l) res)
               )
        )
      )
    )
  )

  (defun getDups (mtexts)
    (vl-remove-if
      (function (lambda (l) (< (length (cdr l)) 2)))
      (groupBy (function (lambda (x) (getpropertyvalue x "Text"))) mtexts)
    )
  )

  (defun sslist (ss / i l)
    (if (and ss (= (type ss) 'PICKSET))
      (repeat (setq i (sslength ss))
        (setq l (cons (ssname ss (setq i (1- i))) l))
      )
    )
  )

  (if (setq dups (getDups (sslist (ssget "_X" '((0 . "MTEXT"))))))
    (progn
      (setq ss (ssadd))
      (foreach l dups
        (prompt (strcat "\n" (car l) ": found " (itoa (length (cdr l))) " times"))
        (foreach e (cdr l) (ssadd e ss))
      )
      (sssetfirst nil ss)
    )
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

ВeekeeCZ
Consultant
Consultant

You can use the DATAEXTRACTION command to get a (quite) quick overview.

 

See the SCREENCAST

Message 4 of 8

Kent1Cooper
Consultant
Consultant

@mickeforsberg wrote:

....

Is there a way to quickly find duplicate entries and highlight them...? For example if 1.100 is used twice in the drawing.

....


How 'bout this [minimally tested]:

 

(defun C:FDM (/ mss n mt mtc mtclist); = Find Duplicate Mtexts
  (if (setq mss (ssget "_X" '((0 . "MTEXT"))))
    (progn ; then
      (setq dup (ssadd)); initially empty duplicates list;
        ; not localized, so you can do something with them
      (repeat (setq n (sslength mss))
        (setq
          mt (ssname mss (setq n (1- n)))
          mtc (cdr (assoc 1 (entget mt)))
        ); setq
        (if (member mtc mtclist); value is already used
          (ssadd mt dup); then -- put object in duplicates set
          (setq mtclist (cons mtc mtclist)); else -- put content in list
        ); if
      ); repeat
      (if (> (sslength dup) 0) (sssetfirst nil dup))
    ); progn
  ); if
  (princ)
); defun

 

It leaves the first-encountered instance of any given value alone, and all subsequent instances of the same value selected/gripped/highlighted.  If you want all  instances of any value that's duplicated to be identified, that could be done, but it's more complicated.

 

It assumes there is no internal formatting within the Mtext objects, or at least that any such formatting [color override, etc.] is identical between ones that you would consider duplicates, since it looks at the entire  string content of each object, including internal formatting codes.

 

Since it doesn't localize the 'dup' variable [so you can use that selection set in some kind of process apart from just having them be selected], if you want to run it again in the same editing session in the same drawing, you should wipe that out --  (setq dup nil)  -- first.

Kent Cooper, AIA
0 Likes
Message 5 of 8

zph
Collaborator
Collaborator

Good day, mickeforsberg!

 

I wrote this routine (the attachment) years ago and developed it for a similar purpose/result that you are now seeking.

 

It creates a .txt file that displays all the selected, and sorted, text values in the drawing followed respectively by each of their quantities.  Quantities greater than 1 are listed first.

 

You'll need alter the file path in the code so it corresponds to your local file location.

 

Let me know if it works for you 🙂

 

/r

~Z

Message 6 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kent1Cooper wrote:

....

It leaves the first-encountered instance of any given value alone, and all subsequent instances of the same value selected/gripped/highlighted.  If you want all  instances of any value that's duplicated to be identified, that could be done, but it's more complicated.

....


Since one can't assume that the first-encountered instance of a given duplicate content is the one you'll want to keep, I decided it's better to identify all of them.  The attached further-developed TextFindDuplicates.lsp with its TFD command does that, and some other things:

 

It includes both plain Text and  Mtext [see comments at the top about what constitutes a "duplicate" between the two types];

 

It differentiates each set of duplicates from the others, by giving a different color override  to duplicates of each shared content.  [Undoing afterwards puts them all back.]

 

See further explanation/comments at the top of the file.

 

[Error in Post 4:  The last paragraph is incorrect -- it does wipe that clean on subsequent use, by starting the variable again as an empty selection set.]

Kent Cooper, AIA
Message 7 of 8

mickeforsberg
Contributor
Contributor

_gile: This works exactly as intended, nice work!

 

BeekeeCZ: Hm interesting approach, although I think it's too advanced for my taste. But thanks for the suggestion!

 

Kent1Cooper: This also works as intended, interesting solution with the different colors! Not sure it's needed for my simple use, but interesting none the less!

 

zph: I couldn't get this to work I'm afraid, I tried different paths but it gave me an error. Somewhere I went wrong I'm sure.

 

Thanks everyone for the quick help, this matter has now been solved!

0 Likes
Message 8 of 8

ronjonp
Mentor
Mentor

There is also THIS ...

0 Likes