Collect values of iProperties from sub components

Collect values of iProperties from sub components

Anonymous
Not applicable
815 Views
4 Replies
Message 1 of 5

Collect values of iProperties from sub components

Anonymous
Not applicable

I would like to loop through all referenced documents of any assembly and collect the values of an array of iProperties and present their totals as an iProperty in the assembly.

 

For example, the iProperties I'm searching through are called PUNCH, FORM, WELD

There are various sub-parts with these iProperties and they all have a numerical value (of type Text)

 

I wish to populate my assembly iProperties (PUNCH, FORM, WELD) with the combined totals of all the sub parts.

 

This is what I've tried so far (its incomplete as still need to make use of the array of iProp names) but it's failing at the first hurdle !! Can't get it to return the value from the function back to the sub. Can anyone help or suggest a more efficient way?

 

Its definitely finding values, I can see when I uncomment the message box in the function that shows the value of FORM 

 

Sub Main ()

Dim oDoc As Document = ThisApplication.ActiveDocument

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

 

 

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

 

 

iProperties.Value("Custom", "FORM") = 0

iProperties.Value("Custom", "PUNCH") = 0

iProperties.Value("Custom", "WELD") = 0

 


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

' MyOperations = New String(){"FORM", "PUNCH", "WELD"}
'
' For Each op in MyOperations
' MsgBox(op)
' Next

If oDef.BOMStructure <> BOMStructureEnum.kPhantomBOMStructure 


      Dim Amount As Integer = oADO.AllReferencedOccurrences(aDoc).Count


      collectQuantity(aDoc,Amount,oLibraryPath)

End If

 

FORM_QTY = FORM_QTY + FORM

Next

 

iProperties.Value("Custom", "FORM") = FORM_QTY

 

End Sub

 

Function collectQuantity (aDoc As Document, Amount As Integer,oLibraryPath As String) As Double


Dim oPropsets As PropertySets = aDoc.PropertySets

Dim oCustomPropSet As PropertySet = oPropsets.Item("User Defined Properties")


Dim FORM As Double

Try
FORM =  oCustomPropSet("FORM").Value * Amount

'MsgBox(FORM)
Catch
FORM = 0
End Try

Return FORM

End Function

0 Likes
Accepted solutions (1)
816 Views
4 Replies
Replies (4)
Message 2 of 5

MechMachineMan
Advisor
Advisor
Accepted solution

 

I think your issue was that you weren't returning FORM from your function to a variable.

 

Sub Main ()

	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
	 
	Dim oADO As Inventor.ComponentOccurrences = oDoc.ComponentDefinition.Occurrences	 
	iProperties.Value("Custom", "FORM") = 0
	iProperties.Value("Custom", "PUNCH") = 0
	iProperties.Value("Custom", "WELD") = 0
	
	For Each aDoc As Document In oDoc.AllReferencedDocuments
		
	 	Form = 0
		
		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

	' MyOperations = New String(){"FORM", "PUNCH", "WELD"}
	'
	' For Each op in MyOperations
	' MsgBox(op)
	' Next

		If oDef.BOMStructure <> BOMStructureEnum.kPhantomBOMStructure 
	      Dim Amount As Integer = oADO.AllReferencedOccurrences(aDoc).Count
	      Form = collectQuantity(aDoc,Amount,oLibraryPath)
		End If
		
		FORM_QTY = FORM_QTY + Form
	Next
	
	iProperties.Value("Custom", "FORM") = FORM_QTY
 
End Sub
 
Function collectQuantity (aDoc As Document, Amount As Integer,oLibraryPath As String) As Double

	Dim oPropsets As PropertySets = aDoc.PropertySets
	Dim oCustomPropSet As PropertySet = oPropsets.Item("User Defined Properties")
	Dim FORM As Double

	Try
		FORM =  oCustomPropSet("FORM").Value * Amount
		'MsgBox(FORM)
	Catch
		FORM = 0
	End Try

	Return FORM

End Function

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 5

Anonymous
Not applicable

@MechMachineMan thanks for picking that up. I'll give it a shot tomorrow when I'm back at my PC. I was originally trying it in a Sub instead of a function but was not having any joy. Should I be able to set the value of a variable in a Sub & have the Sub Main continue with that value?

0 Likes
Message 4 of 5

MechMachineMan
Advisor
Advisor

Only if you designate the variable as ByRef in the function declaration.

 

 

Sub Main()
     x = 2
     Call DoMath(x)
    
     MsgBox(x)
     'x = 2 returns 2 if ByVal
     'x = 2 returns 4 if ByRef

End Sub

'Sub DoMath(ByVal variable As integer)
Sub DoMath(ByRef variable As integer)
    variable = variable + variable
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 5

Anonymous
Not applicable

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

 

0 Likes