PDA

View Full Version : XrefAttach & XrefDetach - Please Help



n8wex
2009-09-17, 03:34 PM
Having trouble attaching and detaching

Attach will overlay but not show in the drawing unless forced via the command dialog

Wont detach as there is multiple references...

any advise appreciated




Public Sub XrefAttach(ByVal Xrefname As String, ByVal XrefFile As String)

Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database = Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction

Dim tbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

If Not tbl.Has(Xrefname) = True Then

Dim objID As ObjectId = db.OverlayXref(XrefFile, Xrefname)

Dim tBlock As BlockTableRecord = CType(objID.GetObject(OpenMode.ForWrite), BlockTableRecord)
Dim newRef As BlockReference = New BlockReference(New Point3d, tBlock.ObjectId)
tBlock.AppendEntity(newRef)
tr.AddNewlyCreatedDBObject(newRef, True)

End If

tr.Commit()
End Using
End Using
End Using
End Sub

Public Sub XrefDetach(ByVal Xrefname As String)
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database = Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction

Dim tbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

If tbl.Has(Xrefname) = True Then
Dim rec As BlockTableRecord = tr.GetObject(tbl.Item(Xrefname), OpenMode.ForWrite)

db.DetachXref(tbl.Item(Xrefname))
rec.Erase(True)

End If

tr.Commit()
End Using
End Using
End Using
End Sub

n8wex
2009-09-18, 08:49 AM
Anyone? You'd think this would be simple...?

n8wex
2009-09-20, 03:13 AM
I worked it out




Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database = Application.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction

Dim tbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = tr.GetObject(tbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

If Not tbl.Has(Xrefname) = True Then

Dim objID As ObjectId = db.OverlayXref(XrefFile, Xrefname)


Dim tBlock As BlockTableRecord = CType(objID.GetObject(OpenMode.ForWrite), BlockTableRecord)
Dim XRef As BlockReference = New BlockReference(New Point3d, tBlock.ObjectId)

acBlkTblRec.AppendEntity(XRef)
tr.AddNewlyCreatedDBObject(XRef, True)


End If

tr.Commit()
ActiveDoc.Editor.Regen()
End Using
End Using
End Using

james.231335
2009-10-09, 07:22 PM
Try this tutoiral link and see if that helps understand the Xref concept. It's pretty plain and simple, http://www.2learncad.com/tutorials/xref.html