Insert BOM balloons into drawing view using SOLIDWORKS API

Edit ArticleEdit Article

BOM Balloons in the component
BOM Balloons in the component

This VBA macro demonstrates how to insert balloons for all visible components of the first drawing view in the active drawing sheet using SOLIDWORKS API.

This view must already contain the BOM table linked to it. Macro will traverse all visible components and all visible entities of the view and will attach balloon linked to Item Number to the first visible entity.

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    Dim swDraw As SldWorks.DrawingDoc
    Set swDraw = swApp.ActiveDoc
    Dim swSheet As SldWorks.Sheet
    Dim swView As SldWorks.View
    Set swSheet = swDraw.GetCurrentSheet()
    Set swView = swSheet.GetViews()(0)
    Dim vComps As Variant
    vComps = swView.GetVisibleComponents
    Dim i As Integer
    Dim swBomBalloonParams As SldWorks.BalloonOptions
    Set swBomBalloonParams = swDraw.Extension.CreateBalloonOptions()
    swBomBalloonParams.UpperTextContent = swBalloonTextItemNumber
    For i = 0 To UBound(vComps)
        Dim swDrawComp As SldWorks.Component2
        Set swDrawComp = vComps(i)
        Dim vVisEnts As Variant
        vVisEnts = swView.GetVisibleEntities2(swDrawComp, swSelectType_e.swSelEDGES)
        Dim swEnt As SldWorks.Entity
        Set swEnt = vVisEnts(0)
        If False <> swEnt.Select4(False, Nothing) Then
            Dim swNote As SldWorks.Note
            Set swNote = swDraw.Extension.InsertBOMBalloon2(swBomBalloonParams)
            Err.Raise vbError, "", "Failed to select entity for baloon"
        End If
End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial