Wednesday, March 13, 2019

ComponentOccurrence.Replace method in VBA

Public Sub ReplaceOccurance()
Dim oAssDoc As Inventor.AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument

Dim oAssCompDef As Inventor.AssemblyComponentDefinition
Set oAssCompDef = oAssDoc.ComponentDefinition

Dim oAssOccurences As Inventor.ComponentOccurrences
Set oAssOccurences = oAssCompDef.Occurrences

Dim oCompOccur As Inventor.ComponentOccurrence
For Each oCompOccur In oAssOccurences
MsgBox oCompOccur.Name
'Either you can use the name of the occurence or the index of the occurence
'to replace a particular occurence by a different part or sub assembly

'Please note that if the second parameter is true and the same occurence has multiple instances
'i.e. a car has four wheels, and if the replace is called on any of the wheel occurence with
'second parameter true, all the wheels will be replaced otherwise only that instance
'will be replaced on which will be called.
If oCompOccur.Name = "MYOCCURANCE" Then
Call oCompOccur.Replace("C:\Myoccorence.ipt", False)
End If

'If the occurence is attached by imate and the replacing part or assembly does not have the
'correct imate then the imates will fail
Next
End Sub

In this sample I have used the name of the occurence to replace one. You can use the index too. The indexing is same as the position in the browser pan. For useing the index you need to change the for loop as follows

Dim i As Long
For i = 1 To oAssOccurences.Count
Set oCompOccur = oAssOccurences(i)
Select Case i
Case 1:
'replace at index 1
Case 2:
'replace at index 2
Case Else:
'replace default
End Select
Next

Hope this will help you problem

Manoj

No comments:

Post a Comment