Inventor 2016 – iLogic – Replace a Component In a Sub-Assembly

Marketing
Marketing
  • Updated

By Luke Davenport 

Problem:

You need to replace a component using iLogic, but the component is contained within a sub-assembly, not the top-level assembly.

Solution:

Well – I’ll ruin the suspense. As far as iLogic is concerned, there’s no difference between replacing a component in a sub-assembly, or the top-level assembly. 

But it took me a while to figure this out! 

Here was my thought process, using the above picture as an example:

Ok, so If we want to replace the sub-component called ‘CHANNEL2:1’ with another part, we’ll first need to activate the sub-assembly called ‘SUB-ASM1:1’ for edit. Otherwise we can’t edit it. This is exactly what happens if you try to do this manually:

Therefore in my naiveté I assumed that we have to do the same thing using iLogic – with a couple of lines of code such as this:

 

' Define the sub-assembly we want to replace something in

Dim oOcc As ComponentOccurrence = Component.InventorComponent("SUB-ASM1:1")

                    ' Activate it for edit

oOcc.Edit

 

So I went off into my iLogic coding happy place, WITHOUT TESTING whether iLogic actually requires you to be editing a sub-assembly to replace. 

And guess what? It doesn’t. As long as the sub assembly is writeable (not read-only) then it appears that you can simply use a standard iLogic replace line of code such as this: 

' Replace the desired component with an alternative

Component.Replace("CHANNEL2:1", "CHANNEL1.ipt", True)

Without specifying the sub-assembly that ‘CHANNEL2:1’ is in. And that’s it. Its excellent news for me – although I do feel a bit silly about spending an hour on it!

One caviat (for the more advance iLogic users….)

 

If you have more than one occurrence with the same name contained within different sub-assemblies, the above method will replace only the first instance of these, as you haven’t specified which sub-assembly the component is contained within.

So if you want a little more control of this process, feel free to use the code below – it activates a sub-assembly of your choice, and then runs the ‘replace’ operation from within that sub-assembly.

Download the code below also if you like (either with or without error checking included) 

Thanks!

 

' Start of iLogic rule =======================================================

 

' This rule replaces a component in a sub-assembly with an alternative, including

' error checking to ensure the component names are correct. 

Dim oAsmDoc As AssemblyDocument = Nothing

Dim oOcc As ComponentOccurrence = Nothing

' Define the names of the components...

Dim SubAssemblyName As String = "SUB-ASM1:1"

Dim OriginalCompName As String = "CHANNEL1:1"

Dim NewFileName As String = "CHANNEL2.ipt"

' Check we are in an assembly file...

Try

oAsmDoc = ThisApplication.ActiveEditDocument

Catch

MsgBox("This rule can only be run from an assembly file - it should be run from top-level assembly", 64, "Cadline iLogic")

Return

End Try

 

' Define the sub-assembly we want to replace something in

Dim oSubAsmOcc As ComponentOccurrence = Component.InventorComponent(SubAssemblyName)

 

' Activate it for edit

oSubAsmOcc.Edit

 

Try

' Check that the component we want to replace exists...

oOcc = Component.InventorComponent(OriginalCompName)

Catch

    MsgBox("There is no component called '" & OriginalCompName & "' in sub assembly '" & SubAssemblyName & "' - Exiting....", 64, "Cadline iLogic")

        ' Finish editing sub-assembly and return to the top-level

    oSubAsmOcc.ExitEdit(ExitTypeEnum.kExitToTop)

    Exit Sub

End Try

 

Try

' Replace the desired component with an alternative

Component.Replace(OriginalCompName, NewFileName, True)

Catch

    MsgBox("Couldn't find a file called '" & NewFileName & "' in the same folder as the current assembly - Exiting....", 64, "Cadline iLogic")

        ' Finish editing sub-assembly and return to the top-level

    oSubAsmOcc.ExitEdit(ExitTypeEnum.kExitToTop)

    Exit Sub

End Try

 

' Finish editing the sub-assembly and return to the top-level

oSubAsmOcc.ExitEdit(ExitTypeEnum.kExitToTop) 

' End of iLogic rule =======================================================

 

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.