Jump to content

Get the block name by picking it using VB.net


Recommended Posts

Posted

AutoCAD 2010+ and VB.net

 

How does someone get the block name in a drawing by picking the block reference in

the drawing using vb.net?

Thank you,

Posted

Consider this example from Kean's site (converted C#->VB.NET), paying special attention to the GetSelection Method:

 

Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

Namespace MyApplication

   Public Class DumpAttributes

       <CommandMethod("LISTATT")> _
       Public Sub ListAttributes()

           Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

           Dim db As Database = HostApplicationServices.WorkingDatabase

           Dim tr As Transaction = db.TransactionManager.StartTransaction()

           ' Start the transaction

           Try

               ' Build a filter list so that only

               ' block references are selected

               Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "INSERT")}

               Dim filter As New SelectionFilter(filList)

               Dim opts As New PromptSelectionOptions()

               opts.MessageForAdding = "Select block references: "

               Dim res As PromptSelectionResult = ed.[color=red][b]GetSelection[/b][/color](opts, filter)

               ' Do nothing if selection is unsuccessful

               If res.Status <> PromptStatus.OK Then

                   Return
               End If

               Dim selSet As SelectionSet = res.Value

               Dim idArray As ObjectId() = selSet.GetObjectIds()

               For Each blkId As ObjectId In idArray

                   Dim blkRef As BlockReference = DirectCast(tr.GetObject(blkId, OpenMode.ForRead), BlockReference)

                   Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)

                   ed.WriteMessage(vbLf & "Block: " + btr.Name)

                   btr.Dispose()

                   Dim attCol As AttributeCollection = blkRef.AttributeCollection

                   For Each attId As ObjectId In attCol

                       Dim attRef As AttributeReference = DirectCast(tr.GetObject(attId, OpenMode.ForRead), AttributeReference)

                       Dim str As String = ((vbLf & "  Attribute Tag: " + attRef.Tag & vbLf & "    Attribute String: ") + attRef.TextString)

                       ed.WriteMessage(str)

                   Next
               Next

               tr.Commit()

           Catch ex As Autodesk.AutoCAD.Runtime.Exception

               ed.WriteMessage(("Exception: " + ex.Message))

           Finally

               tr.Dispose()

           End Try

       End Sub

   End Class

End Namespace

Source: http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html

 

HTH

Posted

Sorry, the code went to being all on one line after I posted; code has been reformatted in the above post.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...