Absolute Path to Relative Path
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I found an old post on devBlog about changing absolute paths to relative paths for XREFs. Seems straight forward but I keep getting a eFileAccessEr during the Reload Xref's portion. I will eventually turn this into a batch processor to run on all drawing in a folder but for now I'm just trying to get the core of it done.
As you will see I take the full xref drawing name and path from the ReadDwgFile drawing file. For Example: R:\Projects\2018\18001808A\Survey\dwg\ASBLT\18001808A-BaseFile.dwg (This is the xref in the ReadDwgFile)
Then in this example I want it to set the relative path to .\Survey\dwg\ASBLT\18001808A-BaseFile.dwg
So it will start at that top folder and look down for the xref file.
The program sets the new paths during debugging but when it gets to reloading them, that's where the error appears.
<Autodesk.AutoCAD.Runtime.CommandMethod("ChangeXRefPath")>
Public Sub ChangeXRefPathMethod()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim collection As ObjectIdCollection = New ObjectIdCollection()
Using db As Database = New Database(False, True)
db.ReadDwgFile("R:\Projects\2018\18001808A\Survey\dwg\ASBLT\18001808A-FENCE As Built.dwg", FileOpenMode.OpenForReadAndWriteNoShare, False, "")
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each btrId As ObjectId In bt
Dim btr As BlockTableRecord = TryCast(tr.GetObject(btrId, OpenMode.ForRead), BlockTableRecord)
If btr.IsFromExternalReference Then
btr.UpgradeOpen()
Dim oldPath As String = btr.PathName
Dim oldPathArray As String() = oldPath.Split("\"c)
Dim oldPathRoot As String = oldPathArray(0) & "\" & oldPathArray(1) & "\" & oldPathArray(2) & "\" & oldPathArray(3)
Dim newPath As String = oldPath.Replace(oldPathRoot, ".")
btr.PathName = newPath
collection.Add(btrId)
ed.WriteMessage(String.Format("{0}Old Path : {1} New Path : {2}", Environment.NewLine, oldPath, newPath))
End If
Next
tr.Commit()
End Using
If collection.Count > 0 Then
db.ReloadXrefs(collection) '<--- errors here
End If
db.SaveAs(db.OriginalFileName, True, db.OriginalFileVersion, db.SecurityParameters)
End Using
End Sub