TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: PM on November 07, 2022, 11:39:51 AM

Title: Find duplicate block on polyline vertices
Post by: PM on November 07, 2022, 11:39:51 AM
Hi i use this code to find duplicate block on polyline vertices. If there no duplicate block in drawing i want to give me an princ  "<< No duplicate block found >>". I add this text but every time i run the code i see it. Something is not correct. Can any one help?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:blockdup ( / ent idx ins lst rtn sel )
  2.    (if (setq sel (ssget "_X" '((0 . "INSERT"))))
  3.        (progn
  4.            (setq rtn (ssadd))
  5.            (repeat (setq idx (sslength sel))
  6.                (setq ent (ssname sel (setq idx (1- idx)))
  7.                      ins (cdr (assoc 10 (entget ent)))
  8.                )
  9.                (if (vl-some '(lambda ( x ) (equal ins x 1e-4)) lst)
  10.                    (ssadd ent rtn)
  11.                    (setq lst (cons ins lst))
  12.                )
  13.            )
  14.                    (princ (strcat "\n" (itoa (sslength rtn)) " found."))
  15.            (sssetfirst nil rtn)
  16.                    (princ "\n<< No duplicate block found >>")) <-- I have problem here
  17.        )
  18.    )
  19.    (princ)
  20. )
  21.  
  22.  

Thanks
Title: Re: Find duplicate block on polyline vertices
Post by: ronjonp on November 07, 2022, 12:38:15 PM
You need to add some simple IF logic to your prompts. Do you know what sslength returns?
Title: Re: Find duplicate block on polyline vertices
Post by: PM on November 07, 2022, 12:57:59 PM
Hi  ronjonp. sslength returns the number of duplicated block
Title: Re: Find duplicate block on polyline vertices
Post by: PM on November 07, 2022, 01:15:33 PM
I fix it thanks ronjonp

Code - Auto/Visual Lisp: [Select]
  1. (defun c:blockdup ( / ent idx ins lst rtn sel )
  2.    (if (setq sel (ssget "_X" '((0 . "INSERT"))))
  3.        (progn
  4.            (setq rtn (ssadd))
  5.            (repeat (setq idx (sslength sel))
  6.                (setq ent (ssname sel (setq idx (1- idx)))
  7.                      ins (cdr (assoc 10 (entget ent)))
  8.                )
  9.                (if (vl-some '(lambda ( x ) (equal ins x 1e-4)) lst)
  10.                    (ssadd ent rtn)
  11.                    (setq lst (cons ins lst))
  12.                )
  13.            )
  14.                    (princ (strcat "\n" (itoa (sslength rtn)) " found."))
  15.            (sssetfirst nil rtn)
  16.  
  17.         (if (= (sslength rtn) 0)
  18.                  (princ "\n<< No duplicate block found >>")
  19.         )
  20.        )
  21.    )
  22.    (princ)
  23. )
  24.  
  25.  
  26.  
Title: Re: Find duplicate block on polyline vertices
Post by: ronjonp on November 07, 2022, 03:39:34 PM
I fix it thanks ronjonp

Code - Auto/Visual Lisp: [Select]
  1. (defun c:blockdup ( / ent idx ins lst rtn sel )
  2.    (if (setq sel (ssget "_X" '((0 . "INSERT"))))
  3.        (progn
  4.            (setq rtn (ssadd))
  5.            (repeat (setq idx (sslength sel))
  6.                (setq ent (ssname sel (setq idx (1- idx)))
  7.                      ins (cdr (assoc 10 (entget ent)))
  8.                )
  9.                (if (vl-some '(lambda ( x ) (equal ins x 1e-4)) lst)
  10.                    (ssadd ent rtn)
  11.                    (setq lst (cons ins lst))
  12.                )
  13.            )
  14.                    (princ (strcat "\n" (itoa (sslength rtn)) " found."))
  15.            (sssetfirst nil rtn)
  16.  
  17.         (if (= (sslength rtn) 0)
  18.                  (princ "\n<< No duplicate block found >>")
  19.         )
  20.        )
  21.    )
  22.    (princ)
  23. )
  24.  
Close:
Code - Auto/Visual Lisp: [Select]
  1. (if (= (sslength rtn) 0)
  2.   (princ "\n<< No duplicate block found >>")
  3.   (princ (strcat "\n" (itoa (sslength rtn)) " found."))
  4. )
Which could also be nested like so:
Code - Auto/Visual Lisp: [Select]
  1. (princ (strcat "\n"
  2.                (if (= (sslength rtn) 0)
  3.                  "No duplicate block "
  4.                  (itoa (sslength rtn))
  5.                )
  6.                " found."
  7.        )
  8. )
Title: Re: Find duplicate block on polyline vertices
Post by: PM on November 08, 2022, 01:35:58 AM
Thank you  ronjonp