Assembly SaveAs New .idw and Rename Parts to Same Folder

Assembly SaveAs New .idw and Rename Parts to Same Folder

swatkins3YWDA
Contributor Contributor
1,561 Views
6 Replies
Message 1 of 7

Assembly SaveAs New .idw and Rename Parts to Same Folder

swatkins3YWDA
Contributor
Contributor

I have found two codes for Save As.  One code will rename my assembly and two parts to new names in the current folder.  I have created a template .iam and named the customizable parts named XXXX WHEEL TEMPLATE and XXXX AXLE TEMPLATE.  The other code allows me to Save As assembly file to a specified folder location using the File Dialog Box.  This will allow me to save the assembly as a specified file name.  This works well but I'm having trouble combining the two codes into one.  My goal is to select folder destination to save as, specified file name, and rename the two customizable parts and save as to the same destination folder.  These two codes together are creating four file names, two .iam's and two .ipt's.  It should only be one .iam and two .ipt's.  Also the file name specified in the first code XXXX Z10 1234AB is not carried over to  the second code where I need it to properly rename the two .ipt files.  Goal is to open a template, trigger iLogic, specify destination, give assembly name, and carry over assembly name as prefix for the two parts. 

 

Thank you,

Samantha

 

Here is the first code,

 

oDoc = ThisDoc.Document

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
If oDoc.DocumentType = kPartDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Else If oDoc.DocumentType = kAssemblyDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
Else If oDoc.DocumentType = kDrawingDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
End If
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
oFileDlg.FileName = iProperties.Value("Project", "Part Number")
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowSave()
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

Second code,

'Save As new Assembly File and Save As new Wheel and Axle Files

Dim Path As String = ThisDoc.Path & "\"

InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document
Dim PartN As String = "XXXX Z10 1234AB"

For Each refDoc In refDocs
    'MessageBox.Show(refDoc.DisplayName)
    If refDoc.DisplayName.EndsWith("WHEEL TEMPLATE.ipt")= True Then
        'MessageBox.Show("found2")
        NewFileName = PartN & " WHEEL.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    ElseIf refDoc.DisplayName.EndsWith("STUB AXLE TEMPLATE.ipt")= True Then
        'MessageBox.Show("found1")
        NewFileName = PartN & " AXLE.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    End If
Next


ThisDoc.Document.SaveAs(Path & PartN & ".iam", False)

iLogicVb.UpdateWhenDone = True


 Inventor Pro 2022, WIN10 Pro, Beginner of iLogic

0 Likes
Accepted solutions (1)
1,562 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

I think the issue is your looking to save the assembly file with the first dialog box  before saving the part files. If you look for the folder path first then save the parts then save the assembly this will work. 

' Get current location of this file
Dim ExportPath As String = ThisDoc.Path

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
	MsgBox("This file has not yet been saved and doesn't exist on disk! - please save it first",64, "File Check")
Return
End If

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
	' User clicked 'ok' on dialog box - capture the export path
	ExportPath = Dialog.SelectedPath & "\"
	
Else
	' User clicked 'cancel' on dialog box - exit
	Return
End If


Dim Path As String = ExportPath
MessageBox.Show(Path, "Directory path")

InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document

Dim PartN As String = "XXXX Z10 1234AB"
PartN = InputBox("Prompt", "Title", PartN)
For Each refDoc In refDocs
    'MessageBox.Show(refDoc.DisplayName)
    If refDoc.DisplayName.EndsWith("WHEEL TEMPLATE.ipt")= True Then
        'MessageBox.Show("found2")
        NewFileName = PartN & " WHEEL.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    ElseIf refDoc.DisplayName.EndsWith("STUB AXLE TEMPLATE.ipt")= True Then
        'MessageBox.Show("found1")
        NewFileName = PartN & " AXLE.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    End If
Next


ThisDoc.Document.SaveAs(Path & PartN & ".iam", False)

iLogicVb.UpdateWhenDone = True

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

swatkins3YWDA
Contributor
Contributor

Code works good but the file is being created by a template and it gives a message to save the file first when I run the code.  

swatkins3YWDA_0-1644416948386.png

I set a trigger to happen when creating a New Document.  

 

Please bear with me, this is all new to me.

Samantha

 

0 Likes
Message 4 of 7

swatkins3YWDA
Contributor
Contributor

Removed where it checks if the file has been saved.  So far works better.  Also how do I specify a location besides the current folder?

 

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
	MsgBox("This file has not yet been saved and doesn't exist on disk! - please save it first",64, "File Check")
Return
End If


0 Likes
Message 5 of 7

A.Acheson
Mentor
Mentor
Accepted solution

Ok so since you don’t really want to be starting from the template path for your base to start the folder dialogue. You can supply a path that you will logically save this folder. So if you don’t know you would give an open net and it would be the project path or if you want to save time and get more specific you can give a path exactly where it should be. 

' Get current location of where you want the search area to be.
Dim ExportPath As String = ”C\project files****”)

Delete this section as it is not required

' Check that this file has been saved and actually exists on disk
If String.IsNullOrEmpty(ExportPath) Then
	MsgBox("This file has not yet been saved and doesn't exist on disk! - please save it first",64, "File Check")
Return
End If

 


 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 6 of 7

swatkins3YWDA
Contributor
Contributor

Thank you so much.  That worked as expected. It did need a parenthesis in front of the "C: in your code.

 

Here is the final code,

 

' Get current location of where you want the search area to be.
Dim ExportPath As String = (”Z:\ENGINEERING\CAD\MFG. DRAWINGS\ROCKENG”)


' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
	' User clicked 'ok' on dialog box - capture the export path
	ExportPath = Dialog.SelectedPath & "\"
	
Else
	' User clicked 'cancel' on dialog box - exit
	Return
End If


Dim Path As String = ExportPath
MessageBox.Show(Path, "Directory path")

InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document

Dim PartN As String = "FileName"
PartN = InputBox("Prompt", "Title", PartN)
For Each refDoc In refDocs
    'MessageBox.Show(refDoc.DisplayName)
    If refDoc.DisplayName.EndsWith("WHEEL TEMPLATE.ipt")= True Then
        NewFileName = PartN & " WHEEL.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    ElseIf refDoc.DisplayName.EndsWith("STUB AXLE TEMPLATE.ipt")= True Then
        NewFileName = PartN & " AXLE.ipt"
        refDoc.SaveAs(Path & NewFileName, False)
    End If
Next


ThisDoc.Document.SaveAs(Path & PartN & ".iam", False)

iLogicVb.UpdateWhenDone = True

 

Samantha

 

Hope this may help others getting started.  

 

I feel like the Matrix won't upload the manual for iLogic to me.  

0 Likes
Message 7 of 7

swatkins3YWDA
Contributor
Contributor

 A.Acheson the code works good.  I think I may have missed something in my description.  The three files are created as expected but I also need to replace the two new part files in the assembly.  

 

Files structure after renaming.

swatkins3YWDA_0-1645458734363.png

 

Three files created with iLogic code

swatkins3YWDA_1-1645458804486.png

I need the R10S WHEEL TEMPLATE and STUB AXLE TEMPLATE to be replaced in the assembly after the new file names created.

 

Samantha

 

Edit, not sure if something happened to the code or the template file.  I did have a backup of the file with intact code.  Inventor seems to be having some troubles.  Had to restart Inventor and it seems to work fine and replaces the template axle and wheel files as expected.  I guess restarting Inventor done something.

0 Likes