By Barbara Han
Issue
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?
Solution
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:
VBA code:
Sub replaceComponent()
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)
End Sub
Equivalent C# code:
public static void replaceComponent()
{
try
{
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)
{
invCO.ParentOccurrence.Edit();
invCO.Replace("c:\\Part1.ipt", true);
}
}
catch
{
}
}