.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

.NET c# unable to detaching FileNotFound xref without opening the drawing.

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
raghulan
1755 Views, 4 Replies

.NET c# unable to detaching FileNotFound xref without opening the drawing.

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();
}

}
}
}

}

 

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
4 REPLIES 4
Message 2 of 5
fieldguy
in reply to: raghulan

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.  

Message 3 of 5
raghulan
in reply to: raghulan

I tried I am getting another error

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
Message 4 of 5
Balaji_Ram
in reply to: raghulan

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);
			}
		}

 

>>>



Balaji
Developer Technical Services
Autodesk Developer Network

Message 5 of 5
raghulan
in reply to: Balaji_Ram

Thanks Balaji,

Genious..!! solved my issue...

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost