PDA

View Full Version : Copying all objects from Xref to Drawings



jasonyctam
2015-01-13, 08:11 PM
Hi All,

As a newbie to AUTOLisp, I am trying to accomplish the following with a script/function, however I still haven't found much useful information

1. While looping through the files in the directory, check if each drawing has any xrefs attached.
2. If yes, then copy all objects from all attached xrefs to the current drawing, and detach them. If no, skip the the next drawing. (If there are xref objects that cannot be copied to the drawing (such as jpgs), then leave them as they are).
3. Save the drawing with the same file name in another folder.

I know this sounds like asking someone else to do the homework for me, but any kind of help is appreciated as a lot of time has been used already googling around without much progress.....
Cheers

Coloradomrg
2015-01-13, 10:46 PM
Sounds like a script is needed... I can't actually help with that part. Check out script pro though.

You don't need to copy and paste the xref, just bind it and then burst the resulting block.

peter
2015-01-15, 12:29 AM
IMHO the first thing to do is bind the xref's (convert them into blocks) in the block definition collection.

P=



;______________________________________________________________________________
;
; Function to bind selected xref's (as blocks) in a document
; Written By Peter Jamtgaard copyright 2015 all rights reserved
; Syntax: (xrefbind (vla-get-activedocument (vlax-get-acad-object)) "B*"); <-bind xrefs that start with B
;______________________________________________________________________________

(defun XrefBind (objDocument strWCBlockName /
lstBlockDefinitions
objBlockDefinition)
(vlax-for objBlockDefinition (vla-get-blocks objDocument)
(and
(= (vla-get-isxref objBlockDefinition) :vlax-true)
(wcmatch (strcase (vla-get-name objBlockDefinition))
(strcase strWCBlockName)
)
(setq lstBlockDefinitions (cons objBlockDefinition lstBlockDefinitions))
)
)
(and
lstBlockDefinitions
(apply 'and (mapcar '(lambda (objBlockDefinition)(vla-bind objBlockDefinition 1))
lstBlockDefinitions
)
)
)
)

; Function to bind all xref's in a drawing as blocks.

(defun C:XrefBindAll ()
(xrefbind (vla-get-activedocument
(vlax-get-acad-object))
"*")
)

(vl-load-com)

peter
2015-01-15, 02:53 AM
The next step would be to explode all of the instances of those blocks in the document (including nested ones) so here is a
general solution for exploding blocks.

I would include a function to unlock layers before running it because that could cause this function to not explode items on locked
layers.

I included two methods of exploding blocks, one with a string wildcard and the other with a list of blocknames.

Shall I go on?

(This version was revised from my original post.)

P=



;___________________________________________________________________________________________________________
;
; Written By: Peter Jamtgaard copyright 2015 All Rights Reserved
;
; Function to explode all instances of specified blocks in a document
;___________________________________________________________________________________________________________
;
; Function List
;___________________________________________________________________________________________________________
;
; C:BlocksExplodeAll Explodes all blocks in a drawing (and in block definitions)
; BlocksExplode Encapsulation of two types of specification arguments (see below)
; BlocksExplodeByWildCard Explodes specified blocks in a document by wildcard string filter
; BlocksExplodeByList Explodes specified blocks in a document by list of effectivenames
; Errortrap General function to trap lisp errors.
;___________________________________________________________________________________________________________

(defun C:BlocksExplodeAll ()
(while (blocksexplode (vla-get-activedocument (vlax-get-acad-object)) "*"));<- Multiple iterations may be necessary
)

;___________________________________________________________________________________________________________

(defun BlocksExplode (objDocument Value )
(or
(and
(= (type Value) 'STR)
(BlocksExplodeByWildCard objDocument Value)
)
(and
(= (type Value) 'LIST)
(BlocksExplodeByLIST objDocument Value)
)
)
)

;___________________________________________________________________________________________________________
;
; Function to explode all blocks (including nested blocks) in a document based on wildcard string
;___________________________________________________________________________________________________________

(defun BlocksExplodeByWildCard (objDocument strWCBlockName / lstBlockObjects objBlockDefinition objItem)
(vlax-for objBlockDefinition (vla-get-blocks objDocument)
(vlax-for objItem objBlockDefinition
(and
(wcmatch (vla-get-objectname objItem) "AcDbBlockReference,AcDbMInsertBlock")
(wcmatch (strcase (vla-get-effectivename objItem)) (strcase strWCBlockName))
(setq lstBlockObjects (cons objItem lstBlockObjects))
)
)
)
(and
lstBlockObjects
(apply 'and (mapcar 'objectexplode lstBlockObjects))
)
)

;___________________________________________________________________________________________________________
;
; Function to explode a block object (and delete original block)
;___________________________________________________________________________________________________________


(defun ObjectExplode (objBlock)
(and
(errortrap (quote (vla-explode objBlock) ))
(errortrap (quote (vla-delete objBlock) ))
)
)

;___________________________________________________________________________________________________________
;
; Function to explode all blocks (including nested blocks) in a document based on a list of effective names
;___________________________________________________________________________________________________________

(defun BlocksExplodeByList (objDocument lstEffectiveBlockNames / lstBlockObjects strBlockName)
(apply 'or
(mapcar '(lambda (strBlockName)(BlockExplodeByWildcard objDocument strBlockName))
lstEffectiveBlockNames
)
)
)

;___________________________________________________________________________________________________________
;
; Standardized Error Trap
;___________________________________________________________________________________________________________

(defun ErrorTrap (symFunction / objError result)
(if (vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X123)(set X123 (eval symFunction)))
(list 'result))))
nil
(if result result 'T)
)
)

(vl-load-com)

jasonyctam
2015-01-18, 06:24 PM
Hi Peter,

Yes please go on, your help is very helpful and seems to be exactly what I need.

Thanks in advance,
Jason

peter
2015-01-19, 06:44 AM
These functions can do what you want with a script file.

If you get more complicated you could create a dbx module to iterate through documents and bind and explode the xrefs.

You could also do that with all open documents using the documents collection.

Peter