Why is Inventor not recognizing the occurrences for Component.Replace?

Why is Inventor not recognizing the occurrences for Component.Replace?

bbhattaraiSZ83G
Participant Participant
184 Views
2 Replies
Message 1 of 3

Why is Inventor not recognizing the occurrences for Component.Replace?

bbhattaraiSZ83G
Participant
Participant

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.

0 Likes
185 Views
2 Replies
Replies (2)
Message 2 of 3

johnsonshiue
Community Manager
Community Manager

Hi! I could be wrong but I don't think the ability of replacing all occurrences of a component in an assembly has been implemented in Inventor. It is always for components within the same level, not across the levels. 

The only brutal way to do that is to rename the component file name so that Inventor cannot find it. Then resolve to the new component. I believe Vault may have the ability to replace components across levels.

Many thanks!

 



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
0 Likes
Message 3 of 3

ohdaddy
Observer
Observer

Well explained

Owner of https://ohdady.net/
0 Likes