.NET

Reply
Valued Contributor
edweberg
Posts: 96
Registered: ‎01-29-2007
Message 1 of 7 (1,435 Views)
Accepted Solution

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

1435 Views, 6 Replies
08-23-2012 10:55 AM

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

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

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 2 of 7 (1,428 Views)

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

08-23-2012 11:20 AM 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

Valued Contributor
edweberg
Posts: 96
Registered: ‎01-29-2007
Message 3 of 7 (1,409 Views)

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

08-23-2012 01:07 PM 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

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 4 of 7 (1,401 Views)

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

08-23-2012 02:18 PM in reply to: edweberg

http://converter.telerik.com/

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



(defun botsbuildbots() (botsbuildbots))
Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 5 of 7 (1,399 Views)

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

08-23-2012 02:24 PM 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

Valued Contributor
edweberg
Posts: 96
Registered: ‎01-29-2007
Message 6 of 7 (1,387 Views)

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

08-23-2012 02:38 PM 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

 

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 7 of 7 (1,385 Views)

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

08-23-2012 02:50 PM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!