AnsweredAssumed Answered

Part macro doesn't work in assembly editing

Question asked by Tyler Folger on May 10, 2019
Latest reply on May 13, 2019 by Tyler Folger

Hello. I've written a macro to supplement the custom properties builder that modifies a template part according to user input. But I've noticed that it doesn't work as expected when I try to edit the part in an assembly and run the macro from there (it doesn't perform any of the changes and triggers the "Base color not yet supported" message I've built into the program. I assume it has something to do with what SW is considering the active document, but I'm not sure. Is there an easy fix for this?

 

    'Declare Variables
    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim swFeatureManager    As SldWorks.FeatureManager
    Dim swModelDocExt       As SldWorks.ModelDocExtension
    Dim swCustProp          As SldWorks.CustomPropertyManager
    Dim swSelMgr            As SldWorks.SelectionMgr
    Dim swFace              As SldWorks.Face2
    Dim WireSize            As String
    Dim WireSelectState     As String
    Dim UserSizeSelection   As String
    Dim swEquationMgr       As SldWorks.EquationMgr
    Dim ValOut              As String
    Dim PropName            As Variant
    Dim IDNewValue          As Double
    Dim ODNewValue          As Double
    Dim BaseColor           As String
    Dim StripeColor         As String
    Dim ConfigSelect        As String
    Dim ConfigStatus        As Boolean
    Dim AppearanceFolder    As String
   
Sub main()

    'Set Variables
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelDocExt = swModel.Extension
    Set swFeatureManager = swModel.FeatureManager
    Set swCustProp = swModel.Extension.CustomPropertyManager("")
    Set swSelMgr = swModel.SelectionManager
    Set swEquationMgr = swModel.GetEquationMgr
    WireSelectState = swCustProp.Get("Wire_Select_State")
    BaseColor = swCustProp.Get("Wire_Color")
    AppearanceFolder = "V:\AAA\AAA SolidWorks Templates\Antaya Appearances\"
   
    'Set OD & ID Global Variables Based on WireSize
    Select Case WireSelectState
   
        'For Selection by Part Number
        Case "0"
        MsgBox "Part Number selection not yet supported"
       
        'For AWG Selection
        Case "1"
            WireSize = swCustProp.Get("Size_AWG")
            Select Case WireSize
               
                'Each case relates to a wire guage provided by the user in the "Size_AWG" custom property
                Case "10"
                'Set ID 2.59
                'Set OD
                IDNewValue = 2.59
                ODNewValue = 4.59
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "11"
                'Set ID 2.3
                'Set OD
                IDNewValue = 2.3
                ODNewValue = 4
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "12"
                'Set ID 2.050
                'Set OD
                IDNewValue = 2.05
                ODNewValue = 3.5
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "13"
                'Set ID 1.830
                'Set OD
                IDNewValue = 1.83
                ODNewValue = 3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "14"
                'Set ID 1.630
                'Set OD
                IDNewValue = 1.63
                ODNewValue = 3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "15"
                'Set ID 1.450
                'Set OD
                IDNewValue = 1.45
                ODNewValue = 2.8
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "16"
                'Set ID 1.290
                'Set OD
                IDNewValue = 1.29
                ODNewValue = 2.5
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "17"
                'Set ID 1.150
                'Set OD
                IDNewValue = 1.15
                ODNewValue = 2.3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "18"
                'Set ID 1.020
                'Set OD
                IDNewValue = 1.02
                ODNewValue = 2.2
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "19"
                'Set ID .910
                'Set OD
                IDNewValue = 0.91
                ODNewValue = 2
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "20"
                'Set ID .810
                'Set OD
                IDNewValue = 0.81
                ODNewValue = 2
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "21"
                'Set ID .720
                'Set OD
                IDNewValue = 0.72
                ODNewValue = 1.6
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "22"
                'Set ID .640
                'Set OD
                IDNewValue = 0.64
                ODNewValue = 1.4
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "23"
                'Set ID .570
                'Set OD
                IDNewValue = 0.57
                ODNewValue = 1.3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "24"
                'Set ID .510
                'Set OD
                IDNewValue = 0.51
                ODNewValue = 1.2
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case Else
                MsgBox "Unsupported AWG Size"
               
                End Select
       
        'For Metric Selection
        Case "2"
        WireSize = swCustProp.Get("Size_Metric")
            Select Case WireSize
               
                Case "0.25"
                'Set ID .564
                'Set OD
                IDNewValue = 0.564
                ODNewValue = 1.5
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "0.35"
                'Set ID .668
                'Set OD
                IDNewValue = 0.668
                ODNewValue = 1.7
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "0.5"
                'Set ID .798
                'Set OD
                IDNewValue = 0.798
                ODNewValue = 1.9
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "0.75"
                'Set ID .977
                'Set OD
                IDNewValue = 0.977
                ODNewValue = 2.1
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "1"
                'Set ID 1.128
                'Set OD
                IDNewValue = 1.128
                ODNewValue = 2.3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "1.25"
                'Set ID 1.262
                'Set OD
                IDNewValue = 1.262
                ODNewValue = 2.5
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "1.5"
                'Set ID 1.382
                'Set OD
                IDNewValue = 1.382
                ODNewValue = 2.7
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
                               
                Case "1.75"
                'Set ID 1.493
                'Set OD
                IDNewValue = 1.493
                ODNewValue = 2.9
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "2"
                'Set ID 1.596
                'Set OD
                IDNewValue = 1.596
                ODNewValue = 3.1
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "2.25"
                'Set ID 1.693
                'Set OD
                IDNewValue = 1.693
                ODNewValue = 3.3
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "2.5"
                'Set ID 1.784
                'Set OD
                IDNewValue = 1.784
                ODNewValue = 3.5
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "2.75"
                'Set ID 1.871
                'Set OD
                IDNewValue = 1.871
                ODNewValue = 3.7
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "3"
                'Set ID 1.954
                'Set OD
                IDNewValue = 1.954
                ODNewValue = 3.8
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "3.25"
                'Set ID 2.034
                'Set OD
                IDNewValue = 2.034
                ODNewValue = 4
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "3.5"
                'Set ID 2.111
                'Set OD
                IDNewValue = 2.111
                ODNewValue = 4
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "3.75"
                'Set ID 2.185
                'Set OD
                IDNewValue = 2.185
                ODNewValue = 4
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case "4"
                'Set ID 2.257
                'Set OD
                IDNewValue = 2.257
                ODNewValue = 4.25
                Call SetEquationValue(swEquationMgr, "ID", IDNewValue)
                Call SetEquationValue(swEquationMgr, "OD", ODNewValue)
               
                Case Else
                MsgBox "Unsupported Metric Size"
               
                End Select
       
    End Select

    'Set Wire Color based on user selection
    Select Case BaseColor

    Case "Red"
        BaseColor = AppearanceFolder + "Wire Red.p2m"
        Call SetWireColor(BaseColor)

    Case "Green"
        BaseColor = AppearanceFolder + "Wire Green.p2m"
        Call SetWireColor(BaseColor)

    Case "Dark Green"
        BaseColor = AppearanceFolder + "Wire Dark Green.p2m"
        Call SetWireColor(BaseColor)

    Case "Blue"
        BaseColor = AppearanceFolder + "Wire Blue.p2m"
        Call SetWireColor(BaseColor)

    Case "Dark Blue"
        BaseColor = AppearanceFolder + "Wire Dark Blue.p2m"
        Call SetWireColor(BaseColor)

    Case "Violet"
        BaseColor = AppearanceFolder + "Wire Violet.p2m"
        Call SetWireColor(BaseColor)

    Case "Purple"
        BaseColor = AppearanceFolder + "Wire Purple.p2m"
        Call SetWireColor(BaseColor)
       
    Case "Dark Purple"
        BaseColor = AppearanceFolder + "Wire Dark Purple.p2m"
        Call SetWireColor(BaseColor)
       
    Case "Yellow"
        BaseColor = AppearanceFolder + "Wire Yellow.p2m"
        Call SetWireColor(BaseColor)

    Case "Orange"
        BaseColor = AppearanceFolder + "Wire Orange.p2m"
        Call SetWireColor(BaseColor)

    Case "Brown"
        BaseColor = AppearanceFolder + "Wire Brown.p2m"
        Call SetWireColor(BaseColor)

    Case "Gray"
        BaseColor = AppearanceFolder + "Wire Gray.p2m"
        Call SetWireColor(BaseColor)
       
    Case "Dark Gray"
        BaseColor = AppearanceFolder + "Wire Dark Gray.p2m"
        Call SetWireColor(BaseColor)

    Case "Black"
        BaseColor = AppearanceFolder + "Wire Black.p2m"
        Call SetWireColor(BaseColor)

    Case Else
        MsgBox "Specified base color not yet supported"

    End Select
   
'Rebuild
swModel.ForceRebuild3 True

End Sub

'Function that colors the wire according to user selections in "Wire_Color" custom property
Function SetWireColor(AppearanceDirectory As String)

    'Declare Variables
    Dim nDecalIDColor           As Long
    Dim PrimaryColor            As SldWorks.RenderMaterial
    Dim ColorBoolStatus         As Boolean

    'Set Variables
    Set PrimaryColor = swModelDocExt.CreateRenderMaterial(AppearanceDirectory)

    'Clear current model color & apply new colors
    swModel.ClearSelection2 True
    ColorBoolStatus = PrimaryColor.AddEntity(swModel)
    ColorBoolStatus = swModelDocExt.AddRenderMaterial(PrimaryColor, nDecalIDColor)

End Function

'Function that gets the index number of a specified global variable in the equation manager (sourced from https://www.codestack.net/solidworks-api/document/change-global-variable-value/)
Function GetEquationIndexByName(swEquationMgr As SldWorks.EquationMgr, name As String) As Integer
   
    Dim i                        As Integer
    Dim eqName                   As String
   
    GetEquationIndexByName = -1
       
    For i = 0 To swEquationMgr.GetCount - 1
        eqName = Trim(Split(swEquationMgr.Equation(i), "=")(0))
        eqName = Mid(eqName, 2, Len(eqName) - 2) 'removing the "" symbols from the name
       
        If UCase(eqName) = UCase(name) Then
            GetEquationIndexByName = i
            Exit Function
        End If
    Next
End Function

'Function that sets a specified global variable in the equation manager to a specified value (sourced from https://www.codestack.net/solidworks-api/document/change-global-variable-value/)
Function SetEquationValue(swEquationMgr As SldWorks.EquationMgr, name As String, value As Double)

    Dim index As Integer
    index = GetEquationIndexByName(swEquationMgr, name)

    If index <> -1 Then
        swEquationMgr.Equation(index) = """" & name & """=" & value
    Else
        MsgBox "Global variable not found"
    End If

End Function

Outcomes