Add up a custom iProperty called hours in sub assemlies or parts.

Add up a custom iProperty called hours in sub assemlies or parts.

tracey
Enthusiast Enthusiast
539 Views
5 Replies
Message 1 of 6

Add up a custom iProperty called hours in sub assemlies or parts.

tracey
Enthusiast
Enthusiast

Hi, I have created a custom parameter called "Hours" in some parts and sub assemblies and populated the value.  I have used this type of rule before but it returns a value of 0 even though some of my parts and sub assemblies have the property added.  I am perplexed.   Anything obviously wrong? 

 

Sub Main()

'Inventor.ComponentOccurrences'.

    ' Get the active assembly.

    Dim oAsmDoc As AssemblyDocument

    oAsmDoc = ThisApplication.ActiveDocument

   

    Dim oHours As Double

    Dim oCustomProps As PropertySet = oAsmDoc.PropertySets.Item("User Defined Properties")

    Dim oProperty As Inventor.Property

     

'Look for property "Total_Hours" this is only required at the top level.

        Try

'    'try to access the property Total_Hours

            oProperty = oCustomProps.Item("Total_Hours")

        Catch

'    'If failure, assume prop doesnt exist and create it

            oProperty = oCustomProps.Add("", "Total_Hours")

        End Try

        'Call the function that does the recursion.

    oHours = TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences)

'   

    oProperty.Value = CDbl(oHours)

MsgBox(oHours)

'   

    End Sub

 

 

Private Function TraverseAssembly(ByVal Occurrences As ComponentOccurrences) As Double

   

    Dim oHours As Double

       

       For Each oOcc As ComponentOccurrence In Occurrences

        Try

            If oOcc.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Or

                oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then

            Else

      

         Dim oDoc As Document

        oDoc = oOcc.Definition.Document

       

         Dim oPropSet As PropertySet

         oPropSet = oDoc.PropertySets.Item("Design Tracking Properties")

        

        ' Get the total of this occurrence

        oHours += oPropSet.Item("Hours").Value

 

         End If

 

        ' Check to see if this occurrence represents a subassembly

        ' and recursively call this function to traverse through it.

        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then

           oHours += TraverseAssembly(oOcc.Definition.Occurrences)

        End If   

    Catch

    End Try

 

    Next

    iProperties.Value(Name, "Custom", "Total_Hours") = oHours

    Return oHours

 

End Function

'Thank you for your help!! 

Accepted solutions (1)
540 Views
5 Replies
Replies (5)
Message 2 of 6

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @tracey ,

See code below

Sub Main()

'Inventor.ComponentOccurrences'.

    ' Get the active assembly.

    Dim oAsmDoc As AssemblyDocument

    oAsmDoc = ThisApplication.ActiveDocument

   

    Dim oHours As Double

    Dim oCustomProps As PropertySet = oAsmDoc.PropertySets.Item("User Defined Properties")

    Dim oProperty As Inventor.Property

     

'Look for property "Total_Hours" this is only required at the top level.

        Try

'    'try to access the property Total_Hours

            oProperty = oCustomProps.Item("Total_Hours")

        Catch

'    'If failure, assume prop doesnt exist and create it

            oProperty = oCustomProps.Add("", "Total_Hours")

        End Try

        'Call the function that does the recursion.

    oHours = TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences)

'   

    oProperty.Value = CDbl(oHours)

MsgBox(oHours)

'   

    End Sub

 

 

Private Function TraverseAssembly(ByVal Occurrences As ComponentOccurrences) As Double

   

    Dim oHours As Double

       

       For Each oOcc As ComponentOccurrence In Occurrences

        Try

            If oOcc.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Or _
				oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then

            Else

      

         Dim oDoc As Document

        oDoc = oOcc.Definition.Document

       

         Dim oPropSet As PropertySet
		 
		 '::::::::::::THIS IS IT:::::::::::::::::::::::::::::::::::::::::

         oPropSet = oDoc.PropertySets.Item("User Defined Properties") 'Changed to User defined props
		 ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        

        ' Get the total of this occurrence
	

        oHours += oPropSet.Item("Hours").Value

 		

         End If

 

        ' Check to see if this occurrence represents a subassembly

        ' and recursively call this function to traverse through it.

        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then

           oHours += TraverseAssembly(oOcc.Definition.Occurrences)

        End If   

    Catch
		
    End Try

 

    Next

    iProperties.Value(Name, "Custom", "Total_Hours") = oHours

    Return oHours

 

End Function

 

Message 3 of 6

AlexFielder
Advisor
Advisor

Oh @tracey  - and there was me thinking I was the only person who needed to account for the time taken working on an Inventor assembly! Welcome to the "Management wants to know what's been taking so long" club!


Here's my old rule that counted Features and Parameters in parts. And Features, Occurrences, Parameters and Constraints in Assemblies:

https://github.com/AlexFielder/iLogic/blob/master/Modelling/RunFeatureCountonAssembly.iLogicVb (Linked here in case I update it)

Imports Inventor
Imports System.IO
Public Sub Main
If TypeOf ThisDoc.Document Is PartDocument Then
PartFeatureCount(ThisDoc)
Else
RunFeatureCount(ThisDoc.Document)
End If
MessageBox.Show("Done!")
End Sub

Public Sub RunFeatureCount(ByVal oDoc as Inventor.Document)
	Dim oAssy As inventor.AssemblyDocument
	Dim oSubDoc as Inventor.Document
	If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
		oAssy = CType(oDoc,AssemblyDocument)
		AssemblyFeatureCount(oAssy)
		For Each oComp In oAssy.ReferencedDocuments
			'oSubDoc = CType(oComp.Definition.Document,Document)
			'MessageBox.Show(oSubDoc.File.FullFileName)
			If oComp.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
				'run FeatureCount and then call RunFeatureCount to recurse the assembly structure
				'iLogicVb.RunExternalRule("FEATURECOUNT")
				FeatureCount(oComp)
				RunFeatureCount(oComp)
			Else
				'run FeatureCount
				'iLogicVb.RunExternalRule("FEATURECOUNT")
				FeatureCount(oComp)
			End If
		Next
	ElseIf oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
		FeatureCount(oDoc)
	End If
End Sub

Sub FeatureCount(ByVal oDoc as Inventor.Document)
	
	Try
	Dim SaveRequired As Boolean = False
	
	'uncomment for debugging purposes!
	'MessageBox.Show(DocName)
		If TypeOf oDoc Is PartDocument Then
			If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then 'skip CC and FACILITY files
				Dim PartDocName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.DisplayName) & ":1"
				Dim oFeats as PartFeatures = oDoc.ComponentDefinition.Features
				Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
				Try
					If Not iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count Then 'or update it
						iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Feature Count for this part is: " & oFeats.Count, "FEATURECOUNT")
					If Not iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count Then 'or update it
						iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Parameter Count for " & oDoc.File.fullfilename &" is: " & oParams.Count, "PARAMETERCOUNT")
					If SaveRequired Then
						'oDoc.Save 'try to save the file.
					End If
				Catch
					iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count
					iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count
					'oDoc.Save 'try to save the file.
					Exit Sub
				End Try
			End If
		ElseIf TypeOf oDoc Is AssemblyDocument Then
			If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
				Dim DocName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.DisplayName) & ":1"
				Dim oFeats as Features = oDoc.ComponentDefinition.Features
				Dim Occs as ComponentOccurrences = oDoc.ComponentDefinition.Occurrences			
				Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
				'Dim oConstraints as Constraints = oDoc.ComponentDefinition.Constraints
				Try
					If Not iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count Then
						iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Feature Count for this assembly is: " & oFeats.Count, "FEATURECOUNT")
					If Not iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count Then
						iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Occurrence Count for " & oDoc.File.fullfilename & " is: " & Occs.Count, "OCCURRENCECOUNT")
					If Not iProperties.Value(DocName, "Custom","PARAMETERCOUNT") = oParams.Count Then
						iProperties.Value(DocName, "Custom", "PARAMETERCOUNT") = oParams.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Parameter Count for this part is: " & oDoc.ComponentDefinition.Constraints.Count, "PARAMETERCOUNT")
					If Not iProperties.Value(DocName, "Custom","CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count Then
						iProperties.Value(DocName,"Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Constraint Count for Assembly " & DocName & " is: " & oDoc.ComponentDefinition.Constraints.Count, "CONSTRAINTCOUNT")
					If SaveRequired Then
						'oDoc.Save 'try to save the file.
					End If
				Catch
					'creates any missing iProperties.
					iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count
					iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count
					iProperties.Value(DocName, "Custom", "PARAMETERCOUNT") = oParams.Count
					iProperties.Value(DocName,"Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
					'oDoc.Save 'saves the assembly
					Exit Sub
				End Try
			End If
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace,oDoc.DisplayName)
	End Try
	
End Sub
	Dim SaveRequired As Boolean = False
Sub PartFeatureCount(ByVal oDoc)
	Try
		If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
			Dim oFeats as PartFeatures = oDoc.Document.ComponentDefinition.Features
			Dim oParams as Parameters = oDoc.Document.ComponentDefinition.Parameters
			Try
				'may need to save the file when we're done, hence the boolean check
				If Not iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count Then
					iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Feature Count for this part is: " & oFeats.Count, "FEATURECOUNT")
		
				If Not iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count Then
					iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Parameter Count for " & oDoc.Document.File.fullfilename &" is: " & oParams.Count, "PARAMETERCOUNT")
				If SaveRequired Then
					'oDoc.Save 'try to save the file.
				End If
			Catch
				'definitely need to save the file!
				iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
				iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count
				'oDoc.Save 'try to save the file.
				'oDoc.Close 'try to close the file - on a vaulted file this will fire the check-in dialogue.
			End Try
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace)
	End Try
	
End Sub

Sub AssemblyFeatureCount(ByVal oDoc)
	Try
		Dim SaveRequired As Boolean = False
		'Dim DocName As String = oDoc.DisplayName
		If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
			Dim oFeats as Features = oDoc.ComponentDefinition.Features
			Dim Occs as ComponentOccurrences = oDoc.ComponentDefinition.Occurrences			
			Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
			'Dim oConstraints as Constraints = oDoc.ComponentDefinition.Constraints
			Try
				If Not iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count Then
					iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Feature Count for this assembly is: " & oFeats.Count, "FEATURECOUNT")
				If Not iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count Then
					iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Occurrence Count for " & oDoc.File.fullfilename & " is: " & Occs.Count, "OCCURRENCECOUNT")
				If Not iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count Then
					iProperties.Value("Custom", "PARAMETERCOUNT") = oParams.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Parameter Count for this part is: " & oDoc.ComponentDefinition.Constraints.Count, "PARAMETERCOUNT")
				If Not iProperties.Value("Custom","CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count Then
					iProperties.Value("Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Constraint Count for Assembly " & DocName & " is: " & oDoc.ComponentDefinition.Constraints.Count, "CONSTRAINTCOUNT")
				If SaveRequired Then
					'oDoc.Save 'try to save the file.
				End If
			Catch
				'creates any missing iProperties.
				iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
				iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count
				iProperties.Value("Custom", "PARAMETERCOUNT") = oParams.Count
				iProperties.Value("Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
				'oDoc.Save 'saves the assembly
				Exit Sub
			End Try
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace)
	End Try
End Sub

 

Message 4 of 6

tracey
Enthusiast
Enthusiast

Thank you!!! This lead to a good read of Inventor help on the subject too.  This works perfectly.Smiley Happy

 

Thank you once again

Message 5 of 6

tracey
Enthusiast
Enthusiast

Thank you Alex, that is quite a rule!!  My hours custom iproperty is an estimate of the build cost per sub assembly which I will input into the parameter where needed.  I have a summary document A4, which give a picture, totals for weight, cost and build hours as a sales document.  I LOVE automation!!!

I am about to run your rule!

0 Likes
Message 6 of 6

AlexFielder
Advisor
Advisor

How did you get on @tracey ?

0 Likes