Add balloons to components based on custom properties values

Hi All,

For our customers we always need to make "Spare part drawings". Which is basically a drawing containing an Iso-view on which we attach balloons to the spare parts which contain their part numbers (like file name, BOMballoon)

Now i created a little macro that inserts that balloon on the selected component. I select the component, click the button, balloon get inserted with the right settings, I select the next component.... you'll get the idea .

However, each "spare part component" has a custom property called "spare part" with a value of 1 for spare part, 0 for non spare part.

Is there a way to have the macro look for this custom property, and when it is set to 1 insert the balloon?

This would be a huge time saver.

Thanks in Advance

SolidworksDrawings And Detailing
Answers 8
DG
DG 2021-04-08 Best Answer

Willem Nagels wrote:

However, each "spare part component" has a custom property called "spare part" with a value of 1 for spare part, 0 for non spare part.

 

Is there a way to have the macro look for this custom property, and when it is set to 1 insert the balloon?

Yes this is possible, share your macro and some would fix it or guide you on what to do to make it better.

WN
WN 2021-04-08

Hi All,

So far I tried a couple of things to select the components origin before inserting the balloon. But with no luck.

The code still adds the balloons somewhere on the sheet. Anybody an idea what to do????


Option Explicit

Sub main()

    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swSelData                   As SldWorks.SelectData
    Dim swModelDocExt               As SldWorks.ModelDocExtension
    Dim swView                      As SldWorks.View
    Dim swRootDrawComp              As SldWorks.DrawingComponent
    Dim vDrawChildCompArr           As Variant
    Dim vDrawChildComp              As Variant
    Dim swDrawComp                  As SldWorks.DrawingComponent
    Dim swComp                      As SldWorks.Component2
    Dim swCompModel                 As SldWorks.ModelDoc2
    Dim assemblyDrawing             As String
    Dim status                      As Boolean
    Dim errors                      As Long
    Dim warnings                    As Long
    Dim lineWeight                  As Long
    Dim lineThickness               As Double
    Dim swNote                      As SldWorks.Note
    Dim swCustProp                  As CustomPropertyManager
    Dim val                         As String
    Dim valout                      As String
    Dim bool                        As Boolean
    Dim Boolstatus                  As Boolean

    
    
    Set swApp = Application.SldWorks
    

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    Set swDraw = swModel
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData

    status = swDraw.ActivateView("Drawing View4")
    status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, False, 0, Nothing, 0)
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    
    
    
    On Error Resume Next
If swView Is Nothing = True Then
swApp.SendMsgToUser2 "Please select a drawing view", swMbWarning, swMbOk
Exit Sub
End If
    
    
    swModel.ViewZoomtofit2
    Set swRootDrawComp = swView.RootDrawingComponent


    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  View = " & swView.Name

    vDrawChildCompArr = swRootDrawComp.GetChildren
    For Each vDrawChildComp In vDrawChildCompArr
        Set swDrawComp = vDrawChildComp

        ' Drawing component selected?
        Debug.Print " Drawing component selected = " & swDrawComp.Select(True, Nothing)

        ' Returns NULL if underlying model is open in a different configuration
        Set swComp = swDrawComp.Component

        If Not Nothing Is swComp Then
            ' Returns NULL if drawing is lightweight
            Set swCompModel = swComp.GetModelDoc2

            Debug.Print "      Component                            = " & swComp.Name2
            Debug.Print "      Configuration                        = " & swComp.ReferencedConfiguration


Set swCustProp = swModelDocExt.CustomPropertyManager("")
Set swCustProp = swCompModel.Extension.CustomPropertyManager("")

bool = swCustProp.Get4("Sparepart", False, val, valout)

Debug.Print "       Value:                              = " & val
Debug.Print "       Evaluated value:                    = " & valout
Debug.Print "       Up-to-date data:                    = " & bool

If valout = "1" Then

Boolstatus = swComp.SelectByID2("Point1@Origin@" + swComp + "@" + swModel, "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)

 Set swNote = swModel.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextPartNumberBOM, "", swBalloonTextCustom, "")


 
End If

            If Not Nothing Is swCompModel Then
                Debug.Print "      File                                 = " & swCompModel.GetPathName
                Debug.Print " "
                
            End If

        End If

    Next

End Sub

WN
WN 2021-04-08

So, I've tweaked it a little bit, and got is sort of working a little bit more...

This is what I've got so far:

Option Explicit

Sub main()

    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swSelData                   As SldWorks.SelectData
    Dim swModelDocExt               As SldWorks.ModelDocExtension
    Dim swView                      As SldWorks.View
    Dim swRootDrawComp              As SldWorks.DrawingComponent
    Dim vDrawChildCompArr           As Variant
    Dim vDrawChildComp              As Variant
    Dim swDrawComp                  As SldWorks.DrawingComponent
    Dim swComp                      As SldWorks.Component2
    Dim swCompModel                 As SldWorks.ModelDoc2
    Dim assemblyDrawing             As String
    Dim status                      As Boolean
    Dim errors                      As Long
    Dim warnings                    As Long
    Dim lineWeight                  As Long
    Dim lineThickness               As Double
    Dim swNote                      As SldWorks.Note
    Dim swCustProp                  As CustomPropertyManager
    Dim val                         As String
    Dim valout                      As String
    Dim bool                        As Boolean

    
    
    Set swApp = Application.SldWorks
    

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    Set swDraw = swModel
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData

    status = swDraw.ActivateView("Drawing View4")
    status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, False, 0, Nothing, 0)
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    
    
    
    On Error Resume Next
If swView Is Nothing = True Then
swApp.SendMsgToUser2 "Please select a drawing view", swMbWarning, swMbOk
Exit Sub
End If
    
    
    swModel.ViewZoomtofit2
    Set swRootDrawComp = swView.RootDrawingComponent


    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  View = " & swView.Name

    vDrawChildCompArr = swRootDrawComp.GetChildren
    For Each vDrawChildComp In vDrawChildCompArr
        Set swDrawComp = vDrawChildComp

        ' Drawing component selected?
        Debug.Print " Drawing component selected = " & swDrawComp.Select(True, Nothing)

        ' Returns NULL if underlying model is open in a different configuration
        Set swComp = swDrawComp.Component

        If Not Nothing Is swComp Then
            ' Returns NULL if drawing is lightweight
            Set swCompModel = swComp.GetModelDoc2

            Debug.Print "      Component                            = " & swComp.Name2
            Debug.Print "      Configuration                        = " & swComp.ReferencedConfiguration


'Set swCustProp = swModelDocExt.CustomPropertyManager("")
Set swCustProp = swCompModel.Extension.CustomPropertyManager("")

bool = swCustProp.Get4("Sparepart", False, val, valout)

Debug.Print "       Value:                              = " & val
Debug.Print "       Evaluated value:                    = " & valout
Debug.Print "       Up-to-date data:                    = " & bool

If valout = "1" Then
    Set swNote = swModel.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextPartNumberBOM, "", swBalloonTextCustom, "")
End If

            If Not Nothing Is swCompModel Then
                Debug.Print "      File                                 = " & swCompModel.GetPathName
                Debug.Print " "
                
            End If

        End If

    Next

End Sub

And it will look like this:

I got the balloons as I want them on the parts that I want them on. But is it possible to attach the arrows to the correct parts?

I don't mind that i have to reposition the text, but with the arrow not pointing to the part, I don't know which part it belongs to.

Also I now get a balloon for every instance of the components. Say a spare part component has 10 instances in this assembly, I will end up with 10 balloons in the exact same spot.

Anybody having any ideas?

Thanks in advance 

comment imagecomment image
WN
WN 2021-04-08

So I added the

Get Components in Drawing View Example (VBA)

Part from the help document.

Now the balloon is automatically inserted on every part.

Can anyone suggest how to look for the custom property?

I have tried an If statement with swCustProp.Get4("Sparepart", False, val, valout)

But no luck there

Maybe this is too difficult for me?

Here is what I've got so far:

Option Explicit

Sub main()

    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swSelData                   As SldWorks.SelectData
    Dim swModelDocExt               As SldWorks.ModelDocExtension
    Dim swView                      As SldWorks.View
    Dim swRootDrawComp              As SldWorks.DrawingComponent
    Dim vDrawChildCompArr           As Variant
    Dim vDrawChildComp              As Variant
    Dim swDrawComp                  As SldWorks.DrawingComponent
    Dim swComp                      As SldWorks.Component2
    Dim swCompModel                 As SldWorks.ModelDoc2
    Dim assemblyDrawing             As String
    Dim status                      As Boolean
    Dim errors                      As Long
    Dim warnings                    As Long
    Dim lineWeight                  As Long
    Dim lineThickness               As Double
    Dim swNote As SldWorks.Note
    Dim boolstatus As Boolean
    Dim Cusprop As String
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
Dim bool As Boolean
    
    

    Set swApp = Application.SldWorks
    

    assemblyDrawing = "D:\Temp\Test assy - kopie\TEST SW-0000963.SLDDRW"
    Set swModel = swApp.OpenDoc6(assemblyDrawing, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
    

    Set swDraw = swModel
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    

    status = swDraw.ActivateView("Drawing View4")
    status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, False, 0, Nothing, 0)
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    swModel.ViewZoomtofit2
    Set swRootDrawComp = swView.RootDrawingComponent

    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  View = " & swView.Name

    vDrawChildCompArr = swRootDrawComp.GetChildren
    For Each vDrawChildComp In vDrawChildCompArr
        Set swDrawComp = vDrawChildComp

        ' Drawing component selected?
        Debug.Print " Drawing component selected = " & swDrawComp.Select(True, Nothing)

        ' Returns NULL if underlying model is open in a different configuration
        Set swComp = swDrawComp.Component

        If Not Nothing Is swComp Then
            ' Returns NULL if drawing is lightweight
            Set swCompModel = swComp.GetModelDoc2

            Debug.Print "      Component                            = " & swComp.Name2
            Debug.Print "      Configuration                        = " & swComp.ReferencedConfiguration

            If Not Nothing Is swCompModel Then
                Debug.Print "      File                                 = " & swCompModel.GetPathName
                Debug.Print " "
                
                'Insert balloon
                Set swNote = swModel.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextPartNumberBOM, "", 0, "")

            End If

        End If

    Next

End Sub

WN
WN 2021-04-08

This is the code i use to insert the balloon on the selected part.

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swSelComp As SldWorks.Component2
    Dim sCompName As String
    Dim swNote As SldWorks.Note
    Dim swAnn As SldWorks.Annotation
    Dim bRet As Boolean
    

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swNote = swModel.InsertBOMBalloon2(swBS_Underline, swBF_Tightest, swBalloonTextPartNumberBOM, "", swBalloonTextCustom, "")

End Sub

Now I would need a code that somehow cycles through all the parts in the first view and check for the " sparepart"  property

WN
WN 2021-04-08

Deepak,

Thank you for your reply,

As I only have the 'insert balloon' command in the macro (so far), I did not think it would do any good to post it with my question

I will see if I can post it next week, as I'm not at the office for a few days.

GG
GG 2025-01-29

Hi, I also have the same question. are you able to fix the Macro ? 

RK
RK 2025-07-26

Can you share me the code ?