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

Working with Xrefs in C# Binding or Changing path

3 REPLIES 3
Reply
Message 1 of 4
joelkarr
1812 Views, 3 Replies

Working with Xrefs in C# Binding or Changing path

I realized many people need code in a txt file so I have attached two from my previous posts.

Both are having the same problem where the code runs to completion but does not update the xrefs. I am still guessing there is some xref edit that I am missing other than database.xrefeditenabled

Any help would be greatly appreciated

Joel
3 REPLIES 3
Message 2 of 4
joelkarr
in reply to: joelkarr

Thanks to T.Willey over on an Augi Forum I found I was making the dumb mistake of forgetting to commit the transaction.

Any other advice or suggestions on the code would still be greatly appreciated.
Message 3 of 4
raghulan
in reply to: joelkarr

I did as per your suggestion..I get the following error...

Thanks for your advice..

===============================================================================

System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Attach(IntPtr unmanagedPointer, Boolean autoDelete)
at Autodesk.AutoCAD.DatabaseServices.XrefGraph.GetXrefNode(Int32 idx)
at Xref.MyCommands.detach_xref() in C:\Users\Administrator\documents\visual studio 2010\Projects\Xref\Xref\myCommands.cs:line 419
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

=================================================================================

code follows

==================================================================================================================================================================

 

 

 

 

[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);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
XrefGraph xg = db.GetHostDwgXrefGraph(true);
int xrefcount = xg.NumNodes - 1;
ObjectIdCollection ids = new ObjectIdCollection();
XrefGraphNode xrNode = xg.GetXrefNode(i);

if(xrefcount > 0)
{

ObjectIdCollection btcoll = new ObjectIdCollection();
for (int j = 1; i < xrefcount; i++)
{
XrefGraphNode xrNode1 = xg.GetXrefNode(j);

if (xrNode1.XrefStatus == XrefStatus.FileNotFound)
{
//BlockTableRecord btr = (BlockTableRecord)tr.GetObject(xrNode1.BlockTableRecordId, OpenMode.ForWrite);
db.XrefEditEnabled = true;
ObjectId detachid = xrNode1.BlockTableRecordId;

db.DetachXref(detachid);
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
Message 4 of 4
Balaji_Ram
in reply to: raghulan

Hi,

 

There are several coding issues with your code. Check the index "for(j=1;i<xrefcount;i++)". Do you intend to increment i or j. You may need to clean-up the code.

 

Here is a sample code to detach Xref from a drawing. This might help you get started. Only the direct external references (not Nested Xrefs) will get detached using this if they are not found.

 

		[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;
					ObjectIdCollection ids = new ObjectIdCollection();
					ObjectIdCollection btcoll = new ObjectIdCollection();
					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

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