iLogic visibilty search going wrong

iLogic visibilty search going wrong

peter.ensing
Enthusiast Enthusiast
1,174 Views
6 Replies
Message 1 of 7

iLogic visibilty search going wrong

peter.ensing
Enthusiast
Enthusiast

Hello all!

 

I'm trying to get an iLogic rule to work but cant find out whats going wrong.

To let it find all parts in an assembly incl. sub assemblies is not very difficult but when I added a visibilty filter to it some weird stuff happend.

 

It should iterate through the whole assembly to see if a part is visible (or not) and if its visible the mass weight is added to a double to sum up everything and get a total weight string of all thats visible in the current view representation.

 

It goes wrong at subassemblies, the code seems to ignore parts that are used multiple times in that subassembly.

I tested the code with an adjusment to get a list of part names instead of the total mass sum. At the master view rep I get a list of all part names like it should do. When I switch to a view rep it shows a list of the names of all unique parts excl. multiple used parts instead of incl multiple used parts. it ignores a hidden subassembly and adds some parts to the list but it does correctly excludes  parts in the direct main assembly that are hidden... I just cant wrap my mind around it what I told the code to do with this outcome.


Something is not right in the code, but I do no understand what is going wrong.

I added all 3 version of the code I tried for testing. The simplyfied code is where I want to go to, but maybe this just simply wont work. 

 

'Simplyfied value code:
objDef
= ThisDoc.Document.ComponentDefinition Dim oLeafOccs As ComponentOccurrencesEnumerator oLeafOccs = objDef.Occurrences.AllLeafOccurrences Dim objComp As ComponentOccurrence
Dim val As Double Dim total As Double = 0 For Each objComp In oLeafOccs If Component.Visible(objComp.Name) = True Then val=objComp.MassProperties.Mass total=total+val Next Dim sTotal As String sTotal = total MessageBox.Show(sTotal, "Total weight") InventorVb.DocumentUpdate()

 

'Value code:
objApp
= ThisApplication objApp.ScreenUpdating = False objApp.SilentOperation = True objDef = ThisDoc.Document.ComponentDefinition Dim oLeafOccs As ComponentOccurrencesEnumerator oLeafOccs = objDef.Occurrences.AllLeafOccurrences Dim objComp As ComponentOccurrence Dim lisOccs As New ArrayList Dim val As Double Dim total As Double = 0 For Each objComp In oLeafOccs If Component.Visible(objComp.Name) = True Then lisOccs.Add(objComp.MassProperties.Mass) Next Dim sNames As String For Each val In lisOccs sNames = sNames & "; " & val Next MessageBox.Show(sNames, "Title") objApp.ScreenUpdating = True objApp.SilentOperation = False InventorVb.DocumentUpdate()

 

'Partname code:
objApp
= ThisApplication objApp.ScreenUpdating = False objApp.SilentOperation = True objDef = ThisDoc.Document.ComponentDefinition Dim oLeafOccs As ComponentOccurrencesEnumerator oLeafOccs = objDef.Occurrences.AllLeafOccurrences Dim objComp As ComponentOccurrence Dim lisOccs As New ArrayList Dim val As Double Dim total As Double = 0 For Each objComp In oLeafOccs If Component.Visible(objComp.Name) = True Then lisOccs.Add(objComp) Next Dim sNames As String For Each objComp In lisOccs 'objComp.Delete sNames = sNames & "; " & objComp.Name Next MessageBox.Show(sNames, "Title") objApp.ScreenUpdating = True objApp.SilentOperation = False InventorVb.DocumentUpdate()

 

0 Likes
Accepted solutions (2)
1,175 Views
6 Replies
Replies (6)
Message 2 of 7

dutt.thakar
Collaborator
Collaborator

@peter.ensing 

 

I have tried creating something, please test it and see if this works. Unfortunately, I have not got time to test it.

 

Class MassCalc
	Shared total As Double
Sub Main


'Simplyfied value code:
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim objDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim objComp As ComponentOccurrence
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)

Dim sTotal As String = Nothing
	sTotal = total

MessageBox.Show(sTotal, "Total weight")
InventorVb.DocumentUpdate()
total = 0
End Sub


Sub TraverseAssembly(Occurrences As ComponentOccurrences,Level As Integer)
Dim val As Double
        ' Iterate through all of the occurrence in this collection.  This
        ' represents the occurrences at the top level of an assembly.
        Dim oOcc As ComponentOccurrence
        For Each oOcc In Occurrences
            If oOcc.ParentOccurrence Is Nothing Then
                	If Component.Visible(oOcc.Name) = True Then val=oOcc.MassProperties.Mass
						total = total + val
						'MessageBox.Show(oOcc.Name & "-" & total)
            End If
            If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
            End If
        Next
End Sub

End Class

Hope this will help you.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 3 of 7

peter.ensing
Enthusiast
Enthusiast

Hello @dutt.thakar ,

 

Thanks for your reply and sorry for my late response.

I like your different approach in the code.

It did give me some different results so I changed the code to object names to test what it actually picks up:

 

Class MassCalc
	Shared total As String
Sub Main


'Simplyfied value code:
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim objDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim objComp As ComponentOccurrence
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)

Dim sTotal As String = Nothing
	sTotal = total

MessageBox.Show(sTotal, "Total weight")
InventorVb.DocumentUpdate()
total = Nothing
End Sub


Sub TraverseAssembly(Occurrences As ComponentOccurrences,Level As Integer)
Dim val As String
        ' Iterate through all of the occurrence in this collection.  This
        ' represents the occurrences at the top level of an assembly.
        Dim oOcc As ComponentOccurrence
        For Each oOcc In Occurrences
            If oOcc.ParentOccurrence Is Nothing Then
                	If Component.Visible(oOcc.Name) = True Then val=oOcc.Name
						total = total + val
						'MessageBox.Show(oOcc.Name & "-" & total)
            End If
            If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
            End If
        Next
End Sub

End Class

On Master view reps it works fine, it takes all toplevel items in a list.

When I change to a view rep with hidden parts or assemblies the code is doing some weird things I dont understand.

It doesnt matter what I object I hide, the code results are always the same.


I also tried to change the code a little bit to ignore the assembly object to check and take the parts inside. 

It works besides that the results are still weird to me. 

Maybe I'll try to rebuild it with VBA to use a debugger to see what actually happens .

Any tips are always welcome, thanks in advance!

 

Class MassCalc
	Shared total As String
Sub Main


'Simplyfied value code:
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim objDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim objComp As ComponentOccurrence
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)

Dim sTotal As String = Nothing
	sTotal = total

MessageBox.Show(sTotal, "Total weight")
InventorVb.DocumentUpdate()
total = Nothing
End Sub


Sub TraverseAssembly(Occurrences As ComponentOccurrences,Level As Integer)
Dim val As String
        ' Iterate through all of the occurrence in this collection.  This
        ' represents the occurrences at the top level of an assembly.
        Dim oOcc As ComponentOccurrence
        For Each oOcc In Occurrences
			If Not oOcc.ParentOccurrence Is Nothing Then
          	If Component.Visible(oOcc.ParentOccurrence.Name)= True Then
                	If Component.Visible(oOcc.Name) = True Then val=oOcc.Name
						total = total + val
						'MessageBox.Show(oOcc.Name & "-" & total)
            End If
		End If
            If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
            End If
        Next
End Sub

End Class

 

0 Likes
Message 4 of 7

dutt.thakar
Collaborator
Collaborator

@peter.ensing 

 

Can you record a screencast or explain what kind of weird behaviour you are getting?

 

It will be easy to understand what is happening and than troubleshooting the problem.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 5 of 7

peter.ensing
Enthusiast
Enthusiast
Accepted solution

Hi! 

Had some time today and got it working in VBA.
Not sure this is the common way to get things done but it works like a charm.

 

 

Sub ViewRep_Weight()

Dim val As Double
Dim total As Double

Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
Dim oDoc As Document

Dim oAsmDef As AssemblyComponentDefinition
Set oAsmDef = oAsmDoc.ComponentDefinition
Dim oOccs As ComponentOccurrencesEnumerator
Dim oOcc As ComponentOccurrence

    ' Iterate through all of the referenced documents at
    ' all levels of the assembly.
  For Each oDoc In oAsmDoc.AllReferencedDocuments
         ' Check for part documents.
        If oDoc.DocumentType = kPartDocumentObject Then
        'set Doc to occurrence
        Set oOccs = oAsmDef.Occurrences.AllReferencedOccurrences(oDoc)
        For Each oOcc In oOccs
        'get occurrence weigt
                    val = oOcc.MassProperties.Mass
         'check if part is visible than add occurrence weight to sum
         'Note: BOM reference parts are ignored
                    If oOcc.Visible = True Then If Not oOcc.Definition.BOMStructure = kPhantomBOMStructure Then total = total + val
                    
        Next
        End If

Next
    ' Get the custom property set.
    Dim customPropSet As PropertySet
    Set customPropSet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")

    ' Get the property named "ViewRep_Weight".
    Dim customProp As Property
    Set customProp = customPropSet.Item("ViewRep_Weight")

customProp.Value = total

total = 0

End Sub

 

 

0 Likes
Message 6 of 7

J-Camper
Advisor
Advisor
Accepted solution

@peter.ensing,

 

I believe you original simple code will work.  The issue was you were adding Val to total regardless of the visibility of a component, without clearing val for each step of the loop. 

 

When you have:

 

If [condition] = Boolean Then [result]

 

You are condensing the entire If statement into a single line.  Your For Each loop had the If statement to change "val", but it was always adding to total, and since you aren't clearing it between Occurrences, it was adding whatever the last visible component set "val" to again.

 

I modified the code to only add "val" to total if it is visible, by expanding the If statement [you could also just clear "val" each time, dealers choice]:

 

'Simplyfied value code:
Dim objDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim oLeafOccs As ComponentOccurrencesEnumerator = objDef.Occurrences.AllLeafOccurrences
Dim objComp As ComponentOccurrence

Dim val As Double
Dim total As Double = 0
	
For Each objComp In oLeafOccs
	If objComp.Visible = True
		val=objComp.MassProperties.Mass
		total = total + val	
	End If
Next

Dim sTotal As String = total.ToString

MessageBox.Show(sTotal, "Total weight")
InventorVb.DocumentUpdate()

 

Message 7 of 7

peter.ensing
Enthusiast
Enthusiast

@J-Camper 

Thanks! That totaly make sense now.

I had the same mistake building up the VBA code but noticed it through debugging and fixed it.

Your adjustment to the iLogic code also works like a charm, I just tested it and the results are what to be expected.

 

Have a nice day all!

 

Kind regards,

 

Peter Ensing

 

0 Likes