Handy Code to find your Title Block…

 1:  <CommandMethod("GetTBlk")> _
   2:      Public Sub TestGetBlock()
   3:   
   4:          'Get TextString
   5:          Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
   6:          Dim pStrOpts As PromptStringOptions = New PromptStringOptions(vbLf & _
   7:                "Enter the attribute in a block to search for: ")
   8:          pStrOpts.AllowSpaces = True
   9:          Dim pStrRes As PromptResult = acDoc.Editor.GetString(pStrOpts)
  10:          Application.ShowAlertDialog("The name entered was: " & _
  11:             pStrRes.StringResult)
  12:   
  13:          'Search For Block Object
  14:          Dim acCurDb As Database = Application.DocumentManager.MdiActiveDocument.Database
  15:          Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  16:   
  17:              Dim myBlkref As BlockReference
  18:              Dim myEnt As Entity
  19:   
  20:              Dim myObjID As Autodesk.AutoCAD.DatabaseServices.ObjectId
  21:              myObjID = GetBlockID_ByAttributeTag(acCurDb, pStrRes.StringResult)
  22:              If Not myObjID.IsNull Then
  23:   
  24:                  myEnt = acTrans.GetObject(myObjID, OpenMode.ForRead)
  25:   
  26:                  If TypeOf myEnt Is Autodesk.AutoCAD.DatabaseServices.BlockReference Then
  27:                      myBlkref = myEnt
  28:                      MsgBox(myBlkref.Name)
  29:                      MsgBox(myBlkref.Layer)
  30:                  End If
  31:   
  32:              Else
  33:                  MsgBox("Can't Find Block with Attribute Tagged as:" & pStrRes.StringResult)
  34:   
  35:              End If
  36:   
  37:   
  38:          End Using
  39:   
  40:   
  41:      End Sub
  42:  ''' <summary>
  43:      ''' Gets the block Id_ by attribute tag.
  44:      ''' </summary>
  45:      ''' <param name="DatabaseIn">The database in.</param>
  46:      ''' <param name="s_AttrTag">The s_ attr tag.</param><returns></returns>
  47:      Public Function GetBlockID_ByAttributeTag(ByVal DatabaseIn As Database, ByVal s_AttrTag As String) As ObjectId
  48:          '-->>Rev 1.0 by VBAutoCadGuy 10/31/2011 -Search Through BlockTable Definitions For Block with Attribute TextString
  49:   
  50:          Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
  51:   
  52:              'BlockTable
  53:              Dim myBT As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
  54:              For Each myBtrID As ObjectId In myBT
  55:   
  56:                  'Is Not Erased
  57:                  If Not myBtrID.IsEffectivelyErased Then
  58:   
  59:                      'Block Records
  60:                      Dim myBTR As BlockTableRecord = myBtrID.GetObject(OpenMode.ForRead)
  61:   
  62:                      'BlockReferene Instances Object
  63:                      Dim myIDs As ObjectIdCollection = myBTR.GetBlockReferenceIds(True, True)
  64:   
  65:                      'For Each BlockReferencesID
  66:                      For Each myBRefID As ObjectId In myIDs
  67:                          'GetBlockReference Object
  68:                          Dim myBlockRef As BlockReference = myBRefID.GetObject(OpenMode.ForRead)
  69:   
  70:                          'Attributes
  71:                          For Each attRefID As ObjectId In myBlockRef.AttributeCollection
  72:                              Dim myAttRef As AttributeReference = attRefID.GetObject(OpenMode.ForRead)
  73:   
  74:                              If myAttRef.Tag.ToUpper = s_AttrTag.ToUpper Then
  75:                                  Return myBlockRef.ObjectId
  76:                                  Exit Function
  77:                              Else
  78:                                  Debug.Print(myBlockRef.Name & " Not Valid Block")
  79:                              End If
  80:   
  81:                          Next
  82:   
  83:                      Next
  84:   
  85:                  End If
  86:   
  87:              Next
  88:   
  89:   
  90:          End Using
  91:   
  92:          Return Autodesk.AutoCAD.DatabaseServices.ObjectId.Null
  93:   
  94:      End Function
  95:      ''' <summary>
  96:      ''' Gets the block Id by attribute text string.
  97:      ''' </summary>
  98:      ''' <param name="DatabaseIn">The database in.</param>
  99:      ''' <param name="s_AttrTextString">The s_ attr text string.</param><returns></returns>
 100:      Public Function GetBlockID_ByAttributeTextString(ByVal DatabaseIn As Database, ByVal s_AttrTextString As String) As ObjectId
 101:          Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
 102:   
 103:              'BlockTable
 104:              Dim myBT As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
 105:              For Each myBtrID As ObjectId In myBT
 106:   
 107:                  'Is Not Erased
 108:                  If Not myBtrID.IsEffectivelyErased Then
 109:   
 110:                      'Block Records
 111:                      Dim myBTR As BlockTableRecord = myBtrID.GetObject(OpenMode.ForRead)
 112:   
 113:                      'BlockReferene Instances Object
 114:                      Dim myIDs As ObjectIdCollection = myBTR.GetBlockReferenceIds(True, True)
 115:   
 116:                      'For Each BlockReferencesID
 117:                      For Each myBRefID As ObjectId In myIDs
 118:                          'GetBlockReference Object
 119:                          Dim myBlockRef As BlockReference = myBRefID.GetObject(OpenMode.ForRead)
 120:   
 121:                          'Attributes
 122:                          For Each attRefID As ObjectId In myBlockRef.AttributeCollection
 123:                              Dim myAttRef As AttributeReference = attRefID.GetObject(OpenMode.ForRead)
 124:   
 125:                              If myAttRef.TextString.ToUpper = s_AttrTextString.ToUpper Then
 126:                                  Return myBlockRef.ObjectId
 127:                                  Exit Function
 128:                              Else
 129:                                  Debug.Print(myBlockRef.Name & " Not Valid Block")
 130:                              End If
 131:   
 132:                          Next
 133:   
 134:                      Next
 135:   
 136:                  End If
 137:   
 138:              Next
 139:   
 140:   
 141:          End Using
 142:   
 143:          Return Autodesk.AutoCAD.DatabaseServices.ObjectId.Null
 144:      End Function

Welcome to VB AutoCad Guy’s Blog

Welcome to The VbAutoCadGuy Blog!

Today’s Post:

“The determining factor for the adoption and success of a technology is invariably human, political, and economic -not technological!” (Appleman, 2003, Moving to VB .NET: Strategies, Concepts, and Code)