PDA

View Full Version : Change Xref Paths to Relative (Requesting the mastermind of TonyT)



joelkarr
2009-02-02, 05:05 PM
I run into the same problem I had when trying to bind xrefs. I think I must be missing some sort of open Xref for edit or something. Any help would be greatly appreciated. It seems this should be a simple thing to do. The code runs to completion but xref paths are not changed.

Thanks to all that help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acad = Autodesk.AutoCAD.ApplicationServices.Application;


namespace BindXref
{
public class Class1
{
[CommandMethod("RELXREF")]
public void RelXref()
{

Document doc = acad.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = doc.TransactionManager.StartTransaction();


try
{


using (tr)
{
ObjectIdCollection btrCol = new ObjectIdCollection();
XrefGraph xrgraph = doc.Database.GetHostDwgXrefGraph(false);
// look at all Nodes in the XrefGraph. Skip 0 node since it is the drawing itself.
for (int i = 1; i < (xrgraph.NumNodes - 1); i++)
{
XrefGraphNode xrNode = xrgraph.GetXrefNode(i);
if (!xrNode.IsNested)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject
(xrNode.BlockTableRecordId, OpenMode.ForWrite);
string origPath = btr.PathName;

string relativePath = AbstoRel(origPath);
ed.WriteMessage("\n Xref: " + btr.Name + " has path " + origPath + " or relative path " + relativePath);
db.XrefEditEnabled = true;
btr.PathName = relativePath;
}
}
}
}
catch
{
}
}
private string AbstoRel(string absolutePath)
{
string[] absoluteDirectories = absolutePath.Split('\\');
string relativePath = @"..\";
for (int i = absoluteDirectories.Length-2; i < absoluteDirectories.Length ; i++)
{
relativePath = string.Concat(relativePath, absoluteDirectories[i]);
if (i < absoluteDirectories.Length - 1)
{
relativePath = string.Concat(relativePath, "\\");

}

}


return relativePath;
}
}
}

T.Willey
2009-02-02, 06:06 PM
Doesn't look like you are committing your changes.

tr.Commit(); ??

p.s. If you use the code tags, then your code will be more readable in the posts. [ code ] and [ /code ] ( no spaces ).

joelkarr
2009-02-02, 06:25 PM
you were right. I need it was a stupid mistake. Thanks a ton.

I was making the same mistake for binding as well.

The attached txt has the same error with missing tr.commit() but any other comments or suggestions on the code would be appreciated.

I attached a txt file of both my attempt at switching xref's from absolute paths to relative and binding xrefs

thanks for your help

T.Willey
2009-02-02, 07:05 PM
They look fine to me, but still don't see the ' tr.Commit() ', but I'm no master at C# either, so take it for what it's worth.

I don't see anything reason for you to use ' switch ' unless you are going to fill out the other cases with something beside ' break '.