Loading a dialog box through the form to copy and create new files using ilogic

Loading a dialog box through the form to copy and create new files using ilogic

moraesorlando
Advocate Advocate
3,457 Views
25 Replies
Message 1 of 26

Loading a dialog box through the form to copy and create new files using ilogic

moraesorlando
Advocate
Advocate

Hello fellows Inventor experts!
I am here again looking for your help to try to solve an issue.

I have searched here in this forum and on the internet, I found some information about it, but not specific that helped me to achieve my target.

 

Here is my goal: I have a form created to standard an operation performed repeatedly in a manually way.

I don’t know very much about ilogic, but I think that it is possible to build this form.


I need help to place some buttons, fields and create the illogic codes.

 

The buttons and fields I need to create are:
• One field to input some values;
• One button that load a dialog box that allowed the user to select a folder where some files will be pasted. These files are templates and your origin will be always the same, but the destination will be always different;
• One button to start the copy of files;

 

I imagine that these will be the main steps, in details:

1 – Create a field to input the Project number;
2 – Create a button to load a dialog box. This button should be similar to the image 2 in the file attached;
3 – Load a dialog box like shown in the file attached – image 3 (I think it’s a standard dialog box in windows);
4 – Create a button (image 4) to start the copy of files to the folder indicated in the image 3
The name of the files pasted should contain the values informed in the Project Number shown in the image 1 and other standard values;
5 – Show a pop-up message informing that the creation of new files were successfully.

 

I have created the illustrations attached in other office programs, to explain in the best way I could.
I appreciate the help of the colleagues.
Thanks in advance.

0 Likes
Accepted solutions (1)
3,458 Views
25 Replies
Replies (25)
Message 21 of 26

JhoelForshav
Mentor
Mentor

Hi @moraesorlando 

I think that the files are there because they already exists in the destination path. Thats why the CopyFile command fails aswell.

 

Try this rule. If the file exists it'll ask you if you want to replace it 🙂

 

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
			Call ThisApplication.FileManager.CopyFile(origin_path & sFilesToCopy(i), new_path & "\" & tmp_str)
		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")
Message 22 of 26

JhoelForshav
Mentor
Mentor

I noticed an error in the code i first posted so I've edited it. Just thought i should mention that if you've already copied it and it doesn't work 😉

Message 23 of 26

moraesorlando
Advocate
Advocate

Hello @JhoelForshav !

 

I have tested, the folder was completely empty before the rule started to run. It's confirmed did not exist any file in the destination path.

 

When I ran the rule, I received the error message attached several times.

 

The message says to check if the file require is in the origin folder. I checked the file is there (with the correct name).

 

And, the message says that Couldn't copy the file, I click ok, and when I check the destination path, all files are there with the name correct.

 

I fell we are almost there, maybe only some adjusts in the code are necessary.

 

 

 

0 Likes
Message 24 of 26

JhoelForshav
Mentor
Mentor

Hi @moraesorlando 

that error message is the one i added for when the filecopy returns an error so it’s really strange that you get it if the file was copied successfully🤔

Today is the swedish holiday midsummer so I’ll not be able to look in to this further until monday. If @fullevent has time to look at this before I think he will figure it out😊. Maybe try using system.IO to copy the files instead? Seems like there could be some kind of bug in the function currently being used.

I’ll get back to this monday if you guys havn’t solved it by then😊

Message 25 of 26

fullevent
Advisor
Advisor
Accepted solution

Hello together,

 

@JhoelForshav enjoy the midsummer holiday 😊

 

@moraesorlando

I added a little piece. Instead of the FileManager the FileSystem is used. With me both work perfectly. I hope at least version 2 works for you.

 

If you still get an error.. there would be a dirty way..
Since the data will be copied as you say, as soon as you press OK, we can suppress the error message by using the SilentOperation property so it won't pop up. 

 

2020-06-19 09_39_33-Regel bearbeiten_ Regel0.png

 

Entire iLogic rule:

 

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")

 

 

Hope it works out with FileSystem..

 

regards,


Aleksandar Krstic
Produkt- und Projektmanager

Message 26 of 26

moraesorlando
Advocate
Advocate

Hello @fullevent !

 

Success!!! Everything is working properly.

The only message shown is “I hope you like it”

This second version Instead of the FileManager the FileSystem is used, solved the problem.

 

It wasn't necessary to use SilentOperation property.

 

Hello @fullevent !!!

 

Success!!! Everything is working properly.

 

The only message shown is “I hope you like it”

 

This second version using Instead of the FileManager the FileSystem is used, solved the problem.

 

It wasn’t necessary to use SilentOperation property.

 

Thank you very much you @fullevent  and @JhoelForshav!!!

 

I will mark this as solution.

 

Now I am going to work to adapt this situation to my real situation (I have more files, one assembly and a excel sheet in the origin folder that I am needed to copy too).

 

I have opened another post to try to create the links between these new files copied and the assembly contained in this new folder, using iLogic.

The link is:

https://forums.autodesk.com/t5/inventor-customization/restoration-of-broken-links-using-ilogic/td-p/...

 

I would be glad if you guys could take a look @fullevent  and @JhoelForshav 

 

Thank you very much you too for the attention.

 

Best regards,