; xreftree.lsp prints xref treeview to file
(princ"\nLoading Command Line XrefTree...")
(defun c:XrefTree ( / GetXrefNesting PrintNestedList outputfile fopen)

(defun *error*(msg)(if fopen (close fopen)))

(defun GetXrefNesting (/ GetXrefInfo tempData tempEnt XrefList NonNestedXrefList NestedXrefList )

(defun GetXrefInfo ( ent / NestList )
(foreach i (member '(102 . "{BLKREFS") (entget ent))
(if (equal (car i) 332)
(progn
(setq NestedList (cons (cdr i) NestedList))
(setq NestList
(cons
(cons
(cdr i)
(GetXrefInfo (cdr i))
)
NestList
)
)
)
)
)
NestList
)
;-------------------------------------

(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 tempEnt (cdr (assoc 330 (entget tempEnt))))
(setq XrefList
(cons
(cons
tempEnt
(GetXrefInfo tempEnt)
)
XrefList
)
)
)
)
)
(foreach i XrefList
(if (not (member (car i) NestedList))
(setq NonNestedXrefList (cons i NonNestedXrefList))
)
)
NonNestedXrefList
)
;------------------------------------------------
(defun PrintNestedList ( lst spc )
(foreach i
(vl-sort lst (function (lambda ( a b ) ( " (cdr(assoc 1 (tblsearch"BLOCK"(cdr (assoc 2 (entget (car i)))))))) fopen)
; (prompt (strcat "\n" spc " Nested xref: " (cdr (assoc 2 (entget (car i))))))
(PrintNestedList (cdr i) (strcat " " spc))
)
)
;-----------------------------------------------------

(setq outputfile (open (setq Path (strcat (getvar 'DwgPrefix) (vl-filename-base (getvar 'DwgName)) "_Xref.txt")) "w"))

(setq outputfile
(strcat
(getvar 'dwgprefix)
(vl-filename-base (getvar 'dwgname))
"_Xref-path.txt"
)
)
(setq fopen (open outputfile "w"))
(write-line (strcat "Drawing Name --> " (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".dwg") fopen)
(foreach i
(vl-sort (GetXrefNesting) (function (lambda ( a b ) ( " (cdr(assoc 1 (tblsearch"BLOCK"(cdr(assoc 2 (entget (car i)))))))) fopen)
; (prompt (strcat "\n Main xref: " (cdr (assoc 2 (entget (car i))))))
(PrintNestedList (cdr i) " |-")
)
(close fopen)
(startapp "notepad.exe" outputfile)
(princ)
) ; defun
;(princ"\n...Loaded. To Start Command type: XrefTree.")(princ)

(defun c:PXI ( / GetXrefs XrList Opened Data Path )

(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
)
;-----------------------------------------------------
(if (setq XrList (GetXrefs))
(progn
(setq Opened (open (setq Path (strcat (getvar 'DwgPrefix) (vl-filename-base (getvar 'DwgName)) "_Xref.txt")) "w"))
(foreach i XrList
(setq Data (entget (car i)))
(write-line (strcat "Xref name: " (cdr (assoc 2 Data))) Opened)
(write-line (strcat "\tSaved path: " (cdr (assoc 1 Data))) Opened)
(write-line (strcat "\tFound at path: " (findfile (cdr (assoc 1 Data)))) Opened)
(write-line "\tInserted at:" Opened)
(foreach j (cadr i)
(setq Data (entget j))
(write-line (strcat "\t\tLayout: " (cdr (assoc 410 Data))) Opened)
(write-line (strcat "\t\tInsertion point: " (vl-princ-to-string (cdr (assoc 10 Data)))) Opened)
(write-line (strcat "\t\tScale[X Y Z]: " (rtos (cdr (assoc 41 Data))) " " (rtos (cdr (assoc 42 Data))) " " (rtos (cdr (assoc 43 Data)))) Opened)
(write-line (strcat "\t\tRotation: " (rtos (cdr (assoc 50 Data)))) Opened)
(write-line (strcat "\t\tLayer: " (cdr (assoc 8 Data))) Opened)
)
(write-line "\tNested xrefs:" Opened)
(foreach j (caddr i)
(write-line (strcat "\t\t" (cdr (assoc 2 (entget j)))) Opened)
)
)
(close Opened)
(startapp "notepad.exe" Path)
(prompt (strcat "\n Report at: " Path))

)
(prompt "\n No xrefs within drawing.")
)
(princ)
)
(c:XrefTree)
(c:pxi)

Advertisements