SOLIDWORKS macro to change configuration specific properties for component in pattern

Edit ArticleEdit Article

Configuration specific properties for the seed component of the sketch driven pattern
Configuration specific properties for the seed component of the sketch driven pattern

This macro example demonstrates how to change the following configuration specific properties using SOLIDWORKS API.

  • Use same configuration as pattern seed component
  • Use named configuration

IAssemblyDoc::CompConfigProperties5 SOLIDWORKS API is used to modify the multiple properties at a time for the selected component.

In the instance component of the pattern (e.g. Sketch Driven Pattern)

Enum SeedCompConfigOpts_e
End Enum

Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc

Sub main()

    Set swApp = Application.SldWorks
    Set swAssy = swApp.ActiveDoc
    Dim swComp As SldWorks.Component2
    Set swComp = swAssy.SelectionManager.GetSelectedObject6(1, -1)
    If Not swComp Is Nothing Then
        'Set use named to 'Config1'
        SetConfigSpecProps swComp, SeedCompConfigOpts_e.UseNamedConfiguration, "Config1"
        'Set use same configuration as pattern seed component
        SetConfigSpecProps swComp, SeedCompConfigOpts_e.UseSameConfigAsPatternSeed
        MsgBox "Please select component"
    End If
End Sub

Sub SetConfigSpecProps(comp As SldWorks.Component2, opts As SeedCompConfigOpts_e, Optional namedConf As String = "")
    comp.Select4 False, Nothing, False
    Dim suppOpts As Long
    Dim solveOpts As Long
    Dim isVisible As Boolean
    Dim exlFromBom As Boolean
    Dim isEnv As Boolean
    Dim useNamedConf As Boolean
    Dim refConfName As String
    suppOpts = comp.GetSuppression()
    solveOpts = comp.Solving
    If solveOpts = -1 Then
        'Note: for part components this will be -1 but the CompConfigProperties5 failse if -1 is passed
        solveOpts = swComponentSolvingOption_e.swComponentRigidSolving
    End If
    isVisible = comp.Visible
    exlFromBom = comp.ExcludeFromBOM
    isEnv = comp.isEnvelope
    Select Case opts
        Case SeedCompConfigOpts_e.UseSameConfigAsPatternSeed
            useNamedConf = False
        Case SeedCompConfigOpts_e.UseNamedConfiguration
            useNamedConf = True
            refConfName = namedConf
    End Select
    swAssy.CompConfigProperties5 suppOpts, solveOpts, isVisible, useNamedConf, refConfName, exlFromBom, isEnv
End Sub

Product of Xarial Product of Xarial