Extracting Information From Blocks

Extracting Information From Blocks

felix.cortes5K3Y2
Advocate Advocate
967 Views
9 Replies
Message 1 of 10

Extracting Information From Blocks

felix.cortes5K3Y2
Advocate
Advocate

Hi Forum,

 

I have a block used several times on an Autocad sheet and I want to get each instance as well as all of its attributes. The name of the block is called "MASS INTERMEDIATE DYNAMIC". I can't seem to get the count of the amount of instances used on the drawing and get the attributes. The code below only returns one instance of the block but I know it's used 20 times on my autocad sheet.

 


Dim acad1 As AcadApplication
acad1 = Marshal.GetActiveObject("Autocad.Application")
Dim oDoc As AcadDocument = acad1.ActiveDocument
Dim oblocks As AcadBlocks = oDoc.Blocks
Dim sblock As String = "MASS INTERMEDIATE DYNAMIC"
Dim blockname As String
For Each oblock As AcadBlock In oblocks
    blockname = oblock.Name
    If Not InStr(1, blockname, sblock) > 0 Then Continue For
    Debug.Print(blockname)
Next

 

 

Thanks,

Felix

0 Likes
Accepted solutions (1)
968 Views
9 Replies
Replies (9)
Message 2 of 10

MakCADD
Advocate
Advocate

if it is dynamic block then the name is 

block.effectivename  not block.name

0 Likes
Message 3 of 10

felix.cortes5K3Y2
Advocate
Advocate

Thanks for the response... that doesn't really help me with this question

0 Likes
Message 4 of 10

MakCADD
Advocate
Advocate

your document contains only one block definition in blocks collections

instances are blockreferences inserted in the drawing

 

you declared variable as block  you may be asking about block not about blockreference.....

 

 

0 Likes
Message 5 of 10

felix.cortes5K3Y2
Advocate
Advocate

I see, so I should be using AcadBlockReference then. How do I retrieve all of the block references of a document?

 

I'm trying to re-write the For-Next loop to this:

 

For Each oblockref As AcadBlockReference in oDoc.Blocks
    blockname = oblockref.Name

Next

 

I seem to get an error because I am not getting the collection of the block references correctly.

 

Thanks,

Felix

0 Likes
Message 6 of 10

MakCADD
Advocate
Advocate

Loop through all objects,. Find the entityname AcDbBlockreference, set the object,. Then verify the block reference name or effective name if it is dynamic block.  

 

But I think u are not using VBA.  It look like something else

 

0 Likes
Message 7 of 10

seabrahenrique
Advocate
Advocate

Hey mate!

 

If i understand correctly, a Selection Set can help u in this case!

 

Try this code to interact with each block reference in your drawing:

 

Function FA_SST(nomeT As String) As AcadSelectionSet 'Create a Selection Set for a especific type

Dim filterType(0) As Integer, filterData(0) As Variant
Set FA_SST = ThisDrawing.SelectionSets.ADD("SS01")
filterType(0) = 0: filterData(0) = nomeT
FA_SST.Select acSelectionSetAll, , , filterType, filterData

End Function

Sub ForEachBlock()

Dim SS As AcadSelectionSet, Obj As AcadEntity
Set SS = FA_SST("INSERT")

For Each Obj In SS

    Debug.Print Obj.EffectiveName

Next Obj

End Sub

 

Glad to help

 

0 Likes
Message 8 of 10

felix.cortes5K3Y2
Advocate
Advocate

Hey thanks for the response! Is it possible to just get all of the references of the Autocad sheet? I don't really need a selection set. Just need to retrieve the instances that block is used on the autocad sheet and its attributes.

 

Thanks,

Felix

0 Likes
Message 9 of 10

norman.yuan
Mentor
Mentor
Accepted solution

Assuming by saying "AutoCAD sheet", you mean Layout/PaperSpace. If using AcadSelectionSet, you can add layout name filter to select block reference on given layout. But looping through Layout block would be more straightforward/simple enough (assume you want to go through all layouts for block reference with given name):

 

Dim ent As AcadEntity

Dim lay As AcadLayout

Dim blk As AcadReference

For Each lay in ThisDrawing.Layouts

  For Each ent in lay.Block

    If TypeOf ent Is acadBlockreference Then

      Set blk=ent

      If UCase(blk.EffectiveName)="MY_TARGET_BLOCK" Then

         MsgBox "This is the block I am looking for on Layout """ & lay.Name & """".

         GetDataFromBlockAttributes blk

      End If

    End If

  Next

Next

 

Private Sub GetDataFromBlockAttributes(blk As AcadBlockReference)

   Dim atts As Variant

   Dim att As AcadAttributereference

   Dim i As Integer

   Dim attValue As String

   atts = blk.GetAttributes()

   For i=0 to UBound(atts)

      Set att=atts(i)

      Select Case UCase(att.TagString)

         Case "TAG1"

            attValue=att.TextString

            '' Do whatever with the attribute value...

         Case "TAG2"

          ...

          

     End Select

   Next

End Sub

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 10 of 10

felix.cortes5K3Y2
Advocate
Advocate

Sweet! This helped a ton. Thank you for your help.

0 Likes