Enable Structured BOM through using VBA

Enable Structured BOM through using VBA

Inventor_Enjoyer
Contributor Contributor
366 Views
4 Replies
Message 1 of 5

Enable Structured BOM through using VBA

Inventor_Enjoyer
Contributor
Contributor

Hey, I'm currently trying to enable structured bom (see picture) for all subassemblies inside main assembly

Inventor_Enjoyer_0-1667831122814.png

However I don't seem to get deep enough to subassembly level. I can enable or disable on main assembly level.

Any idea how to look into this better?

0 Likes
Accepted solutions (1)
367 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @Inventor_Enjoyer.  I assume you mean for the code to only effect the top level sub assemblies, and not any sub assemblies that are deeper than that, is this correct?  If so, then I have some iLogic code below that I think may work for you.

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 oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
For Each oOcc As ComponentOccurrence In oOccs
	If oOcc.Suppressed Then Continue For
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
	If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim oSubADef As AssemblyComponentDefinition = oOcc.Definition
		Dim oSubAsmBOM As BOM = oSubADef.BOM
		If Not oSubAsmBOM.StructuredViewEnabled Then
			oSubAsmBOM.StructuredViewEnabled = True
		End If
	End If
Next
If oADoc.RequiresUpdate Then oADoc.Update2(True)
If oADoc.Dirty Then oADoc.Save2

But if you need it to effect all sub-assemblies in every level of the assembly, then this code may need to be changed a bit.  It wouldn't be that difficult to do though.  Instead of iterating through components, we could iterate through 'AllReferencedDocuments' to do that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

Inventor_Enjoyer
Contributor
Contributor

Hi @WCrihfield. Actually I think it needs to effect in every level, because some subassemblies may contain older assemblies which have it disabled

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

OK, sure.  Here is an example of an iLogic rule for that scenario you can try out.

Dim oDoc As Document = ThisDoc.Document
Dim oRefDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments
For Each oRefDoc As Document In oRefDocs
	'if it is not an assembly, skip to next referenced document
	If oRefDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Continue For
	Dim oSubADoc As AssemblyDocument = oRefDoc
	Dim oSubADef As AssemblyComponentDefinition = oSubADoc.ComponentDefinition
	Dim oSubAsmBOM As BOM = oSubADef.BOM
	If Not oSubAsmBOM.StructuredViewEnabled Then
		oSubAsmBOM.StructuredViewEnabled = True
	End If
Next 'oRefDoc
If oDoc.RequiresUpdate Then oDoc.Update2(True)
If oDoc.Dirty Then oDoc.Save2

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Oops...I forgot you wanted the solution in VBA, not in iLogic, so here is the VBA version of that last code I posted.  Sorry about that.

Sub SubAsmStrBOMViewEnable()
    Dim oDoc As Document
    Set oDoc = ThisDoc.Document
    Dim oRefDocs As DocumentsEnumerator
    Set oRefDocs = oDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    For Each oRefDoc In oRefDocs
        'if it is not an assembly, skip to next referenced document
        If oRefDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then GoTo ContinueToNext
        Dim oSubADoc As AssemblyDocument
        Set oSubADoc = oRefDoc
        Dim oSubADef As AssemblyComponentDefinition
        Set oSubADef = oSubADoc.ComponentDefinition
        Dim oSubAsmBOM As BOM
        Set oSubAsmBOM = oSubADef.BOM
        If Not oSubAsmBOM.StructuredViewEnabled Then
            oSubAsmBOM.StructuredViewEnabled = True
        End If
ContinueToNext:
    Next 'oRefDoc
    If oDoc.RequiresUpdate Then oDoc.Update2 (True)
    If oDoc.Dirty Then oDoc.Save2
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes