Active Contributor
Posts: 45
Registered: ‎04-20-2012
Message 1 of 4 (508 Views)
Accepted Solution

Add Xref to newly created dwg

508 Views, 3 Replies
05-18-2012 04:35 AM

I am having an issue with trying to get and xref or multiple xrefs attached/overlayed in a drawing that I just create.


I have a command that will create a sheet set, then add sheets in that sheet set based on a number the user puts in.


I then ask the user if they want to add xrefs to the drawings, if they press yes, I want to have the dwgs that are selected by the user to be referenced into the newly created dwgs.


I believe I am mising making the dwg that is created the active dwg before adding the xrefs, but I may also be missing locking the dwg.  At the moment I have it set to open the dwg after it is created then attemping to add the xrefs to it.


any suggestions?


'Starts the process of adding sheets based on the number specified
            For i As Integer = 1 To info.TotalSheetNum.Value
                'shows dialog box for sheet information to user
                'adds the sheet to the sheet set
                AddSheet(ssdb, sheetinfo.SheetName.Text, sheetinfo.SheetDesc.Text, sheetinfo.SheetNum.Text, sheetinfo.SheetName.Text)
                'opens the newly created dwg
                doc.Open(ssf & sheetinfo.SheetName.Text & ".dwg", False)
                'this is where I need to add the xref and then save and close the dwg that I opened
                'it would be nice if I could have the xrefs added without opening the new dwg
                doc.MdiActiveDocument.Database.OverlayXref(efd.Filename, efd.Filename)
                doc.MdiActiveDocument.Database.OverlayXref(pfd.Filename, pfd.Filename)
                doc.MdiActiveDocument.CloseAndSave(ssf & sheetinfo.SheetName.Text & ".dwg")


Valued Mentor
Posts: 331
Registered: ‎05-11-2006
Message 2 of 4 (495 Views)

Re: Add Xref to newly created dwg

05-18-2012 07:24 AM in reply to: bkenyon13

You can open the database in the background and overlay an xref into it that way. See the attached C# code sample.

Calling 'Database.OverlayXref()' doesn't physically add a block reference of the overlay to your drawing you still have to insert a block reference of the returned BlockTableRecord id from 'OverlayXref()', the code sample shows one way of going about that.

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 4 (486 Views)

Re: Add Xref to newly created dwg

05-18-2012 07:47 AM in reply to: bkenyon13

Here is quick sample how to do it

You have to create List of string from your controls,

then use this code, sorry this one is on C#, but you can easily to

convert it on VB.NET, almost not tested


        [CommandMethod("Xrefs", CommandFlags.Session)]
        public void TestForXrefs()
            bool result = true;
         List<string> docs= new List<string>{  @"C:\Test\1.dwg",@"C:\Test\2.dwg",@"C:\Test\3.dwg"};
         List<string> xrefs= new List<string>{  @"C:\UsedFiles\abc1.dwg",@"C:\UsedFiles\abc2.dwg"};
             OverlayXrefs(docs, xrefs);
         catch (System.Exception ex)
                 ex.Message + "\n" + ex.StackTrace);
                              "\nProgram ended up with result of " + result.ToString());
        // See also: http://www.theswamp.org/index.php?topic=24524.msg296051#msg296051
        static public void OverlayXrefs(List<string> currdocs, List<string> xrefdocs)
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock doclock = doc.LockDocument())

                foreach (string filename in currdocs)
                    // To add xrefs:
                    //Database db = new Database(true, false);

                    //   db.ReadDwgFile(filename, System.IO.FileShare.ReadWrite,false, "");
                    //   using (Transaction tr = db.TransactionManager.StartTransaction())
                    //   {
                    //           BlockTable xrefBt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    //       BlockTableRecord btrMs = xrefBt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
                    //       foreach (string xref in xrefdocs)
                    //       {
                    //           string blockName = Path.GetFileNameWithoutExtension(xref);
                    //           ObjectId xrefObjId = db.AttachXref(xref, blockName);

                    //           BlockReference bref = new BlockReference(Point3d.Origin, xrefObjId);

                    //           btrMs.AppendEntity(bref);
                    //           tr.AddNewlyCreatedDBObject(bref, true);
                    //       }

                    //       db.SaveAs(filename, true, DwgVersion.Current, db.SecurityParameters);
                    //       db.CloseInput(true);
                    //       tr.Commit();
                    //   }
                    //To overlay xrefs:
                    Database db = new Database(true, false);

                    db.ReadDwgFile(filename, System.IO.FileShare.ReadWrite, false, "");

                    using (Transaction tr = db.TransactionManager.StartTransaction())

                        foreach (string xref in xrefdocs)
                            string blockName = Path.GetFileNameWithoutExtension(xref);
                            db.OverlayXref(xref, blockName);


                        db.SaveAs(filename, true, DwgVersion.Current, db.SecurityParameters);






Active Contributor
Posts: 45
Registered: ‎04-20-2012
Message 4 of 4 (468 Views)

Re: Add Xref to newly created dwg

05-18-2012 02:05 PM in reply to: cadMeUp

Thanks for both of your help, with the information provided I was able to get it working.


Here is my resulting code:

db.ReadDwgFile(ssf & sheetinfo.SheetName.Text & ".dwg", FileOpenMode.OpenForReadAndAllShare, False, "")
                Dim xrefid As ObjectId = db.OverlayXref(efd.Filename, efd.Filename)
                Dim xrefid2 As ObjectId = db.OverlayXref(pfd.Filename, pfd.Filename)
                Using trans As Transaction = db.TransactionManager.StartTransaction()
                    Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
                    Dim btr As BlockTableRecord = TryCast(bt(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite), BlockTableRecord)
                    Dim bref As New BlockReference(Point3d.Origin, xrefid)
                    Dim bref2 As New BlockReference(Point3d.Origin, xrefid2)
                    trans.AddNewlyCreatedDBObject(bref, True)
                    trans.AddNewlyCreatedDBObject(bref2, True)
                End Using
                db.SaveAs(ssf & sheetinfo.SheetName.Text & ".dwg", DwgVersion.Current)


Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.