Get Tags, Prompts and Result strings of Attributes of Acad Block

By Xiaodong Liang

The AutoCAD DWG has a type of entity called Block which can encloses with the attributes. It is similar to the prompt string in the Inventor drawing. API allows you to access the relevant information.

Assume we have a DWG file as below:

image

The VBA code below dumps the tags, prompts and result strings.

 

Public Sub Test1()

    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    Dim oTags() As String
    Dim oValues() As String
    Dim oAcadBlkDef As AutoCADBlockDefinition
    Dim oCnt As Integer
    Debug.Print ""
    For Each oAcadBlkDef In oDoc.AutoCADBlockDefinitions
        Call oAcadBlkDef.GetPromptTags(oTags, oValues)
        Debug.Print "Block Name=" & oAcadBlkDef.Name
        Dim i As Integer
        For i = LBound(oTags) To UBound(oTags)
            Debug.Print "Tag Name=" & oTags(i) & _
            " Prompt= " & oValues(i)
        Next
    Next
    Debug.Print "———————-"
    Dim oAcadBlk As AutoCADBlock
    For Each oAcadBlk In oDoc.ActiveSheet.AutoCADBlocks
        Call oAcadBlk.GetPromptTextValues(oTags, oValues)
        Debug.Print "Block Name=" & oAcadBlk.Name
        For i = LBound(oTags) To UBound(oTags)
            Debug.Print "Tag Name=" & oTags(i) & _
            " Prompt= " & oValues(i)
        Next
    Next
    Debug.Print ""

End Sub

 

The code above could be used in .NET directly except one detail. The arguments of GetPromptTags or GetPromptTextValues require array which must be initialized correctly. The following code explains:

 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click            ' Connect to a running instance of Inventor.             Dim invApp As Inventor.Application                 ' Check to see if a drawing is active            invApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")            If invApp.ActiveDocument.DocumentType  Inventor.DocumentTypeEnum.kDrawingDocumentObject Then                MsgBox("Need to have a drawing document active")                Return                End If                 ' Create ref to active drawing document            Dim oDrawingDoc As Inventor.DrawingDocument            oDrawingDoc = invApp.ActiveDocument                 ' Create ref to AutoCADBlocks collection in the active sheet            Dim cAcadBlocks As Inventor.AutoCADBlocks            cAcadBlocks = oDrawingDoc.ActiveSheet.AutoCADBlocks                 ' Set the name of the block                Dim AcadBlockName As String                AcadBlockName = "Myblock"                 ' Create ref to the AutoCAD block object in the active sheet            Dim oAcadBlock As Inventor.AutoCADBlock            oAcadBlock = cAcadBlocks.Item(AcadBlockName)                 ' check to see if block object code is working (it does)            Debug.Print(oAcadBlock.Scale)            Debug.Print(oAcadBlock.LineWeight)            Debug.Print(oAcadBlock.Name)            Debug.Print(oAcadBlock.AttributeSets.Count)                  ' Try and display block tags and values                 'correct                Dim sTags() As String = New String() {}                Dim sAttr() As String = New String() {}                      'wrong            'Dim sTags() As String = New Object() {}            'Dim sAttr() As String = New Object() {}                 'correct only when  you have known the count of texts            ' e.g. 23                 'Dim sTags(23) As String            'Dim sAttr(23) As String                 'wrong            'Dim sTags() As String            'Dim sAttr() As String                  oAcadBlock.GetPromptTextValues(sTags, sAttr)                     Dim i As Integer                Dim iCount As Integer            iCount = UBound(sTags)            Debug.Print("icount = " & iCount)                 ' display attribute tag and value            For i = 0 To iCount                MsgBox(sTags(i) & " = " & sAttr(i))                i = i + 1            Next                 End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading