If STEP file exists, prompt message if user wants to overwrite file

If STEP file exists, prompt message if user wants to overwrite file

kwilson_design
Collaborator Collaborator
384 Views
8 Replies
Message 1 of 9

If STEP file exists, prompt message if user wants to overwrite file

kwilson_design
Collaborator
Collaborator

Hello everyone and I hope your Friday is going well!

 

I'm automating STEP file creation upon saving the native Inventor file. I would like for the code to detect if the STEP file already exists, and then prompt the user with a message box and ask them if they want to overwrite the STEP file or not. 

 

I've tried adding the System IO check on the frontend of the code to try and catch if it exists to at least prompt a message box so I know that part of the code is working, but I can't seem to get a message box to prompt. What am I missing to #1 check if t file already exists and #2 create a message box asking them if they want to overwrite the file or not?

 

If System.IO.File.Exists(ThisDoc.Path & "\" & oStepFileName & ".stp") Then
    MessageBox.Show("The STEP file already exists in this location.")
    Else 

trigger = iTrigger0 

' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Get Part Number
Dim sPN As String = iProperties.Value("Project", "Part Number")
If sPN.StartsWith("U") Then 
oStepFileName = iProperties.Value("Custom", "FS Part Number")
Else If sPN.StartsWith("PS") Then
oStepFileName = iProperties.Value("Project", "Part Number")
End If


If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 3
    ' Other options...
    'oOptions.Value("Author") = ""
    'oOptions.Value("Authorization") = ""
    'oOptions.Value("Description") = ""
    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
    oData.FileName = ThisDoc.Path & "\" & oStepFileName & ".stp"
	oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
	End If
End If 

 

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
385 Views
8 Replies
Replies (8)
Message 2 of 9

Pineapple2024
Advocate
Advocate

I use this:

Imports System.IO

Dim oFileInfo As IO.FileInfo
Dim oPathName As String
Dim oFileName As String

oFileInfo = New FileInfo(oPathName + oFileName + ".idw")
If oFileInfo.Exists Then
End If

 

iLogic will give security warning and you need to allow it.

0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor

This is a block of code I have in pretty much all of my export code routines.  You can switch it over to using MessageBox.Show if you want, but if you do, then also replace 'MsgBoxResult' Type with 'DialogResult' Type, because they return different types of values.  Those vbYes & vbNo constants were originally designed for use with the MsgBox, hence their Type being MsgBoxResult, but it is a good idea to use the proper Type objects, when possible, in production related tools.

If System.IO.File.Exists(sFullFileName) = True Then
	Dim oAns As MsgBoxResult = MsgBox("The following file already exists." & vbCrLf & _
	sFullFileName & vbCrLf & "Do you want to overwrite it?", _
	vbYesNo + vbQuestion + vbDefaultButton2, "FILE ALREADY EXISTS")
	If oAns = MsgBoxResult.No Then Exit Sub
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor
  • You said your code was running when you save a document, right?
  • Are you using the iLogic Event Triggers dialog, with that rule listed under one of its events to make that happen? 
    • If so, which event (Before Save Document or After Save Document)?
  • Is this rule in a 'template' file?
    • If it is in a template, then how are you creating new documents from it?
      • Open template, then using SaveAs
      • Or using New command, then choosing template, then Save (which acts like SaveAs the first time)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

kwilson_design
Collaborator
Collaborator

Hey @WCrihfield. I've answered your questions with blue text below.

 

  • You said your code was running when you save a document, right? Manually running it since I'm just testing, but this will move to an Event Trigger once ready for production.
  • Are you using the iLogic Event Triggers dialog, with that rule listed under one of its events to make that happen? 
    • If so, which event (Before Save Document or After Save Document)? Yes, I intend to use Before Save trigger so that user does not forget to run the rule manually.
  • Is this rule in a 'template' file? Currently just testing but yes the goal is to have this in the templates.
    • If it is in a template, then how are you creating new documents from it?
      • Open template, then using SaveAs
      • Or using New command, then choosing template, then Save (which acts like SaveAs the first time) New and then save.
Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

I'm on my way out now for the weekend, so in a hurry.

First, I would advise against using the 'before save' event, because if your document has not been saved yet, it will not have a file path, or a file name yet, so 'ThisDoc.Path' will not have a value (empty String).  It would be better 'after save', so that it will always have a file path and file name, even though I rarely use that event myself.

Second, I would not mix the terms 'ThisDoc' and 'ThisApplication.ActiveDocument', because they can be pointing to different documents when this rule gets triggered by 'events', instead of you having the document visibly open on your screen at that time.  I am usually also a little cautious about using 'iProperties.Value()' snippets in a code that will get triggered to run by events, because they do not have a really good way to specify which specific document they will be focused on.  I usually switch to doing those things the Inventor API way [Document.PropertySets.Item().Item().Value].  Just to be super sure about which document every line of code will be working on.  I would use 'ThisDoc.Document' for obtaining a reference to the current document, when the rule gets ran by an event, because it will be the most appropriate in most situations.  It's good that you are using 'New' then save process, because that avoids accidentally saving over your template, or the code referring to the template, instead of the 'truly' new document.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

kwilson_design
Collaborator
Collaborator

@WCrihfield Thanks for the great info. I will make those changes to further clarify the document.

 

I did end up getting the file detection aspect of the script working once I moved System IO below the portion of the code gathering the part number. Now I'm having a heck of a time trying to understand how to get the vbYes and vbNo to work. I found your blog post related to using Yes and No (https://inventortrenches.blogspot.com/2011/03/using-yes-no-message-box-in-your-ilogic.html) but I'm having a hard time trying to understand how to add in if Yes then run the rest of my code but if No then exit the code entirely and disregard overwriting the STEP file.

....snipped code....

 

' Get Part Number
Dim sPN As String = iProperties.Value("Project", "Part Number")
If sPN.StartsWith("U") Then 
oStepFileName = iProperties.Value("Custom", "FS Part Number")
Else If sPN.StartsWith("PS") Then
oStepFileName = iProperties.Value("Project", "Part Number")
Else oStepFileName = iProperties.Value("Project", "Part Number")
End If
 
If System.IO.File.Exists(ThisDoc.Path & "\" & oStepFileName & ".stp") Then
oSTEPDetMess = MessageBox.Show("The STEP file with this name already exists in this folder location. Do you wish to overwrite the file?", _
"STEP file detected", MessageBoxButtons.YesNo, _
MessageBoxIcon.Hand, _
MessageBoxDefaultButton.Button1)
 
If oSTEPDetMess = vbYes
 
This is where I'm having trouble. How can I run this Yes answer into the Else below?
 
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 3
    ' Other options...
    'oOptions.Value("Author") = ""
    'oOptions.Value("Authorization") = ""
    'oOptions.Value("Description") = ""
    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
    oData.FileName = ThisDoc.Path & "\" & oStepFileName & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If
 
And then how can I run this No answer to cancel this rule entirely so that the STEP file does not get overwritten?
 
If oSTEPDetMess = vbNo ???
 
Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Hi @kwilson_design.  Most of the time, when we use MsgBox or MessageBox.Show, we do not expect to get anything back from them.  However, those are both Functions, which means that they are designed to 'Return' something to the routine that called them to run.  Both return a slightly different Type of value.  The MsgBox Function returns a MsgBoxResult Type value.  The MessageBox.Show Function returns a DialogResult Type value.  Both types of values represent a different 'Enum'.  So, when you want to use one of those to ask a question, where you need to check the result of that question, then you need to have a variable to record the result of the Function to.  The Type of that variable will depend on which of those two Functions you are using.  Then, to check which value you received from that question, you check the variable that the Function set the value of.  When a variable represents an Enum, you usually need to know the name of that Enum (such as MsgBoxResult or DialogResult), because you always start with that name, then type the dot (.) after it, then Intellisense (pop-up helper suggestions) will show you the list of possible values of that Enum, so you can choose one of them.  However, if you did not 'declare the Type' of that variable, then the Intellisense system will not know what to suggest, and will not be able to help you.  Sometimes these Enum's have a numerical value associated with each variation they contain.  When this is the case, you can just specify that numerical value, instead of the name of the Enum and its variation name.  However, that is harder to read later, unless you include comments about which variation that number represents.

 

When asking a question about whether a code should proceed past that point in a code, always handle the 'NO' answer first, instead of the 'YES' answer.  That way, if they say No, you can just exit the rule (or current code routine) at that point, and you do not need to handle the 'YES' answer.  If they did not say NO, then do not react at all...just let the rest of the code continue as usual.  No need for a super long If...ElseIf...End If statement with tons of code between them, where it is easy to loose your place.  There is also the possibility that the user clicked the small 'X' in the corner of the dialog, instead of clicking either the 'Yes' or 'No' buttons.  If that is possible, and it is important for you to handle that, then you can check for both 'NO' or 'Cancel' and similar values at the same time.  So, if they canceled the dialog, or answered No, then stop this whole process right now.  If neither, then let the rest of the code playout...with no check for Yes value.

 

When you look at my example in Message 3 above, you will notice that I am both 'declaring' (using Dim), and specifying the Type of the variable called "oAns", within the If...Then...End If statement, and I am also checking the value of that variable within that same If...Then...End If statement.  Then you will notice that I am only checking if the value was 'NO'.  I could have also used an 'Or' operator there to check for 'No' or 'Cancel'.  The only value I care about is if the user does not want to proceed (answers No), then I want to end that code routine right there.  To end the code routine right there, I am using the 'Exit Sub' keywords.  That will only work if used within a Sub routine, not if used within a Function.  I could have also used 'Return' there, instead of 'Exit Sub'.  If it was within a Function routine, I could have also used 'Exit Function' there.  However, 'Return' works in both a Sub or a Function.  It is just more natural to use a 'Return' from within a Function than a Sub routine, because a Sub routine does not return anything, and a Function does...usually.  I do not bother checking if they answered Yes, because that would require putting the whole rest of my code between that point and the 'End If' keywords, which is not necessary, if I do not check for Yes.

 

PS.  That is not 'my' blog post.  That is Curtis Waguespack's blog.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Another little note about using those constants.

Yes values:

The 'vbYes' constant has the numerical value of 6.

The MsgBoxResult.Yes variation also has the numerical value of 6.

The DialogResult.Yes variation also has the numerical value of 6.

This is the reason why vbYes 'can' work in both situations.

 

The same goes for 'No', which has a numerical value of 7 in both Enum's.

Cancel is 2 in both Enum's, and the constant vbCancel.

OK is 1 in both Enum's, and the constant vbOK.

And so on.  Even though vbOK is a MsgBoxResult variation, instead of a DialogResult variation, it has the same numerical value as both variations of the two different Enums.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes