I was just checking recent comments on the blog (as part of my ongoing war on spammers ), and noticed a new comment by Gautham on an old post of mine which reminded me I’d forgotten to respond to the original question he asked about that post.
To change the path of an xref from absolute to relative, you simply have to edit the BlockTableRecord.PathName to a string that defines the relative path you need (or to have no path at all, if you want to rely on FindFile to locate your Xref). Here’s some simple code written by Balaji in response to a similar question from an ADN partner:
[CommandMethod("ChangeXRefPath")]
public void ChangeXRefPathMethod()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ObjectIdCollection collection = new ObjectIdCollection();
using (Database db = new Database(false, true))
{
db.ReadDwgFile("c:\\Temp\\Test.dwg",
FileOpenMode.OpenForReadAndWriteNoShare, false, "");
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
as BlockTable;
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead)
as BlockTableRecord;
if (btr.IsFromExternalReference)
{
btr.UpgradeOpen();
String oldPath = btr.PathName;
String newPath = oldPath.Replace(@"C:\Temp", ".");
btr.PathName = newPath;
collection.Add(btrId);
ed.WriteMessage(String.Format("{0}Old Path : {1} New Path : {2}",
Environment.NewLine, oldPath, newPath));
}
}
tr.Commit();
}
if (collection.Count > 0)
{
//Perform the reload operation
db.ReloadXrefs(collection);
}
db.SaveAs(db.OriginalFileName, true, db.OriginalFileVersion,
db.SecurityParameters);
}
}
The code is quite simple in that it assumes a drawing is located in c:\temp, and that the xrefs are in a subfolder of that. Its also loading the drawing being processed as a side database, which means that (in this example) the drawing mustn’t be open in the AutoCAD editor when you run the command.
I’ll leave it as an exercise to the reader to edit it to be more generic. I don’t have time for that just now because I’m off home to spend some quality time with the latest addition to my family. He’s the one on the left -
