Restoration of broken links using iLogic

Restoration of broken links using iLogic

moraesorlando
Advocate Advocate
2,266 Views
19 Replies
Message 1 of 20

Restoration of broken links using iLogic

moraesorlando
Advocate
Advocate

Hello fellows Inventor experts!

 

I have a particular situation being solved in other post and this led me to this new asked for help.

 

I have a group of files (one assembly and their components approximately 11).

 

And using ilogic code was possible to copy all these files for a new folder and rename them with new codes.

 

The problem is: due the changes of the file names the link of these files with the assembly was broken.

 

So, I think maybe is possible to create an iLogic code to restore these broken links and I need some assistance to do that.

 

To restore this connection, it is possible to use part of the file name that was not change.

 

I am available to provide any more necessary information.

 

Thanks everyone in advance.

Best regards,

0 Likes
Accepted solutions (1)
2,267 Views
19 Replies
Replies (19)
Message 2 of 20

marcin_otręba
Advisor
Advisor

it is better to correct your copy rule to work without link breaking.

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 3 of 20

WCrihfield
Mentor
Mentor

After your code copies and renames all the files, it will have to continue further.  You will have to 'Replace' each ComonentOccurrence within the oAsemDef.Occurrences with the new ones using oOcc.Replace() or oOcc.Replace2() Sub.  They both have the option to use ReplaceAll, which would be the better choice in this situation.  This should fix your issues.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 20

Anonymous
Not applicable

I just this week wrote a script to do exactly what you are talking about, @WCrihfield is correct in that you really want to use the replace method to do the swap. The key is the order of operation, in my case I was using powershell to make copies of all the files with the new names in a new folder(but leaving the old ones in the same project), opening the top level which will still have the old links, then using replace to swap them all out. After that you can delete the older files.

Message 5 of 20

moraesorlando
Advocate
Advocate

Hello felows @marcin_otręba@WCrihfield  and @Anonymous!

Thanks for your answers.

 

It is very good, look likes the solution is not going to be so hard to achieve.

 

@WCrihfield,could you please give me an example about how it is going to be the code?

I am just starting using iLogic recently, I am beginner in this subject.

 

In a simplified way I have the following situation:

 

Files with original names with links working

DEPT-YYYY-XXX-05-01 SUPPORT.ipt

DEPT-YYYY-XXX-05-01 SUPPORT.dwg

DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt

DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg

DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt

DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg

DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt

DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg

DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt

DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg

DEPT-YYYY-XXX-00-01 MACH ASSEMBLY

 

Files with new names, that require restoration of links

DEPT-2020-101-05-01 SUPPORT.ipt

DEPT-2020-101-05-01 SUPPORT.dwg

DEPT-2020-101-04-01 EXTERNAL PLATE.ipt

DEPT-2020-101-04-01 EXTERNAL PLATE.dwg

DEPT-2020-101-03-01 INTERNAL PLATE.ipt

DEPT-2020-101-03-01 INTERNAL PLATE.dwg

DEPT-2020-101-02-01 SIDE LEFT PLATE.ipt

DEPT-2020-101-02-01 SIDE LEFT PLATE.dwg

DEPT-2020-101-01-01 SIDE RIGHT PLATE.ipt

DEPT-2020-101-01-01 SIDE RIGHT PLATE.dwg

DEPT-2020-101-00-01 MACH ASSEMBLY

 

Thanks in advance,

Best regards,

0 Likes
Message 6 of 20

WCrihfield
Mentor
Mentor

This isn't fully customized to your file names and paths, but in a hurry (because I'm leaving my office) I threw this code together to show one way to do the whole process.

'Specify path for new files
Dim oNewPath As String = "C:\Temp\"
'Get current Assembly (should be open & active)
Dim oCAsm As AssemblyDocument = ThisApplication.ActiveDocument 'Current Assembly
oCAsm.SaveAs(oNewPath & IO.Path.GetFileNameWithoutExtension(oCoCAsm.FullFileName) & "_Copy.iam",False)

''The active assembly should now be the New Assembly (because we said False to SaveCopyAs option)
Dim oNAsm As AssemblyDocument = ThisApplication.ActiveDocument
Dim oNDef As AssemblyComponentDefinition = oNAsm.ComponentDefinition

For Each oOcc As ComponentOccurrence In oNDef.Occurrences
	Dim oOccDoc As Document = oOcc.Definition.Document
	Dim oCPath As String = IO.Path.GetDirectoryName(oOccDoc.FullFileName) & "\"   'Current Path
	Dim oCFName As String = IO.Path.GetFileName(oOccDoc.FullFileName) 'Current File Name (with extension)
	oOccDoc.SaveAs(oNewPath & oCFName)
	oOcc.Replace(oNewPath & oCFName, True)
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 20

moraesorlando
Advocate
Advocate

Hello @WCrihfield !

 

Thanks for code example, analyzing this snippet, I could understand which are the data and the sequence necessary to be implemented in my rule.

 

The first difference I noted, is that in my case the assembly file is not open.

The replacing of links should be performed with the assembly closed.

 

Below is the code I received from other very nice colleagues here on this forum.

 

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument
If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
	MessageBox.Show("No assembly open..", "KrA")
	Exit Sub
End If

Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
Dim sFilesToCopy(9) As String

user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
If Len(user_input) <> 8 Then
	MessageBox.Show("Invalid input..", "KrA")
	Exit Sub
End If

origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
Dim path As String = ""
If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
	new_path = folderBrowserDialog1.SelectedPath
Else
	Return
End If

If new_path = "" Then
	MessageBox.Show("User cancelled out of dialog", "KrA")
	Exit Sub
End If

sFilesToCopy(0) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
sFilesToCopy(2) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
sFilesToCopy(4) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
sFilesToCopy(6) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
sFilesToCopy(8) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"

For i = 0 To 9
	Try
		Dim oProceed As Boolean = True
		tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
		If System.IO.File.Exists(new_path & "\" & tmp_str) Then
			Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
			& "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

			If oResult = DialogResult.Yes
				Try 
					ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
				Catch
				End Try
				System.IO.File.Delete(new_path & "\" & tmp_str)
			Else
				oProceed = False
			End If
		End If
		If oProceed = True
			'ThisApplication.SilentOperation = True		'use this if still getting an error
			Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
			'Call ThisApplication.FileManager.CopyFile(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
			ThisApplication.SilentOperation = False
		End If
	Catch
		MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
	End Try
Next

MessageBox.Show("I hope you like it", "KrA")

 

In my case I need to restore the links between the assembly and the components and the links between the components and their 2D drawings.

 

First let’s talk about the links between the assembly and the components.

 

Important points, that I imagine should be present in the final code.

 

‘Assembly path

The assembly path is provided by the location indicated in the dialog box

 

‘Target of links that must be restore

DEPT-YYYY-XXX-05-01 SUPPORT.ipt

DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt

DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt

DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt

DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt

 

‘Value to be replace

The value that should be replace is YYYY-XXX. The new value was provided by another dialog box (for example 2020-101).

 

I know these are the important points and the steps, but I don’t know how to create the code.

 

Does anyone could help?

 

Thanks in advance,

0 Likes
Message 8 of 20

JhoelForshav
Mentor
Mentor

Hi @moraesorlando 

Since I don't have your files I haven't been able to test this code, but I think something like this should work... Try it and let me know 🙂

 

Sub Main
	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
		MessageBox.Show("No assembly open..", "KrA")
		Exit Sub
	End If

	Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
	Dim sFilesToCopy(10) As String

	user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
	If Len(user_input) <> 8 Then
		MessageBox.Show("Invalid input..", "KrA")
		Exit Sub
	End If

	origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

	Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
	Dim path As String = ""
	If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
		new_path = folderBrowserDialog1.SelectedPath
	Else
		Return
	End If

	If new_path = "" Then
		MessageBox.Show("User cancelled out of dialog", "KrA")
		Exit Sub
	End If

	sFilesToCopy(0) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
	sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
	sFilesToCopy(2) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
	sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
	sFilesToCopy(4) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
	sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
	sFilesToCopy(6) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
	sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
	sFilesToCopy(8) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
	sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"
	sFilesToCopy(10) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.iam"
	Dim prevIpt As String
	Dim copiedParts As New List(Of KeyValuePair(Of String, String))
	For i = 0 To 10
		Try
			Dim oProceed As Boolean = True
			tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
			If System.IO.File.Exists(new_path & "\" & tmp_str) Then
				Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
				& "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

				If oResult = DialogResult.Yes
					Try
						ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
					Catch
					End Try
					System.IO.File.Delete(new_path & "\" & tmp_str)
				Else
					oProceed = False
				End If
			End If
			If oProceed = True
				'ThisApplication.SilentOperation = True		'use this if still getting an error
				Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)

				If tmp_str.EndsWith(".ipt") Then
					prevIpt = new_path & "\" & tmp_str
					copiedParts.Add(New KeyValuePair(Of String, String)(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str))
				End If
				If tmp_str.EndsWith(".dwg")
					Dim oDWG As DrawingDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceFileRef(oDWG, origin_path & sFilesToCopy(i - 1), prevIpt)
					oDWG.Save
					oDWG.Close
				End If
				If tmp_str.EndsWith(".iam")
					Dim oIAM As AssemblyDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					For Each oPair As KeyValuePair(Of String, String) In copiedParts
						ReplaceFileRef(oIAM, oPair.Key, oPair.Value)
					Next
					oIAM.Save
					oIAM.Close
				End If

				'Call ThisApplication.FileManager.CopyFile(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
				ThisApplication.SilentOperation = False
			End If
		Catch
			MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
		End Try
	Next

	MessageBox.Show("I hope you like it", "KrA")
End Sub

Sub ReplaceFileRef(oDoc As Document, FileToReplace As String, NewFile As String)
	Try
		oDoc.File.ReferencedFileDescriptors.Item(FileToReplace).ReplaceReference(NewFile)
	Catch
		'Couldn't replace reference
	End Try
End Sub
Message 9 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav!

 

First of all, thank you very much for your help.

 

I have tested the rule and we are almost there!

 

I ran the rule, have indicated the project number, have indicated the destination folder and clicked Ok.

And those message for resolve link between assembly and components was shown (image attached).

 

The message asks for indicate the path of the original files (those with name YYYY-XXX). I have clicked in skip all.

 

I went to check the destination folder to see the results and all files were there renamed correctly.

 

So, I have opened the assembly to check if the components contained were correct, and they are. All components are correct (with new names).

I also checked the links between the 2D drawings and the 3D models and is correct too.

Very good congratulations!

 

The only adjust to make is eliminate the message of resolve links.

 

I noted that is necessary to have two more files in this package, one is the 2D drawing of the assembly.

The name is:

DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.dwg

I tried to add by myself, I change the number of files from 10 to 11 and add one more line in the list of files in the code, But I wasn’t succeeded. The file was copied but the link between this 2D drawing and the assembly was not changed. Could you please add this new file in the list?

 

The other file is an Excel sheet where information about this project is stored.

Do you think is it also possible to add in this rule a command to copy an Excel file together of these 11 files?

The name of file is:

DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO.xlsx

 

Thanks in advance,

 

Best regards,

0 Likes
Message 10 of 20

JhoelForshav
Mentor
Mentor

Hi @moraesorlando 

I cant replicate the error of getting the assembly to prompt for file references. I think we can get around the problem by using silent operation though. Try this code. I also added your additional files to copy.

 

Try this:

 

Sub Main
	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
		MessageBox.Show("No assembly open..", "KrA")
		Exit Sub
	End If

	Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
	Dim sFilesToCopy(12) As String

	user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
	If Len(user_input) <> 8 Then
		MessageBox.Show("Invalid input..", "KrA")
		Exit Sub
	End If

	origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

	Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
	Dim path As String = ""
	If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
		new_path = folderBrowserDialog1.SelectedPath
	Else
		Return
	End If

	If new_path = "" Then
		MessageBox.Show("User cancelled out of dialog", "KrA")
		Exit Sub
	End If

	sFilesToCopy(0) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
	sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
	sFilesToCopy(2) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
	sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
	sFilesToCopy(4) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
	sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
	sFilesToCopy(6) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
	sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
	sFilesToCopy(8) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
	sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"
	sFilesToCopy(10) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.iam"
	sFilesToCopy(11) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.dwg"
	sFilesToCopy(12) ="DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO.xlsx"
	Dim prevIpt As String
	Dim copiedParts As New List(Of KeyValuePair(Of String, String))
	For i = 0 To 12
		Try
			Dim oProceed As Boolean = True
			tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
			If System.IO.File.Exists(new_path & "\" & tmp_str) Then
				Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
				& "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

				If oResult = DialogResult.Yes
					Try
						ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
					Catch
					End Try
					System.IO.File.Delete(new_path & "\" & tmp_str)
				Else
					oProceed = False
				End If
			End If
			If oProceed = True
				ThisApplication.SilentOperation = True		'use this if still getting an error
				Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)

				If tmp_str.EndsWith(".ipt") Then
					prevIpt = new_path & "\" & tmp_str
					copiedParts.Add(New KeyValuePair(Of String, String)(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str))
				End If
				If tmp_str.EndsWith(".dwg")
					Dim oDWG As DrawingDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceFileRef(oDWG, origin_path & sFilesToCopy(i - 1), prevIpt)
					oDWG.Save
					oDWG.Close
				End If
				If tmp_str.EndsWith(".iam")
					prevIpt = new_path & "\" & tmp_str
					Dim oIAM As AssemblyDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					For Each oPair As KeyValuePair(Of String, String) In copiedParts
						ReplaceFileRef(oIAM, oPair.Key, oPair.Value)
					Next
					oIAM.Save
					oIAM.Close
				End If

				'Call ThisApplication.FileManager.CopyFile(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
				ThisApplication.SilentOperation = False
			End If
		Catch
			ThisApplication.SilentOperation = False
			MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
		End Try
	Next

	MessageBox.Show("I hope you like it", "KrA")
End Sub

Sub ReplaceFileRef(oDoc As Document, FileToReplace As String, NewFile As String)
	Try
		oDoc.File.ReferencedFileDescriptors.Item(FileToReplace).ReplaceReference(NewFile)
	Catch
	End Try
End Sub

 

Message 11 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav !

 

I ran this code and the silent operation, provided perfect results, the messages of resolve links were all eliminated and all the files were copied and renamed correctly. Good idea of yours to use this silent operator!

 

Only one other error message was shown.

That is my fault, I forgot to tell you that this Excel file has linked with the all the 3D files and the assembly file.

These links were created in: manage>parameters>links

 

The Excel file already is copied and renamed correctly, but as I forgot to give you this information before, of course there is no line of code to restore these links between the 3D files and the Excel sheet, and the assembly and Excel sheet.

I am really sorry about that.

 

Thanks a lot for your help, if these links are restored our target will be achieved.

Best regards,

 

 

 

 

0 Likes
Message 12 of 20

JhoelForshav
Mentor
Mentor

Hi @moraesorlando 

This should replace the excel-link in the part files and assembly:

Sub Main
	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
		MessageBox.Show("No assembly open..", "KrA")
		Exit Sub
	End If

	Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
	Dim sFilesToCopy(12) As String

	user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
	If Len(user_input) <> 8 Then
		MessageBox.Show("Invalid input..", "KrA")
		Exit Sub
	End If

	origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

	Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
	Dim path As String = ""
	If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
		new_path = folderBrowserDialog1.SelectedPath
	Else
		Return
	End If

	If new_path = "" Then
		MessageBox.Show("User cancelled out of dialog", "KrA")
		Exit Sub
	End If
	sFilesToCopy(0) = "DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO.xlsx"
	sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
	sFilesToCopy(2) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
	sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
	sFilesToCopy(4) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
	sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
	sFilesToCopy(6) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
	sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
	sFilesToCopy(8) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
	sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
	sFilesToCopy(10) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"
	sFilesToCopy(11) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.iam"
	sFilesToCopy(12) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.dwg"
	
	Dim prevIpt As String
	Dim newExcel As String = new_path & "\" & "DEPT-" & user_input & Right$(sFilesToCopy(0), Len(sFilesToCopy(0)) -13)
	Dim copiedParts As New List(Of KeyValuePair(Of String, String))
	For i = 0 To 12
		Try
			Dim oProceed As Boolean = True
			tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
			If System.IO.File.Exists(new_path & "\" & tmp_str) Then
				Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
				& "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

				If oResult = DialogResult.Yes
					Try
						ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
					Catch
					End Try
					System.IO.File.Delete(new_path & "\" & tmp_str)
				Else
					oProceed = False
				End If
			End If
			If oProceed = True
				ThisApplication.SilentOperation = True		'use this if still getting an error
				Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
				
				If tmp_str.EndsWith(".ipt") Then
					prevIpt = new_path & "\" & tmp_str
					copiedParts.Add(New KeyValuePair(Of String, String)(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str))
					Dim oIPT As PartDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceOLERef(oIPT, sFilesToCopy(0), newExcel)
					oIPT.Save
					oIPT.Close
				End If
				If tmp_str.EndsWith(".dwg")
					Dim oDWG As DrawingDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceFileRef(oDWG, origin_path & sFilesToCopy(i - 1), prevIpt)
					oDWG.Save
					oDWG.Close
				End If
				If tmp_str.EndsWith(".iam")
					prevIpt = new_path & "\" & tmp_str
					Dim oIAM As AssemblyDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					For Each oPair As KeyValuePair(Of String, String) In copiedParts
						ReplaceFileRef(oIAM, oPair.Key, oPair.Value)
					Next
					ReplaceOLERef(oIAM, sFilesToCopy(0), newExcel)
					oIAM.Save
					oIAM.Close
				End If

				'Call ThisApplication.FileManager.CopyFile(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
				ThisApplication.SilentOperation = False
			End If
		Catch
			ThisApplication.SilentOperation = False
			MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
		End Try
	Next

	MessageBox.Show("I hope you like it", "KrA")
End Sub

Sub ReplaceFileRef(oDoc As Document, FileToReplace As String, NewFile As String)
	Try
		oDoc.File.ReferencedFileDescriptors.Item(FileToReplace).ReplaceReference(NewFile)
	Catch
	End Try
End Sub
Sub ReplaceOLERef(oDoc As Document, oldPath As String, newPath As String)
	For Each oTable As ParameterTable In oDoc.ComponentDefinition.Parameters.ParameterTables
		If oTable.FileName = oldPath
			oTable.FileName = newPath
		End If
	Next
	oDoc.Update2(True)
End Sub
Message 13 of 20

WCrihfield
Mentor
Mentor

Nice code.

I think it may be simpler and more stable to replace the value of "tmp_str" with:

tmp_str = sFilesToCopy(i).Replace("YYYY-XXX",user_input)

instead of

tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)

Just a suggestion.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav !

 

This time the results are not so good like the previous code!

 

Running this last code, I received the error message of "Couldn't copy" for all the five 3D models (image attached), but all files are copied and renamed correctly.

Besides, the error message of Resolve links related to Excel file continue being shown.

 

I also tried the suggestion of @WCrihfieldbut the result is the same.

 

0 Likes
Message 15 of 20

JhoelForshav
Mentor
Mentor

@moraesorlando 

Now I have created my own file structure with assembly, parts, drawings and excel document. All with the same names as yours so I could test this properly. There was an error with the sub to replace the excel reference. I fixed it now and for me everything works 100%. I don't know what made the error message appear for you. That I couldn't reproduce.

This code works for me though. Give it a try 🙂

 

Sub Main
	Dim oDoc As Document
	oDoc = ThisApplication.ActiveDocument
	If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
		MessageBox.Show("No assembly open..", "KrA")
		Exit Sub
	End If

	Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
	Dim sFilesToCopy(12) As String

	user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
	If Len(user_input) <> 8 Then
		MessageBox.Show("Invalid input..", "KrA")
		Exit Sub
	End If

	origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

	Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
	Dim path As String = ""
	If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
		new_path = folderBrowserDialog1.SelectedPath
	Else
		Return
	End If

	If new_path = "" Then
		MessageBox.Show("User cancelled out of dialog", "KrA")
		Exit Sub
	End If
	sFilesToCopy(0) = "DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO.xlsx"
	sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
	sFilesToCopy(2) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
	sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
	sFilesToCopy(4) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
	sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
	sFilesToCopy(6) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
	sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
	sFilesToCopy(8) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
	sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
	sFilesToCopy(10) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"
	sFilesToCopy(11) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.iam"
	sFilesToCopy(12) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.dwg"
	
	Dim prevIpt As String
	Dim newExcel As String = new_path & "\" & "DEPT-" & user_input & Right$(sFilesToCopy(0), Len(sFilesToCopy(0)) -13)
	Dim copiedParts As New List(Of KeyValuePair(Of String, String))
	For i = 0 To 12
		Try
			Dim oProceed As Boolean = True
			tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
			If System.IO.File.Exists(new_path & "\" & tmp_str) Then
				Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
				& "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

				If oResult = DialogResult.Yes
					Try
						ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
					Catch
					End Try
					System.IO.File.Delete(new_path & "\" & tmp_str)
				Else
					oProceed = False
				End If
			End If
			If oProceed = True
				ThisApplication.SilentOperation = True		'use this if still getting an error
				Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
				
				If tmp_str.EndsWith(".ipt") Then
					prevIpt = new_path & "\" & tmp_str
					copiedParts.Add(New KeyValuePair(Of String, String)(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str))
					Dim oIPT As PartDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceOLERef(oIPT, sFilesToCopy(0), newExcel)
					oIPT.Save
					oIPT.Close
				End If
				If tmp_str.EndsWith(".dwg")
					Dim oDWG As DrawingDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					ReplaceFileRef(oDWG, origin_path & sFilesToCopy(i - 1), prevIpt)
					oDWG.Save
					oDWG.Close
				End If
				If tmp_str.EndsWith(".iam")
					prevIpt = new_path & "\" & tmp_str
					Dim oIAM As AssemblyDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
					For Each oPair As KeyValuePair(Of String, String) In copiedParts
						ReplaceFileRef(oIAM, oPair.Key, oPair.Value)
					Next
					ReplaceOLERef(oIAM, sFilesToCopy(0), newExcel)
					oIAM.Save
					oIAM.Close
				End If
				ThisApplication.SilentOperation = False
			End If
		Catch
			MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
		End Try
	Next
	ThisApplication.SilentOperation = False
	MessageBox.Show("I hope you like it", "KrA")
End Sub

Sub ReplaceFileRef(oDoc As Document, FileToReplace As String, NewFile As String)
	Try
		oDoc.File.ReferencedFileDescriptors.Item(FileToReplace).ReplaceReference(NewFile)
	Catch
	End Try
End Sub
Sub ReplaceOLERef(oDoc As Document, oldName As String, newPath As String)
	For Each oTable As ParameterTable In oDoc.ComponentDefinition.Parameters.ParameterTables
		If oTable.FileName.Contains(oldName)
			oTable.FileName = newPath
		End If
	Next
	oDoc.Update2(True)
End Sub
Message 16 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav!

 

I am glad you create a structure to help me in this code development.

 

I ran the code and I got  some different results than the previous code.

Here they are:

 

1º - the message of resolve links for the five 3D models continue pop up and the files continue being copied and renamed correctly;

 

2º The link between Excel and the assembly , was restored correctly, is working. Very good!!!

 

3º But the link between the Excel and the 3D models are not working, They continue with the same name of the original Excel (YYYY-XXX). I attached an image of the file named DEPT-2020-122-03-01 INTERNAL PLATE.

 

Please take a look in the image attached.

The Excel file indicated in the link is:

DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO

 

And the expected link is:

DEPT-2020-122-00-01 INI ASS PARAMETER INFO

 

I feel we are almost there, solving one problem each time.

 

About the error  messages, is not possible to use those silent operation again?

The process of copying is OK, It is just necessary eliminate these messages.

 

Best regards,

 

0 Likes
Message 17 of 20

JhoelForshav
Mentor
Mentor

@moraesorlando wrote:

"About the error  messages, is not possible to use those silent operation again?

The process of copying is OK, It is just necessary eliminate these messages."

 

We are using silent operation still. I don't know what the problem is now. It's difficult for me to debug this when it works as expected when I try it...

 

 

 


 

Message 18 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav!

 

I can see your efforts and really thanks for it.

 

As you mentioned, that for you everything worked 100%, I will recreate all these files and run the code again.

 

The package of files I am using could be polluted with several attempt to build this code by myself, perhaps something is avoiding your code of work properly.

 

I believe this is going to work.

I will let you know the result.

 

Tell me, what Inventor version are you using?

 

I just have one last thing to ask you.

 

For the better use of this rule, It is important that this code to be automatically run when the file is opened, and that file should be closed automatically when the rule be finished (after all the files have being created).

 

Probably when the last OK was clicked in the message "I hope you like it".

Could you please add this in the rule?

 

Best regards,

0 Likes
Message 19 of 20

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @moraesorlando 

I'm using Inventor 2020 at the moment.

 

In order to run the rule automatically when the file is opened we'll have to add an Event Trigger for that event.

I guess you don't want the rule to be triggered for the copied file when that is openes aswell, so I added a line to remove all event triggers from the copy.

 

In order to be able to close the document after the rule has finished, the best thing would be to run the rule as an external rule. It's difficult to have the rule close the document in which it's running. You'd have to create a new thread to do that.

 

I have modified a code with regards to these last two requests and also created a screencast to show you how to add the event trigger and external rule. The screencast will also show you that everything is working. My computer could really use a reboot, so code runs a bit slow in the screencast. It was a lot faster when I ran it at home earlier.

 

The rule:

Sub Main
 Dim oDoc As Document
 oDoc = ThisApplication.ActiveDocument
 If Not ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
  MessageBox.Show("No assembly open..", "KrA")
  Exit Sub
 End If

 Dim origin_path As String, new_path As String, user_input As String, tmp_str As String
 Dim sFilesToCopy(12) As String

 user_input = InputBox("What is the new numebr", "YYYY-XXX", "")
 If Len(user_input) <> 8 Then
  MessageBox.Show("Invalid input..", "KrA")
  Exit Sub
 End If

 origin_path = Left$(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))

 Dim folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
 Dim path As String = ""
 If (folderBrowserDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
  new_path = folderBrowserDialog1.SelectedPath
 Else
  Return
 End If

 If new_path = "" Then
  MessageBox.Show("User cancelled out of dialog", "KrA")
  Exit Sub
 End If
 sFilesToCopy(0) = "DEPT-YYYY-XXX-00-01 INI ASS PARAMETER INFO.xlsx"
 sFilesToCopy(1) = "DEPT-YYYY-XXX-05-01 SUPPORT.ipt"
 sFilesToCopy(2) = "DEPT-YYYY-XXX-05-01 SUPPORT.dwg"
 sFilesToCopy(3) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.ipt"
 sFilesToCopy(4) = "DEPT-YYYY-XXX-04-01 EXTERNAL PLATE.dwg"
 sFilesToCopy(5) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.ipt"
 sFilesToCopy(6) = "DEPT-YYYY-XXX-03-01 INTERNAL PLATE.dwg"
 sFilesToCopy(7) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.ipt"
 sFilesToCopy(8) = "DEPT-YYYY-XXX-02-01 SIDE LEFT PLATE.dwg"
 sFilesToCopy(9) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.ipt"
 sFilesToCopy(10) = "DEPT-YYYY-XXX-01-01 SIDE RIGHT PLATE.dwg"
 sFilesToCopy(11) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.iam"
 sFilesToCopy(12) = "DEPT-YYYY-XXX-00-01 MACH ASSEMBLY.dwg"
 
 Dim prevIpt As String
 Dim newExcel As String = new_path & "\" & "DEPT-" & user_input & Right$(sFilesToCopy(0), Len(sFilesToCopy(0)) -13)
 Dim copiedParts As New List(Of KeyValuePair(Of String, String))
 For i = 0 To 12
  Try
   Dim oProceed As Boolean = True
   tmp_str = "DEPT-" & user_input & Right$(sFilesToCopy(i), Len(sFilesToCopy(i)) -13)
   If System.IO.File.Exists(new_path & "\" & tmp_str) Then
    Dim oResult As DialogResult = MessageBox.Show("The file: " & new_path & "\" & tmp_str & " already exists" & vbCrLf _
    & "Replace?", "File already exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

    If oResult = DialogResult.Yes
     Try
      ThisApplication.Documents.ItemByName(new_path & "\" & tmp_str).Close 'Cant delete if open
     Catch
     End Try
     System.IO.File.Delete(new_path & "\" & tmp_str)
    Else
     oProceed = False
    End If
   End If
   If oProceed = True
    ThisApplication.SilentOperation = True
    Call FileSystem.FileCopy(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
    
    If tmp_str.EndsWith(".ipt") Then
     prevIpt = new_path & "\" & tmp_str
     copiedParts.Add(New KeyValuePair(Of String, String)(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str))
     Dim oIPT As PartDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
     ReplaceOLERef(oIPT, sFilesToCopy(0), newExcel)
     oIPT.Save
     oIPT.Close
    End If
    If tmp_str.EndsWith(".dwg")
     Dim oDWG As DrawingDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
     ReplaceFileRef(oDWG, origin_path & sFilesToCopy(i - 1), prevIpt)
     oDWG.Save
     oDWG.Close
    End If
    If tmp_str.EndsWith(".iam")
     prevIpt = new_path & "\" & tmp_str
     Dim oIAM As AssemblyDocument = ThisApplication.Documents.Open(new_path & "\" & tmp_str, False)
     For Each oPair As KeyValuePair(Of String, String) In copiedParts
      ReplaceFileRef(oIAM, oPair.Key, oPair.Value)
     Next
     ReplaceOLERef(oIAM, sFilesToCopy(0), newExcel)
     oIAM.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete() 'Delete trigger
     oIAM.Save
     oIAM.Close
    End If
    ThisApplication.SilentOperation = False
   End If
  Catch
   MessageBox.Show("Please make sure the file: " & origin_path & sFilesToCopy(i) & " exists!", "Couldn't copy file")
  End Try
 Next
 ThisApplication.SilentOperation = False
 MessageBox.Show("I hope you like it", "KrA")
 oDoc.Close()
End Sub

Sub ReplaceFileRef(oDoc As Document, FileToReplace As String, NewFile As String)
 Try
  oDoc.File.ReferencedFileDescriptors.Item(FileToReplace).ReplaceReference(NewFile)
 Catch
 End Try
End Sub
Sub ReplaceOLERef(oDoc As Document, oldName As String, newPath As String)
 For Each oTable As ParameterTable In oDoc.ComponentDefinition.Parameters.ParameterTables
  If oTable.FileName.Contains(oldName)
   oTable.FileName = newPath
  End If
 Next
 oDoc.Update2(True)
End Sub

Screencast:

 

Message 20 of 20

moraesorlando
Advocate
Advocate

Hello @JhoelForshav !

 

First of all, thanks for your video, it is very clarifying.

 

After recreate all the files I bring good news!

 

My first attempt was made in Inventor version 2018.

 

But an error message of "Couldn't copy" continuing being show in one only file due the reasons I couldn't find.

 

As everything worked for you in the Inventor 2020, I also initiated the recreation of files in this version.

 

And everything worked for me too! Very good, congratulations!

 

Besides to inform that you found a solution I would like to say that you were very helpful and attentive.

 

Even experiencing a lot of troubles, you didn't give up.

I really appreciate that. Congratulations.

 

I will mark this as a solution and give you all the Likes

 

I hope counting on you in future posts.

 

Best regards,