.NET

Reply
Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 1 of 5 (502 Views)
Accepted Solution

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

502 Views, 4 Replies
07-29-2012 02:09 AM

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

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

 

>>>

Valued Mentor
fieldguy
Posts: 372
Registered: ‎03-31-2005
Message 2 of 5 (485 Views)

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

07-30-2012 08:28 AM 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.  

Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 3 of 5 (445 Views)

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

08-09-2012 12:03 AM 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
ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 4 of 5 (417 Views)

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

08-10-2012 04:31 AM 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

Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 5 of 5 (408 Views)

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

08-13-2012 01:43 AM 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
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.