If a Sub Assembly is suppressed then do something to that assembly

If a Sub Assembly is suppressed then do something to that assembly

AMN3161
Advocate Advocate
750 Views
9 Replies
Message 1 of 10

If a Sub Assembly is suppressed then do something to that assembly

AMN3161
Advocate
Advocate

I basically want a piece of code that will look at sub assemblies but only the top layer of subassemblies to do something to any sub assemblies that are suppressed. So I have a main assembly with only assemblies within it. But some of those sub assemblies will have sub assemblies of their own that i want unaffected. 

 

Any help would be appreciated, i am novice to logic. 

 

 

0 Likes
751 Views
9 Replies
Replies (9)
Message 2 of 10

snappyjazz
Collaborator
Collaborator

I don't fully understand what you're wanting to do with the suppressed subassemblies; but my code can get you a list of suppressed subassembly objects (ComponentOccurrence).

 

    Public Sub ForumHelp()
        'get inventor session
        Dim InvApp As Inv.Application = SysInter.Marshal.GetActiveObject("Inventor.Application")
        Dim ThisDoc As Inv.AssemblyDocument = Convert.Doc(InvApp.ActiveDocument)
        'Dim CompDef As Inv.ComponentDefinition = ThisDoc.def
        Dim SAList As New List(Of Inv.ComponentOccurrence)

        'Cycle through top level documents
        For Each OccObj As Inv.ComponentOccurrence In ThisDoc.ComponentDefinition.Occurrences
            Console.WriteLine("Occurence Name: " & OccObj.Name.Split(":").First)
            'If the current occurence object is an assembly document and is suppressed
            If OccObj.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject AndAlso OccObj.Suppressed = True Then
                'Add the document to the list
                SAList.Add(OccObj)
            End If
        Next

        'add code to do something to the suppressed subassemblies (in SAList)
    End Sub
0 Likes
Message 3 of 10

AMN3161
Advocate
Advocate

I am unfamiliar with how lists work, is the list name "SAList"? Basically what I want to do is if a assembly is Suppressed i want the BOM structure to be "Normal" and if it isn't Suppressed I want it do be "Phantom"

 

0 Likes
Message 4 of 10

snappyjazz
Collaborator
Collaborator

So this code works for me. I hope it achieves your goal.

 

    Public Sub ForumHelp()
        'get inventor session
        Dim InvApp As Inv.Application = SysInter.Marshal.GetActiveObject("Inventor.Application")
        Dim ThisDoc As Inv.AssemblyDocument = Convert.Doc(InvApp.ActiveDocument)
        Dim SAList As New List(Of Inv.ComponentOccurrence)

        'Cycle through top level documents
        For Each OccObj As Inv.ComponentOccurrence In ThisDoc.ComponentDefinition.Occurrences
            Console.WriteLine("Occurence Name: " & OccObj.Name.Split(":").First)
            'If the current occurence object is an assembly document and is suppressed
            If OccObj.DefinitionDocumentType = Inv.DocumentTypeEnum.kAssemblyDocumentObject AndAlso OccObj.Suppressed = True Then
                OccObj.Unsuppress()
                Dim ColorVar As ConsoleColor = Console.ForegroundColor : Console.ForegroundColor = ConsoleColor.Green : Console.WriteLine("Suppressed? " & OccObj.Suppressed.ToString) : Console.ForegroundColor = ColorVar
                'Dim TempDoc As Inv.AssemblyDocument = OccObj.Definition.Document
                Dim ObjDocDef As Inv.ComponentDefinition = OccObj.Definition
                ObjDocDef.BOMStructure = Inv.BOMStructureEnum.kNormalBOMStructure
                OccObj.Suppress(True)
            ElseIf OccObj.DefinitionDocumentType = Inv.DocumentTypeEnum.kAssemblyDocumentObject AndAlso OccObj.Suppressed = False Then
                Dim ColorVar As ConsoleColor = Console.ForegroundColor : Console.ForegroundColor = ConsoleColor.DarkRed : Console.WriteLine("Suppressed? " & OccObj.Suppressed.ToString) : Console.ForegroundColor = ColorVar
                'Dim TempDoc As Inv.AssemblyDocument = OccObj.Definition.Document
                Dim ObjDocDef As Inv.ComponentDefinition = OccObj.Definition
                ObjDocDef.BOMStructure = Inv.BOMStructureEnum.kPhantomBOMStructure
            End If
        Next

    End Sub

 

 

0 Likes
Message 5 of 10

shiftctrl.io
Enthusiast
Enthusiast

I haven't tested this to make sure it works as I'm working something important and couldn't check. 

 

Nevertheless I wrote the following code which should do what you're asking with the BOM. I ignored the "list" part since you don't really need that for what you're asking. 

 

You can copy and paste the following into a new rule and just run it. 

 

 

 

Sub Main()
	'Get this document
	Dim doc As Document = ThisDoc.Document
	'Check if document is an Assembly Document
	If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim assyDoc As AssemblyDocument = doc
		Dim occs As ComponentOccurrences = assyDoc.ComponentDefinition.Occurrences
		'Loop through all occurrences
		For Each occ As ComponentOccurrence In occs
			'Check if the occurrence is an Assembly Document
			If occ.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				'Check if occurrence is suppressed
				If occ.Suppressed Then
					'Set the Bom Structure to Normal if suppressed
					occ.BOMStructure = BOMStructureEnum.kNormalBOMStructure
				Else
					'Set the Bom Structure to Phantom if not suppressed
					occ.BOMStructure = BOMStructureEnum.kPhantomBOMStructure
				End If
			End If
		Next
	Else
		MessageBox.Show("This can only be run in an Assembly Document")
	End If
End Sub

 

 

 

0 Likes
Message 6 of 10

Anonymous
Not applicable

I did a test and it doesnt work. I beleived it is because once you suppressed an occurence, bom structure cannot be changed anymore. Same as if you do it manually.

suppressed.jpg

 

It will the other way around but Im not sure if it is applicble with your workflow.

Here is the code i modified that will suppress the occurence if BOM was set to reference.

 

Sub Main()
	'Get this document
	Dim doc As Document = ThisDoc.Document
	'Check if document is an Assembly Document
	If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim assyDoc As AssemblyDocument = doc
		Dim occs As ComponentOccurrences = assyDoc.ComponentDefinition.Occurrences
		'Loop through all occurrences
		For Each occ As ComponentOccurrence In occs
			'Check if the occurrence is an Assembly Document
			If occ.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				'Check if occurrence bom is reference only.
				If occ.BOMStructure = BOMStructureEnum.kReferenceBOMStructure  Then
					'Suppress the occurence
					occ.Suppress
				End If
			End If
		Next
	Else
		MessageBox.Show("This can only be run in an Assembly Document")
	End If
End Sub

 

0 Likes
Message 7 of 10

shiftctrl.io
Enthusiast
Enthusiast

That had crossed my mind while writing it, however didn't have the time to check if it would work or not. Nevertheless, good catch. 

 

The other option is to iterate over each occurrence and open the file file invisibly then change the document's default BOM Structure to the desired selection. 

0 Likes
Message 8 of 10

shiftctrl.io
Enthusiast
Enthusiast

This is the updated code:

 

Sub Main()
	Dim doc As Document = ThisDoc.Document

	'Check if document is an Assembly Document
	If doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim assyDoc As AssemblyDocument = doc
		Dim occs As ComponentOccurrences = assyDoc.ComponentDefinition.Occurrences
		
		'Loop through all occurrences
		For Each occ As ComponentOccurrence In occs
			
			'Check if the occurrence is an Assembly Document
			If occ.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			
				Dim newBOMStructure = BOMStructureEnum.kPhantomBOMStructure 'Set to Phantom by default

				'Set the Bom Structure to Normal if suppressed
				If occ.Suppressed Then newBOMStructure = BOMStructureEnum.kNormalBOMStructure
				
				'Skip to the next occurrence if BOM Structure matches what is required
				If occ.BOMStructure = newBOMStructure Then Continue For

				'Set the Assembly's BOM Structure
				Dim activeDoc As AssemblyDocument = occ.ReferencedDocumentDescriptor.ReferencedDocument
				activeDoc.ComponentDefinition.BOMStructure = newBOMStructure
			End If
		Next
	Else
		MessageBox.Show("This can only be run in an Assembly Document")
	End If
End Sub
0 Likes
Message 9 of 10

AMN3161
Advocate
Advocate

Didn't expect so many replies during the weekend but I cant get it to work. I have bunch of level of details with sub assemblies suppressed, i put the code in and hit run, it seems to make every sub assembly phantom that is not suppressed but doesn't make them normal when they are suppressed

0 Likes
Message 10 of 10

AMN3161
Advocate
Advocate

so I just found this out but I didn't know that quantities can follow view rep and parts list filters which basically fixes my problem.

 

I think i become too reliant on ilogic especially when what i need is already built into the software 🙂

0 Likes