[VB.Net] Subassembly parts suppression based on suppression state

[VB.Net] Subassembly parts suppression based on suppression state

matroosoft
Advocate Advocate
1,581 Views
7 Replies
Message 1 of 8

[VB.Net] Subassembly parts suppression based on suppression state

matroosoft
Advocate
Advocate

For a project I have to suppress subassembly parts based on the suppression state of parts in another subassembly, using VB.Net/iLogic. I'm quite new to both of them so it would be great if someone could give some help. What the code has to do is that it looks for the first part in subassembly 1 and reads it suppression state. It then has to copy that suppression state to the first part in subassembly 2. Then it has to move to part 2 in subassembly 1, reads its suppression state and copy that state to the second part of subassembly 2. And so on until it has 'copied' all suppression states of all parts of the first subassembly to the corresponding parts in the second subassembly.

 

What I know after some reading that I have to open and close the code with a sub, have to define all objects, probably have to use a 'for each-loop' and  an if-then-else statement. I thought it would be relatively easy to do using a 'for each-loop' but the rule editor keeps throwing errors on me without me teaching how to do it the good way. 🙂

 

For defining the objects I use the Inventor API Object Model to see what object I have to define. If I for example define oComponent and oAssembly and say something like:

 

For Each oComponent In oAssembly

It returns that the expression is of the type of Inventor.AssemblyDocument, which isn't of a collection type. Can't quite get which objects I have to define and how. Hope someone can help me a little. 

0 Likes
Accepted solutions (1)
1,582 Views
7 Replies
Replies (7)
Message 2 of 8

waynehelley
Collaborator
Collaborator

Here is something to get you started which will run in iLogic...

 

Dim assDoc As AssemblyDocument = ThisDoc.Document

Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In assDoc.ComponentDefinition.Occurrences
    If oOccurrence.Suppressed = False Then
        MsgBox(oOccurrence.Name)
    End If
Next

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 3 of 8

matroosoft
Advocate
Advocate

Thanks alot, am going to play with it 🙂

0 Likes
Message 4 of 8

waynehelley
Collaborator
Collaborator

I've had a go at writing some code which will do what I think you are wanting.  At the moment, it will error out if the first subassembly has more components than the second subassembly.

 

Dim assDoc As AssemblyDocument = ThisDoc.Document

 

Dim subAssDoc1 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(1).Definition.Document
Dim subAssDoc2 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(2).Definition.Document

 

If subAssDoc2.LevelOfDetailName = "Master" Then
        MsgBox("This logic only works if subassembly 2 is not set to Master Level of Detail")
         Exit Sub
 End If

 

 Dim noOfComponentsInSubAssembly1 As Integer = subAssDoc1.ComponentDefinition.Occurrences.Count
 For i = 1 To noOfComponentsInSubAssembly1
        If subAssDoc1.ComponentDefinition.Occurrences(i).Suppressed Then
            subAssDoc2.ComponentDefinition.Occurrences(i).Suppress()
        Else
            subAssDoc2.ComponentDefinition.Occurrences(i).Unsuppress()
        End If

Next

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 5 of 8

matroosoft
Advocate
Advocate

This is amazing, works almost exactly as I want it! The 2 subassemblies will always have the same amount of parts by the way so the error you mentioned shouldn't occur. I've a few questions though: 

 

- Right mouse button on the rule and then selecting 'run rule' does nothing while if I select edit and click on OK to close the rule, it executes the rule as wanted. Do you know why it doesn't execute the rule when clicking 'run rule'?

- If I suppress the second subassembly the rule can't suppress or unsuppress parts in that subassembly, so it throws an error on me. I rewrote the code a little bit, added 2 more subassemblies and wrote an if-statement to prevent the rule trying to (un)suppress parts in a suppressed subassembly. However it keep showing the error. What am I doing wrong?

 

SyntaxEditor Code Snippet

Sub Main()
Sub updateElementsSuppression
Dim assDoc As AssemblyDocument = ThisDoc.Document

Dim subAssDoc1 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(1).Definition.Document
Dim subAssDoc2 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(2).Definition.Document
Dim subAssDoc3 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(3).Definition.Document
Dim subAssDoc4 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(4).Definition.Document
 

If subAssDoc2.LevelOfDetailName = "Master" Then
        MsgBox("This logic only works if subassembly 2 is not set to Master Level of Detail")
         Exit Sub
End If


Dim noOfComponentsInSubAssembly1 As Integer = subAssDoc1.ComponentDefinition.Occurrences.Count
For i = 1 To noOfComponentsInSubAssembly1
        If subAssDoc1.ComponentDefinition.Occurrences(i).Suppressed Then
            If assDoc.ComponentDefinition.Occurrences(2).Suppressed=False Then
            subAssDoc2.ComponentDefinition.Occurrences(i).Suppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(3).Suppressed=False Then
            subAssDoc3.ComponentDefinition.Occurrences(i).Suppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(4).Suppressed=False Then
            subAssDoc4.ComponentDefinition.Occurrences(i).Suppress()
            End If
        Else
            If assDoc.ComponentDefinition.Occurrences(2).Suppressed=False Then
            subAssDoc2.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(3).Suppressed=False Then
            subAssDoc3.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(4).Suppressed=False Then
            subAssDoc4.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
        End If

Next
End Sub

 

0 Likes
Message 6 of 8

waynehelley
Collaborator
Collaborator

- Right mouse button on the rule and then selecting 'run rule' does nothing while if I select edit and click on OK to close the rule, it executes the rule as wanted. Do you know why it doesn't execute the rule when clicking 'run rule'?

 

I'm really not sure why this is happening I'm afraid.  Maybe the logic has run correctly but the Document just hasn't yet updated itself.  If this is the case, adding a InventorVb.DocumentUpdate() to the end of the rule might fix it.

 

- If I suppress the second subassembly the rule can't suppress or unsuppress parts in that subassembly, so it throws an error on me. I rewrote the code a little bit, added 2 more subassemblies and wrote an if-statement to prevent the rule trying to (un)suppress parts in a suppressed subassembly. However it keep showing the error. What am I doing wrong?

 

This is a tricky one.  If subassembly2 is suppressed, your code will actually error out on 'Dim subAssDoc2 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(2).Definition.Document'.  Reason being because the 'Definition.Document' objects of the Component Occurrence are actually inaccessible due to suppression.

 

It's a bit long winded but something like this would work...

 

'Declare the ComponentOccurrence. This should never fail as long as there is an second component/assembly existing

Dim subAssOcc2 As ComponentOccurrence = assDoc.ComponentDefinition.Occurrences(2)

 

'Declare an object to store the sub assembly 2 document
Dim subAssDoc2 As AssemblyDocument

 

If subAssOcc2.Suppressed = False Then
     subAssDoc2 = subAssOcc2.Definition.Document
End If

 

'This is just for testing.  The logic 'Is Nothing' will check if subAssDoc2 is empty

If subAssDoc2 Is Nothing Then
      MsgBox("Object subAssDoc2 holds a value of Nothing. It must have been suppressed")
End If

 

 

''''''''''''''''''''

 

Another method would be to just remove the line...

'Dim subAssDoc2 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(2).Definition.Document'

then replace every instance of 'subAssDoc2' with 'assDoc.ComponentDefinition.Occurrences(2).Definition.Document'

 

 

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 7 of 8

waynehelley
Collaborator
Collaborator

The following seems to work.  Another thing to watch out for is the bit I put in about LevelOfDetailName = "Master" will also fail if it is attempted to be executed on a suppressed assembly.

 

Dim assDoc As AssemblyDocument = ThisDoc.Document

Dim subAssDoc1 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(1).Definition.Document

 

'Declare the ComponentOccurrence. This should never fail as long as there is an second component/assembly existing
Dim subAssOcc2 As ComponentOccurrence = assDoc.ComponentDefinition.Occurrences(2)

 

'Declare an object to store the sub assembly 2 document
Dim subAssDoc2 As AssemblyDocument

 

If subAssOcc2.Suppressed = False Then
     subAssDoc2 = subAssOcc2.Definition.Document 
      If subAssDoc2.LevelOfDetailName = "Master" Then
               MsgBox("This logic only works if subassembly 2 is not set to Master Level of Detail")
               Exit Sub
      End If 
End If

 

Dim noOfComponentsInSubAssembly1 As Integer = subAssDoc1.ComponentDefinition.Occurrences.Count
For i = 1 To noOfComponentsInSubAssembly1
   If subAssDoc2 IsNot Nothing Then
        If subAssDoc1.ComponentDefinition.Occurrences(i).Suppressed Then
              subAssDoc2.ComponentDefinition.Occurrences(i).Suppress()
        Else
              subAssDoc2.ComponentDefinition.Occurrences(i).Unsuppress()
        End If
   End If
Next

 

InventorVb.DocumentUpdate()

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
Message 8 of 8

matroosoft
Advocate
Advocate
Accepted solution

Thanks again for the extensive and useful replies! Had a few busy days so I wasn't able to test things out, but this morning I changed a few things based on what you said and now it works flawlessly.

 

First of all, I added the InventorVb.DocumentUpdate(), which fixed the issue where clicking on 'run rule' didn't update the document.

 

Second, I deleted all the dim's related to the assemblies that could possibly be suppressed and choose to define them each time I use them to prevent Inventor trying to address assemblies which are suppressed. Seemed to me as the better solution.

 

Here's the code I have right now:

 

SyntaxEditor Code Snippet

Sub Main()
Sub updateElementsSuppression
Dim assDoc As AssemblyDocument = ThisDoc.Document

If assDoc.ComponentDefinition.Occurrences(2).Definition.Document.LevelOfDetailName = "Master" Then
        MsgBox("This logic only works if subassembly 2 is not set to Master Level of Detail")
         Exit Sub
End If

If assDoc.ComponentDefinition.Occurrences(1).Suppressed=False Then

    Dim subAssDoc1 As AssemblyDocument = assDoc.ComponentDefinition.Occurrences(1).Definition.Document
    Dim noOfComponentsInSubAssembly1 As Integer = subAssDoc1.ComponentDefinition.Occurrences.Count

    For i = 1 To noOfComponentsInSubAssembly1
        If subAssDoc1.ComponentDefinition.Occurrences(i).Suppressed Then
            If assDoc.ComponentDefinition.Occurrences(2).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(2).Definition.Document.ComponentDefinition.Occurrences(i).Suppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(3).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(3).Definition.Document.ComponentDefinition.Occurrences(i).Suppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(4).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(4).Definition.Document.ComponentDefinition.Occurrences(i).Suppress()
            End If
        Else
            If assDoc.ComponentDefinition.Occurrences(2).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(2).Definition.Document.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(3).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(3).Definition.Document.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
            If assDoc.ComponentDefinition.Occurrences(4).Suppressed=False Then
            assDoc.ComponentDefinition.Occurrences(4).Definition.Document.ComponentDefinition.Occurrences(i).Unsuppress()
            End If
        End If

    Next

End If

iLogicVb.UpdateWhenDone = True

End Sub

 

Thanks again! I'll give you some kudo's 🙂