Why is Inventor not recognizing the occurrences for Component.Replace?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to write a subroutine where I make local copies of a template assembly and two of its component part files, then Component Replace the template part files within the local assembly to their local part files. I've managed to successfully copy the assembly and parts, but for whatever reason, Inventor will not recognize the components within the assembly in order to Component Replace them. Here's the code I'm using:
Sub createAssembly() Dim newAssemblyAddress As String Dim newComponentOneAddress As String Dim newComponentTwoAddress As String ' Create local copies of the files newAssemblyAddress = makeLocalCopy(templateAssemblyAddress) newComponentOneAddress = makeLocalCopy(templateComponentOneAddress) newComponentTwoAddress = makeLocalCopy(templateComponentTwoAddress) Call replaceComponent("VTHFL", newComponentOneAddress, newAssemblyAddress) Call replaceComponent("COLUMN", newComponentTwoAddress, newAssemblyAddress) 'Call listOccurrences(newAssemblyAddress) End Sub
Function makeLocalCopy(originalFilePath As String) As String ' Get the active project folder path Dim activeProjectFolder As String = ThisDoc.WorkspacePath ' Define the PARTS subfolder path Dim partsFolder As String = System.IO.Path.Combine(activeProjectFolder, "PARTS") ' Ensure the PARTS folder exists, if not create it If Not System.IO.Directory.Exists(partsFolder) Then System.IO.Directory.CreateDirectory(partsFolder) End If ' Declare the destination path Dim destinationPath As String ' Replace the word "TEMPLATE" in the originalFilePath with "LOCAL"
Dim modifiedFilePath As String = originalFilePath.Replace("TEMPLATE", "LOCAL") ' Check the file extension to determine if it's a part or assembly If System.IO.Path.GetExtension(modifiedFilePath).ToLower() = ".iam" Then ' If it's an assembly, copy to the project folder destinationPath = System.IO.Path.Combine(activeProjectFolder, System.IO.Path.GetFileName(modifiedFilePath)) ElseIf System.IO.Path.GetExtension(modifiedFilePath).ToLower() = ".ipt" Then ' If it's a part, copy to the PARTS subfolder destinationPath = System.IO.Path.Combine(partsFolder, System.IO.Path.GetFileName(modifiedFilePath)) Else ' If the file is neither a part nor assembly, return an empty string or handle as needed destinationPath = "" End If ' Copy the file using the modified path ThisApplication.FileManager.CopyFile(originalFilePath, destinationPath, FileManagementEnum.kCopyFileMask) ' Return the destination path of the copied file Return destinationPath End Function
Sub replaceComponent(oldComponentName As String, newComponentAddress As String, assemblyAddress As String) ' Open the assembly document in the background Dim assemblyDoc As Document assemblyDoc = ThisApplication.Documents.Open(assemblyAddress, False) ' Access the assembly component definition Dim assemblyCompDef As AssemblyComponentDefinition assemblyCompDef = assemblyDoc.ComponentDefinition Dim targetOccurrence As ComponentOccurrence Dim asmOcc As ComponentOccurrence For Each asmOcc In assemblyCompDef.Occurrences ' Identify and assign components based on name If asmOcc.Name.Contains(oldComponentName) Then targetOccurrence = asmOcc Component.Replace(targetOccurrence.Name, newComponentAddress, True) Else MsgBox("No component by that name was found") End If Next ' Save and close the assembly assemblyDoc.Save() assemblyDoc.Close() End Sub
Sub listOccurrences(assemblyAddress As String) Dim assemblyDoc As Document assemblyDoc = ThisApplication.Documents.Open(assemblyAddress, False) Dim assemblyCompDef As AssemblyComponentDefinition assemblyCompDef = assemblyDoc.ComponentDefinition Dim occurrences As ComponentOccurrences occurrences = assemblyCompDef.Occurrences Dim occurrence As ComponentOccurrence Dim output As String output = "Occurrences in assembly:" & vbCrLf For Each occurrence In occurrences output = output & occurrence.Name & vbCrLf Next MsgBox(output) assemblyDoc.Close() End Sub
The template file addresses are declared as global variables so that's why I'm not declaring them inside the routine.
I am using listOccurrences as a debugging tool to make sure I'm grabbing the right component names, and it's returning the right ones. I've checked endlessly in the template assembly file to make sure that I have the right component names and as far as I'm aware everything seems right, so I really can't tell what's going on.
I've used a similar algorithm for replacing components before in other projects and it's always worked without fail. However, there is one key difference between this usage and the previous ones -- every other time I've used this algorithm for replacing components has been within a local rule, whereas this time I'm trying to use it in a global rule (hence the copying from a template location). Does that have any impact?
I'm not formally trained in iLogic, just learning as I go by referencing the forums and ChatGPT, so apologies if I'm missing something very basic.