Derived Assembly, Including specific entities

Derived Assembly, Including specific entities

HogueOne
Advocate Advocate
553 Views
3 Replies
Message 1 of 4

Derived Assembly, Including specific entities

HogueOne
Advocate
Advocate

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:

 

Screenshot 2026-02-23 100326.png

 

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:

 

Screenshot 2026-02-23 100430.png

 

This is the result I want to achieve, which can easily be done manually with the standard derive dialog:

 

Screenshot 2026-02-23 100536.png

 

Can anybody tell me the correct way to do this?

0 Likes
554 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @HogueOne.  This is not really something I do much by code, but have done similar manually many times.  Have you tried doing it manually, the way you know works the way you want, then 'inspect' it by code afterwards, to see how everything is set?  Sometimes those 'reverse engineer' steps work out pretty well, depending on the circumstances.  This may not be the most ideal route, but one thing that comes to mind is a bit of preparation work just ahead of the actual derive step, similar to what you were doing with the DVR (DesignViewRepresentation) version of your code above, but instead of using the Visible property, using the Exported property instead.  Both the SurfaceBody object and the PlanarSketch object have that property, which dictates if they will be included when derived (SurfaceBody.Exported & PlanarSketch.Exported).  Then just set the Design View setting to the default [Primary] or Master, to avoid that hassle, which opens up more possibilities to exclude bodies, no matter if they were visible in that view or not.  It might also help to uncheck (set to False) the 'Link face color from source component' setting, because if that was on, it might create some sort of dependency on at least one 'body' being included.  Just some thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

HogueOne
Advocate
Advocate

Thank you very much for replying. I've been trying this, but I haven't been successful yet. Are you sure your logic isn't exclusive to derived parts? there's a lot more control over parts than assemblies. Could I trouble you to give me just a small snippet of what you mean?

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Hi @HogueOne.  I do not have a working code example for that exact scenario to share right now, because I have never needed to derive only a single specific sketch of a single specific component in a specific assembly by code before.  And now that we have Vault Professional involved on our systems, creating a quickie code example is not that easy, due to pretty much everything being 'ReadOnly', unless we create multiple new files just for testing, and do not put them into Vault yet.  After a closer look at all of the available code resources, I can see why this is challenging, if possible at all.  There does not appear to be a good way to modify those types of settings directly within the derive definition.  It seems like we would have to modify the actual files involved in this process, as you are doing in your second code example, where you are changing object visibility then saving the files before doing the derive.  I was just suggesting that in the same area of your second code example, where you are setting the Visibility of those objects, also set their 'Exported' property.  If I were to try that I would get errors, because of the files being ReadOnly, unless I put a bunch of extra work into it and checked existing design files out, or created new ones, just for testing purposes.  If you already tried setting 'Exported' and it is not working, then I may be wrong, but it seems perfectly logical that it should work, since that seems to be the very purpose of those properties.  I'm heading out for the day, but will check back tomorrow, and if time permits, I may be able to create a special set of files outside of Vault to test with.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)