ILogic-save as Rule

ILogic-save as Rule

Nauld1
Enthusiast Enthusiast
12,271 Views
7 Replies
Message 1 of 8

ILogic-save as Rule

Nauld1
Enthusiast
Enthusiast

So I've come up with the following iLogic rule

 

New_Name = InputBox("Enter New File Name", "Save as", "Name")

ThisDoc.Document.SaveAs(New_Name , True)   

 But it's not working. I also wanted to add a way to choose a file path if possible.

0 Likes
Accepted solutions (1)
12,272 Views
7 Replies
Replies (7)
Message 2 of 8

Nauld1
Enthusiast
Enthusiast
Accepted solution

I found the answer:

 

'define the active document
oDoc = ThisDoc.Document

CurrentFileName = ThisDoc.PathAndFileName(False)

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

Dim Filetype As String

'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    Filetype = ".ipt"
ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    Filetype = ".iam"
ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
    Filetype = ".idw"
End If

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = ThisDoc.FileName(False) 'without extension

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
    'MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
    MyFile = oFileDlg.FileName
    'save the file
    oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As

    NewDocPathName = ThisDoc.PathAndFileName(False)
    
    'open original drawing
    oDestinationDoc = ThisApplication.Documents.Open(CurrentFileName & ".idw")
    oDestinationDoc.saveas(NewDocPathName & ".idw",False)
    oDestinationDoc.Close

    'open new drawing
    oDestinationDoc = ThisApplication.Documents.Open(NewDocPathName & ".idw")
    Dim oDocDescriptor As DocumentDescriptor
    oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
    
    Dim oFileDescriptor As FileDescriptor
    oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
    
    oFileDescriptor.ReplaceReference(NewDocPathName & Filetype)
    oDestinationDoc.Update()
    oDestinationDoc.Save
End If

Message 3 of 8

Owner2229
Advisor
Advisor

Hi, you can do so like this:

 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim FName As String = oDoc.FullFileName
Dim FNP As Integer = InStrRev(FName, "\", -1)
Dim oPath As String = Left(FName, FNP)
Dim oNewName As String = InputBox("Enter New File Name", "Save as", "Name")

' If the user closes the input box, then stop the function If oNewName = vbNullString Then Exit Sub Dim oType As String Select Case oDoc.DocumentType Case kPartDocumentObject: oType = ".ipt" Case kAssemblyDocumentObject: oType = ".iam" Case kDrawingDocumentObject: oType = ".idw" End Select oDoc.SaveAs(oPath & oNewName & oType, True)
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 4 of 8

Anonymous
Not applicable

Hello, I wan't to do the same, but i wan't to know in witch folder I'm going to put the Part.

 

I have created a tube configurator and when I run the rule i wan't to first save(-as) the part and then edit the part with a Form that can chance the parameters.

 

Do you know more about this?

 

MVG thomas de Vries

0 Likes
Message 5 of 8

aronmatheus
Advocate
Advocate

Hi @Nauld1. Does this macro have to increase the ipt files save in the same folder?

0 Likes
Message 6 of 8

Chris_TPR
Contributor
Contributor

Is there a way to "save as" a list of files?

 

I am having to create pipe spool tags to be laser cut, and they are in the hundreds at times. I have a rule in the template set up to create a variable parameter for etching the spool number via the filename.

 

Currently I have been saving as XXXX.ipt one at a time, loading them into an assembly and batch export DXF. This is reasonably efficient but if there was a way to just input the data and automate the rest, it would make my life a lot easier. I can't find any info close enough to what I want to do for me to be able to make that happen with my limited coding knowledge.

 

I would prefer to be able to save as using one of the following:

 

1. A list from an excel sheet.

2. List them in the rule (it's not something I'd have tomultiple times a day, so using the rule as a "one off" isn't an issue)

3. List them separated by comma in a dialogue box.

 

Any help would be appreciated!

0 Likes
Message 7 of 8

Chris_TPR
Contributor
Contributor

Disregard my question.

 

I used ChatGPT and after a few back and forths it gave me a working code.

 

Dim fileList As List(Of String) = New List(Of String) From {"Part1.ipt", "Part2.ipt", "Part3.ipt"} ' Replace with your own list of file names
Dim doc As PartDocument = ThisApplication.ActiveDocument

For Each fileSuffix As String In fileList
doc.SaveCopyAs(doc.FullFileName.Replace(".ipt", "_" & fileSuffix & ".ipt"), True)
Next

 

I am very impressed.

0 Likes
Message 8 of 8

Victor.Tuguinay
Enthusiast
Enthusiast
Hi,
Can it be done without opening a file dialogue box? It will just save directly to a predetermined file path?
0 Likes