.NET

.NET

Reply
Distinguished Contributor
109 Posts
0 Kudos
Registered: ‎01-29-2007
Post 1 of 7
Accepted Solution

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

1546 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
225 Posts
24 Kudos
Registered: ‎09-15-2011
Post 2 of 7

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

Distinguished Contributor
109 Posts
0 Kudos
Registered: ‎01-29-2007
Post 3 of 7

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*
1,286 Posts
37 Kudos
Registered: ‎06-21-2004
Post 4 of 7

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
225 Posts
24 Kudos
Registered: ‎09-15-2011
Post 5 of 7

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

Distinguished Contributor
109 Posts
0 Kudos
Registered: ‎01-29-2007
Post 6 of 7

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
225 Posts
24 Kudos
Registered: ‎09-15-2011
Post 7 of 7

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 interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!