The following code is desigend to copy selected objects from an xref to the active file using the Xref Database object as the source. The copied objects are placed on the the active layer.
It works as expected in both 2008 and 2010, but when undo is called after the routine ends, 2010 reports a fatal crash with a "wrong database" error, which does not occur in 2008.
(defun c:WrongDatabase ( / *error* doc blocks layers mspace
curlay copyobj GetXref copylst)
(vl-load-com)
(defun *error* (msg)
(cond
((not msg))
((wcmatch (strcase msg) "*QUIT*,*CANCEL*"))
(T (princ (strcat "\nError: " msg)))
)
(princ)
) ;end error
;;; START SUB-FUNCTION ;;;
;; Argument: a list of block enames as returned by (last nentsel or nentselp).
;; Returns the fisrt vla-object block which is an xref.
(defun GetXref (lst / o blkdef blkdeflst)
(foreach x lst
(setq o (vlax-ename->vla-object x))
(setq blkdef (vla-item blocks (vlax-get o 'Name)))
(setq blkdeflst (cons blkdef blkdeflst))
)
(setq blkdeflst (reverse blkdeflst))
(while (eq acFalse (vlax-get (car blkdeflst) 'IsXref))
(setq blkdeflst (cdr blkdeflst))
)
(car blkdeflst)
) ;end
;;; END SUB-FUNCTION ;;;
;;; START MAIN FUNCTION ;;;
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
blocks (vla-get-Blocks doc)
layers (vla-get-Layers doc)
mspace (vla-get-ModelSpace doc)
curlay (getvar "clayer")
)
(while
(and
(setq elst (nentselp "\nSelect object nested in an xref: "))
(= 4 (length elst))
(setq obj (vlax-ename->vla-object (car elst)))
(setq xref (GetXref (last elst)))
(setq xdb (vlax-get xref 'XRefDatabase))
) ; and
(setq copyobj (car (vlax-invoke xdb 'CopyObjects (list obj) mspace)))
(vla-update copyobj)
(vla-transformby copyobj (vlax-tmatrix (caddr elst)))
;; the following line is what's causing the undo crash in 2010
(vlax-put copyobj 'Layer curlay)
) ; while
(*error* nil)
) ;end WrongDatabase
;shortcut
(defun c:WDB () (c:WrongDatabase))