.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add Xref to newly created dwg

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
bkenyon13
1475 Views, 3 Replies

Add Xref to newly created dwg

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
                sheetinfo.ShowDialog()
                '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")
            Next

 

3 REPLIES 3
Message 2 of 4
cadMeUp
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.

Message 3 of 4
Hallex
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"};
         try
         {
             OverlayXrefs(docs, xrefs);
         }
         catch (System.Exception ex)
         {
             Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                 ex.Message + "\n" + ex.StackTrace);
         }
         finally
         {
             Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                              "\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);

                        }

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

                    }
                    //-------------------------------------------------------------------//
                }

            }
        }

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 4
bkenyon13
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)
                    btr.AppendEntity(bref)
                    btr.AppendEntity(bref2)
                    trans.AddNewlyCreatedDBObject(bref, True)
                    trans.AddNewlyCreatedDBObject(bref2, True)
                    trans.Commit()
                End Using
                db.SaveAs(ssf & sheetinfo.SheetName.Text & ".dwg", DwgVersion.Current)
                db.CloseInput(True)
                db.Dispose()

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost