Bind Xrefs (.NET)

Binding an xref to a drawing using the Bind method makes the xref a permanent part of the drawing and no longer an externally referenced file. The externally referenced information becomes a block. When the externally referenced drawing is updated, the bound xref is not updated. This process binds the entire drawing's database, including all of its dependent symbols.

Dependent symbols are named objects such as blocks, dimension styles, layers, linetypes, and text styles. Binding the xref allows named objects from the xref to be used in the current drawing.

The BindXref method requires two parameters: xrefIds (collection of ObjectIDs) and insertBind (boolean). If the insertBind parameter is set to True, the symbol names of the xref drawing are prefixed in the current drawing with <blockname>$x$, where x is an integer that is automatically incremented to avoid overriding existing block definitions. If the insertBind parameter is set to False, the symbol names of the xref drawing are merged into the current drawing without the prefix. If duplicate names exist, AutoCAD uses the symbols already defined in the local drawing. If you are unsure whether your drawing contains duplicate symbol names, it is recommended that you set insertBind to True.

For more information on binding xrefs, see “About Archiving Drawings with Xrefs (Binding)” in the product Help system.

Bind an xref definition

This example attaches an external reference and then binds the external -reference to the drawing. This example uses the Exterior Elevations.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.

C# Example

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("BindingExternalReference")]
public void BindingExternalReference()
{
    // Get the current database and start a transaction
    Database acCurDb;
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Create a reference to a DWG file
        string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
        ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

        // If a valid reference is created then continue
        if (!acXrefId.IsNull)
        {
            // Attach the DWG reference to the current space
            Point3d insPt = new Point3d(1, 1, 0);
            using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
            {
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
            }

            Application.ShowAlertDialog("The external reference is attached.");

            using (ObjectIdCollection acXrefIdCol = new ObjectIdCollection())
            {
                acXrefIdCol.Add(acXrefId);

                acCurDb.BindXrefs(acXrefIdCol, false);
            }

            Application.ShowAlertDialog("The external reference is bound.");
        }

        // Save the new objects to the database
        acTrans.Commit();

        // Dispose of the transaction
    }
}