@andrew_canfield,
I was looking into restricting the Part Level sketches, and I think I have it working as long as you don't want any of them to show up. I'm setting the ObjectVisibility Property of the Assembly document before creating the new part. For what ever reason, as long as they aren't visible at the time, they don't show up in the derived part. It doesn't hold the ObjectVisibility toggle if you are taking the master view, but it should work with any user created view representation.
Full 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("Select a View Rep to derive", RepIDlist, RepIDlist.Item(0), Title := "View Representations", ListName := "Available:")
If RepID = "" Then RepID = "Master" 'Setting a Default Value for nothing selected
'Set View representation so visibility can be verified
Dim visRep As DesignViewRepresentation = AsmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Item(RepID)
visRep.Activate
Dim ReturnState As Boolean = visRep.Locked
If visRep.DesignViewType = DesignViewTypeEnum.kMasterDesignViewType
ReturnState = False
MessageBox.Show("ObjectVisibility Toggles don't affect Derived Parts using the ""Master"" View Representation", "Limited Functionality")
End If
If ReturnState Then visRep.Locked = Not(ReturnState)
Call ObjectVisibility(AsmDoc, False)
'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)
'Limit bounding box to smaller than target
Dim minLimit As Double = 20 'cm [internal units are cm]
Dim RefinedpartCol As ObjectCollection =RefinePartsMin(partCol, minLimit)
For Each Oc As DerivedAssemblyOccurrence In RefinedpartCol
Oc.InclusionOption = DerivedComponentOptionEnum.kDerivedBoundingBox
Next
Dim derivedAsmComp As DerivedAssemblyComponent = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)
derivedAsmComp.BreakLinkToFile()
'Save New Document
Call SaveFile(NewDoc, FileName)
'Open new document
ThisApplication.Documents.Open(FileName, True)
'Activate original assembly
AsmDoc.Activate
'Call Toggle Sub
Call ObjectVisibility(AsmDoc, True)
If ReturnState Then visRep.Locked = ReturnState
End Sub
Sub ObjectVisibility(AsmDoc As AssemblyDocument, oToggle As Boolean)
Dim oVis As ObjectVisibility = AsmDoc.ObjectVisibility
oVis.Sketches = oToggle
oVis.Sketches3D = oToggle
oVis.UserWorkPlanes = oToggle
oVis.UserWorkAxes = oToggle
oVis.UserWorkPoints = oToggle
oVis.OriginWorkPlanes =oToggle
oVis.OriginWorkAxes = oToggle
oVis.OriginWorkPoints = oToggle
End Sub
Sub SaveFile(oDoc As Document, NewFileName As String)
Try
oDoc.SaveAs(NewFileName, False)
Catch
MessageBox.Show(oDoc.DisplayName & " cannot be overwritten.", "Terminating")
oDoc.Close(True)
Exit Sub
End Try
End Sub
Function RefinePartsMin(mainCol As ObjectCollection, limit As Double) As ObjectCollection
Dim Result As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each Oc As DerivedAssemblyOccurrence In mainCol
Dim oBox As Box = Oc.ReferencedOccurrence.RangeBox
Dim diagonal As Vector = oBox.MinPoint.VectorTo(oBox.MaxPoint)
If diagonal.Length <= limit Then Result.Add(Oc)
Next
Return Result
End Function
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
Let me know if you have any questions, or if this is not working as intended.