target audience:{beginner}
Hi,
Here is a function (which needs improvements) in order to select BlockReferences in PaperSpace (in one or all layouts) depending of the block name and the layer:
''' <summary>
''' Selection des blocs par leur nom
''' sur l'onglet courant ou sur tous l'espace papier
''' en fonction du nom de calque
''' </summary>
''' <param name="BlocRefNom">Nom de la référence de bloc</param>
''' <param name="DessinEntier">l'onglet courant(false) ou tous l'espace papier(true)</param>
''' <param name="Calque">nom du calque</param>
''' <returns>Collection d'objectid des blocs</returns>
''' <remarks></remarks>
Public Function F_SelectBlockRefInLayouts(ByVal BlocRefNom As String, ByVal DessinEntier As Boolean, ByVal Calque As String) As ObjectIdCollection
'=========================================================
'NOM: GetBlockRefInLayouts
'DESCRIPTION: Selection des blocs par leur nom sur l'onglet courant ou sur tous l'espace papier
'ARGUMENTS: BlocRefNom: Nom de la référence de bloc
'RETOUR: Collection d'objectid des blocs
'DATE: 15/11/2010
'=========================================================
Dim ids As New ObjectIdCollection()
Dim db As Database = AcadAp.DocumentManager.MdiActiveDocument.Database
Using tr As Transaction = db.TransactionManager.StartTransaction()
Try
Dim layoutDict As DBDictionary = DirectCast(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
For Each entry As DBDictionaryEntry In layoutDict
If entry.Key = "Model" Then Continue For 'espace objet non pris en compte
Dim layout As Layout = DirectCast(tr.GetObject(entry.Value, OpenMode.ForRead), Layout)
If Not layout.TabSelected And Not DessinEntier Then Continue For 'Selection sur le dessin entier ou l'onglet courant
Dim layoutBtr As BlockTableRecord = DirectCast(tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead, False), BlockTableRecord)
If Not layoutBtr.IsAnonymous Then
For Each id As ObjectId In layoutBtr
Dim blk As BlockReference = TryCast(tr.GetObject(id, OpenMode.ForRead), BlockReference)
If Not blk Is Nothing Then
If Calque = "" Or blk.Layer = Calque Then 'Selection par calque
If blk.IsDynamicBlock Then 'si bloc dynamique
Dim btr As BlockTableRecord = blk.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)
If btr.Name Like BlocRefNom Then
ids.Add(id)
End If
ElseIf blk.Name Like BlocRefNom Then 'si bloc normal
ids.Add(id)
End If
End If
End If
Next
End If
Next
Catch ex As Exception
Dim Msg As String = String.Format("Erreur en tentant de sélectionner les blocs {0}", BlocRefNom)
Dim Titre As String = "Opération annulée"
MessageBox.Show(Msg, Titre, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
tr.Abort()
Return ids
Finally
tr.Commit()
End Try
End Using
Return ids
End Function
(Comments are in French, i'll change it soon)
Thanks for your feedback