Hello coders,
I am building a plugin which needs to change the xref path within a drawing without opening it.
here is my. please throw some light on where i went wrong..I am a newbiee..to .Net c#
=====================================================================
[CommandMethod("refremap")]
public void refremap() // This method can have any name
{
//Get the document
Document Doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Doc.Editor;
// Ask the user to select a folder
PromptResult res = ed.GetString("\nEnter the path of the folder: ");
if (res.Status == PromptStatus.OK)
{
string[] filepaths = Directory.GetFiles(res.StringResult, "*.dwg", SearchOption.AllDirectories);
int filecount = filepaths.Length;
ed.WriteMessage("\nScanning " + filecount + " files");
int finalxrefcount = 0;
int xrefedfile = 0;
for (int i = 0; i < filecount; i++)
{
ed.WriteMessage("\n File Name : " + filepaths[i]);
//create a database and try to load the file
Database db = new Database(false, true);
using (db)
{
try
{
db.ReadDwgFile(filepaths[i], System.IO.FileShare.Read, false, "");
}
catch (System.Exception)
{
ed.WriteMessage("\nUnable to read the drawingfile.");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ed.WriteMessage("\n--------Xrefs Details--------");
db.ResolveXrefs(true, false);
XrefGraph xg = db.GetHostDwgXrefGraph(true);
GraphNode root = xg.RootNode;
//XrefObjectId xobj = new XrefObjectId();
ObjectIdCollection objcoll = new ObjectIdCollection();
int xrefcount = xg.NumNodes - 1;
if (xrefcount == 0)
{
ed.WriteMessage("\nNo xrefs found in the drawing");
}
else
{
for (int r = 1; r < (xrefcount+1); r++)
{
XrefGraphNode child = xg.GetXrefNode(r);
if (child.XrefStatus == XrefStatus.FileNotFound)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);
db.XrefEditEnabled = true;
string originalpath = btr.PathName;
string childname = Path.GetFileName(originalpath);
string newpath = @"E:\acad_dev_testing22\test\" + childname;
btr.PathName = newpath;
ed.WriteMessage("\n xref old path: " + originalpath);
ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");
}
}
}
tr.Commit();
}
}
}
ed.WriteMessage("\nTotal number of files scanned: " + filecount);
ed.WriteMessage("\nTotal number of files that contains xref : " + xrefedfile);
ed.WriteMessage("\nTotal number of xrefs : " + finalxrefcount);
ed.WriteMessage("\nTotal number of xrefs attached and working : " + filecount);
ed.WriteMessage("\nTotal number of xrefs that are not working : " + filecount);
ed.WriteMessage("\n----------------------------------------\n");
ed.WriteMessage("\n----------------------------------------\n");
}
else
{
return;
}
}
We can fix the following code (in red):
using (db)
{
try
{
db.ReadDwgFile(filepaths[i], FileShare.Read, false, "");
}
catch (System.Exception)
{
ed.WriteMessage("\nUnable to read the drawingfile.");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ed.WriteMessage("\n--------Xrefs Details--------");
db.ResolveXrefs(true, false);
XrefGraph xg = db.GetHostDwgXrefGraph(true);
int xrefcount = xg.NumNodes - 1;
if (xrefcount == 0)
{
ed.WriteMessage("\nNo xrefs found in the drawing");
}
else
{
for (int r = 1; r < (xrefcount + 1); r++)
{
XrefGraphNode child = xg.GetXrefNode(r);
if (child.XrefStatus == XrefStatus.Resolved)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);
db.XrefEditEnabled = true;
string originalpath = btr.PathName;
string childname = Path.GetFileName(originalpath);
string newpath = @"E:\acad_dev_testing22\test\" + childname;
btr.PathName = newpath;
ed.WriteMessage("\n xref old path: " + originalpath);
ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");
}
}
tr.Commit();
}
}
// Overwrite the current drawing file with new updated XRef paths
db.SaveAs(filepaths[i], DwgVersion.Current);
}
You should save the database back to its file after making changes. Don't need to commit transaction if this file does not have xrefs.
-Khoa
Thanks mate for you advice,
I am getting following error:
Autodesk.AutoCAD.Runtime.Exception: eFileSharingViolation
at Autodesk.AutoCAD.DatabaseServices.Database.SaveAs(String fileName, DwgVersion version)
at Xref.MyCommands.refremap() in D:\Dev Zone\Ms.NET\Projects\Xref\Xref\myCommands.cs:line 361
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
========================================================================================
XrefGraph xg = db.GetHostDwgXrefGraph(true);
GraphNode root = xg.RootNode;
//XrefObjectId xobj = new XrefObjectId();
ObjectIdCollection objcoll = new ObjectIdCollection();
int xrefcount = xg.NumNodes - 1;
if (xrefcount == 0)
{
ed.WriteMessage("\nNo xrefs found in the drawing");
}
else
{
for (int r = 1; r < (xrefcount + 1); r++)
{
XrefGraphNode child = xg.GetXrefNode(r);
if (child.XrefStatus == XrefStatus.Resolved)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);
db.XrefEditEnabled = true;
string originalpath = btr.PathName;
string childname = Path.GetFileName(originalpath);
string newpath = @"D:\test_area\xref\sub_folder\" + childname;
btr.PathName = newpath;
ed.WriteMessage("\n xref old path: " + originalpath);
ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");
}
}
tr.Commit();
}
}
db.SaveAs(filepaths[i], DwgVersion.Current);
}
Try using "System.IO.FileShare.ReadWrite" as the parameter in "ReadDwgFile".
Can't find what you're looking for? Ask the community or share your knowledge.