Author Topic: FYI... List xref inserts and nested ones  (Read 8863 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
FYI... List xref inserts and nested ones
« on: March 27, 2009, 01:32:00 PM »
This is part of a post I did over at AUGI, and wanted to share it as I learned something new, and figured it could help people here.  It may need to be tested more, as I don't use nested xrefs, so I don't have a lot to test this on, but it worked on my small test drawing.

Found out something interesting, and figured I'd code it up real quick.  This will get all the xrefs within the drawing.  It will list all the inserts of said xref, and will list the xrefs that are nested within it.  At least it did on my small test.  It returns a list of list of enames.  The first ename is the xref definition.  Then next list within that list will be the list of inserts.  The next list will be the list of nested block table record for the xrefs that are nested.

Code: [Select]
(defun GetXrefs (/ tempData tempEnt XrefList)
   
    (while (setq tempData (tblnext "block" (not tempData)))
        (if (equal (logand (cdr (assoc 70 tempData)) 4) 4)
            (progn
                (setq tempEnt (tblobjname "block" (cdr (assoc 2 tempData))))
                (setq tempData (entget (cdr (assoc 330 (entget tempEnt)))))
                (setq XrefList
                    (cons
                        (cons
                            tempEnt
                            (
                                (lambda ( x / InsList NestList )
                                    (foreach i x
                                        (cond
                                            ((equal (car i) 331)
                                                (setq InsList (cons (cdr i) InsList))
                                            )
                                            ((equal (car i) 332)
                                                (setq NestList (cons (cdr i) NestList))
                                            )
                                        )
                                    )
                                    (list InsList NestList)
                                )
                                (member '(102 . "{BLKREFS") tempData)
                            )
                        )
                        XrefList
                    )
                )
            )
        )
    )
    XrefList
)

Quote
Command: (setq lst (GetXrefs))
((<Entity name: 7ed545c0> nil nil) (<Entity name: 7ed54530> (<Entity name:
7ed54548>) (<Entity name: 7ed545b8>)) (<Entity name: 7ed54500> (<Entity name:
7ed54520>) nil))

No inserts for nested xrefs
Quote
(<Entity name: 7ed545c0> nil nil)

One insert, no nested xrefs
Quote
(<Entity name: 7ed54500> (<Entity name: 7ed54520>) nil)

One insert, one nested xref
Quote
(<Entity name: 7ed54530> (<Entity name: 7ed54548>) (<Entity name: 7ed545b8>))

Hope that all makes sense.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mkweaver

  • Bull Frog
  • Posts: 352
Re: FYI... List xref inserts and nested ones
« Reply #1 on: March 31, 2009, 05:42:28 PM »
Very nice, Tim!

To get my head wrapped around the output from this I had to run the routine and document what came back.  Here it is in hopes that it will be of use to others:
Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  Routine: GetXrefs  ;;;
;;;  Purpose: Get all the xrefs in the active drawing along with their nesting ;;;
;;;  Arguments: none ;;;
;;;  Returns: A list of lists, thus: ;;;
;;;...;;;
;;;( ;;;
;;; (<Entity name: 7ed080a8> 1 ;;;
;;; nil 2 ;;;
;;; nil) 3 ;;;
;;; (<Entity name: 7ed07cd8> 1 ;;;
;;; nil 2 ;;;
;;; nil) 3 ;;;
;;; (<Entity name: 7ed07398> 1 ;;;
;;;    nil              2 ;;;
;;;    (<Entity name: 7ed08018> 4 ;;;
;;;      <Entity name: 7ed07d00> 4 ;;;
;;;      <Entity name: 7ed07ce8> 4 ;;;
;;;      <Entity name: 7ed07cd0> 4 ;;;
;;;      <Entity name: 7ed07cb8> 4 ;;;
;;;    ) 5 ;;;
;;;  ) 6 ;;;
;;; (<Entity name: 7ed080a8> 1 ;;;
;;; nil 2 ;;;
;;; nil) 2 ;;;
;;; (<Entity name: 7ed07338> 1 ;;;
;;;   (<Entity name: 7ed07f28>) 2 ;;;
;;;   (<Entity name: 7ed080a0> 4 ;;;
;;;     <Entity name: 7ed07390> 4 ;;;
;;;     <Entity name: 7ed07378> 4 ;;;
;;;     <Entity name: 7ed07360> 4 ;;;
;;;     <Entity name: 7ed07348> 4 ;;;
;;;   ) 5 ;;;
;;; ) 6 ;;;
;;; (<Entity name: 7ed07150> 1 ;;;
;;;   (<Entity name: 7ed07188>) 2 ;;;
;;;   nil 3 ;;;
;;; ) 6 ;;;
;;; (<Entity name: 7ed03dd0> 1 ;;;
;;;   (<Entity name: 7ed03e30>) 2 ;;;
;;;   nil 3 ;;;
;;; ) 6 ;;;
;;;) ;;;
;;;-----------------------------------------------------------------------------------------;;;
;;;  In the above list, the line numbers represent:               ;;;
;;;  1 - The block definition entity for this xref                           ;;;
;;;  2 - A list of inserts of this xref, nil indicates that this is a nested ;;;
;;;      xref. ;;;
;;;  3 - A list of xrefs nested in this xref. ;;;
;;;  4 - One of the nested xrefs ;;;
;;;  5 - End of the list of nested xrefs ;;;
;;;  6 - End of the list of information on this xref ;;;
;;;=====================================================================;;;
;;;  This procedure pulled from a post on TheSwamp.org by Tim Willey, dated ;;;
;;;  3/27/09 (http://www.theswamp.org/index.php?topic=28062.0) by Mike Weaver ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


This is going in my library.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: FYI... List xref inserts and nested ones
« Reply #2 on: March 31, 2009, 06:28:34 PM »
Glad it works in your testing.  It seems like you have some nesting, and that is what I hoped would happen.  Here is a simple way to show what is returned also; printed text to the command line.

Thanks for posting back your results.

Code: [Select]
    (foreach i (GetXrefs)
        (princ "\nXref name: ")
        (princ (cdr (assoc 2 (entget (car i)))))
        (princ "\n    Inserted amount: ")
        (princ (itoa (length (cadr i))))
        (if (caddr i)
            (progn
                (princ "\n    Nested xref names: ")
                (foreach j (caddr i)
                    (princ "\n        ")
                    (princ (cdr (assoc 2 (entget j))))
                )
            )
        )
        (princ "\n")
    )

Quote
Xref name: Cube
    Inserted amount: 0

Xref name: Xref2
    Inserted amount: 1
    Nested xref names:
        Cube

Xref name: Xref1
    Inserted amount: 1
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mkweaver

  • Bull Frog
  • Posts: 352
Re: FYI... List xref inserts and nested ones
« Reply #3 on: March 31, 2009, 07:14:40 PM »
Sweet!  That helps too.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: FYI... List xref inserts and nested ones
« Reply #4 on: April 01, 2009, 03:09:51 AM »
I should point out one thing that I forgot.  The insert list returns erased enames, thanks to Gile for pointing that out.  I think this is due to the undo ability of Acad.  So we either have to check that part of the returned list, or maybe put in an option to return erased enames for not.  A quick way is to use ' vlax-erased-p ' on the ename, or I think you can use ' entget ', and if nil is returned, then the entity is erased.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

mkweaver

  • Bull Frog
  • Posts: 352
Re: FYI... List xref inserts and nested ones
« Reply #5 on: April 01, 2009, 11:57:54 AM »
I should point out one thing that I forgot.  The insert list returns erased enames, thanks to Gile for pointing that out.  I think this is due to the undo ability of Acad.  So we either have to check that part of the returned list, or maybe put in an option to return erased enames for not.  A quick way is to use ' vlax-erased-p ' on the ename, or I think you can use ' entget ', and if nil is returned, then the entity is erased.

Here it is with the test for deleted inserts:
Code: [Select]
(defun xrl:GetXrefs (/ tempData tempEnt XrefList)
   
    (while (setq tempData (tblnext "block" (not tempData)))
        (if (equal (logand (cdr (assoc 70 tempData)) 4) 4)
            (progn
                (setq tempEnt (tblobjname "block" (cdr (assoc 2 tempData))))
                (setq tempData (entget (cdr (assoc 330 (entget tempEnt)))))
                (setq XrefList
                    (cons
                        (cons
                            tempEnt
                            (
                                (lambda ( x / InsList NestList )
                                    (foreach i x
                                        (cond
  ((and
     (equal (car i) 331)
     (entget (cdr i))
     )
                                                (setq InsList (cons (cdr i) InsList))
                                            )
                                            ((equal (car i) 332)
                                                (setq NestList (cons (cdr i) NestList))
                                            )
                                        )
                                    )
                                    (list InsList NestList)
                                )
                                (member '(102 . "{BLKREFS") tempData)
                            )
                        )
                        XrefList
                    )
                )
            )
        )
    )
    XrefList
)code]

danglar

  • Newt
  • Posts: 161
  • Read My Li(s)(p)
Re: FYI... List xref inserts and nested ones
« Reply #6 on: October 25, 2018, 10:00:44 AM »
..probably this can be useful for output
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq Openedd (open (setq Path (strcat (getvar 'DwgPrefix) (vl-filename-base (getvar 'DwgName)) "_Xref-count.txt")) "w"))
  3.        
  4.         (foreach i (GetXrefs)
  5.         (write-line (strcat "\nXref name: ") Openedd)
  6.         (write-line (strcat "\t\t" (cdr (assoc 2 (entget (car i))))) Openedd)
  7.         (write-line (strcat "\n    Inserted amount: ") Openedd)
  8.         (write-line (strcat "\t\t" (itoa (length (cadr i)))) Openedd)
  9.         (if (caddr i)
  10.             (progn
  11.                 (write-line (strcat "\n    Nested xref names: ") Openedd)
  12.                 (foreach j (caddr i)
  13.                     (write-line (strcat "\n        ") Openedd)
  14.                     (write-line (strcat "\t\t" (cdr (assoc 2 (entget j)))) Openedd)
  15.                 )
  16.             )
  17.         )
  18.         (write-line (strcat "\n") Openedd)
  19.     )
  20.        
  21.         (close Openedd)
  22. (startapp "notepad.exe" Path)
  23.             (prompt (strcat "\n Report at: " Path))
  24.        
  25.        
  26.  
  27.