iLogic: Save As

iLogic: Save As

Anonymous
Not applicable
6,843 Views
29 Replies
Message 1 of 30

iLogic: Save As

Anonymous
Not applicable

Does anyone know how to make a rule that automatically performs a "Save As" to a specific directory AND a specific name?

Ideally, the rule is run from the part document. The rule then opens up a specific drawing document, performs a "Save As" function to the same directory as the file part, and names the drawing file the same as the part file.

I already figured out how to get drawing file name to be the same as the part file name, but I cant get it to save to a specific directory. It always pops up a file dialog box.

Accepted solutions (1)
6,844 Views
29 Replies
Replies (29)
Message 2 of 30

mwighton
Collaborator
Collaborator

I found this code online, which displays a window to allow the user to choose the save location.

 

SyntaxEditor Code Snippet

 
'define the active document
    oDoc = ThisDoc.Document
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    'check file type and set dialog filter
    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
    
    '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 = iProperties.Value("Project", "Part Number")
    
    '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
    End If

 You can change the line which states:

SyntaxEditor Code Snippet

oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

so a specific path(if you know the extension)

 

You can also remove the lines that display the window.

 

Hope this helps. 

Did this post help out? Hope it did.
If so please use the 'Accept as Solution' or 'Kudos' Button below.
God Bless

Mwighton
0 Likes
Message 3 of 30

Anonymous
Not applicable

Hmm, but the issue is i DONT want a window to display asking the user where he wants to save the drawing file. The reason is a little complicated, but it boils down to the fact that the user would be specifying where he wants to save the drawing file beforehand (specifically when he is prompted to save his part file). The drawing file is only allowed to open AFTER the user saves his part file. Upon the drawing file opening, I need it to automatically do a "Save As" into the same directory where the user just saved his part file.

 

It is funny, because the code you posted is the exact same one I use for the saving of the users part file.

0 Likes
Message 4 of 30

mwighton
Collaborator
Collaborator

So after the first save add a line that will save the workspace path as a variable and use that as the save location in the code I provided. Once that is completed add the open document line.

Did this post help out? Hope it did.
If so please use the 'Accept as Solution' or 'Kudos' Button below.
God Bless

Mwighton
0 Likes
Message 5 of 30

Anonymous
Not applicable

Yes but how do I specify the parameter for the save location without having a save file dialog box open up?

Here is my relevant section of code.

 

SyntaxEditor Code Snippet

    MyFile = oFileDlg.FileName
    'save the file
    oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
    MessageBox.Show("Loaded")
    ThisDoc.Launch(oDrawingFileTemplate)
    Dim oDrawingDoc as DrawingDocument = ThisApplication.Documents.Item(oDrawingFileTemplate)
    oDrawingDoc.DisplayName = ThisDoc.Document.DisplayName
    oDrawingDoc.SaveAs(MyFile & ".idw", False)
0 Likes
Message 6 of 30

BrandonBG
Collaborator
Collaborator

Are you trying to get to the path of the part file? Or is the drawing being save elsewhere?

 

With the part file open, try oPath = ThisDoc.Path

 

Then load the drawing, and save with 

oDrawingDoc.SaveAs(oPath & MyFile & ".idw", False)

 

Brandon

0 Likes
Message 7 of 30

Anonymous
Not applicable

I am trying to save in the same directory as the part file. Ah, silly me, I didnt realize that the first parameter in the SaveAs function can be extended to also include the desired path, and is not just for the file name.

 

However, I tried this and it still opens up a Save filedialog box. I am not sure how to get to save without opening a dialog box.

0 Likes
Message 8 of 30

BrandonBG
Collaborator
Collaborator

I think the dialog pops up because MyFile is declared as oFileDlg.FileName.

 

Does it work to insert this before the SaveAs?

 

Dim oDrawingFile As String

oDrawingFile = MyFile

 

oDrawingDoc.SaveAs(oPath & oDrawingFile & ".idw", False)

 

Brandon

0 Likes
Message 9 of 30

Anonymous
Not applicable

I dont think thats the issue.

I enetered:

 

SyntaxEditor Code Snippet

MessageBox.Show(ThisDoc.Path & "\" & MyFile & ".idw")

to see what it said. It said:

 

 2015-07-22 13_45_46-Autodesk Inventor Professional 2014.png

 

So Im pretty sure it was right before.

0 Likes
Message 10 of 30

BrandonBG
Collaborator
Collaborator

I was using the string manipulation to get past the File Dialog prompt. Did it do that?

 

I forgot that you'll need to extract the path/filename and strip off the file extension.

 

Brandon

0 Likes
Message 11 of 30

Anonymous
Not applicable

Ok, using messagebox, I got the correct file name and path finally to:

 

SyntaxEditor Code Snippet

oDrawingDoc.SaveAs(ThisDoc.PathAndFileName(False) & ".idw", False)

However, it is still popping up a SaveAs dialog box, with the initial directory not changing to the same one as the part file. Its almost as if the SaveAs function for the drawing is completely ignoring the parameters I've set for it, and is using the parameters set for the SaveAs dialog for the part file.

0 Likes
Message 12 of 30

Anonymous
Not applicable

Using the following I get the correct path and everything. It still pulls up the file dialog box.

 

SyntaxEditor Code Snippet

MessageBox.Show(ThisDoc.PathAndFileName(False) & ".idw")
    oDrawingDoc.SaveAs(ThisDoc.PathAndFileName(False) & ".idw", False)

 2015-07-22 13_45_46-Autodesk Inventor Professional 2014.png

0 Likes
Message 13 of 30

MechMachineMan
Advisor
Advisor

@Anonymous wrote:

Yes but how do I specify the parameter for the save location without having a save file dialog box open up?

Here is my relevant section of code.

 

SyntaxEditor Code Snippet

    MyFile = oFileDlg.FileName
    'save the file
    oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
    MessageBox.Show("Loaded")
    ThisDoc.Launch(oDrawingFileTemplate)
    Dim oDrawingDoc as DrawingDocument = ThisApplication.Documents.Item(oDrawingFileTemplate)
    oDrawingDoc.DisplayName = ThisDoc.Document.DisplayName
    oDrawingDoc.SaveAs(MyFile & ".idw", False)

 

The oFileDialog is telling it to open up the large dialog window.

 

I don't know what other window you would be talking about...


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 14 of 30

Anonymous
Not applicable

Sorry, I way I copied it might have been a bit miseading, but the oFileDialog is actually for saving the part file, not the drawing file. From the code, you can see my Drawing document (dimensioned as oDrawingDoc) is only launched after the part file is succesfully saved. The problems I am running into involve the saving of the drawing document (oDrawingDoc) in the same directory and with the same file name as my part file.

 

Here is a larger section of the relevant code. Its the same base code that you posted in your first reply, except with additional items in the "ElseIf" section where the file actually gets saved.

(Edit: Sorry MechMachineMan, not the same base code that you posted, I meant the base code that BrandonBG posted in his first reply)

 

SyntaxEditor Code Snippet

ElseIf oFileDlg.FileName <> "" Then
        oShowSave = False
        MyFile = oFileDlg.FileName
        
        'save the file
        oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
        oPath = ThisDoc.PathAndFileName(False)
        
        'Launch the Drawing Document
        ThisDoc.Launch(oDrawingFileTemplate)
        MessageBox.Show("Loaded")
        Dim oDrawingDoc as DrawingDocument = ThisApplication.Documents.Item(oDrawingFileTemplate)
        MessageBox.Show(oDrawingDoc.FullFileName) 'for troubleshooting purposes
        oPath = ThisDoc.PathAndFileName(False)
        oDrawingDoc.SaveAs(oPath & ".idw", False)
        Update_Drawing = True
    End If

 

0 Likes
Message 15 of 30

MechMachineMan
Advisor
Advisor
So what part are you having issues with/ what's happening?

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 16 of 30

Anonymous
Not applicable

The issue is the File dialog box showing twice. I need it to only show once, when I am getting the user to save his/her part file. It should not be showing again when saving the drawing file. It should be automatically saving (specifically "Saving As") the drawing file with the same file name and to the same directory as the part file.

 

Example of my code running ideally:

  • I open my part file, titled "GENERIC-RING.ipt"
  • My rule is run
  • It prompts the user to Save As to a directory (eg. Save As "C:\Users\thukon\My Documents\12345.ipt")
  • The Drawing document, GENERIC-RING.idw, is launched
  • The Drawing document is then, also saved as "C:\Users\thukon\My Documents\12345.idw"

And this needs to all be done all in one swoop, with the only user intervention being the user choosing where he wants to save the part file.

 

0 Likes
Message 17 of 30

MechMachineMan
Advisor
Advisor
K. I think you are calling the function twice then. Whatever the name of your function is, should only appear once, and immediately assigned to a variable. then manipulated from there.

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 18 of 30

Anonymous
Not applicable

The only thing being called twice is the SaveAs function. From what I gathered on other posts, the SaveAs function itself should not trigger a file dialog.

Thats what the Inventor.FileDialog.ShowSave is for, right? At least FileDialog.ShowSave is what my code uses to prompt the user to save the part file. Hoping someone can correct me here.

0 Likes
Message 19 of 30

mwighton
Collaborator
Collaborator
if you are using the same code that i posted it will prompt a window.
Did this post help out? Hope it did.
If so please use the 'Accept as Solution' or 'Kudos' Button below.
God Bless

Mwighton
0 Likes
Message 20 of 30

BrandonBG
Collaborator
Collaborator

I stripped this down to the essential SaveAs function.

 

Dim oDrawing as DrawingDocument
oDrawing = ThisDrawing.Document

MessageBox.Show(ThisDoc.Path)

oDrawing.SaveAs(ThisDoc.Path & "\1234.idw", True)

I did not get a file dialog window. However, when I swith the boolean to False, I crash out of the rule.

 

Brandon

0 Likes