Hello Coders,
I am new here, I am a bigginer.
Here is my issue.
I wanted to detach xrefs that having status File Not found from multiple drawings (fodlers and sub folders)
without opening the drawing file.
- I am able to get all the dwg files from folders and subfodlers.
- am able to find the xrefs that are working and the ones that arent
I am not able to detach the xrefs that are having the status FileNotfound.
I dont know how to get the objectIds of those xrefs. I tried several ways..please help me.
===================================================================================
Here is my code:
=============
[CommandMethod("Dref")]
public void detach_xref()
{
//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: ");
string[] filepaths = Directory.GetFiles(res.StringResult, "*.dwg", SearchOption.AllDirectories);
int filecount = filepaths.Length;
ed.WriteMessage("\nScanning " + filecount + " files");
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;
}
db.ResolveXrefs(true, false);
XrefGraph xg = db.GetHostDwgXrefGraph(true);
int xrefcount = xg.NumNodes - 1;
if(xrefcount > 0)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectIdCollection btcoll = new ObjectIdCollection();
for (int j = 1; i < xrefcount; i++)
{
XrefGraphNode xrNode = xg.GetXrefNode(j);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(xrNode.BlockTableRecordId, OpenMode.ForWrite);
if (xrNode.XrefStatus == XrefStatus.FileNotFound)
{
foreach (ObjectId id in btr)
{
db.DetachXref(id);
ed.WriteMessage("\nDetached successfully");
}
}
}
//transaction
tr.Commit();
}
}
}
}
}
Solved! Go to Solution.
Solved by Balaji_Ram. Go to Solution.
Try this.
XrefGraphNode xrNode = xg.GetXrefNode(j);
if (xrNode.XrefStatus == XrefStatus.FileNotFound)
{
ObjectID detachid = xrNode.BlockTableRecordId;
db.DetachXref(detachid);
ed.WriteMessage("\nDetached successfully");
}
That should be close - I have not coded C# in a while.
I tried I am getting another error
Hi,
You need to be more specific about the kind of issue you are having.
Here is the code sample that I posted in response to your other post on the same topic :
This sample code can only detach external references directly inserted in the drawing (not nested ones).
<<<
[CommandMethod("DetachXref")] public void detach_xref() { Document Doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Doc.Editor; string mainDrawingFile = @"C:\Temp\Test.dwg"; Database db = new Database(false, false); using (db) { try { db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, false, ""); } catch (System.Exception) { ed.WriteMessage("\nUnable to read the drawingfile."); return; } bool saveRequired = false; db.ResolveXrefs(true, false); using (Transaction tr = db.TransactionManager.StartTransaction()) { XrefGraph xg = db.GetHostDwgXrefGraph(true); int xrefcount = xg.NumNodes; for (int j = 0; j < xrefcount; j++) { XrefGraphNode xrNode = xg.GetXrefNode(j); String nodeName = xrNode.Name; if (xrNode.XrefStatus == XrefStatus.FileNotFound) { ObjectId detachid = xrNode.BlockTableRecordId; db.DetachXref(detachid); saveRequired = true; ed.WriteMessage("\nDetached successfully"); break; } } tr.Commit(); } if(saveRequired) db.SaveAs(mainDrawingFile, DwgVersion.Current); } }
>>>
Thanks Balaji,
Genious..!! solved my issue...