autocad

Change Tags of all MultiLeader objects

This example shown is for a custom multileader that has additional attributes; although with slight modification it can also work with standard multileader. This is useful when trying to match the pcmarks with the Bill of Material items in a table. The code can be expanded to loop through a BOM table, for simplicity I wont be covering this for now. The code uses advanced filtered selection sets along with looping through entities in the selection.

Attributes of the Custom MultiLeader

pcmk_partnumber block with attributes
changing the source block
final result after running the code
Option Explicit
Sub exampleCode()
    Dim acadApp As AcadApplication
    Dim acadDoc As AcadDocument
    Dim FilterData(0) As Variant
    Dim FilterType(0) As Integer
    Dim oAttDef As Variant
    Dim oAttDef_desc As Variant
    Dim oAttDef_PN_imperial As Variant
    Dim oAttDef_PN_metric As Variant
    Dim oAttDef_tagnumber As Variant
    Dim oEnt As AcadEntity
    Dim oEnt2 As AcadEntity
    Dim oMLeader As Variant
    Dim oSset As AcadSelectionSet
    Dim PNinLeader As Variant
    Set acadApp = GetObject(, "AutoCAD.Application")
    Set acadDoc = acadApp.ActiveDocument
    'figure out the object ID for each attribute in the custom multileader
    For Each oEnt2 In acadDoc.Blocks("pcmk_partnumber")
        If TypeOf oEnt2 Is AcadAttribute Then
            Set oAttDef = oEnt2
            If oAttDef.PromptString = "Enter tag number" Then
                Set oAttDef_tagnumber = oEnt2
            ElseIf oAttDef.PromptString = "Enter part number imperial" Then
                Set oAttDef_PN_imperial = oEnt2
            ElseIf oAttDef.PromptString = "Enter part number metric" Then
                Set oAttDef_PN_metric = oEnt2
            ElseIf oAttDef.PromptString = "Enter part description" Then
                Set oAttDef_desc = oEnt2
            End If
        End If
    Next oEnt2
    'create a selection set that only selects multileader objects with filters
    With acadDoc.SelectionSets
        While .Count > 0
            .Item(0).Delete
        Wend
    End With
    FilterType(0) = 0
    FilterData(0) = "MULTILEADER"
    Set oSset = acadDoc.SelectionSets.Add("selectmulileader2")
    oSset.Select acSelectionSetAll, , , FilterType, FilterData
    For Each oEnt In oSset
        Set oMLeader = oEnt
        If oMLeader.ContentBlockName = "pcmk_partnumber" Then
            PNinLeader = oMLeader.GetBlockAttributeValue(oAttDef_PN_imperial.ObjectID)
            PNinLeader = Val(PNinLeader)  'converts the text to a value
            If PNinLeader = 43038 Then
                oMLeader.SetBlockAttributeValue oAttDef_tagnumber.ObjectID, 21
            Else
                oMLeader.SetBlockAttributeValue oAttDef_tagnumber.ObjectID, "???"
            End If
        End If
    Next
    acadDoc.Regen acActiveViewport
End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s