iLogic to set initial filename on first save

iLogic to set initial filename on first save

DRoam
Mentor Mentor
11,946 Views
9 Replies
Message 1 of 10

iLogic to set initial filename on first save

DRoam
Mentor
Mentor

I'd like to create an iLogic code to do two things:

 

1. On the first save of a part, check if the Description is filled out, and give me the option to fill it out if I want

2. After that, allow the Save As dialog to come up, with the Filename populated with the current Description if it exists

 

Here's the code that I have right now, a lot of which I got from the forums:

 

 

If ThisDoc.Path="" Then
    'If document has not been saved before...
    
    If iProperties.Value("Project", "Description")="" Then
        'If the Description is empty...
        GiveDescription=MessageBox.Show("You have not given this part a Description (Component name). Would you like to do so now?", "Missing Description",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation)
        If GiveDescription=DialogResult.Yes Then
            'If the user wants to provide a Description...
            iProperties.Value("Project", "Description") = InputBox("Component Name:", "Part Description", "")
        End If
    End If
    
    'SAVE AS DIALOG BOX
    'define the active document
    oDoc = ThisDoc.Document
    'create a file dialog box
    Dim oFileDlg As inventor.FileDialog = Nothing
    InventorVb.Application.CreateFileDialog(oFileDlg)
    
    'Set dialog filter
    'oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    
    'set the directory to open the dialog at
    'oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
    'set the file name string to use in the input box as the value entered in Excel Spreadsheet
    oFileDlg.FileName = iProperties.Value("Project", "Description")
    
    '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
    'save the file
    oDoc.SaveAs(iProperties.Value("Project", "Description"), False) 'True = Save As Copy & False = Save As
    End If
Else
    'If document has been saved before...
    Return
End If

 

 

Here are the two main issues with it:

 

1. If I put it under the "Before Save Document" trigger, Inventor runs it AFTER displaying the Save As dialog box, so that makes it useless.

2. If I just run the rule manually before ever saving the document, it appears that everything works as intended, but after hitting "Save", nothing happens. The part isn't actually saved.

 

So, does anyone know how I can get my code to:

 

1. Intercept the Save As dialog and run BEFORE it comes up

2. Actually save the file once it's done

 

Thanks!

0 Likes
11,947 Views
9 Replies
Replies (9)
Message 2 of 10

MechMachineMan
Advisor
Advisor
Guessing you will have to look into events manager, and actually just intercept the save action itself by looking for the button press or something similiar....

--------------------------------------
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 3 of 10

tmuel
Advocate
Advocate

I realize this is an old post, but maybe this will help someone. I am doing something similar. I want the users to name a new file according to properties which should be in the file. Instead of watching for the save event, I just added the external rule to the template and trigger it on a new document event. It fires a form to fill out the properties, and then populates the save dialog with the constructed name. It works great for my purposes.

 

Thanks for posting the code, it saved me some time.

 

 

Tim

0 Likes
Message 4 of 10

antoniobaron
Contributor
Contributor

I actually did exactly the same as the last response, however, if you're loading an excel file it will just take a while to process the information.

Now, I did this to create a file naming application that reads the excel file containing the naming characteristics. Then every time I open a new inventor file, it grabs the number from excel, populates this name into the browser node and then when i click save, it just takes the browser name and appears as the name file. (Cool it really, works) now i have a naming application for every file, assembly, parts, sheet metal, drawings (for drawings uses a different number) 

 

However, it's absolutely slow, especially with content centre files or frame generated parts, or well i have good computer so it doesn't bother me, but I really would like to find a different file instead of an excel file to process this.  Can anyone give me a recommendation for this? How could i speed up this opening and reading the excel file every time i create a new file.

Cheers.

0 Likes
Message 5 of 10

Eide.N
Advocate
Advocate

@antoniobaron That sounds simple enough, but how do you change the browser node?

0 Likes
Message 6 of 10

antoniobaron
Contributor
Contributor

Im not a programmer but Thanks to COVID-19 I have plenty of time to experiment, hope this works for you

 

Sub Main()
' Define a variable that will set the type of document

Dim oComp1occ As Document = ThisDoc.Document

 

'Define the iproperty value that you want in your browser node

Dim strText As String = iProperties.Value("Custom", "DocumentID")

 

'Looks for the type of document and replaces the node value

 

If oComp1occ.DocumentType = kAssemblyDocumentObject Then
Dim oPart As AssemblyDocument = ThisDoc.Document
oPart.DisplayName = strText

 

ElseIf oComp1occ.DocumentType = kPartDocumentObject Then
Dim oPart As PartDocument = ThisDoc.Document
oPart.DisplayName = strText

 

Elseif oComp1occ.DocumentType = kDrawingDocumentObject Then
Dim oPart As DrawingDocument = ThisDoc.Document
oPart.DisplayName = strText

End If

 

end Sub

0 Likes
Message 7 of 10

Eide.N
Advocate
Advocate

Nice! 

It seems to work without checking the file type. I then combined that with some excel tomfoolery - just put the starting PN in cell A2 in your excel file... (This script only runs if the file hasn't been saved before. ) 

 

'If not saved
If ThisDoc.Path = "" Then

	'look at the excel file, update PN and save
	GoExcel.Open("C:\Work\Local\SequentialPN.xlsx", "Sheet1")
		PreviousNumber = GoExcel.CellValue("A2")
		NewNumber = (PreviousNumber + 1)
		'MessageBox.Show(PreviousNumber & vbLf & NewNumber)
		GoExcel.CellValue("A2") = NewNumber
		iProperties.Value("Project", "Part Number") = NewNumber
	GoExcel.Save()

	'Set Browser Node Name - When part is saved, saves as this name
	ThisDoc.Document.DisplayName = NewNumber
End If

 

 

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

Don't for get this handy little tool:

Dim oDoc As Document = ThisApplication.ActiveDocument
'If oDoc.FileSaveCounter > 0 Then
If oDoc.FileSaveCounter = 0 Then
	'Your code here
Else
	'Your other code here
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 10

antoniobaron
Contributor
Contributor

Fantastic that little piece of code is all i needed to solve all the problems. 

0 Likes
Message 10 of 10

CattabianiI
Collaborator
Collaborator

For the ones who came here looking for that handy little tool in Inventor 2022 (and followings), check this post:
https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/how-to-check-if-a-file-has-been-saved-i...

0 Likes