By Barbara Han
I have the following structure: "assembly->subassembly->part" and I'm trying to replace part with another part. The part occurrence is in Edit mode when I run the ComponentOccurrence.Replace() function, but instead of replacing the part, my code adds the new part to subassembly. What is wrong?
It's always worth having a look at the behaviour of the user interface.
In this particular case, when the SubPart is in Edit mode, then in the user interface the "Component->Replace" command is not available in the context menu of the Model Browser.
So, if you want to replace the subcomponent then edit its parent instead. Here is the sample code:
Dim invAD As AssemblyDocument
Set invAD = ThisApplication.ActiveDocument
Dim invCO As ComponentOccurrence
Set invCO = invAD.ComponentDefinition.ActiveOccurrence
If Not invCO.ParentOccurrence Is Nothing Then invCO.ParentOccurrence.Edit
Call invCO.Replace("C:\Part1.ipt", True)
Equivalent C# code:
public static void replaceComponent()
Inventor.Application app = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
AssemblyDocument invAD = app.ActiveDocument as AssemblyDocument;
ComponentOccurrence invCO = invAD.ComponentDefinition.ActiveOccurrence;
if (invCO.ParentOccurrence != null)