Attach Xrefs

Attaching an xref links one drawing (the reference file, or xref) to the current drawing. When a drawing references an xref, AutoCAD attaches only the xref definition to the drawing, unlike regular blocks, where the block definition and the contents of the block are stored with the current drawing. AutoCAD reads the reference drawing to determine what to display in the current drawing. If the reference file is missing or corrupt, its data is not displayed in the current drawing. Each time you open a drawing, AutoCAD loads all graphical and nongraphical (such as layers, linetypes, and text styles) objects from referenced files. If VISRETAIN is on, AutoCAD stores any updated xref-dependent layer information in the current drawing.

You can attach as many copies of an xref as you want, and each can have a different position, scale, and rotation. You can also control the dependent layers and linetype properties that are defined in the xref.

To attach an xref, use the AttachExternalReference method. This method requires you to input the path and file name of the drawing to be referenced, the name the xref is to use in the current drawing, the insertion point, the scale, and rotation information for the xref. The AttachExternalReference method returns the newly created ExternalReference object.

For more information on attaching xrefs, see “Attach External References” in the User's Guide.

Attach an external reference to a drawing

This example displays all the blocks in the current drawing before and after adding an external reference. This example uses the 3D House.dwg file found in the sample directory. If you do not have this image, or if it is located in a different directory, insert a valid path and file name for the PathName variable.

Sub Ch10_AttachingExternalReference()
	Dim InsertPoint(0 To 2) As Double
	Dim insertedBlock As AcadExternalReference
	Dim tempBlock As AcadBlock
	Dim msg As String, PathName As String

	' Define external reference to be inserted
	InsertPoint(0) = 1
	InsertPoint(1) = 1
	InsertPoint(2) = 0
	PathName = "C:/Program Files/AutoCAD 2008/sample/3D House.dwg"

	' Display current Block information for this drawing
	GoSub ListBlocks

	' Add the external reference to the drawing
	Set insertedBlock = ThisDrawing.ModelSpace. _
			AttachExternalReference(PathName, "XREF_IMAGE", _
			InsertPoint, 1, 1, 1, 0, False)

	' Display new Block information for this drawing
	GoSub ListBlocks
	Exit Sub
	msg = vbCrLf	' Reset message
	For Each tempBlock In ThisDrawing.Blocks
		msg = msg & tempBlock.Name & vbCrLf
	MsgBox "The current blocks in this drawing are: " & msg

	MsgBox Err.Description
End Sub