06/16/2012

Adding an xref to a drawing (and converting attachments to overlays)

Its easy to add an xref to a drawing, but it was only in AutoCAD 2012 that we exposed a function to convert an xref attachment to an overlay. The following VB.NET code snippet shows two functions. The first inserts an xref, the second iterates through all BlockTableRecords in the drawing converting all attachment xrefs to overlays.

<CommandMethod("addAnXref")> _

PublicSub addAnXref()

Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Using trans AsTransaction = db.TransactionManager.StartTransaction()

' Attach the Xref

Dim xrefObj AsObjectId =

db.AttachXref("C:\temp\TestXef.dwg", "test2")

Dim br AsNewBlockReference(NewPoint3d(0, 0, 0), xrefObj)

' Add the xref to model space

Dim bt AsBlockTable =

trans.GetObject(db.BlockTableId, OpenMode.ForRead)

Dim modelSpace AsBlockTableRecord =

trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

modelSpace.AppendEntity(br)

trans.AddNewlyCreatedDBObject(br, True)

trans.Commit()

EndUsing

EndSub

<CommandMethod("changeToOverlay")> _

PublicSub changeToOverlay()

Dim doc AsDocument =

Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Using trans AsTransaction =

db.TransactionManager.StartTransaction()

Dim bt AsBlockTable =

trans.GetObject(db.BlockTableId, OpenMode.ForRead)

ForEach btrId AsObjectIdIn bt

Dim btr AsBlockTableRecord =

trans.GetObject(btrId, OpenMode.ForRead)

If btr.IsFromExternalReference Then

btr.UpgradeOpen()

btr.IsFromOverlayReference = True

EndIf

Next

trans.Commit()

EndUsing

EndSub

Its also worth noting these comments in the ObjectARX SDK documentation (that aren’t reproduced in the documentation for the .NET class:

This function will set this AcDbBlockTableRecord to be an overlay if bIsOverlay is true; otherwise, it will set it to be in insert type xref. This just sets the BlockTableRecord's overlay flag.

This function will only work on a AcDbBlockTableRecord that is an xref, and that is NOT a nested xref.

In order to accurately determine if this BlockTableRecord represents a nested xref, this BlockTableRecord must have been closed since the last BlockReference that references it was created. This is necessary so that the BlockTableRecord's list of referencing BlockReferences is up to date.

In fact, its always a good idea to review the (C++) ObjectARX documentation when reading about a new function. The C++ information will often give you some useful hints for your .NET code.

Comments

Its easy to add an xref to a drawing, but it was only in AutoCAD 2012 that we exposed a function to convert an xref attachment to an overlay. The following VB.NET code snippet shows two functions. The first inserts an xref, the second iterates through all BlockTableRecords in the drawing converting all attachment xrefs to overlays.

<CommandMethod("addAnXref")> _

PublicSub addAnXref()

Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Using trans AsTransaction = db.TransactionManager.StartTransaction()

' Attach the Xref

Dim xrefObj AsObjectId =

db.AttachXref("C:\temp\TestXef.dwg", "test2")

Dim br AsNewBlockReference(NewPoint3d(0, 0, 0), xrefObj)

' Add the xref to model space

Dim bt AsBlockTable =

trans.GetObject(db.BlockTableId, OpenMode.ForRead)

Dim modelSpace AsBlockTableRecord =

trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

modelSpace.AppendEntity(br)

trans.AddNewlyCreatedDBObject(br, True)

trans.Commit()

EndUsing

EndSub

<CommandMethod("changeToOverlay")> _

PublicSub changeToOverlay()

Dim doc AsDocument =

Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Using trans AsTransaction =

db.TransactionManager.StartTransaction()

Dim bt AsBlockTable =

trans.GetObject(db.BlockTableId, OpenMode.ForRead)

ForEach btrId AsObjectIdIn bt

Dim btr AsBlockTableRecord =

trans.GetObject(btrId, OpenMode.ForRead)

If btr.IsFromExternalReference Then

btr.UpgradeOpen()

btr.IsFromOverlayReference = True

EndIf

Next

trans.Commit()

EndUsing

EndSub

Its also worth noting these comments in the ObjectARX SDK documentation (that aren’t reproduced in the documentation for the .NET class:

This function will set this AcDbBlockTableRecord to be an overlay if bIsOverlay is true; otherwise, it will set it to be in insert type xref. This just sets the BlockTableRecord's overlay flag.

This function will only work on a AcDbBlockTableRecord that is an xref, and that is NOT a nested xref.

In order to accurately determine if this BlockTableRecord represents a nested xref, this BlockTableRecord must have been closed since the last BlockReference that references it was created. This is necessary so that the BlockTableRecord's list of referencing BlockReferences is up to date.

In fact, its always a good idea to review the (C++) ObjectARX documentation when reading about a new function. The C++ information will often give you some useful hints for your .NET code.