Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Anonymous
in reply to: MechMachineMan

Here is my final code. Its functioning correctly but takes a long time to process. That's due to my large array of properties to search for in every part, even though most of my documents only have 2 or 3 of these properties in them.

 

Any suggestions on how to make it more efficient?

 

Sub Main ()
'define the file to create/open
Dim oDoc As Document = ThisApplication.ActiveDocument

If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub

Dim oProjectMgr As DesignProjectManager
oProjectMgr = ThisApplication.DesignProjectManager

' Get the active project
Dim oProject As DesignProject
oProject = oProjectMgr.ActiveDesignProject

Dim oLibraryPaths As ProjectPaths
oLibraryPaths = oProject.LibraryPaths

' look at all library paths
Dim oLibraryPath As String = oProject.LibraryPaths.Item(1).Path

Dim oADO As Inventor.ComponentOccurrences = oDoc.ComponentDefinition.Occurrences

MyOperations = New String(){"SAW_SET","SAW_RUN","CNCF_SET","CNCF_RUN","SS_SET","SS_RUN","PUNCH_SET","PUNCH_RUN","FORM_SET","FORM_RUN","WELD_SET","WELD_RUN","CNCC_SET","CNCC_RUN","JOINERY_SET","JOINERY_RUN","FASSY_SET","FASSY_RUN","SANDING_RUN","PCOAT_SET","PCOAT_RUN","COMP_SET","COMP_RUN","GLASS_SET","GLASS_RUN","BPG_SET","BPG_RUN","BOND_SET","BOND_RUN","WPAINT_SET","WPAINT_RUN","PANFIN_SET","PANFIN_RUN","CASSY_SET","CASSY_RUN","PACK_SET","PACK_RUN"}
For Each Operation In MyOperations
OpName = Left(Operation,Len(Operation)-4) Try iProperties.Value(OpName & ":1","Custom", "QTY") = 0 Catch MsgBox(OpName & ":1 does not exist.") Exit Sub End Try Next For Each aDoc As Document In oDoc.AllReferencedDocuments If aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oAssmDoc As AssemblyDocument = aDoc oDef = oAssmDoc.ComponentDefinition ElseIf aDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then Dim oPartDoc As PartDocument = aDoc oDef = oPartDoc.ComponentDefinition End If If oDef.BOMStructure <> BOMStructureEnum.kPhantomBOMStructure And Not aDoc.FullFileName.Contains(oLibraryPath) 'MsgBox(aDoc.FullFileName) Dim Amount As Integer = oADO.AllReferencedOccurrences(aDoc).Count For Each Operation In MyOperations OpQTY = 0 OpQTY = collectQuantity(aDoc,Amount,Operation) 'MsgBox(OP_QTY)
OpName = Left(Operation,Len(Operation)-4) iProperties.Value(OpName & ":1","Custom", "QTY") += OpQTY Next End If Next End Sub Function collectQuantity (aDoc As Document, Amount As Integer,Op As String) As Double Dim oPropsets As PropertySets = aDoc.PropertySets Dim oCustomPropSet As PropertySet = oPropsets.Item("User Defined Properties") Dim Op_Qty As Double Try Op_Qty = oCustomPropSet(Op).Value * Amount 'MessageBox.Show(Op & "---" & Op_Qty,aDoc.DisplayName) Catch Op_Qty = 0 End Try Return Op_Qty End Function