Non-existent cut-list members found while iterating over

Programming and macros
makos
Posts: 3
Joined: Thu Jul 04, 2024 2:40 pm
Answers: 0
Location: Poland
x 1

Non-existent cut-list members found while iterating over

Unread post by makos »

Hi,
I made a macro that iterates over all features of a part file, when it finds the cut-list folder it goes over it and changes a custom property I want. That property is an ID number I use internally. Problem is, I often have a part with, let's say, 8 bodies. But the ID goes to 10 or 12 or some other random number and assigns ID's to bodies that do not exist in the part file? Here's my code:

Code: Select all

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim featMgr As SldWorks.FeatureManager
Dim curMod As SldWorks.ModelDoc2 'currently traversed Model
Dim swBodyFolder As BodyFolder 'bodyfolder = top-level cut-list folder
Dim vComponents As Variant 'all components in the assy
Dim id As Integer
Dim swFeat As Feature

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set curMod = swApp.ActiveDoc
    Set featMgr = swModel.FeatureManager
    
    If (swModel.GetType <> swDocPART) Then
        MsgBox "Please open a part file", vbCritical, "Error"
        Exit Sub
    End If
    
    id = CInt(InputBox("Start ID:", "ID Macro", "1"))
    'id = 1
        
    If Not curMod Is Nothing Then
        Set swFeat = curMod.FirstFeature
    
        Do While Not swFeat Is Nothing
            If Not swFeat.IsSuppressed Then
                'Debug.Print swFeat.Name
                If swFeat.GetTypeName2 = "SolidBodyFolder" Then
                    Set swBodyFolder = swFeat.GetSpecificFeature2
                    swBodyFolder.UpdateCutList
                ElseIf swFeat.GetTypeName2 = "CutListFolder" Then
                    Dim vProperties As Long
                    Dim vPropnames As Variant
                    Dim vProptypes As Variant
                    Dim vPropvalues As Variant
                    Dim vResolved As Variant
                    
                    vProperties = swFeat.CustomPropertyManager.GetAll2(vPropnames, vProptypes, vPropvalues, vResolved)
                    If vProperties <> 0 Then
                        'Debug.Print "Number props: " & vProperties
                        Dim j As Integer
                        For j = 0 To vProperties - 1
                            If vPropnames(j) = "Opis" Then
                                Debug.Print "Opis: " & vPropvalues(j)
                            End If
                            
                            If vPropnames(j) = "DŁUGOŚĆ" Then
                                Debug.Print "Długość: " & vPropvalues(j)
                            End If
                            
                            If vPropnames(j) = "id" Then
                                Debug.Print "id: " & vPropvalues(j)
                                If vPropvalues(j) >= id Then
                                    ' Debug.Print "Found id value: " & vPropvalues(j)
                                    ' Debug.Print "Found id, assigning " & id
                                    swFeat.CustomPropertyManager.LinkProperty "id", False
                                    swFeat.CustomPropertyManager.Set2 vPropnames(j), CStr(id)
                                    id = id + 1
                                End If
                            End If
                        Next j
                        Debug.Print "============================================================="
                    End If
                End If
            End If
            
            Set swFeat = swFeat.GetNextFeature
            
        Loop
    End If
    'Debug.Print "Last ID assigned: " & id - 1
    MsgBox "Last ID: " & id - 1, vbOKOnly, "ID macro"
End Sub
For context, here's my cut-list in Solidworks:
image.png
image.png (9.27 KiB) Viewed 333 times
Here's the ID's assigned by the macro, as you can see they are in wrong order, with some missing:
image.png
image.png (8.41 KiB) Viewed 333 times
And here's my Immediate output, showing that it finds erroneous members that do not exist and assign ID's to them:

Code: Select all

(...)
=============================================================
Długość: "LENGTH@@@RP 40x20x2<1>@Powtarzalny front.SLDPRT"
Opis: RP 40x20x2
id: 3
=============================================================
Długość: "LENGTH@@@PL 40x5<1>@Powtarzalny front.SLDPRT"
Opis: PL 40x5
id: 4
=============================================================
(...)
There's not even a "PL 40x5" (flat plate) weldment member in the whole part feature tree. Where it comes from?

Info: "Długość" is the length property, "Opis" is the description. Any idea what happens here?

Edit: checking for body count seems to have solved the issue. Thanks!
by gupta9665 » Wed Sep 25, 2024 11:51 am
This is a issue with API, as it would count the bodies even if you have deleted (using delete feature) them. So you would need to check the body count for each cut list, and if it is great than 0, then only add the ID value.
Go to full post
User avatar
Eddy Alleman
Posts: 50
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 80
x 89
Contact:

Re: Non-existent cut-list members found while iterating over

Unread post by Eddy Alleman »

Hi Makos,

What SolidWorks version are you on?

Are you using configurations?
User avatar
gupta9665
Posts: 404
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 424
x 444

Re: Non-existent cut-list members found while iterating over

Unread post by gupta9665 »

This is a issue with API, as it would count the bodies even if you have deleted (using delete feature) them. So you would need to check the body count for each cut list, and if it is great than 0, then only add the ID value.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
makos
Posts: 3
Joined: Thu Jul 04, 2024 2:40 pm
Answers: 0
Location: Poland
x 1

Re: Non-existent cut-list members found while iterating over

Unread post by makos »

Eddy Alleman wrote: Wed Sep 25, 2024 10:23 am Hi Makos,

What SolidWorks version are you on?

Are you using configurations?
SW2020, I'm using configurations in weldment profiles - single .sldlfp file with multiple configurations using a design table. In the part I'm having problems (and others), no configurations.
gupta9665 wrote: Wed Sep 25, 2024 11:51 am This is a issue with API, as it would count the bodies even if you have deleted (using delete feature) them. So you would need to check the body count for each cut list, and if it is great than 0, then only add the ID value.
I didn't use the delete feature, but checking for a body count is an idea that I will implement and test. Thank you.
User avatar
gupta9665
Posts: 404
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 424
x 444

Re: Non-existent cut-list members found while iterating over

Unread post by gupta9665 »

I always reply on body count as SW API will count for hidden items as well.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
Post Reply