Hi,
A part of an application I'm writing involves detaching a specific xref of known filename and path from an open drawing. I'm having a surprising amount of trouble figuring out how to do this or finding any examples "out there" (A'desk forum, the googles, A'desk developer's guide). Can anyone point me to a good example of how to do this? I have the code for attaching an xref, but detaching is presenting unexpected problems.
Thanks so much,
Erik
Solved! Go to Solution.
Solved by khoa.ho. Go to Solution.
The following code works for me to detach an xref name "MyXref" out of the open current drawing:
[CommandMethod("DetachXref")] public void DetachXref() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; string xrefName = "MyXref"; DetachXref(db, xrefName); } private static void DetachXref(Database db, string xrefName) { using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction()) { XrefGraph xrefGraph = db.GetHostDwgXrefGraph(true); int xrefCount = xrefGraph.NumNodes; for (int i = 0; i < xrefCount; i++) { XrefGraphNode xrefNode = xrefGraph.GetXrefNode(i); if (xrefNode.Name.ToLower() == xrefName.ToLower()) { ObjectId xrefId = xrefNode.BlockTableRecordId; db.DetachXref(xrefId); break; } } trans.Commit(); } }
-Khoa
Thank you for the reply. I'm trying to parse this out into VB.NET language. I'm not familiar with C#. Any hints as to how to use the section after starting the transaction in VB.NET?
Erik
Sorry for a bit confusing with C#. I copied the code from my C# project and did not convert it into VB.NET. You can use http://converter.telerik.com/ to convert C# to VB.NET or vice versa.
After starting the transaction, you put the code inside its section normally. There are a lot of tutorials explaining why and how to use transaction in AutoCAD .NET. You can do research and see many hints.
The link from AutoCAD DevBlog ( http://adndevblog.typepad.com/autocad/2012/08/the-right-tools-for-the-job-autocad-part-5.html ) has the explanation of how to use transaction. This will give some advanced hints for you.
Please see my previous code in VB.NET:
<CommandMethod("DetachXref")> _ Public Sub DetachXref() Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim xrefName As String = "MyXref" DetachXref(db, xrefName) End Sub Private Shared Sub DetachXref(db As Database, xrefName As String) Using trans As Transaction = db.TransactionManager.StartOpenCloseTransaction() Dim xrefGraph As XrefGraph = db.GetHostDwgXrefGraph(True) Dim xrefCount As Integer = xrefGraph.NumNodes For i As Integer = 0 To xrefCount - 1 Dim xrefNode As XrefGraphNode = xrefGraph.GetXrefNode(i) If xrefNode.Name.ToLower() = xrefName.ToLower() Then Dim xrefId As ObjectId = xrefNode.BlockTableRecordId db.DetachXref(xrefId) Exit For End If Next trans.Commit() End Using End Sub
-Khoa
Awesome! Thank you both for the help. Although I'm on a steep learning curve, I am familiar with transactions. I didn't know about the C# to VB converter. A lot of what I find in google searches is in C# and have put those hits aside because I don't know the language. This converter opens up doors in very useful ways.
Thank you!
Erik
I am glad that it works for you. Code converter between C# and VB.NET is my friend everyday. You can lookup Google for free online tools. There are some links that work good:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
http://www.carlosag.net/tools/codetranslator/
-Khoa