MACRO LIBRARY

Referenced configuration example

Free public access. No account or subscription required.

Files

Create configurations at component level and reference them at assembly level Associated model(s)

Code

Module: mMain

'Demonstrates how to add configurations in a component and have
'    the assembly reference those configurations. To make the macro
'    more interesting, some dimensions in the component are modified.
    
'Preconditions:
'   1. An assembly is open and a component is selected
'   2. DIMENSION_NAME constant refers to an dimension in the
'      selected component.
'   3. Optionally, DIMENSION_MIN_VAL and DIMENSION_VAL_INCREMENT
'      can be used to control the dimension values.
    
'Results:
'   1. Five configurations are added to component and assembly,
'      named 1, 2, 3, 4, and 5.
'   2. The specified compoenent dimension is modified according to
'      the values specified in the constants.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const DIMENSION_NAME As String = "D2@Sketch1"
Const DIMENSION_MIN_VAL As Double = 3
Const DIMENSION_VAL_INCREMENT As Double = 1
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swComp As SldWorks.Component2
    Dim swCompModel As SldWorks.ModelDoc2
    Dim i As Integer
    Dim dblIncrement As Double
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swComp = swSelMgr.GetSelectedObject6(1, -1)
    Set swCompModel = swComp.GetModelDoc2
    dblIncrement = 0
    
    For i = 1 To 5
        
        'Create new configuration at part level
        swModel.AddConfiguration3 CStr(i), Empty, Empty, Empty
        
        'Create new config at assembly level
        swCompModel.AddConfiguration3 CStr(i), Empty, Empty, _
            swConfigOption_LinkToParent
        
        'Reference new part config
        swModel.Extension.SelectByID2 swComp.GetSelectByIDString, _
            "COMPONENT", 0, 0, 0, False, 0, Nothing, 0
        Set swComp = swSelMgr.GetSelectedObject6(1, -1)
        swComp.ReferencedConfiguration = CStr(i)
        
        'Modify dimensions in that config
        dblIncrement = dblIncrement + DIMENSION_VAL_INCREMENT
        ModifyDimension swCompModel, DIMENSION_NAME, _
            DIMENSION_MIN_VAL + dblIncrement, i
    Next i
    
    MsgBox "Processing complete."
End Sub
    
Private Sub ModifyDimension(swModel As SldWorks.ModelDoc2, _
    strDimName, dblDimVal, strConfig)
    
    Dim strConfigNames(0) As String
    strConfigNames(0) = strConfig
    
    Dim swDim As SldWorks.Dimension
    Set swDim = swModel.Parameter(strDimName)
    
    If swDim Is Nothing Then
        MsgBox "Dimension not found: " & strDimName
    Else
        swDim.SetValue3 dblDimVal, swSetValue_InSpecificConfigurations, _
            strConfigNames
    End If
End Sub