.NET

Reply
Active Contributor
joelkarr
Posts: 30
Registered: ‎11-25-2008
Message 1 of 4 (472 Views)

Working with Xrefs in C# Binding or Changing path

472 Views, 3 Replies
02-02-2009 09:58 AM
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
Active Contributor
joelkarr
Posts: 30
Registered: ‎11-25-2008
Message 2 of 4 (472 Views)

Re: Working with Xrefs in C# Binding or Changing path

02-02-2009 10:31 AM 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.
Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 3 of 4 (277 Views)

Re: Working with Xrefs in C# Binding or Changing path

08-06-2012 05:13 AM 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
ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 4 of 4 (235 Views)

Re: Working with Xrefs in C# Binding or Changing path

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

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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