Need help iterating through Configurations... sort of

Programming and macros
User avatar
loeb
Posts: 69
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

Need help iterating through Configurations... sort of

Unread post by loeb »

I'm trying to get a macro that hides all primary planes to work on all configurations. It works on the current configuration. It even iterates through all the other configurations and successfully prints their names. But the command that turns off the primary planes is only working on the 'first' configuration.

I tried to iterate through the configurations two ways, one way just cycling through the names, and the other way using the configuration manager. Here are my two attempts.

Thank you for any advice.

Attempt 1:

Code: Select all

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swConfMgr                   As SldWorks.ConfigurationManager
    Dim swConfig                    As SldWorks.Configuration
    Dim vConfigName                 As Variant
    Dim vConfigNameArr              As Variant
    Dim swFeat                      As SldWorks.Feature
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swConfMgr = swModel.ConfigurationManager
    Set swConfig = swConfMgr.ActiveConfiguration
    vConfigNameArr = swModel.GetConfigurationNames
    For Each vConfigName In vConfigNameArr
       Set swConfig = swModel.GetConfigurationByName(vConfigName)
        Debug.Print "Name: ", swConfig.Name
        Set swFeat = swModel.FirstFeature
        While Not swFeat Is Nothing
            If swFeat.GetTypeName2 = "RefPlane" Then
                swFeat.Select2 False, -1
                swModel.BlankRefGeom
            End If
            Set swFeat = swFeat.GetNextFeature
        Wend
    Next vConfigName
End Sub
 
Attempt 2:

Code: Select all

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim params                      As Variant
    Dim vName                       As Variant
    Dim CfgName                     As String
    Dim ThisConfig                  As Configuration
    Dim swFeat                      As SldWorks.Feature
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    params = swModel.GetConfigurationNames
    For Each vName In params
        CfgName = vName
        Set ThisConfig = swModel.GetConfigurationByName(CfgName)
        Debug.Print "Name: ", ThisConfig.Name
        Set swFeat = swModel.FirstFeature
        While Not swFeat Is Nothing
            If swFeat.GetTypeName2 = "RefPlane" Then
                swFeat.Select2 False, -1
                swModel.BlankRefGeom
            End If
            Set swFeat = swFeat.GetNextFeature
        Wend
    Next vName
End Sub
by AlexB » Fri Mar 11, 2022 8:18 am
The only thing that I have in my similar macro is that I am calling to show the configuration before I begin trying to hide the geometry. In your code, this can be the first line in your For Each loop.

Code: Select all

swModel.ShowConfiguration2 vConfigName
Another thing to note, to speed things up you can check to see if the thing you want to hide is visible and unsuppressed before calling the BlankRefGeom function. I noticed a huge speed improvement in my parts with large feature trees when I run mine.

This is what I'm using in my code.

Code: Select all

If curFeature.Visible = swVisibilityState_e.swVisibilityStateShown And Not curFeature.IsSuppressed Then
Go to full post
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Need help iterating through Configurations... sort of

Unread post by gupta9665 »

Both codes works fine for me i.e. hiding al the planes.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
AlexB
Posts: 493
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 27
x 265
x 440

Re: Need help iterating through Configurations... sort of

Unread post by AlexB »

The only thing that I have in my similar macro is that I am calling to show the configuration before I begin trying to hide the geometry. In your code, this can be the first line in your For Each loop.

Code: Select all

swModel.ShowConfiguration2 vConfigName
Another thing to note, to speed things up you can check to see if the thing you want to hide is visible and unsuppressed before calling the BlankRefGeom function. I noticed a huge speed improvement in my parts with large feature trees when I run mine.

This is what I'm using in my code.

Code: Select all

If curFeature.Visible = swVisibilityState_e.swVisibilityStateShown And Not curFeature.IsSuppressed Then
User avatar
loeb
Posts: 69
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

Re: Need help iterating through Configurations... sort of

Unread post by loeb »

AlexB wrote: Fri Mar 11, 2022 8:18 am The only thing that I have in my similar macro is that I am calling to show the configuration before I begin trying to hide the geometry. In your code, this can be the first line in your For Each loop.

Code: Select all

swModel.ShowConfiguration2 vConfigName
Another thing to note, to speed things up you can check to see if the thing you want to hide is visible and unsuppressed before calling the BlankRefGeom function. I noticed a huge speed improvement in my parts with large feature trees when I run mine.

This is what I'm using in my code.

Code: Select all

If curFeature.Visible = swVisibilityState_e.swVisibilityStateShown And Not curFeature.IsSuppressed Then
AlexB, Thank You, That was it. Calling ShowConfiguration2 was needed to make the config active so that it could be operated on. What an unfortunate name: "Show".

Thanks for the tip on speed. The macro that I'm working on does a lot more than I posted here, so this will help speed things up a lot. I had assumed that going through the steps of checking the state would take more time than just changing the state, but I guess not.

I also, just ran a test and It looks like I don't need to use Configuration Manager for this code to work.

Thanks for the advice!
User avatar
loeb
Posts: 69
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 39
x 10

Re: Need help iterating through Configurations... sort of

Unread post by loeb »

gupta9665 wrote: Fri Mar 11, 2022 2:40 am Both codes works fine for me i.e. hiding al the planes.
Deepak, it works on the active config, but none of the others. Using ShowConfiguration2 solved the issue.
Post Reply