By Barbara Han
Assuming you have a assembly structure as shown below:
Main Assembly
|
+—Part1
|
—-sub-assembly
|
|
+—Part1
Now you want to replace part1.ipt in the sub-assembly by selecting it in the browser tree and then use the Replace command to replace it with Part2.ipt.
How can we do the replacing through code? First, you have to traverse the assembly tree and check whether an occurrence is the selected one; then get the parent occurrence of the selected occurrence, that will be the sub-assembly, then use the index to get the occurrence from the definition of the parent occurrence; finally call the ComponentOccurrence.Replace() method.
The following sample code shown below demonstrates this. To test the code you need to create a similar assembly structure.
VBA code:
Public Sub ReplacePart()
‘ Get an occurrence from the select set.
On Error Resume Next
Dim oOccurrence As ComponentOccurrence
Set oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)
If Err Then
MsgBox "An occurrence must be selected."
Err.Clear
On Error GoTo 0
Exit Sub
End If
‘get the parent of this occurrence
Dim oParentOcc As ComponentOccurrence
Set oParentOcc = oOccurrence.ParentOccurrence
‘ Now get the selected occurrence in scope of parent definition
‘ Make sure you got parent occurrence
If (Not oParentOcc Is Nothing) Then
Call getOccInScopeOfSubAsmDef(oParentOcc, oOccurrence)
If (oOccurrence Is Nothing) Then
Exit Sub
End If
End If
‘ Now replace will work as desired
On Error Resume Next
oOccurrence.Replace "d:\temp\replacetest\part2.ipt", False
If Err Then
Err.Clear
On Error GoTo 0
MsgBox "Failed to replace component"
End If
End Sub
Private Sub getOccInScopeOfSubAsmDef(oParentOcc As ComponentOccurrence, ByRef oOccurrence As ComponentOccurrence)
Dim indx As Integer
indx = 1
Dim bFound As Boolean
bFound = False
Dim oOcc As ComponentOccurrence
For Each oOcc In oParentOcc.SubOccurrences
If oOcc Is oOccurrence Then
bFound = True
Exit For
End If
indx = indx + 1
Next
If (bFound) Then
Set oOccurrence = oParentOcc.Definition.Occurrences(indx)
Else
Set oOccurrence = Nothing
End If
End Sub
VB.NET code:
public static void ReplacePart()
{
try
{
Inventor.Application app = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
// Get an occurrence from the select set.
ComponentOccurrence oOccurrence = app.ActiveDocument.SelectSet[1] as ComponentOccurrence;
//get the parent of this occurrence
ComponentOccurrence oParentOcc = oOccurrence.ParentOccurrence;
//Now get the selected occurrence in scope of parent definition
// Make sure you got parent occurrence
if (oParentOcc != null)
{
getOccInScopeOfSubAsmDef(oParentOcc, ref oOccurrence);
}
if (oOccurrence != null)
{
oOccurrence.Replace("c:\\temp\\replacetest\\part2.ipt", false);
}
}
catch
{
}
}
public static void getOccInScopeOfSubAsmDef(ComponentOccurrence oParentOcc, ref ComponentOccurrence oOccurrence)
{
int indx = 1;
bool bFound = false;
foreach(ComponentOccurrence oOcc in oParentOcc.SubOccurrences)
{
if(oOcc == oOccurrence)
{
bFound = true;
break;
}
indx = indx + 1;
}
if (bFound)
{
oOccurrence = o
ParentOcc.Definition.Occurrences[indx];
}
else
{
oOccurrence = null;
}
}

Leave a Reply