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

VB.NET code to detach xref of known filename/path

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
edweberg
2733 Views, 6 Replies

VB.NET code to detach xref of known filename/path

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

6 REPLIES 6
Message 2 of 7
khoa.ho
in reply to: edweberg

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

Message 3 of 7
edweberg
in reply to: khoa.ho

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

Message 4 of 7
arcticad
in reply to: edweberg

http://converter.telerik.com/

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 5 of 7
khoa.ho
in reply to: edweberg

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

Message 6 of 7
edweberg
in reply to: khoa.ho

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

 

Message 7 of 7
khoa.ho
in reply to: edweberg

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://converter.telerik.com/

http://www.developerfusion.com/tools/convert/csharp-to-vb/

http://www.carlosag.net/tools/codetranslator/

 

-Khoa

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