Using iLogic to add up an assemblies component weights

Using iLogic to add up an assemblies component weights

ober2558
Enthusiast Enthusiast
1,291 Views
23 Replies
Message 1 of 24

Using iLogic to add up an assemblies component weights

ober2558
Enthusiast
Enthusiast

I am looking to incorporate accurate weights for a large CAD Library without having completely accurate designs/models. I have added a custom iProperty to each part variation within each iPart (located in the iTables) in order to remedy this. My code currently allows for adding up all custom weights from all iPart components and subassembly components within the active assembly. However, there are complex components that I had to turn into iAssemblies containing a large number of iParts and non-iParts in order to function as needed. Therefore I am hoping to add a custom weight column to these iAssemblies themselves to overwrite the custom weights of the subcomponents within them. However, I need a code that will detect if an iAssembly contains the custom weight column and if so use the specified custom weights without also counting the subcomponent custom weights. But if the custom weight column is not detected, to add up the custom weights of the sub-components. I have attached the code I am currently utilizing, Please provide any help you can. Thank you.

Class ThisRule
	Sub Main
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
			MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
			Exit Sub
		End If
		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
		oWeightList = New List(Of Double)
		RecursivelyProcessComponents(oOccs)
		Dim oTotaliPartsWeight As Double = oWeightList.Sum
		MsgBox("Total iParts Weight = " & oTotaliPartsWeight, vbInformation, "Total iParts Weight")
	End Sub
	
	Dim oWeightList As List(Of Double) 'shared by both routines
	
	Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			If oComp.Suppressed Then Continue For
			If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
			If oComp.IsiPartMember Then
				Dim oMember As iPartMember = oComp.Definition.iPartMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			End If
			If oComp.SubOccurrences.Count > 0 Then
				RecursivelyProcessComponents(oComp.SubOccurrences)
			End If
		Next
	End Sub
End Class

 

0 Likes
1,292 Views
23 Replies
Replies (23)
Message 21 of 24

ober2558
Enthusiast
Enthusiast

I believe I have the code running as I want to at the moment. On a separate note, is there a way for the code to detect a non-iPart or non-iAssembly and notify the user of the part name and have them type in a value for that part's weight to be added to the total?

0 Likes
Message 22 of 24

A.Acheson
Mentor
Mentor

That seems straightforward Filter for document or definition type then use an input box and add to list or the rhe custom part number.

 

In the rule you are currently using two filter types one for document type at the very top of the code detecting it's an assembly and one for definition type later on. 

 

Document Type

 

 

If Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
ElseIf Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
End If

 

Definition type

 

If TypeOf oComp.Definition Is PartComponentDefinition
ElseIf TypeOf oComp.Definition is AssemblyComponentDefinition
End If

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 23 of 24

ober2558
Enthusiast
Enthusiast

Could you elaborate a bit further? As I have mentioned I'm not the most experienced coder. I will attach my current code below. Where should I input the code you listed? And how do I set up the input box to work with this code? I apologize again for this tedious task. Thank you again for all your help.

Class ThisRule
	Sub Main
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
			MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
			Exit Sub
		End If
		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
		oWeightList = New List(Of Double)
		RecursivelyProcessComponents(oOccs)
		Dim oTotaliPartsWeight As Double = oWeightList.Sum
		MsgBox("Total iParts Weight = " & oTotaliPartsWeight, vbInformation, "Total iParts Weight")
	End Sub
	
	Dim oWeightList As List(Of Double) 'shared by both routines
	
	Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			If oComp.Suppressed Then Continue For
			If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
			If oComp.IsiAssemblyMember Then
				Dim oMember As iAssemblyMember = oComp.Definition.iAssemblyMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			Else If oComp.IsiPartMember Then
				Dim oMember As iPartMember = oComp.Definition.iPartMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			End If
		Next
	End Sub
End Class
0 Likes
Message 24 of 24

A.Acheson
Mentor
Mentor

Unfortuantely these are reasonably difficult task and they all take time to implement around your working code especially when the design is changing. I dont have much time to assist in creating the code but if you want to post your attempts then it will help you learn and others along with you. You have to spend time to save time.

 

Here are a few pointers. 

Where your checking type of definition virtual in order to skip this definition place and else if and check for a part/assembly definition.

 

Once you have this you need to check if its not a iassembly/ipart member occurrence as your trying to avoid those. 

 

I assume you know where the input box snippet is located in the ilogic editor under the messagebox. 

 

Here is links for modifying custom iproperty

Link 1

Link 2

 

 

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes