Component Replacement in an assembly using Inventor VBA

Component Replacement in an assembly using Inventor VBA

Anonymous
Not applicable
1,639 Views
4 Replies
Message 1 of 5

Component Replacement in an assembly using Inventor VBA

Anonymous
Not applicable

Hi,

I have an Inventor assembly in which I need to replace some of the components using Inventor VBA code, in short I need to perform the following operation

 
 

1.png

 

Can anyone please help me with the appropriate API and a sample VBA code to replace the component in the assembly by giving the path of the new part.

 

Thanks,

Vishal

0 Likes
1,640 Views
4 Replies
Replies (4)
Message 2 of 5

andrewiv
Advisor
Advisor

This should get you started, you just have to define the path and file name of the new part that you want.

' Set a reference to the active assembly document.
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

' Prompt user to pick an occurrence
Dim oOcc As ComponentOccurrence
Set oOcc = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Pick occurrence to replace")

Dim oPartFileName as String
Set oPartFileName = "Your part path and file name here"
oOcc.Replace(oPartFileName, False)

Andrew In’t Veld
Designer / CAD Administrator

Message 3 of 5

WCrihfield
Mentor
Mentor

Or, if you are just looking for 'command' for "Replace", it is called "AssemblyReplaceCmd".

ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyReplaceCmd").Execute

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

Anonymous
Not applicable

Hi Andrew,

The code looks good, rather there is a very minor change in the last 2 lines which i have corrected.

This code allows me to pick the occurrences manually but I need to replace 4-5 components/parts in an assembly which will be replaced by 4-5 new parts, so picking the occurrences manually each time and replacing those parts will be a tedious task. 

So can we pick the parts in the code itself by providing the part name ?

Also how can we replace 2-3 occurrences of same part with new part ?

Can you please help me with this?

 

0 Likes
Message 5 of 5

WCrihfield
Mentor
Mentor

Here is a VBA macro that you can use that will allow you manually enter the names of the components you want to replace, and the names of the files you want to replace them.  I assumed that is how you wanted it set-up, since you didn't seem to like using 'Pick' to select the component to replace, and you wanted to be able to also provide part names to replace the components with each time.  To use this VBA macro though, you would need to manually edit the code each time before running to change component names and replacement part names.  If I'm wrong about your intent/meaning, the this code could fairly easily be modified to suit slightly different needs.

 

The public Sub "Replace" in the lower part, is set-up so that you can provide either the component's 'component name', or its 'file name without path' or its 'FullFileName', to make it easier, since it is set-up to 'replace all'.  The "ReplacementName" or "newFileName" are set-up to use the 'FullFileName' of the replacement part/sub-assembly.

 

Other ideas would be to use two InputBoxes to ask the user for the component name, and the replacement part name ; or use the 'Pick' command to get the component, then use something like a FileDialog (or similar) to allow you to 'Browse' for the replacement file.

 

Here's the code:

Sub ReplaceAll()
    If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
        Call MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
        Exit Sub
    End If
    Call Replace("ComponentName1", "ReplacementName1", True, True)
    Call Replace("ComponentName2", "ReplacementName2", True, True)
    Call Replace("ComponentName3", "ReplacementName3", True, True)
End Sub

Public Sub Replace(componentName As String, newFileName As String, replaceAll As Boolean, Optional SaveEdits As Boolean = vbNull, Optional KeepAdaptivity As Boolean = vbNull)
    Dim oADoc As AssemblyDocument
    Set oADoc = ThisApplication.ActiveDocument
    Dim oADef As AssemblyComponentDefinition
    Set oADef = oADoc.ComponentDefinition
    Dim oMyComp As ComponentOccurrence
    Dim oComp As ComponentOccurrence
    Dim oFSO As New FileSystemObject
    
    If oFSO.FileExists(newFileName) = False Then
        Call MsgBox("No file could be found matching the specified replacement part's path/name.  Exiting.", , "")
        Exit Sub
    End If
    
    On Error GoTo LoopComps
    Set oMyComp = oADef.Occurrences.ItemByName(componentName)
    If Err = 0 Then GoTo ReplacementCall
LoopComps:
    'that didn't work, so try looping through all components & check both 'file name' and 'FullFileName' of each
    On Error GoTo 0
    Dim oFName As String 'to hold file name, without path
    For Each oComp In oADef.Occurrences
        oFName = oFSO.GetFileName(oComp.Definition.Document.FullFileName)
        If oFName = componentName Then
            oMyComp = oComp
            Exit For
        End If
        If oComp.Definition.Document.FullFileName = componentName Then
            oMyComp = oComp
            Exit For
        End If
    Next
    If oMyComp Is Nothing Then
        Call MsgBox("A component with the name provided could not be found.  Exiting.", , "")
        Exit Sub
    End If
ReplacementCall:
    Call oMyComp.Replace2(newFileName, replaceAll, SaveEdits, KeepAdaptivity)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)