TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cmwade77 on May 16, 2019, 06:44:21 PM

Title: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 16, 2019, 06:44:21 PM
Ok, I will probably end up having to roll my own, but in the interest of not reinventing the wheel, does anyone have a routine that automatically detaches any xrefs that are either unloaded, unresolved or not found?

We find that we are getting more and more drawings with almost circular xRefs that have some unloaded and to try to get rid of those is complicated.
Title: Re: Unloaded/Unresolved xRefs
Post by: MSTG007 on May 17, 2019, 08:09:32 AM
lol.... please let me know if you find something. that is all to common here.
Title: Re: Unloaded/Unresolved xRefs
Post by: ronjonp on May 17, 2019, 09:47:52 AM
Perhaps this: https://www.theswamp.org/index.php?topic=4103.0
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 03:39:05 PM
Pretty easy. If an xref is unloaded the xref block def’s count property will be zero, if it can’t be resolved (eg path nfg) the count will be 1. Before performing the detach method delete all instances (cdrs of 331 groups in the xref block def’s DXF data) of the xref (the previously will find multiples as well as nested) lest the detach method fail. Cheers.
Title: Re: Unloaded/Unresolved xRefs
Post by: roy_043 on May 17, 2019, 04:04:49 PM
Regarding unloaded/unresolved:
Since a resolved xref may contain a single object, relying on the count property is risky.

BTW: In BricsCAD there are some dedicated functions for this purpose:
vla-get-isresolved
vla-get-isunloaded
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 04:09:02 PM
In 30+ years of AutoCAD use and support I’ve never encountered an xref with 1 object so I consider the risk inconsequential. Kudos to Bricscad.
Title: Re: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 17, 2019, 04:10:50 PM
In 30+ years of AutoCAD use and support I’ve never encountered an xref with 1 object so I consider the risk inconsequential. Kudos to bricscad tho.
Sadly, I have and these are files from other companies....yes, I know it doesn't make much sense to do that, but it does happen.
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 04:13:10 PM
Well then one has to verify the path (including relative paths) is unresolvable and / or determine a file is unreolsvable via group 70 states yada
Title: Re: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 17, 2019, 04:22:39 PM
Well then one has to verify the path (including relative paths) is unresolvable and / or determine a file is unreolsvable via group 70 states yada
In other words, the final code at the link ronjonp posted above?
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 04:24:16 PM
Sure
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 05:01:43 PM
I believe this should work - detaching xrefs that are unloaded or unresolvable - including drawings where the xrefs sport multiple instances which usually bombs the detach method and is not addressed in the other thread.

Code: [Select]
(defun c:dump-xref-shat ( / e )
    (vl-load-com)
    (vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
        (and
            (eq :vlax-true (vla-get-isxref b))
            (zerop (logand 32 (cdr (assoc 70 (entget (cdr (assoc 360 (entget (vlax-vla-object->ename b)))))))))
            (progn
                (foreach p (entget (vlax-vla-object->ename b))
                    (if (eq 331 (car p))
                        (progn
                            (vl-catch-all-apply 'vla-put-lock
                                (list
                                    (vla-item
                                        (vla-get-layers (vla-get-document b))
                                        (cdr (assoc 8 (entget (setq e (cdr p)))))
                                    )                               
                                    :vlax-false
                                )
                            )
                            (vl-catch-all-apply 'entdel (list e))
                        )
                    )   
                )
                (vl-catch-all-apply 'vla-detach (list b))
            )
        )
    )
    (princ)
)

Cheers.
Title: Re: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 17, 2019, 05:05:07 PM
Thank you, that will be great for backwards compatibility, in AutoCAD 2020 they did fix the issue with multiple instances (finally). Although I did just realize something major, the one routine that I am working on uses AcCoreConsole, so anything that relies on vl-load-com won't work.....grr.

But this will help when I have problem files to deal with.
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 05:09:05 PM
You’re most welcome. Do let me know if you find issue with it. Thanks + cheers.
Title: Re: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 17, 2019, 05:10:51 PM
You’re most welcome. Do let me know if you find issue with it. Thanks + cheers.
As I said, my only issue is it won't run in AcCoreConsole, otherwise it seems like it should do the trick, will need a bit of testing though.
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 05:11:31 PM
Could be eadily re-written using old school dxf type coding which will should work in AcCoreConsole.
Title: Re: Unloaded/Unresolved xRefs
Post by: cmwade77 on May 17, 2019, 05:16:28 PM
Could be eadily re-written using old school dxf type coding which will should work in AcCoreConsole.
Yup, just have to figure out how to invent another 10 hours in the day....lol
Title: Re: Unloaded/Unresolved xRefs
Post by: MP on May 17, 2019, 05:37:34 PM
Fugly but try this quick & dirty stab ...

Code: [Select]
(defun c:dump-xref-shat ( / cmdecho data flags name names f d e )
    (setq cmdecho (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (while (setq data (tblnext "block" (null data)))
        (and
            (eq 4 (logand 4 (setq flags (cdr (assoc 70 (setq data (entget (tblobjname "block" (cdr (assoc 2 data))))))))))
            (zerop (logand 32 flags))
            (wcmatch (setq name (cdr (assoc 2 data))) "~*|*")
            (progn
                (foreach p (entget (cdr (assoc 330 data)))
                    (if (eq 331 (car p))
                        (progn
                            (entmod
                                (subst
                                    (cons
                                        70
                                        (logand
                                            (~ 4)
                                            (cdr
                                                (setq f
                                                    (assoc
                                                        70
                                                        (setq d
                                                            (entget
                                                                (tblobjname
                                                                    "layer"
                                                                    (cdr
                                                                        (assoc 8
                                                                            (entget
                                                                                (setq e (cdr p))
                                                                            )
                                                                        )
                                                                    )
                                                                )
                                                            )
                                                        )
                                                    )
                                                )       
                                            )
                                        )
                                    )   
                                    f
                                    d
                                )
                            )
                            (entdel e)
                        )
                    )
                )
                (setq names (cons name names))
            )
        )           
    )
    (foreach name names (command ".xref" "_detach" name))
    (setvar 'cmdecho cmdecho)
    (princ)
)

Cheers.