TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jbuzbee on January 24, 2020, 08:44:19 AM

Title: finding nested xrefs - AutoLISP
Post by: jbuzbee on January 24, 2020, 08:44:19 AM
I have a routine that "backs up" files.  It works fine but I need find out which xrefs are nested.

Thanks for any help
Title: Re: finding nested xrefs - AutoLISP
Post by: ronjonp on January 24, 2020, 09:59:48 AM
Maybe this? http://www.theswamp.org/index.php?topic=25196.msg303571#msg303571
Title: Re: finding nested xrefs - AutoLISP
Post by: jbuzbee on January 24, 2020, 10:36:49 AM
This looks like a good start - Thanks!
Title: Re: finding nested xrefs - AutoLISP
Post by: Lee Mac on January 24, 2020, 10:39:51 AM
Below is a variation of the code I previously posted here (http://www.cadtutor.net/forum/showthread.php?48702-Find-nested-blocks&p=702984&viewfull=1#post702984), modified to apply to xrefs only:
Code - Auto/Visual Lisp: [Select]
  1. (defun _nestedxrefs ( blk / enx rtn xrn )
  2.    (while (setq blk (entnext blk))
  3.        (if (and (setq enx (entget blk))
  4.                 (= "INSERT" (cdr (assoc 0 enx)))
  5.                 (setq xrn (cdr (assoc 2 enx)))
  6.                 (= 4 (logand 4 (cdr (assoc 70 (tblsearch "block" xrn)))))
  7.                 (not (member xrn rtn))
  8.            )
  9.            (setq rtn (cons xrn rtn))
  10.        )
  11.    )
  12.    rtn
  13. )
  14. (defun _xrefhierarchy ( / blk def nst rtn )
  15.    (while (setq def (tblnext "block" (null def)))
  16.        (if (or (setq blk (cdr (assoc 2 def))
  17.                      nst (_nestedxrefs (tblobjname "block" blk))
  18.                )
  19.                (= 4 (logand 4 (cdr (assoc 70 def))))
  20.            )
  21.            (setq rtn (cons (cons blk nst) rtn))
  22.        )
  23.    )
  24.    rtn
  25. )
  26. (defun _printxrefhierarchy ( blk lst ind )
  27.    (terpri)
  28.    (repeat ind (princ "    "))
  29.    (princ "+---")
  30.    (if (setq nst (cdr (assoc blk lst)))
  31.        (progn
  32.            (princ "+ ")
  33.            (princ blk)
  34.            (foreach sub nst
  35.                (_printxrefhierarchy sub lst (1+ ind))
  36.            )
  37.        )
  38.        (progn
  39.            (princ "> ")
  40.            (princ blk)
  41.        )
  42.    )
  43. )
  44. (defun c:xrefhierarchy ( )
  45.    (   (lambda ( lst )
  46.            (foreach blk lst
  47.                (_printxrefhierarchy (car blk) lst 0)
  48.            )
  49.            (textpage)
  50.        )
  51.        (_xrefhierarchy)
  52.    )
  53.    (princ)
  54. )
Title: Re: finding nested xrefs - AutoLISP
Post by: Lee Mac on January 24, 2020, 10:49:19 AM
Alternatively, here's a predicate function that will return T if the supplied block name corresponds to a nested xref:
Code - Auto/Visual Lisp: [Select]
  1. (defun nestedxref-p ( xrn / def ent enx flg tmp )
  2.     (and
  3.         (setq def (tblsearch "block" (setq xrn (strcase xrn))))
  4.         (= 4 (logand 4 (cdr (assoc 70 def))))
  5.         (progn
  6.             (while
  7.                 (and
  8.                     (setq tmp (tblnext "block" (not tmp)))
  9.                     (not flg)
  10.                 )
  11.                 (setq ent (tblobjname "block" (cdr (assoc 2 tmp))))
  12.                 (while
  13.                     (and
  14.                         (setq ent (entnext ent))
  15.                         (not flg)
  16.                     )
  17.                     (setq flg
  18.                         (and
  19.                             (setq enx (entget ent))
  20.                             (= "INSERT" (cdr (assoc 0 enx)))
  21.                             (= xrn (strcase (cdr (assoc 2 enx))))
  22.                         )
  23.                     )
  24.                 )
  25.             )
  26.             flg
  27.         )
  28.     )
  29. )
Code - Auto/Visual Lisp: [Select]
  1. (nestedxref-p "YourXRefName")
Title: Re: finding nested xrefs - AutoLISP
Post by: ronjonp on January 24, 2020, 11:30:13 AM
@Lee .. would something like this work as well?
Code - Auto/Visual Lisp: [Select]
  1. (defun _nested (bn / e)
  2.   (and (setq e (tblsearch "block" bn))
  3.        (= 4 (logand 4 (cdr (assoc 70 e))))
  4.        (null (ssget "_X" (list '(0 . "insert") (cons 2 bn))))
  5.   )
  6. )
Title: Re: finding nested xrefs - AutoLISP
Post by: Lee Mac on January 24, 2020, 02:12:46 PM
@Lee .. would something like this work as well?
Code - Auto/Visual Lisp: [Select]
  1. (defun _nested (bn / e)
  2.   (and (setq e (tblsearch "block" bn))
  3.        (= 4 (logand 4 (cdr (assoc 70 e))))
  4.        (null (ssget "_X" (list '(0 . "insert") (cons 2 bn))))
  5.   )
  6. )

Good suggestion Ron, but that could return false positives for unreferenced xrefs.
Title: Re: finding nested xrefs - AutoLISP
Post by: ronjonp on January 27, 2020, 09:27:15 AM
@Lee .. would something like this work as well?
Code - Auto/Visual Lisp: [Select]
  1. (defun _nested (bn / e)
  2.   (and (setq e (tblsearch "block" bn))
  3.        (= 4 (logand 4 (cdr (assoc 70 e))))
  4.        (null (ssget "_X" (list '(0 . "insert") (cons 2 bn))))
  5.   )
  6. )

Good suggestion Ron, but that could return false positives for unreferenced xrefs.
Ahhh yes .. good point :)
Title: Re: finding nested xrefs - AutoLISP
Post by: kenkrupa on October 14, 2021, 01:17:18 PM
Thank you Lee Mac for nestedxref-p -- just what I needed!
Title: Re: finding nested xrefs - AutoLISP
Post by: ribarm on June 27, 2022, 11:49:21 PM
Real fun with XXX...