Author Topic: finding nested xrefs - AutoLISP  (Read 1960 times)

0 Members and 1 Guest are viewing this topic.

jbuzbee

  • Swamp Rat
  • Posts: 851
finding nested xrefs - AutoLISP
« 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
James Buzbee
Windows 8

ronjonp

  • Needs a day job
  • Posts: 7335

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: finding nested xrefs - AutoLISP
« Reply #2 on: January 24, 2020, 10:36:49 AM »
This looks like a good start - Thanks!
James Buzbee
Windows 8

Lee Mac

  • Seagull
  • Posts: 12526
  • London, England
Re: finding nested xrefs - AutoLISP
« Reply #3 on: January 24, 2020, 10:39:51 AM »
Below is a variation of the code I previously posted here, 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. )

Lee Mac

  • Seagull
  • Posts: 12526
  • London, England
Re: finding nested xrefs - AutoLISP
« Reply #4 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")

ronjonp

  • Needs a day job
  • Posts: 7335
Re: finding nested xrefs - AutoLISP
« Reply #5 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. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12526
  • London, England
Re: finding nested xrefs - AutoLISP
« Reply #6 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.

ronjonp

  • Needs a day job
  • Posts: 7335
Re: finding nested xrefs - AutoLISP
« Reply #7 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 :)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

kenkrupa

  • Newt
  • Posts: 79
Re: finding nested xrefs - AutoLISP
« Reply #8 on: October 14, 2021, 01:17:18 PM »
Thank you Lee Mac for nestedxref-p -- just what I needed!