TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CADaver on August 22, 2006, 06:19:46 PM

Title: XREF-reload, unload, detach
Post by: CADaver on August 22, 2006, 06:19:46 PM
I have this old code I've been using for years and today one of our new guys wanted to know if there was a more elegant option for these functions.  My first answer was "Surely", when he then asked "What are they?", I said "I dunno". So I thought I just ask you guys.  Each one functions on the selected XREF's to either reload, unload or detach (no error check) tear 'em up, guys:

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:xre ()
(prompt "\N Select Element in XREF to RELOAD  ")
(xref-loader "reload")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:xru ()
(prompt "\N Select Element in XREF to UNLOAD  ")
(xref-loader "unload")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:xrd ()
(prompt "\N Select Element in XREF to DETACH  ")
(xref-loader "detach")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun xref-loader ( xref-action / old setlgth co t2 temp blkname)
(command ".undo" "BEGIN")
   (setvar "cmdecho" 0)
      (setq
        old (ssget)
        setlgth (sslength old)
        co -1
        t2 "t"
      )
  (while (boundp 't2)
     (progn
       (setq
         co (1+ co)
         temp (entget (ssname old co))
         blkname (cdr (assoc 2 temp))
         t2 (ssname old (1+ co))
       )
         (command "-xref" xref-action blkname)
       )      ;; end progn
   )          ;; end while
(setq co (1+ co))
(princ)
(command ".undo" "END")
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Title: Re: XREF-reload, unload, detach
Post by: T.Willey on August 22, 2006, 06:21:56 PM
What is your idea of more elegant?
Title: Re: XREF-reload, unload, detach
Post by: nivuahc on August 22, 2006, 06:51:40 PM
What is your idea of more elegant?

Seriously.

I would say it's elegant in its simplicity.
Title: Re: XREF-reload, unload, detach
Post by: Jeff_M on August 22, 2006, 06:57:38 PM
If you wanted to get it to eliminate the echos to the command line then converting it to use ActiveX is quite simple:

Code: [Select]
;;add near beginning of xref-loader code
(setq doc (vla-get-activedocument
    (vlax-get-acad-object)))

;(command ".undo" "BEGIN")...change to:
(vla-startundomark doc)

;(command "-xref" xref-action blkname)...change to:
(vlax-invoke
  (vla-item
    (vla-get-blocks
      doc)
    blkname)
  xref-action)

;(command ".undo" "END")...change to:
(vla-startundomark doc)

But consider what Chuck said......
Title: Re: XREF-reload, unload, detach
Post by: T.Willey on August 22, 2006, 06:59:25 PM
The only thing I could think of is the use of command.  Take that out, but you don't really gain anything, since you are only using it in the current drawing.

What is your idea of more elegant?

Seriously.

I would say it's elegant in its simplicity.

Second.

Guess Jeff thought the same before I could post.
Title: Re: XREF-reload, unload, detach
Post by: CADaver on August 22, 2006, 10:19:19 PM
I guess I was looking for something along the lines of Jeff's response with the activeX conversion. (Thanks Jeff)  And the reason it's simple is that 'simple" is all I can do.
Title: Re: XREF-reload, unload, detach
Post by: LE on August 22, 2006, 10:33:01 PM
I do not see any control of knowing if the selected item is an xref, or I missing something?
Title: Re: XREF-reload, unload, detach
Post by: CADaver on August 23, 2006, 06:01:30 AM
I do not see any control of knowing if the selected item is an xref, or I missing something?
THere's not, there's very little control of anything.  It'll choke if you get a block in the selection set.
Title: Re: XREF-reload, unload, detach
Post by: Kerry on August 23, 2006, 06:07:24 AM
Nothing to do with elegance, but this ;
(prompt "\N Select Element in XREF to RELOAD  ")

will work as anticipated like this ;
(prompt "\n Select Element in XREF to RELOAD  ")

{ie: lowercase \n }
Title: Re: XREF-reload, unload, detach
Post by: Joe Burke on August 23, 2006, 08:55:37 AM
If you wanted to get it to eliminate the echos to the command line then converting it to use ActiveX is quite simple:

Code: [Select]
;;add near beginning of xref-loader code
(setq doc (vla-get-activedocument
    (vlax-get-acad-object)))

;(command ".undo" "BEGIN")...change to:
(vla-startundomark doc)

;(command "-xref" xref-action blkname)...change to:
(vlax-invoke
  (vla-item
    (vla-get-blocks
      doc)
    blkname)
  xref-action)

;(command ".undo" "END")...change to:
(vla-startundomark doc)

But consider what Chuck said......

Hi Jeff,

I believe there's an additional advantage here. It would allow pre-selection of the xref. Whereas (command ".undo" "BEGIN") would deselect any pre-selection.
Title: Re: XREF-reload, unload, detach
Post by: Jeff_M on August 23, 2006, 11:05:28 AM
I can't believe that no one caught this: 
Code: [Select]
;(command ".undo" "END")...change to:
(vla-startundomark doc)
:oops: should be
Code: [Select]
(vla-endundomark doc)
Joe, that's an excellent point!


Title: Re: XREF-reload, unload, detach
Post by: CADaver on August 23, 2006, 01:00:52 PM
I believe there's an additional advantage here. It would allow pre-selection of the xref. Whereas (command ".undo" "BEGIN") would deselect any pre-selection.
ooooo, excellent point.  We have some new guys that use pickfirst the "other" way, while us older hacks are still doing it the old way.  Thanks.
Title: Re: XREF-reload, unload, detach
Post by: Krushert on August 23, 2006, 01:05:15 PM
I have this old code I've been using for years and today one of our new guys wanted to know if there was a more elegant option for these functions.  My first answer was "Surely", when he then asked "What are they?", I said "I dunno". So I thought I just ask you guys.  Each one functions on the selected XREF's to either reload, unload or detach (no error check) tear 'em up, guys:

hello folks
Recently I barrowed <cough> a piece code from Daron that he posted a while back   REQ: XREF Reload/Open (http://www.theswamp.org/index.php?topic=23.0) ....
dam it wrong button I wasn't done yet
....which a basically copied and added the unload part in place of the reload.  I get a nasty hick up when I select a non-xref.  To me it is a minor nu sinace, so I live with it.  I would say, have the routine filter out non-xrefs so that it keeps going or be intelligent enough to select the xref that is underneath a piece of geometry.  (Yep using draworder over here).

Daron thanks for your routine. I have been using the hell out of it.   :-D
Title: Re: XREF-reload, unload, detach
Post by: CADaver on August 23, 2006, 01:25:47 PM
Recently I barrowed <cough> a piece code from Daron that he posted a while back   REQ: XREF Reload/Open (http://www.theswamp.org/index.php?topic=23.0) ....
hmmm... I don't think I stole the idea from Daron, but I've been known to pinch a bit of code now and then, and if I did, sorry...
... well not really, ... but you know what I mean.... or not.... 
...how about this; If I did, Thanks Daron.
Title: Re: XREF-reload, unload, detach
Post by: Krushert on August 23, 2006, 01:39:45 PM
Recently I barrowed <cough> a piece code from Daron that he posted a while back   REQ: XREF Reload/Open (http://www.theswamp.org/index.php?topic=23.0) ....
hmmm... I don't think I stole the idea from Daron, but I've been known to pinch a bit of code now and then, and if I did, sorry...
... well not really, ... but you know what I mean.... or not.... 
...how about this; If I did, Thanks Daron.
If you pinch a code does it yell.  sorry bad pun.   :-D

CADaver, sorry  I didnt mean to imply that you stole barrow (or whatver) I did and I did thank him.  <I got the proof>   :-)
Title: Re: XREF-reload, unload, detach
Post by: T.Willey on August 23, 2006, 01:46:20 PM
Okay, here you go.  Now you can only select blocks, and it will make sure that it is an xref before preforming the opperation to it. Use with the other portions or Randy's code.
Code: [Select]
(defun Xref-Loader (XrefOpp / ActDoc BlkCol ss Ent BlkObj)

(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(vla-StartUndoMark ActDoc)
(setq BlkCol (vla-get-Blocks ActDoc))
(if (setq ss (ssget '((0 . "INSERT"))))
 (foreach Lst (ssnamex ss)
  (if
   (and
    (= (type (setq Ent (cadr Lst))) 'ENAME)
    (= (vla-get-IsXref (setq BlkObj (vla-Item BlkCol (cdr (assoc 2 (entget Ent)))))) :vlax-true)
   )
   (vlax-invoke BlkObj XrefOpp)
  )
 )
)
(vla-EndUndoMark ActDoc)
(princ)
)
Title: Re: XREF-reload, unload, detach
Post by: nivuahc on August 23, 2006, 01:52:21 PM
Something different but similar, courtesy of MP:

Code: [Select]
(defun GetXrefPath ( ename / object )
    (cond
        (   (and
                (eq 'ename (type ename))
                (eq "AcDbBlockReference"
                    (vla-get-objectname
                        (setq object
                            (vlax-ename->vla-object
                                ename
                            )
                        )
                    )   
                )
                (vlax-property-available-p object 'path)
            )
            (vla-get-path object)
        )   
    )
)


(defun c:XOpen ( / path )
    (if (setq path (GetXrefPath (car (entsel))))
        (if (zerop (getvar "sdi"))
            (vla-open
                (vla-get-documents (vlax-get-acad-object))
                path
            )
            (vla-sendcommand
                (vla-get-activedocument (vlax-get-acad-object))
                (strcat ".open " path "\n")
            )
        )
    )   
    (princ)   
)
Title: Re: XREF-reload, unload, detach
Post by: CADaver on August 23, 2006, 03:26:21 PM
CADaver, sorry  I didnt mean to imply that you stole barrow (or whatver) I did and I did thank him.  <I got the proof>   :-)
Oh, I didn't think you did, it's just that the codes are sorta similar, and I have been known to ...ummm... borrow a line or two. :angel: