Author Topic: hepl : Search duplicate text, change color  (Read 4562 times)

0 Members and 1 Guest are viewing this topic.

mhy3sx

  • Newt
  • Posts: 120
hepl : Search duplicate text, change color
« on: September 26, 2023, 04:48:24 AM »
Hi, I am trying to update an old code

1)  for search duplicate text in the drawing and change them color. The code do this, but I want to search only PST layer not all layers


2) Check the spelling of the text . The text all the time must be like 061676614011 (12 numbers) or something like this 06167AG01949 (5 numbers AG 5 numbers). I want to check this the 12 numbers (with no letters) or 5 numbers AG 5 numbers , the AG in the middle.

This is the code

Code - Auto/Visual Lisp: [Select]
  1. (defun C:TFD (/ *error* doc mss n tc tclist duplist tcss); = Text [& Mtext] Find Duplicates
  2.   (defun *error* (errmsg)
  3.     (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
  4.       (princ (strcat "\nError: " errmsg))
  5.     ); if
  6.     (setvar cmdecho 1)
  7.     (vla-endundomark doc)
  8.     (princ)
  9.   ); defun - *error*
  10.   (if (setq mss (ssget "_X" (list '(0 . "*TEXT") (cons 410 (getvar 'ctab)))))
  11.     (progn ; then
  12.       (setvar 'cmdecho 0)
  13.       (setq TFDss (ssadd)); initially empty duplicates selection set
  14.       (repeat (setq n (sslength mss))
  15.         (setq tc (cdr (assoc 1 (entget (ssname mss (setq n (1- n)))))))
  16.           ; = text content of each
  17.         (cond
  18.           ((member tc duplist))
  19.             ; = already in duplicates list [do nothing]
  20.           ((member tc tclist) (setq duplist (cons tc duplist)))
  21.             ; = already in content list [put in duplicates list]
  22.           ((setq tclist (cons tc tclist)))
  23.             ; = first instance of it [put in content list]
  24.         ); cond
  25.       ); repeat
  26.       (setq col 0); base value for counting color numbers upward
  27.       (foreach tc duplist ; assign colors to sets of duplicates
  28.         (setq tcss (ssget "_X" (list '(0 . "*TEXT") (cons 1 tc))))
  29.         (command "_.layer" "_make" "Text" "_color" 7 "" "")
  30. ;;;        (command "_.chprop" tcss "" "_color" (setq col (1+ col)) "")
  31. ;;;     (command "_.chprop"  tcss "" "_layer" "Text" "")
  32.        (command "_.chprop" tcss "" "_color" 230 "")
  33.         (repeat (setq n (sslength tcss)) (ssadd (ssname tcss (setq n (1- n))) TFDss))
  34.           ; put in collective duplicates selection set
  35.       ); foreach
  36.       (setvar 'cmdecho 1)
  37.       (if (> (sslength TFDss) 0)
  38.         (sssetfirst nil TFDss); select/grip/highlight
  39.         (prompt "\nNo Text/Mtext objects with duplicate text content found."); else
  40.       ); if
  41.     ); progn
  42.   ); if
  43.   (princ)
  44. ); end defun
  45.  

Can anyone help? Thanks

HOSNEYALAA

  • Newt
  • Posts: 105
Re: hepl : Search duplicate text, change color
« Reply #1 on: September 26, 2023, 07:54:11 AM »
hi

Sorry my language is not good
did not I understand  Request 2

1)

Code: [Select]

    (defun C:TFD (/ *error* doc mss n tc tclist duplist tcss); = Text [& Mtext] Find Duplicates
      (defun *error* (errmsg)
        (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
          (princ (strcat "\nError: " errmsg))
        ); if
        (setvar cmdecho 1)
        (vla-endundomark doc)
        (princ)
      ); defun - *error*
      (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
      (if (setq mss (ssget "_X" (list '(0 . "*TEXT") '(8 . "PST")(cons 410 (getvar 'ctab)))))
        (progn ; then
          (setvar 'cmdecho 0)
          (setq TFDss (ssadd)); initially empty duplicates selection set
          (repeat (setq n (sslength mss))
            (setq tc (cdr (assoc 1 (entget (ssname mss (setq n (1- n)))))))
              ; = text content of each
            (cond
              ((member tc duplist))
                ; = already in duplicates list [do nothing]
              ((member tc tclist) (setq duplist (cons tc duplist)))
                ; = already in content list [put in duplicates list]
              ((setq tclist (cons tc tclist)))
                ; = first instance of it [put in content list]
            ); cond
          ); repeat

         
          (setq col 0); base value for counting color numbers upward
         
          (foreach tc duplist ; assign colors to sets of duplicates
            (setq tcss (ssget "_X" (list '(0 . "*TEXT")'(8 . "PST") (cons 1 tc))))
            (command "_.layer" "_make" "Text" "_color" 7 "" "")
    ;;;        (command "_.chprop" tcss "" "_color" (setq col (1+ col)) "")
    ;;;     (command "_.chprop"  tcss "" "_layer" "Text" "")
           (command "_.chprop" tcss "" "_color" 230 "")
            (repeat (setq n (sslength tcss)) (ssadd (ssname tcss (setq n (1- n))) TFDss))
              ; put in collective duplicates selection set
          ); foreach
          (setvar 'cmdecho 1)
          (if (> (sslength TFDss) 0)
            (sssetfirst nil TFDss); select/grip/highlight
            (prompt "\nNo Text/Mtext objects with duplicate text content found."); else
          ); if
        ); progn
      ); if
      (vla-endundomark doc)
      (princ)
    ); end defun
     



Quote
hi

mhy3sx

  • Newt
  • Posts: 120
Re: hepl : Search duplicate text, change color
« Reply #2 on: September 26, 2023, 08:22:17 AM »
Hi HOSNEYALAA .  Thanks for the reply.

I will explain the request 2

If you see the dwg file, all the text have 12 characters. Some of them are 12 numbers like 061676614011 and some of them have 12 character but the 2 of then in the middle is caps letters like 06167AG01949. I want to do this check. If for example the text is 06167ag01949, ds0616701949, R6167oG01949,etc change the color to text. For letters only the AG caps in the middle 06167AG01949 is correct, and for numbers only the 12 numbers like 061676614011. Is it possible to do this check. All the times we are talking for PST layer.

danAllen

  • Newt
  • Posts: 134
Re: hepl : Search duplicate text, change color
« Reply #3 on: September 26, 2023, 12:28:38 PM »
You can use # for wildcard for single digit:

Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" (list '(0 . "*TEXT") '(1 . "#####AG#####")))
  2. (ssget "_X" (list '(0 . "*TEXT") '(1 . "############")))

to exclude you can prefix with ~

if you want to find all text that does not match the above, you can combine logical operators, see http://www.lee-mac.com/ssget.html#logical

mhy3sx

  • Newt
  • Posts: 120
Re: hepl : Search duplicate text, change color
« Reply #4 on: September 27, 2023, 02:15:59 AM »
Hi danAllen

if I am not wrong this lines search to select text like  #####AG##### or ############

Code - Auto/Visual Lisp: [Select]
  1.     (ssget "_X" (list '(0 . "*TEXT") '(1 . "#####AG#####")))
  2.     (ssget "_X" (list '(0 . "*TEXT") '(1 . "############")))
  3.  

I want to select the text and change color if the text  is not like #####AG##### or ############. I want to do this check in the spelling of the text.

Thanks

danAllen

  • Newt
  • Posts: 134
Re: hepl : Search duplicate text, change color
« Reply #5 on: September 27, 2023, 11:53:36 AM »
As I mentioned, combine ~ for not, and AND per LeeMac reference:
Code - Auto/Visual Lisp: [Select]
  1. (ssget "X" '((0 . "*TEXT")(-4 . "<AND") (1 . "~#####AG#####")(1 . "~############")(-4 . "AND>")))

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: hepl : Search duplicate text, change color
« Reply #6 on: September 27, 2023, 01:12:20 PM »
Reminder, you can use a comma.
For example, if you had text:

Code: [Select]
DS0616701949
06167AG01949
061676614011

The following will return the last entries (not the first).
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "############,#####AG#####")))

The following will select the first line (not the second or third).
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT")(-4 . "<NOT") (1 . "#####AG#####,############")(-4 . "NOT>")))


TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

danAllen

  • Newt
  • Posts: 134
Re: hepl : Search duplicate text, change color
« Reply #7 on: September 27, 2023, 02:00:32 PM »
good point. I find it interesting/frustrating that this does not work, as it selects all three:
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "~############,~#####AG#####")))

Logically I understand it. But when using wildcards in dialogs, say a layer filter, I don't know of any workable options. As I understand it, the comma is an OR separator, what is missing is an AND separator.

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: hepl : Search duplicate text, change color
« Reply #8 on: September 27, 2023, 03:46:39 PM »
Sorry, I'm having a hard time contemplating an AND situation because the comparison is a case-by-case basis (or I am just a dunce).

Adding to our list of text.
Code: [Select]
1. DS0616701949
2. 06167AG01949
3. 061676614011
4. 06167ag01949
5. 06167WG01949

The following will grab all lines except #2 (#1,3,4,5):
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "~#####AG####9")))
The following will only grab #4:
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "#####[a-z][a-z]#####")))
The following will grab #2,3,4,5 (not #1):
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "#####[~.][~.]#####")))
The following will grab #2 and 4:
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "#####AG#####,#####ag#####")))


good point. I find it interesting/frustrating that this does not work, as it selects all three:
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "~############,~#####AG#####")))

Logically I understand it. But when using wildcards in dialogs, say a layer filter, I don't know of any workable options. As I understand it, the comma is an OR separator, what is missing is an AND separator.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

danAllen

  • Newt
  • Posts: 134
Re: hepl : Search duplicate text, change color
« Reply #9 on: September 27, 2023, 06:13:25 PM »
I'm not following? My comment was to agree with your point about using commas, it is more efficient than my solution. My second comment was really just about the limits of typical wildcards in CAD dialogs, I wish there was an additional operator to allow for multiple negations. But at least we have code..

Nice option!
The following will grab #2,3,4,5 (not #1):
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "#####[~.][~.]#####")))

mhy3sx

  • Newt
  • Posts: 120
Re: hepl : Search duplicate text, change color
« Reply #10 on: September 28, 2023, 01:41:36 AM »
We want to select only the duplicate text and check the spelling, not all the text in the layer.

Thanks

mhy3sx

  • Newt
  • Posts: 120
Re: hepl : Search duplicate text, change color
« Reply #11 on: September 28, 2023, 09:03:24 AM »
I try this but is not working

Gives me this error

Code: [Select]
; error: bad argument type: consp "<AND"


Code - Auto/Visual Lisp: [Select]
  1. (defun C:TFD (/ *error* doc mss n tc tclist duplist tcss); = Text [& Mtext] Find Duplicates
  2.       (defun *error* (errmsg)
  3.         (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
  4.           (princ (strcat "\nError: " errmsg))
  5.         ); if
  6.         (setvar cmdecho 1)
  7.         (vla-endundomark doc)
  8.         (princ)
  9.       ); defun - *error*
  10.        ;;; (if (setq mss (ssget "_X" (list '(0 . "*TEXT") '(8 . "PST")(cons 410 (getvar 'ctab)))))
  11.          (if (setq mss (ssget "_X" (list '(0 . "*TEXT") '(8 . "PST") (-4 . "<AND") (1 . "~#####AG#####")(1 . "~############")(-4 . "AND>") (cons 410 (getvar 'ctab)))))
  12.          (progn ; then
  13.           (setvar 'cmdecho 0)
  14.           (setq TFDss (ssadd)); initially empty duplicates selection set
  15.           (repeat (setq n (sslength mss))
  16.             (setq tc (cdr (assoc 1 (entget (ssname mss (setq n (1- n)))))))
  17.               ; = text content of each
  18.             (cond
  19.               ((member tc duplist))
  20.                 ; = already in duplicates list [do nothing]
  21.               ((member tc tclist) (setq duplist (cons tc duplist)))
  22.                 ; = already in content list [put in duplicates list]
  23.               ((setq tclist (cons tc tclist)))
  24.                 ; = first instance of it [put in content list]
  25.             ); cond
  26.           ); repeat
  27.  
  28.          
  29.           (setq col 0); base value for counting color numbers upward
  30.          
  31.           (foreach tc duplist ; assign colors to sets of duplicates
  32.     ;;;     (setq tcss (ssget "_X" (list '(0 . "*TEXT")'(8 . "PST") (cons 1 tc))))
  33.             (setq tcss (ssget "X" '(list (0 . "*TEXT") '(8 . "PST")(-4 . "<AND") (1 . "~#####AG#####")(1 . "~############") (-4 . "AND>") (cons 1 tc))))
  34.     ;;;     (command "_.layer" "_make" "Text" "_color" 7 "" "")
  35.     ;;;     (command "_.chprop" tcss "" "_color" (setq col (1+ col)) "")
  36.     ;;;     (command "_.chprop"  tcss "" "_layer" "Text" "")
  37.             (command "_.chprop" tcss "" "_color" 230 "")
  38.             (repeat (setq n (sslength tcss)) (ssadd (ssname tcss (setq n (1- n))) TFDss))
  39.               ; put in collective duplicates selection set
  40.           ); foreach
  41.           (setvar 'cmdecho 1)
  42.           (if (> (sslength TFDss) 0)
  43.             (sssetfirst nil TFDss); select/grip/highlight
  44.             (prompt "\nNo Text/Mtext objects with duplicate text content found."); else
  45.           ); if
  46.         ); progn
  47.       ); if
  48.       (vla-endundomark doc)
  49.       (princ)
  50.     ); end defun
  51.      
  52.  
  53.  
  54.  
  55.  

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: hepl : Search duplicate text, change color
« Reply #12 on: September 28, 2023, 09:05:47 AM »
We want to select only the duplicate text and check the spelling, not all the text in the layer.

Thanks

Do you mean simply this?
Code - Auto/Visual Lisp: [Select]
  1. (defun C:TFD (/ *error* doc mss n tc tclist duplist tcss); = Text [& Mtext] Find Duplicates
  2.   (defun *error* (errmsg)
  3.     (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
  4.       (princ (strcat "\nError: " errmsg))
  5.     ); if
  6.     (setvar cmdecho 1)
  7.     (vla-endundomark doc)
  8.     (princ)
  9.   ); defun - *error*
  10.   (if (setq mss (ssget "_X" (list '(0 . "*TEXT") '(8 . "PST") (cons 410 (getvar 'ctab)))))
  11.     (progn ; then
  12.       (setvar 'cmdecho 0)
  13.       (setq TFDss (ssadd)); initially empty duplicates selection set
  14.       (repeat (setq n (sslength mss))
  15.         (setq tc (cdr (assoc 1 (entget (ssname mss (setq n (1- n)))))))
  16.           ; = text content of each
  17.         (cond
  18.           ((member tc duplist))
  19.             ; = already in duplicates list [do nothing]
  20.           ((member tc tclist) (setq duplist (cons tc duplist)))
  21.             ; = already in content list [put in duplicates list]
  22.           ((setq tclist (cons tc tclist)))
  23.             ; = first instance of it [put in content list]
  24.         ); cond
  25.       ); repeat
  26.       (setq col 0); base value for counting color numbers upward
  27.       (foreach tc duplist ; assign colors to sets of duplicates
  28.         (setq tcss (ssget "_X" (list '(0 . "*TEXT") '(8 . "PST") (cons 1 tc))))
  29.         (command "_.layer" "_make" "Text" "_color" 7 "" "")
  30. ;;;        (command "_.chprop" tcss "" "_color" (setq col (1+ col)) "")
  31. ;;;     (command "_.chprop"  tcss "" "_layer" "Text" "")
  32.        (command "_.chprop" tcss "" "_color" 230 "")
  33.         (repeat (setq n (sslength tcss)) (ssadd (ssname tcss (setq n (1- n))) TFDss))
  34.           ; put in collective duplicates selection set
  35.       ); foreach
  36.       (setvar 'cmdecho 1)
  37.       (if (> (sslength TFDss) 0)
  38.         (sssetfirst nil TFDss); select/grip/highlight
  39.         (prompt "\nNo Text/Mtext objects with duplicate text content found."); else
  40.       ); if
  41.     ); progn
  42.   ); if
  43.   (princ)
  44. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: hepl : Search duplicate text, change color
« Reply #13 on: September 28, 2023, 09:06:57 AM »
I try this but is not working

Gives me this error

Code: [Select]
; error: bad argument type: consp "<AND"

...>%
Your code is missing some quote symbols. Can be fixed, but first try the above code (I'm trying to understand your problem).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: hepl : Search duplicate text, change color
« Reply #14 on: September 28, 2023, 09:08:10 AM »
I'm not following? My comment was to agree with your point about using commas, it is more efficient than my solution. My second comment was really just about the limits of typical wildcards in CAD dialogs, I wish there was an additional operator to allow for multiple negations. But at least we have code..

Nice option!
The following will grab #2,3,4,5 (not #1):
Code - Auto/Visual Lisp: [Select]
  1. (ssget "_X" '((0 . "*TEXT") (1 . "#####[~.][~.]#####")))

Gotcha. All good.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org