Derived Assembly, Including specific entities
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm failing to do something that I thought would be very simple. It seems pretty easy when deriving a part, but it's a nightmare with an assembly. The goal is simple:
1. Prompt the user to pick an assembly from file explorer to derive into the active document.
2. Traverse through the assembly recursively with all it's sub occurrences, adding each planar sketch it finds to a list.
3. Prompt the user to pick a sketch to derive from the list.
4. Derive the assembly in and exclude everything except the chosen sketch.
This is the closest I've gotten, but it requires me to suppress the body. I just want to exclude the body completely.
Sub Main()
Dim oFileDlg As New System.Windows.Forms.OpenFileDialog
oFileDlg.Filter = "Inventor Assemblies (*.iam)|*.iam"
If oFileDlg.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return
Dim sourcePath As String = oFileDlg.FileName
Dim oSourceDoc As AssemblyDocument = ThisApplication.Documents.Open(sourcePath, False)
Dim available As New List(Of String)
For Each oOcc As ComponentOccurrence In oSourceDoc.ComponentDefinition.Occurrences
Try : For Each oSketch In oOcc.Definition.Sketches
available.Add(oOcc.Name & "|" & oSketch.Name)
Next : Catch : End Try
Next
oSourceDoc.Close(True)
Dim selected As String = InputListBox("Select Sketch:", available, "", "Selection", "Sketches")
If String.IsNullOrEmpty(selected) Then Return
Dim targetOccName As String = selected.Split("|")(0)
Dim targetSketchName As String = selected.Split("|")(1)
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDerAsmComps As DerivedAssemblyComponents = oDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents
Dim oDef As DerivedAssemblyDefinition = oDerAsmComps.CreateDefinition(sourcePath)
oDef.InclusionOption = DerivedComponentOptionEnum.kDerivedExcludeAll
oDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
For Each oOcc As DerivedAssemblyOccurrence In oDef.Occurrences
If oOcc.Name = targetOccName Then oOcc.InclusionOption = DerivedComponentOptionEnum.kDerivedIncludeAll
Next
Dim oComp As DerivedAssemblyComponent = oDerAsmComps.Add(oDef)
For Each oRef As ReferenceFeature In oComp.ReferenceFeatures
If Not oRef.Name.Contains(targetSketchName) Then oRef.Suppressed = True
Next
End Sub
This is the result in the model browser:
Here's another approach that uses a linked design view, but it still requires the solids to be derived:
Sub Main()
Dim oFileDlg As New System.Windows.Forms.OpenFileDialog
oFileDlg.Filter = "Inventor Assemblies (*.iam)|*.iam"
If oFileDlg.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return
Dim sourcePath As String = oFileDlg.FileName
Dim oSourceDoc As AssemblyDocument = ThisApplication.Documents.Open(sourcePath, False)
Dim oAsmDef As AssemblyComponentDefinition = oSourceDoc.ComponentDefinition
Dim available As New List(Of String)
For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences
Try : For Each oSketch In oOcc.Definition.Sketches
available.Add(oOcc.Name & "|" & oSketch.Name)
Next : Catch : End Try
Next
Dim selected As String = InputListBox("Select Sketch:", available, "", "Selection", "Sketches")
If String.IsNullOrEmpty(selected) Then oSourceDoc.Close(True) : Return
Dim targetOccName As String = selected.Split("|")(0)
Dim targetSketchName As String = selected.Split("|")(1)
Dim oViewRep As DesignViewRepresentation
Try : oViewRep = oAsmDef.RepresentationsManager.DesignViewRepresentations.Add("SurgicalView")
Catch : oViewRep = oAsmDef.RepresentationsManager.DesignViewRepresentations.Item("SurgicalView") : End Try
oViewRep.Activate()
For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences
oOcc.Visible = (oOcc.Name = targetOccName)
If oOcc.Name = targetOccName Then
For Each oBody As SurfaceBody In oOcc.Definition.SurfaceBodies : oBody.Visible = False : Next
For Each oSketch As PlanarSketch In oOcc.Definition.Sketches : oSketch.Visible = (oSketch.Name = targetSketchName) : Next
End If
Next
oSourceDoc.Save()
oSourceDoc.Close(True)
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As DerivedAssemblyDefinition = oDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(sourcePath)
oDef.InclusionOption = DerivedComponentOptionEnum.kDerivedIndividualDefined
oDef.ActiveDesignViewRepresentation = "SurgicalView"
oDef.IsAssociativeDesignView = True
For Each oDerOcc As DerivedAssemblyOccurrence In oDef.Occurrences
If oDerOcc.Name = targetOccName Then oDerOcc.InclusionOption = DerivedComponentOptionEnum.kDerivedIncludeAll
Next
oDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDef)
End Sub
Here's the result in the model browser:
This is the result I want to achieve, which can easily be done manually with the standard derive dialog:
Can anybody tell me the correct way to do this?