Detaching Xrefs

Detaching Xrefs

Littlerubarb
Advocate Advocate
1,350 Views
5 Replies
Message 1 of 6

Detaching Xrefs

Littlerubarb
Advocate
Advocate

Afternoon, It been a while and Im trying to force a detach in a load of files using a batch script. No errors are being thrown but the image references that are not found are still in the drawing. Its got to be something simple in the code below. Just need another set of eyes.

 

Try
For Each Me.filepath In ListBox1.Items
ProgressBar1.Maximum = fileprogress
Dim fi As New FileInfo(filepath)

Dim db As New Database(False, True)
Try
db.ReadDwgFile(filepath, System.IO.FileShare.ReadWrite, False, "")
t = db.TransactionManager.StartTransaction
Using t
Dim bt As BlockTable = CType(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
Dim id As ObjectId
For Each id In bt
Dim btr As BlockTableRecord = CType(id.GetObject(OpenMode.ForRead), BlockTableRecord)
If btr.XrefStatus = XrefStatus.NotAnXref And btr.XrefStatus = XrefStatus.FileNotFound Or btr.XrefStatus = XrefStatus.Unreferenced Then
db.DetachXref(id)
End If
Next
t.Commit()
End Using
Catch
MsgBox(Err.Description + "ouch")
Finally
db.SaveAs(filepath, DwgVersion.Current)
t.Dispose()
End Try

0 Likes
Accepted solutions (1)
1,351 Views
5 Replies
Replies (5)
Message 2 of 6

Virupaksha_aithal
Autodesk Support
Autodesk Support

Hi

 

Try calling “ResolveXrefs” on the database. See if forum post http://forums.autodesk.com/t5/net/net-c-unable-to-detaching-filenotfound-xref-without-opening-the/td... helps



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Message 3 of 6

brianchapmandesign
Collaborator
Collaborator
The reference manager might work?

"Very funny, Scotty. Now beam down my clothes.
0 Likes
Message 4 of 6

Littlerubarb
Advocate
Advocate

Ok little modification - It doesn't remove images, just dwg xrefs..

0 Likes
Message 5 of 6

Littlerubarb
Advocate
Advocate

Thanks- after reading this i realized it was only the imge refs not working...

0 Likes
Message 6 of 6

Littlerubarb
Advocate
Advocate
Accepted solution

Here's the solution

 

<CommandMethod("DetachImg")> _
Public Sub detach_img(mainDrawingFile As String)
Dim db As New Database(False, True)
db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, True, "")
Using db
Dim saveRequired As Boolean = False
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim imageDictId As ObjectId = RasterImageDef.GetImageDictionary(db)
If imageDictId.IsNull Then
Else
Dim imageDefinitions As New Dictionary(Of ObjectId, RasterImageDef)()
Dim imageDictionary As DBDictionary = tr.GetObject(RasterImageDef.GetImageDictionary(db), OpenMode.ForWrite)
For Each entry As DBDictionaryEntry In imageDictionary
Dim id As ObjectId = entry.Value, ObjectId
Dim rasterImageDef__1 As RasterImageDef = tr.GetObject(id, OpenMode.ForWrite)
If rasterImageDef__1.LocateActivePath = "" Then
rasterImageDef__1.Erase()
saveRequired = True
End If
Next
End If
tr.Commit()
tr.Dispose()
End Using
If saveRequired Then
db.SaveAs(mainDrawingFile, DwgVersion.Current)
End If
End Using
End Sub

0 Likes