Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

What are the rules for links and renaming parts?

MTheDesigner
Advocate

What are the rules for links and renaming parts?

MTheDesigner
Advocate
Advocate

I have an assembly that I would like to copy and use in another project. However, I need to rename all the parts in the assembly as well as the assembly, I need to change the 4 digit project identifier on everything.

I have tried renaming files with windows explorer but It was tedious and broke all my links that had to be relinked by hand. I tried using design assistant but it was also tedious renaming everything.

I would like to write a code that just handles that job for me. I know how to rename things in ilogic and vba but I don't know how to change the file references or if some commands handle that for me. I would love to take my time and figure everything out, but we are time crunched, so if I can't figure it out by the end of the day I just have to brute force it.

0 Likes
Reply
Accepted solutions (1)
888 Views
12 Replies
Replies (12)

theo.bot
Collaborator
Collaborator

MTheDesigner
Advocate
Advocate
Yes I have, the problem with that is that i can only add a suffix, I can't replace parts of the part name
Our naming standard goes Part-Function-Job Number

So if I have an L3-13TP-4424 and I try to use the ilogic design copy tool to add the job number 5565 I get
L3-13TP-44245565 when it should be L3-13TP-5565
0 Likes

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @MTheDesigner 

 

You can just do a SaveAs and the old files are replaced with the new files automatically.

 

This example works with assemblies... it does not work with drawings to trade out references.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Sub Main
	oADoc = ThisApplication.ActiveDocument

	'[ example D:\4444\NG-22-4444-001	
	'oSplit(0) returns "NG"
	'oSplit(1) returns "22"
	'oSplit(2) returns "4444"
	'oSplit(3) returns "001"
	'split the string using the dashes
	']
	oSplit = Split(oADoc.FullFileName, "-")

	'extract the old part number 
	oOldPN = oSplit(2)

	'get new part number from user
	oNewPN = InputBox("Enter New Part Number", "iLogic", oOldPN)

	'skip if the input is blank or the same as the original
	If oNewPN = "" Or oNewPN = oName Then Exit Sub

	'example retturns "D:\4444"
	oOldPath = IO.Path.GetDirectoryName(oADoc.FullFileName)
	oNewpath = Replace(oADoc.FullFileName, oOldPN, oNewPN)

	If Not System.IO.Directory.Exists(oNewpath) Then
		System.IO.Directory.CreateDirectory(oNewpath)
	End If

	oOccNewFileName = Replace(oADoc.FullFileName, oOldPN, oNewPN)
	'save new assembly
	oADoc.SaveAs(oOccNewFileName, False)

	'process components
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Call TraverseAssembly(oOccs, oOldPN, oNewPN)

End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences, oOldPN As String, oNewPN As String)

	Dim oList As New ArrayList
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		oOccDoc = oOcc.Definition.Document
		oOccDocFullName = oOccDoc.fullfilename
		Logger.Info("old file name: " & oOccDocFullName)

		'skip virtual components
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For

		'replace old part number with new part number
		oOccNewFileName = Replace(oOccDoc.fullfilename, oOldPN, oNewPN)
		Logger.Info("new file name: " & oOccNewFileName)

		'handle occurence if it's document has already been processed
		If oList.Contains(oOccDocFullName) Then
			Component.Replace(oOcc.Name, oOccNewFileName, True)
		End If

		'add new name to the list
		'so additional occurence of the file 
		'can be skipped
		oList.Add(oOccNewFileName)

		'skip CC files
		oDef = oOcc.Definition
		If UCase(oExt) = ".IPT" Then
			If oDef.IsContentMember = True Then Continue For
		End If


		If oList.Contains(oOccDocFullName) = False Then

			'update iproperty
			oPN = Replace(IO.Path.GetFileNameWithoutExtension(oOccDocFullName), oOldPN, oNewPN)
			oOccDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oPN

			'save the occurence document 
			'replaces all occurences of it
			'in this assembly
			oOccDoc.SaveAs(oOccNewFileName, False)

			'step into subassembly
			If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				Call TraverseAssembly(oOccDoc.ComponentDefinition.Occurrences, oOldPN, oNewPN)
			End If

		End If
	Next
End Sub

 

MTheDesigner
Advocate
Advocate

An Incredibly detailed and useful solution, as always.

 

Thanks very much for your help @Curtis_Waguespack 

MTheDesigner
Advocate
Advocate

Hey @Curtis_Waguespack Just a heads up, It looks like this code gets stuck in an infinite loop around a replace all command. I checked the parts folder to see its progress, I noticed that it made a folder and then the folder disappeared. After that no part names changed and it got stuck in the loop for 20 min. I am still troubleshooting to see what could have gone wrong, I will let you know if I find anything.

 

0 Likes

Curtis_Waguespack
Consultant
Consultant

Hi @MTheDesigner 

 

My apologies for that.... I probably should have included a "make sure your work is saved, and use at your own risk" warning with that.

 

I can't guess as to what the issue would be, but you might try it with a simple assembly and work your way back up in complexity until you find the issue.

 

It worked on my example, but it was only about 20 files ( one assembly containing 5-6 parts and 3 subassemblies)... but nothing real involved.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

MTheDesigner
Advocate
Advocate

Its no problem, I always make a copy of the project and a test project to work from when it comes to new code. I am also using inventor 2019 which might cause issues. I can at this time confirm that SaveAs with a different name does not get rid of the old file. 

0 Likes

MTheDesigner
Advocate
Advocate

@Curtis_Waguespack How would this code deal with library parts? Because we can not rename library parts, would this cause the code to error out?

0 Likes

Curtis_Waguespack
Consultant
Consultant

@MTheDesigner wrote:

How would this code deal with library parts? Because we can not rename library parts, would this cause the code to error out?



Hi @MTheDesigner,

Yes, you would need to handle library files if they are in the mix. In the past I've just checked the file path to see if it contains the library path, as in the example below. 

 

If oOccDocFullName.contains("D:Engineering\LibraryFiles") Then Continue For

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

MTheDesigner
Advocate
Advocate

Hey @Curtis_Waguespack,

I am working on this code again and while using save as and replace does work, it is very slow, and can sometimes take up to 15 minutes to run. It really seems to be slow around the replace all occurrences code. It takes me only slightly longer to rename the parts with code, reopen the document and just relink the parts with Ctrl+C Ctrl+V. The relink function that opens when the assembly can't find a part file seems to run very quickly, is there any way that you know of to access the relink functionality through code? 

0 Likes

Curtis_Waguespack
Consultant
Consultant

Hi @MTheDesigner 

 

If you're just replacing a few components, then we can use a select, save, replace rule to do this more quickly. 

 

My understanding or assumption was that you were wanting save and replace all the components in the assembly to create a new design copy.

 

If that is the case, here is an updated version of the example rule, with some logger lines in place that might help you determine exactly where it is running slowly. Note you might want to turn on the Detailed Trace. 

 

Be sure to open the iLogic Logger to see the information reported.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Click the + sign next to the Model browser, and then choose iLogic Log from the list.

Curtis_Waguespack_2-1655831377295.png

 

Turn on detailed trace

 

Curtis_Waguespack_1-1655829941581.png

 

Sub Main
	oADoc = ThisApplication.ActiveDocument

	'[ example D:\4444\NG-22-4444-001	
	'oSplit(0) returns "NG"
	'oSplit(1) returns "22"
	'oSplit(2) returns "4444"
	'oSplit(3) returns "001"
	'split the string using the dashes
	']
	oSplit = Split(oADoc.FullFileName, "-")

	'extract the old part number 
	oOldPN = oSplit(2)

	'get new part number from user
	oNewPN = InputBox("Enter New Part Number", "iLogic", oOldPN)

	'skip if the input is blank or the same as the original
	If oNewPN = "" Or oNewPN = oName Then Exit Sub

	'example retturns "D:\4444"
	oOldPath = IO.Path.GetDirectoryName(oADoc.FullFileName)
	oNewpath = Replace(oADoc.FullFileName, oOldPN, oNewPN)

	If Not System.IO.Directory.Exists(oNewpath) Then
		System.IO.Directory.CreateDirectory(oNewpath)
	End If

	oOccNewFileName = Replace(oADoc.FullFileName, oOldPN, oNewPN)
	'save new assembly
	oADoc.SaveAs(oOccNewFileName, False)

	'process components
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Call TraverseAssembly(oOccs, oOldPN, oNewPN)

End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences, oOldPN As String, oNewPN As String)

	Dim oList As New ArrayList
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		oOccDoc = oOcc.Definition.Document
		oOccDocFullName = oOccDoc.fullfilename
		Logger.Info("old file name: " & oOccDocFullName)

		'skip CC parts, library parts, & virtual components		
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Or _
			oOcc.Definition.IsContentMember Or _
			oOccDocFullName.contains("D:Engineering\LibraryFiles") Then
			Logger.Info(oOccDocFullName & " skipped.")
			Continue For
		End If

		'replace old part number with new part number
		oOccNewFileName = Replace(oOccDoc.fullfilename, oOldPN, oNewPN)
		Logger.Info("new file name: " & oOccNewFileName)

		'handle occurence if it's document has already been processed
		If oList.Contains(oOccDocFullName) Then
			Logger.Info(oOccNewFileName & " added to skip list")
			Component.Replace(oOcc.Name, oOccNewFileName, True)
		End If

		'add new name to the list
		'so additional occurence of the file 
		'can be skipped
		oList.Add(oOccNewFileName)

		If oList.Contains(oOccDocFullName) = False Then

			'update iproperty
			oPN = Replace(IO.Path.GetFileNameWithoutExtension(oOccDocFullName), oOldPN, oNewPN)
			oOccDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oPN
			Logger.Info("iProperty Updated.")

			'save the occurence document 
			'replaces all occurences of it
			'in this assembly
			oOccDoc.SaveAs(oOccNewFileName, False)
			Logger.Info(oOccNewFileName & " Saved.")

			'step into subassembly
			If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				Logger.Info("Stepping into " & oOcc.Name)
				Call TraverseAssembly(oOccDoc.ComponentDefinition.Occurrences, oOldPN, oNewPN)
			End If

		End If
	Next
End Sub

 

0 Likes

Curtis_Waguespack
Consultant
Consultant

Here is a very generic Select/Save/Replace rule

 

sPath = "C:\Temp\"

While True

	'get date time to use as name
	sName = Now()
	sName = Replace(sName, ":", "_")
	sName = Replace(sName, "/", "_")
	sName = Replace(sName, " ", "_")
	
	'select component
	oMsg = "Select component (press ESC to continue) "
	Dim oOcc As ComponentOccurrence
	oOcc = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kAssemblyOccurrenceFilter, oMsg)

	' If nothing gets selected then we're done	
	If IsNothing(oOcc) Then Exit While

	'get document from occurence
	Dim oDoc As Document
	oDoc = oOcc.Definition.Document

	'get extension
	oExt = System.IO.Path.GetExtension(oDoc.FullDocumentName)
	
	'put path, name, extension together
	oName = sPath & sName & oExt
	
	'save new file
	oDoc.SaveAs(oName, True)
	
	oCompName = oOcc.Name
	'replace selected component
	Component.Replace(oCompName, oName, True)
	oOcc.Name = oCompName 'set to old browser name (rather than file name)

End While
0 Likes