Update Attributes, optimize code

Update Attributes, optimize code

jplujan
Advocate Advocate
896 Views
3 Replies
Message 1 of 4

Update Attributes, optimize code

jplujan
Advocate
Advocate

Hello:

 

I have the following code that does is look for a nested block called "NUM" and extract attributes on a grid.

 

After finding the block, I seek your handle, you can update its attributes in order.

 

The code works, but I think it goes to slow in making the process go I wonder if it is possible to accelerate it.

 

Thanks in advance.

 

Sub ActualizaAtributoBloqueNum(ByVal oBloqueSeleccion As String, _
                                   ByVal IDHandle As String, _
                                   ByVal Posicion As String, _
                                   ByVal Identificador As String, _
                                   ByVal descripcion1 As String, _
                                   ByVal descripcion2 As String)

        Dim oBloquePadre As Object = Nothing
        Dim oBloqueHijo As Object
        Dim AttArray As Object
        Dim oBlkDefOut As AcadBlock
        Dim NombreBloqueFiltro As String

        ' Nombre del bloque hijo.
        ' 0 = Posicion
        ' 1 = Identificador
        ' 2 = Descripcion 1
        ' 3 = Descripcion 2

        NombreBloqueFiltro = "NUM"

        For Each oBloquePadre In oApp.ActiveDocument.Blocks
            If oBloquePadre.Name = oBloqueSeleccion Then
                oBlkDefOut = oBloquePadre
                For Each oBloqueHijo In oBlkDefOut
                    If oBloqueHijo.handle = IDHandle Then
                        If TypeOf oBloqueHijo Is AcadBlockReference Then
                            If oBloqueHijo.EffectiveName = NombreBloqueFiltro Then
                                If oBloqueHijo.HasAttributes Then
                                    AttArray = oBloqueHijo.GetAttributes
                                    For I = LBound(AttArray) To UBound(AttArray)
                                        AttArray(0).TextString = Posicion
                                        AttArray(1).TextString = Identificador
                                        AttArray(2).TextString = descripcion1
                                        AttArray(3).TextString = descripcion2
                                    Next
                                End If
                            End If
                        End If
                    End If
                Next
            End If
        Next

    End Sub
0 Likes
Accepted solutions (2)
897 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

 

If you are not required to use the COM API (i.e. stand-alone application), using the .NET API should improve the performance.

 

That said, if COM is required:

- you should use early binding instead of late binding (setting the option Strict to On should help)

- you can directly get the block definition within the block table with the Item() method:

oBlkDefOut = oApp.ActiveDocument.Blocks.Item(oBloqueSeleccion)

- as you know the block reference handle, when looking for it within the block definition entities you only need to check this handle (i.e. no need to check for the type, the name)

- the For loop to set the attributes values is absolutely useless, it just repeat the same settings 4 times.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Here's a .NET implementation inspired by your code (optimization rule #1: avoid using COM as far as possible)

 

        Sub ActualizaAtributoBloqueNum(ByVal oBloqueSeleccion As String,
                                   ByVal IDHandle As String,
                                   ByVal Posicion As String,
                                   ByVal Identificador As String,
                                   ByVal descripcion1 As String,
                                   ByVal descripcion2 As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db = doc.Database
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(oBloqueSeleccion), OpenMode.ForRead), BlockTableRecord)
                For Each id As ObjectId In btr
                    If id.Handle.ToString() = IDHandle Then
                        Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForRead), BlockReference)
                        Dim atts As AttributeCollection = br.AttributeCollection
                        DirectCast(tr.GetObject(atts(0), OpenMode.ForWrite), AttributeReference).TextString = Posicion
                        DirectCast(tr.GetObject(atts(1), OpenMode.ForWrite), AttributeReference).TextString = Identificador
                        DirectCast(tr.GetObject(atts(2), OpenMode.ForWrite), AttributeReference).TextString = descripcion1
                        DirectCast(tr.GetObject(atts(3), OpenMode.ForWrite), AttributeReference).TextString = descripcion2
                        Exit For
                    End If
                Next
                tr.Commit()
            End Using
        End Sub

Here's a COM optimization of your code. I wasn't able to totally avoid late binding due to the fact GetAttributes() method returns a variant.

 

        Sub ActualizaAtributoBloqueNum(ByVal oBloqueSeleccion As String,
                                   ByVal IDHandle As String,
                                   ByVal Posicion As String,
                                   ByVal Identificador As String,
                                   ByVal descripcion1 As String,
                                   ByVal descripcion2 As String)

            Dim oBloquePadre As AcadBlock = Nothing
            Dim oBloqueHijo As AcadBlockReference
            Dim AttArray As Object
            Dim oBlkDefOut As AcadBlock

            ' Nombre del bloque hijo.
            ' 0 = Posicion
            ' 1 = Identificador
            ' 2 = Descripcion 1
            ' 3 = Descripcion 2
            oBlkDefOut = oApp.ActiveDocument.Blocks.Item(oBloqueSeleccion)
            For Each ent As AcadEntity In oBlkDefOut
                If ent.Handle = IDHandle Then
                    oBloqueHijo = DirectCast(ent, AcadBlockReference)
                    AttArray = oBloqueHijo.GetAttributes()
                    AttArray(0).TextString = Posicion
                    AttArray(1).TextString = Identificador
                    AttArray(2).TextString = descripcion1
                    AttArray(3).TextString = descripcion2
                    Exit For
                End If
            Next
        End Sub


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

jplujan
Advocate
Advocate
Accepted solution
Thanks for your quick response.
It works perfect.
0 Likes