Layouts block.count problem!

Layouts block.count problem!

Anonymous
Not applicable
507 Views
1 Reply
Message 1 of 2

Layouts block.count problem!

Anonymous
Not applicable

Good evening for everyone,

need help )))

 

I wrote the code for explode some objects in layout. I get Layout.Block.Count before explode and I get Layout.Block.Count after explode objects. These Count(Before) and Count(After) are similar!!! What I do wrong?

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common

    <CommandMethod("GALAUTODESKENTITIESSHOW", CommandFlags.Session)>
    Public Sub SPDS_Autodesk_Entities()
        Dim acApp As IAcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication
        Dim acDoc As IAcadDocument = acApp.ActiveDocument

        For Each acLayout As IAcadLayout In acDoc.Layouts
            acDoc.ActiveLayout = acLayout
            acApp.ZoomExtents()

            Select Case Strings.Trim(acLayout.Name)
                Case "Model", "Модель", "Элементы оформления"
                Case Else

                    Dim acSelSet As IAcadSelectionSet = acDoc.SelectionSets.Add("SS" & acLayout.Name)
                    acSelSet.SelectOnScreen()

                    MsgBox("Before: " & acSelSet.Count)

                    Dim acEntityBefore As Long = acLayout.Block.Count

                    acDoc.SendCommand("_ctab" & vbCr & acLayout.Name & vbCr)

                    For Each acEntity As IAcadObject In acSelSet
                        Select Case acEntity.ObjectName
                            Case "AcDbBlockReference"
                                Dim acEntityCopy As IAcadEntity = acEntity.Copy
                                acEntityCopy.Layer = "Exploded_Entities"
                                acDoc.SendCommand("_explode" & vbCr & "(handent """ & acEntityCopy.Handle & """)" & vbCr & vbCr)
                        End Select
                    Next

                    MsgBox("After: " & acDoc.ActiveLayout.Block.Count)

            End Select

        Next

    End Sub

In fact, until the explosion objects have 2 object, and after the explosion - 76!
Shows: 2 before the explosion, after 3!

 

Can anyone explain, what I do wrong?

 

0 Likes
508 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

The fact, that the 2 message boxes showing no entity count change while your code is executing and drawing shows more entities after your code execution, indicates that your code does something wrong: the code misuses SendCommand(), which is executed asynchorously, meaning, it is only executed after your code completed.

 

To explode AcadBlockReference, you can easily call AcadBlockReference.Explode() method. 

 

Also, use SendCommand() to call command "CTAB" is not necessary (not to mention it is only executed after your code execution is completed), because you have already set the target Layout as ActiveLayout.

 

Over-use of SendCommand()/SendStringToExecute() is a common mistake. One should first look for API (.NET/COM) to find a solution to the issues in hand. If you use AutoCAD 2015 or later, you can use Editor.Command()/CommandAsync() insdead of SendStringToExecute()/SendCommand().

 

Lastly, now that you are programming with .NET API, you'd be better stick with .NET API and avoid COM interop unless it is really needed.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes