Copy iProperty based on File Name

Copy iProperty based on File Name

donaldleigh
Advocate Advocate
665 Views
5 Replies
Message 1 of 6

Copy iProperty based on File Name

donaldleigh
Advocate
Advocate

Hi all

 

Can I ask for a little help on this please. I can't work this out. taken me 2 days so far. Not even sure if it can be done.

 

I have been using the below code for a while now and it works perfect. (Thanks to MechMachineManI would now like to change it a little.

 

From my Top Level assembly I would like to send either the iProperty "Qty Required A" or "Qty Required B" to each sub assembly and part based on their file name.

 

If the file name of a Sub assembly or part is say "Board Assembly" or "Board Weldment" then all sub assemblies and parts of "Board Assembly" or "Board Weldment" are to read "Qty Required B". This assembly or part could be 2 or 3 deep in the top level assembly.

 

Anything else and it reads "Qty Required A"

 

Please let me know if I have not explained it well.

 

SyntaxEditor Code Snippet

Dim openDoc As Document
openDoc = ThisDoc.Document

If openDoc.DocumentType <> 12291 Then
    MsgBox("You must have a document open to run this rule!")
    Exit Sub
End If


Dim BatchQty As Double
Try
    BatchQty = iProperties.Value("Custom", "Qty Required")
Catch
    MsgBox("No 'Qty Required' iProp found in this doc" & vbLf & vbLf & "Aborting rule!")
    Exit Sub
End Try

 Dim docFile As Document
 For Each docFile In openDoc.AllReferencedDocuments
        Dim docFName As String
        docFName = System.IO.Path.GetFileName(docFile.FullFileName)

        If docFile.IsModifiable = True Then
            Dim assemblyDef As AssemblyComponentDefinition
            assemblyDef = openDoc.ComponentDefinition

            Dim partQty As Double
            partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile).Count


            Try 
                oCheck = iProperties.Value(docFName, "Custom", "Qty Required")
            Catch
                iProperties.Value(docFName, "Custom", "Qty Required") = 0
            End Try

            If partQty <> CDbl(iProperties.Value(docFName, "Custom", "Qty Required")) Then 
                iProperties.Value(docFName, "Custom", "Qty Required") = partQty*BatchQty 
                'MessageBox.Show(docFName, "Title")
            End If
        End If
Next

 

0 Likes
666 Views
5 Replies
Replies (5)
Message 2 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

Hello @donaldleigh,

 

Try below iLogic code.

 

Sub Main()    
    Dim oDoc = ThisDoc.Document
	
    Dim oDef As AssemblyComponentDefinition
    oDef = oDoc.ComponentDefinition
    
    Dim BatchQty_A As Double
    Dim BatchQty_B As Double
    Try
        BatchQty_A = iProperties.Value("Custom", "Qty Required A")
    Catch
        MsgBox ("No 'Qty Required A' iProp found in this doc" & vbLf & vbLf & "Aborting rule!")
        Exit Sub
    End Try
    Try
        BatchQty_B = iProperties.Value("Custom", "Qty Required B")
    Catch
        MsgBox ("No 'Qty Required A' iProp found in this doc" & vbLf & vbLf & "Aborting rule!")
        Exit Sub
    End Try

    Dim PartQty As Double
    Dim occ As ComponentOccurrence
    For Each occ In oDef.Occurrences
        If occ.Name Like "Board Assembly*" Or occ.Name Like "Board Weldment*" Then
            If occ.DefinitionDocumentType = kAssemblyDocumentObject Then
                PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
                Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
                Call GetSubOccurrences(occ.SubOccurrences, oDef, BatchQty_A)
            Else
                PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
                Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
            End If
        Else
            PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
            Call UpdateBatchQty(occ.Name, PartQty, BatchQty_B)
        End If
    Next
End Sub

Private Sub GetSubOccurrences(Occurrences As ComponentOccurrences, oDef As AssemblyComponentDefinition, BatchQty_A As Double)
    ' Iterate through each of the occurrences in the collection provided.
    Dim occ As ComponentOccurrence
    For Each occ In Occurrences
        PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
        Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
        
        If occ.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call GetSubOccurrences(occ.SubOccurrences, oDef, BatchQty_A)
        End If
    Next
End Sub

Sub UpdateBatchQty(occName As String, PartQty As Double, BatchQty As Double)
    Try
        oCheck = iProperties.Value(occName, "Custom", "Qty Required")
    Catch
        iProperties.Value(occName, "Custom", "Qty Required") = 0
    End Try

    If PartQty <> CDbl(iProperties.Value(occName, "Custom", "Qty Required")) Then
        iProperties.Value(occName, "Custom", "Qty Required") = PartQty * BatchQty
    End If
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 6

donaldleigh
Advocate
Advocate

Hi Chandra

 

I have been busy this week with other project and will check this out soon I hope

 

Donald

0 Likes
Message 4 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@donaldleigh,

 

Check the same and let me know if there is any assistance needed.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 6

donaldleigh
Advocate
Advocate

Hi Chandra

 

Thanks for the updated code

 

It works only if the "Board Assembly" or "Board Weldment" are sub assemblies in the assembly where the rule is run

 

Problem is that these are normally 2 deep. See attached.

 

If I run your new code in the Assembly "General Assembly" it all works great but not when run in the top level "General Arrangement" . The code will only be run in the very Top level "General Arrangement".

 

Cheers

Donald

0 Likes
Message 6 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@donaldleigh,

 

Hoping that below iLogic would help now.

 

Sub Main()    
    Dim oDoc = ThisDoc.Document
	
    Dim oDef As AssemblyComponentDefinition
    oDef = oDoc.ComponentDefinition
    
    Dim BatchQty_A As Double
    Dim BatchQty_B As Double
    Try
        BatchQty_A = iProperties.Value("Custom", "Qty Required A")
    Catch
        MsgBox ("No 'Qty Required A' iProp found in this doc" & vbLf & vbLf & "Aborting rule!")
        Exit Sub
    End Try
    Try
        BatchQty_B = iProperties.Value("Custom", "Qty Required B")
    Catch
        MsgBox ("No 'Qty Required A' iProp found in this doc" & vbLf & vbLf & "Aborting rule!")
        Exit Sub
    End Try

    Dim PartQty As Double
    Dim occ As ComponentOccurrence
    For Each occ In oDef.Occurrences
		If occ.SubOccurrences.count = 0 Then
			If occ.Name Like "Board Assembly*" Or occ.Name Like "Board Weldment*" Then
				PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
                Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
			Else 
				PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
            	Call UpdateBatchQty(occ.Name, PartQty, BatchQty_B)
			End If 			
		Else
			TraverseSubOccurrences(occ.SubOccurrences,oDef, BatchQty_A,BatchQty_B)
		End If
    Next
End Sub

Private Sub TraverseSubOccurrences(Occurrences As ComponentOccurrences, oDef As AssemblyComponentDefinition, _
BatchQty_A As Double, BatchQty_B As Double)
	Dim occ As Inventor.ComponentOccurrence 
	For Each occ In Occurrences
		If occ.SubOccurrences.count = 0 Then
			If occ.Name Like "Board Assembly*" Or occ.Name Like "Board Weldment*" Then
				PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
                Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
			Else 
				PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
            	Call UpdateBatchQty(occ.Name, PartQty, BatchQty_B)
			End If	
		Else
			If occ.Name Like "Board Assembly*" Or occ.Name Like "Board Weldment*" Then				
                PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
                Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
                Call GetSubOccurrences(occ.SubOccurrences, oDef, BatchQty_A)            	
			Else 
				TraverseSubOccurrences(occ.SubOccurrences,oDef, BatchQty_A,BatchQty_B)
			End If 			
		End If
	Next
End Sub

Private Sub GetSubOccurrences(Occurrences As ComponentOccurrences, oDef As AssemblyComponentDefinition, BatchQty_A As Double)
    ' Iterate through each of the occurrences in the collection provided.
    Dim occ As ComponentOccurrence
    For Each occ In Occurrences
        PartQty = oDef.Occurrences.AllReferencedOccurrences(occ.Definition.Document).Count
        Call UpdateBatchQty(occ.Name, PartQty, BatchQty_A)
        
        If occ.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call GetSubOccurrences(occ.SubOccurrences, oDef, BatchQty_A)
        End If
    Next
End Sub

Sub UpdateBatchQty(occName As String, PartQty As Double, BatchQty As Double)
    Try
        oCheck = iProperties.Value(occName, "Custom", "Qty Required")
    Catch
        iProperties.Value(occName, "Custom", "Qty Required") = 0
    End Try

    If PartQty <> CDbl(iProperties.Value(occName, "Custom", "Qty Required")) Then
        iProperties.Value(occName, "Custom", "Qty Required") = PartQty * BatchQty
    End If
End Sub

Please feel free to contact if there is any queries,

 

If solves problem, click on "Accept as solution' / give a "Like".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes