Component Replace with Target Path

Component Replace with Target Path

W_Barnett
Enthusiast Enthusiast
355 Views
1 Reply
Message 1 of 2

Component Replace with Target Path

W_Barnett
Enthusiast
Enthusiast

***Solved***

1. Manually named components that are replaced will keep their overwritten names.  You can check if "Component.Replace" worked correctly by checking iProperties.

 

2. If a command isn't working as intended, and you know it should, then try restarting Inventor.  Would have saved me about 2 hours this morning if I had just restarted and tried running the rule again.

 

***Original Post***

I've been trying to replace a part file in an assembly using iLogic, however the component is not actually being replaced with anything.  I've tried four different methods:

  • Target object occurrence (oOcc) and replace (oOcc.Replace...)
  • Component Replace using string variables (I want to replace "Comp_A:1" with "P:\...\temp_Comp_A.ipt")
  • Component Replace using manually typed strings.
  • Component Replace using oOcc.Name and file path.

Nothing is working.  I understand that Component.Replace will replace components within the same folder, but I've read several places (including the code snippet) that I can use a target path.  Is there a work around?  Here's the code in question:

 

Sub Main()
	Dim oldPart = "Comp_A:1"
	Dim newPart = "P:\...\temp_Comp_A.ipt".       '*** Demo String for Forum Post.
	
	'*** Method 1
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	oADef = oADoc.ComponentDefinition
	oOccs = oADef.Occurrences
	Dim oOcc As Object
	
	For Each oComp As ComponentOccurrence In oOccs
		If oComp.Name = oldPart Then
			Logger.Debug("oComp is: " & oComp.Name & ".  It should be the same as " & oldPart)
			oOcc = oComp
		End If
	Next
	
	Logger.Debug("Attempting to replace component now...")
	oOcc.Replace(newPart, False)
	
	'*** Method 2
	Component.Replace(oldPart, newPart, False)
	
	
	'*** Method 3
	Component.Replace("Comp_A:1", "temp_Comp_A.ipt", False)
	
	'*** Method 4
	Component.Replace(oOcc.Name, newPart, False)

	InventorVb.DocumentUpdate()
	
End Sub

 

0 Likes
Accepted solutions (1)
356 Views
1 Reply
Reply (1)
Message 2 of 2

W_Barnett
Enthusiast
Enthusiast
Accepted solution

Method 1 works as intended.  Turns out I needed to restart Inventor.

 

I did however learn that manually named components in the model browser do not have their names updated when you "Component.Replace" them.  Makes things extremely confusing.  I couldn't figure out why 4 out of 5 components got replaced correctly, but that one just wouldn't change.

0 Likes