I don't have a 2018 version of Inventor so the sample files got saved to 2019 files while i tested them, but here is the code:
Sub Main
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub
'Main Setup:
Dim AsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim FileName As String = AsmDoc.FullFileName : FileName = Left(FileName, (FileName.Length-4)) & "_SUB" & ".ipt"
'ViewRep Sample Setup:
Dim RepID As String = ""
Dim RepIDlist As New List(Of String)
'Get List from file so we can choose view from list. This Can be automated with desired name
For Each View In AsmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations
RepIDlist.Add(View.Name)
Next
'Temporarily choosing view from list. This can be automated with desired name
RepID = InputListBox("", RepIDlist, RepIDlist.Item(0), Title := "Title", ListName := "List")
If RepID = "" Then RepID = "Master" 'Setting a Default Value for nothing selected
'Set View representation so visibility can be verified
AsmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Item(RepID).Activate
'New Document Setup:
Dim NewDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , False) 'include path to template file [if defualt is insufficient] between , , after DocumentTypeEnum.kPartDocumentObject
Dim oDerivedAsmDef As DerivedAssemblyDefinition = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AsmDoc.FullFileName)
'Derived Assembly Options:
oDerivedAsmDef.ScaleFactor = 1
oDerivedAsmDef.ReducedMemoryMode = True
oDerivedAsmDef.UseColorOverridesFromSource = True
oDerivedAsmDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
oDerivedAsmDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
oDerivedAsmDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
oDerivedAsmDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
oDerivedAsmDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsSingleBodyNoSeams
oDerivedAsmDef.InclusionOption = DerivedComponentOptionEnum.kDerivedExcludeAll
oDerivedAsmDef.ActiveDesignViewRepresentation = RepID 'Where RepID is a string of the View rep name
'Limit bounding box to part files only
Dim partCol As ObjectCollection = GetOnlyParts(oDerivedAsmDef.Occurrences)
For Each Oc As DerivedAssemblyOccurrence In partCol
Oc.InclusionOption = DerivedComponentOptionEnum.kDerivedBoundingBox
Next
Call NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)
Try
NewDoc.SaveAs(FileName, False)
Catch
MessageBox.Show(NewDoc.DisplayName & " cannot be overwritten.", "Terminating")
NewDoc.Close(True)
Exit Sub
End Try
ThisApplication.Documents.Open(NewDoc.FullDocumentName, True)
End Sub
Function GetOnlyParts(mainCol As DerivedAssemblyOccurrences) As ObjectCollection
Dim Result As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each Oc As DerivedAssemblyOccurrence In mainCol
If Oc.IsAssembly = False
If Oc.ReferencedOccurrence.Suppressed = False And Oc.ReferencedOccurrence.Visible = True
Result.Add(Oc)
End If
Else
For Each OcSub As DerivedAssemblyOccurrence In GetOnlyParts(Oc.SubOccurrences)
If OcSub.ReferencedOccurrence.Suppressed = False And OcSub.ReferencedOccurrence.Visible = True
Result.Add(OcSub)
End If
Next
End If
Next
Return Result
End Function
Looks like I had to set the view representation before I could test the solid bodies for visibiliy/suppression, which was added to the GetOnlyParts Function. If subassembly view reps are associated with the main assembly View Representation, It should work as intended, Otherwise digging through sub assemblies might require some extra steps.
I decided to put the view representation selection into a list, but it could be done with logic as long as the View names are consistent throughout all assemblies.