Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Creating an Array of Parts Missing Material

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
0
0
US
I have the below code and want to update it to create a list of all the parts missing material instead of a message box listing only one part at a time.

The main issue with tis code is that it pops the message box even with repeat part numbers, I would like to modify it so it only shows each part missing material once.

My guess is I need to create an Array to list each part missing Material and then each time a part is found missing material, it checks it to the array.
But I couldn't figure out a good way to do that.

The second half of that is then, how do I list all the values in the Array in a Msgbox?

Code:
Sub CATMain()

Dim TheSPAWorkbench As Workbench
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
    
Set Inertias = TheSPAWorkbench.Inertias
Set Document = CATIA.ActiveDocument

Dim Selection1 As Selection
Set selection1 = Document.Selection
selection1.Search "(CATProductSearch.Part),all"

for k = 1 to Selection1.Count

Set Body = Selection1.Item(k).Value

Set inertia = Inertias.Add (Body)

if Inertia.Density <> 1000 Then
'Do Nothing

Else 

MsgBox Selection1.Item(k).LeafProduct.PartNumber & " Has No Material Applied", vbInformation, "No Material Has Been Applied"
End If
Next
MsgBox "If There Were No Previous Notifications, All Details Have Material Applied To Them."

End Sub
 
Replies continue below

Recommended for you

or use a dictionary, where you put the partnumber as a key. then check if next part has already been added to the dictionary.

regards,
LWolf
 
Code:
Sub CATMain()

Dim TheSPAWorkbench As Workbench
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
    
Set Inertias = TheSPAWorkbench.Inertias
Set Document = CATIA.ActiveDocument

Dim Selection1 As Selection
Set Selection1 = Document.Selection
Selection1.Search "(CATProductSearch.Part),all"

Dim DictionaryNoMaterial
Set DictionaryNoMaterial = CreateObject("scripting.dictionary")

For k = 1 To Selection1.Count
    Set Body = Selection1.item(k).Value
    Set Inertia = Inertias.Add(Body)

    If Inertia.Density <> 1000 Then
        'Do Nothing

    Else
        If DictionaryNoMaterial.exists(Selection1.item(k).LeafProduct.PartNumber) = False Then
            DictionaryNoMaterial.Add Selection1.item(k).LeafProduct.PartNumber, aPart
        End If
        'MsgBox Selection1.item(k).LeafProduct.PartNumber & " Has No Material Applied", vbInformation, "No Material Has Been Applied"
    End If
Next

'MsgBox "If There Were No Previous Notifications, All Details Have Material Applied To Them."
MsgBox Join(DictionaryNoMaterial.keys, vbCrLf)

End Sub

regards,
LWolf
 
&LWolf, This worked almost perfectly, I had to add a modified to only pop up the final message box if the Dictionary count was greater than 0.
Without that, the message box was just blank.

I appreciate the help, I have never used Arrays or the Dictionary Functions.

&Azzazil, Can you show me how you would modify the code to create an Array and then reference back to it?
I tired doing that before I made the initial post and I couldn't get it to work.

Here is my updated code:
Code:
Sub CATMain()

Dim TheSPAWorkbench As Workbench
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
    
Set Inertias = TheSPAWorkbench.Inertias
Set Document = CATIA.ActiveDocument

Dim Selection1 As Selection
Set Selection1 = Document.Selection
Selection1.Search "(CATProductSearch.Part),all"

Dim DictionaryNoMaterial
Set DictionaryNoMaterial = CreateObject("scripting.dictionary")

For k = 1 To Selection1.Count
    Set Body = Selection1.item(k).Value
    Set Inertia = Inertias.Add(Body)

    If Inertia.Density <> 1000 Then
        'Do Nothing

    Else
        If DictionaryNoMaterial.exists(Selection1.item(k).LeafProduct.PartNumber) = False Then
            DictionaryNoMaterial.Add Selection1.item(k).LeafProduct.PartNumber, aPart
        End If
        'MsgBox Selection1.item(k).LeafProduct.PartNumber & " Has No Material Applied", vbInformation, "No Material Has Been Applied"
    End If
Next

'Msgbox DictionaryNoMaterial.Count

If DictionaryNoMaterial.Count = 1 Then

MsgBox 	"There Is " & DictionaryNoMaterial.Count & " Detail With No" & vbCrLf &_
		"Material Applied To It:"  & vbCrLf & vbCrLf &_
		Join(DictionaryNoMaterial.keys, vbCrLf)
		
ElseIf DictionaryNoMaterial.Count > 1 Then

MsgBox 	"There Are " & DictionaryNoMaterial.Count & " Different Details" & vbCrLf &_
		"With No Material Applied To Them:"  & vbCrLf & vbCrLf &_
		Join(DictionaryNoMaterial.keys, vbCrLf)
		
		Else

MsgBox "All Details Have Material Applied To Them."

End If

End Sub
 
Status
Not open for further replies.
Back
Top