TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: w64bit on July 02, 2021, 06:02:13 AM

Title: List blocks containing objects with Z>0
Post by: w64bit on July 02, 2021, 06:02:13 AM
I am using this code to check if a drawing has Z>0.
Code: [Select]
(if (and (/= (caddr (getvar "EXTMAX")) -1E+20)
      (or (> (abs (caddr (getvar "EXTMIN"))) 0)
          (> (abs (caddr (getvar "EXTMAX"))) 0)))
(alert "Drawing is 3D !"))
How I can get in addition, in an alert window or in command line, a list with all blocks containing objects with Z>0?
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 02, 2021, 07:29:20 AM
Without any checking
Code - Auto/Visual Lisp: [Select]
  1. (defun t1 (/ is-block-with-3d res temp)
  2.   (defun is-block-with-3d (block-def / f mi ma)
  3.     (vlax-for ent block-def
  4.       (if (not f)
  5.         (progn
  6.           (if (not (vl-catch-all-apply
  7.                      (function (lambda ()
  8.                                  (vla-getboundingbox ent 'mi 'ma)
  9.                                  (setq mi (vlax-safearray->list mi)
  10.                                        ma (vlax-safearray->list ma)
  11.                                  ) ;_ end of setq
  12.                                ) ;_ end of lambda
  13.                      ) ;_ end of function
  14.                    ) ;_ end of vl-catch-all-apply
  15.               ) ;_ end of not
  16.             (setq f (apply (function and) (mapcar (function (lambda (x) (equal (caddr x) 0 1e-9))) (list mi ma))))
  17.           ) ;_ end of if
  18.         ) ;_ end of progn
  19.       ) ;_ end of if
  20.     ) ;_ end of vlax-for
  21.     f
  22.   ) ;_ end of defun
  23.     (if (setq temp (is-block-with-3d blk-def))
  24.       (setq res (cons (vla-get-name temp) res))
  25.     ) ;_ end of if
  26.   ) ;_ end of vlax-for
  27.   (if res
  28.     (princ (strcat "\nThese blocks has 3d geom:"
  29.                    (mapcar (function (lambda (x) (strcat "\n\t" x))) (vl-sort res (function <)))
  30.            ) ;_ end of strcat
  31.     ) ;_ end of princ
  32.   ) ;_ end of if
  33.   (princ)
  34. ) ;_ end of defun
Title: Re: List blocks containing objects with Z>0
Post by: w64bit on July 02, 2021, 04:35:44 PM
Tried with the file attached. No message.
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 04, 2021, 01:07:25 PM
Try this
Code - Auto/Visual Lisp: [Select]
  1. (defun t1 (/ is-block-with-3d res)
  2.   (defun is-block-with-3d (block-def / f mi ma)
  3.     (vlax-for ent block-def
  4.       (if (not f)
  5.         (progn
  6.           (if (not (vl-catch-all-error-p
  7.                      (vl-catch-all-apply
  8.                        (function (lambda ()
  9.                                    (vla-getboundingbox ent 'mi 'ma)
  10.                                    (setq mi (vlax-safearray->list mi)
  11.                                          ma (vlax-safearray->list ma)
  12.                                    ) ;_ end of setq
  13.                                  ) ;_ end of lambda
  14.                        ) ;_ end of function
  15.                      ) ;_ end of vl-catch-all-apply
  16.                    ) ;_ end of vl-catch-all-error-p
  17.               ) ;_ end of not
  18. ;; modified. Thanks to ribarm
  19.             (setq f (apply (function or) (mapcar (function (lambda (x) (not (equal (caddr x) 0 1e-9)))) (list mi ma))))
  20.           ) ;_ end of if
  21.         ) ;_ end of progn
  22.       ) ;_ end of if
  23.     ) ;_ end of vlax-for
  24.     f
  25.   ) ;_ end of defun
  26.     (if (is-block-with-3d blk-def)
  27.       (setq res (cons (vla-get-name blk-def) res))
  28.     ) ;_ end of if
  29.   ) ;_ end of vlax-for
  30.   (if res
  31.     (princ (strcat "\nThese blocks has 3d geom:"
  32.                    (apply (function strcat)
  33.                           (mapcar (function (lambda (x) (strcat "\n\t" x))) (vl-sort res (function <)))
  34.                    ) ;_ end of apply
  35.            ) ;_ end of strcat
  36.     ) ;_ end of princ
  37.   ) ;_ end of if
  38.   (princ)
  39. ) ;_ end of defun
Title: Re: List blocks containing objects with Z>0
Post by: roy_043 on July 04, 2021, 03:18:46 PM
There is a not missing on line 19.
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 04, 2021, 04:57:12 PM
Ok, fine, you're right. Right now threre is a midnight, I can't create any kind of code. Could you corrent it please?
Title: Re: List blocks containing objects with Z>0
Post by: ribarm on July 05, 2021, 01:53:51 AM
  (defun is-block-with-3d (block-def / f mi ma)
    (vlax-for ent block-def
      (if (not f)
        (progn
          (if (not (vl-catch-all-error-p
                     (vl-catch-all-apply
                       (function (lambda ()
                                   (vla-getboundingbox ent 'mi 'ma)
                                   (setq mi (vlax-safearray->list mi)
                                         ma (vlax-safearray->list ma)
                                   ) ;_ end of setq
                                 ) ;_ end of lambda
                       ) ;_ end of function
                     ) ;_ end of vl-catch-all-apply
                   ) ;_ end of vl-catch-all-error-p
              ) ;_ end of not
            (setq f (apply (function or) (mapcar (function (lambda (x) (not (equal (caddr x) 0 1e-9)))) (list mi ma))))
          ) ;_ end of if
        ) ;_ end of progn
      ) ;_ end of if
    ) ;_ end of vlax-for
    f
  ) ;_ end of defun
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 05, 2021, 02:46:35 AM
Thank you. Code modified.
Sorry, I forgot about this thread.
Title: Re: List blocks containing objects with Z>0
Post by: w64bit on July 05, 2021, 05:32:57 AM
Thank you. It's working but it did not list all the blocks' names, only reports IS-BLOCK-WITH-3D for the whole DWG.
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 05, 2021, 07:49:03 AM
Use t1 function from https://www.theswamp.org/index.php?topic=56854.msg605407#msg605407
Title: Re: List blocks containing objects with Z>0
Post by: w64bit on July 05, 2021, 09:18:56 AM
Thank you.
I have to pay more attention.
Title: Re: List blocks containing objects with Z>0
Post by: w64bit on July 11, 2021, 06:52:22 AM
One question.
Is it possible to have the list of blocks in alert window instead of command line?
Title: Re: List blocks containing objects with Z>0
Post by: kpblc on July 11, 2021, 12:14:21 PM
Replace princ to alert
Title: Re: List blocks containing objects with Z>0
Post by: w64bit on July 11, 2021, 06:57:24 PM
Excellent. Thank you.