Trigger external rule per file only once

Trigger external rule per file only once

SevInventor
Advocate Advocate
466 Views
5 Replies
Message 1 of 6

Trigger external rule per file only once

SevInventor
Advocate
Advocate

Hey Guys,

we have a Jobserver creating PDFs and Step file automatically. Now I've created external rule to do some checks and send an E-mail if the drawings have problems like unattached Ballons or unfilled iproperties.

The Jobserver launches the files in new Inventor sessions  to many times (3 times) in lets say 1 hour and sends the check E-mail to many times. Because the Inventor is closed every time i can't share a variable.

 

To solve the Problem, my Idea is to write the filenames of the already processed files to a text file and the date and time.

If the same file was already processed short time ago i want to exit the sub so the same e-mail is not being sent several times. Therefore the textfile must be read and check if the filename was already processed a short time ago. 

How can i write and find the filename in a textfile and compare the timestamp?

 

 

 

0 Likes
Accepted solutions (1)
467 Views
5 Replies
Replies (5)
Message 2 of 6

Michael.Navara
Advisor
Advisor
Accepted solution

Here is short sample without error checking, but it shows how to read, write and update data from/to text file.

It is just a starting point for you.

Sub Main()
WorkWithTextFileSample()
End Sub

Private emailInfosFile As String = "C:\Temp\emailInfos.csv"
Private encoding As System.Text.Encoding = System.Text.Encoding.UTF8

Private Sub WorkWithTextFileSample()

	'Read data from file
	Dim emailInfos As Dictionary(Of String, DateTime) = ReadEmailInfosFromTextFile(emailInfosFile)
	Logger.Debug(emailInfos.Count)

	'Add new entry
	Dim fileName As String = "C:\My\File.ipt"
	Dim timeStamp As DateTime = DateTime.Now
	AppendEmailInfo(emailInfos, fileName, timeStamp)
	Logger.Debug(emailInfos.Count)

	'Get timeStamp from emailInfos by file name
	Dim timeStamp3 As DateTime = GetTimeStamp(emailInfos, fileName)
	Logger.Debug(timeStamp3)

	'Add and remove next entry
	Dim fileName2 As String = "C:\My\File2.ipt"
	Dim timeStamp2 As DateTime = DateTime.Now
	AppendEmailInfo(emailInfos, fileName2, timeStamp2)
	RemoveEmailInfo(emailInfos, fileName2)
	Logger.Debug(emailInfos.Count)

	'Save data to file
	SaveEmailInfosToTextFile(emailInfos)
	Logger.Debug("Saved")
End Sub


Private Function GetTimeStamp(emailInfos As Dictionary(Of String, DateTime), fileName As String) As DateTime
	If emailInfos.ContainsKey(fileName) Then Return emailInfos(fileName)

	'fileName not found
	Return DateTime.MinValue

End Function

Private Sub RemoveEmailInfo(emailInfos As Dictionary(Of String, DateTime), fileName As String)
	If emailInfos.ContainsKey(fileName) Then
		emailInfos.Remove(fileName)
	End If
End Sub


Private Function ReadEmailInfosFromTextFile(emailInfosFile As String) As Dictionary(Of String, DateTime)

	'File is missing
	If Not System.IO.File.Exists(emailInfosFile) Then Return New Dictionary(Of String, DateTime)

	'Load file content
	Dim emailInfos As New Dictionary(Of String, DateTime)
	Dim lines As String() = System.IO.File.ReadAllLines(emailInfosFile, encoding)
	For Each line As String In lines
		Dim strings As String() = Line.Split(";")
		emailInfos.Add(strings(0), DateTime.Parse(strings(1)))
	Next
	Return emailInfos
End Function

Private Sub AppendEmailInfo(emailInfos As Dictionary(Of String, DateTime), fileName As String, timeStamp As DateTime)
	If (emailInfos.ContainsKey(fileName)) Then
		emailInfos(fileName) = timeStamp
	Else
		emailInfos.Add(fileName, timeStamp)
	End If

End Sub

Private Sub SaveEmailInfosToTextFile(emailInfos As Dictionary(Of String, DateTime))
	Dim stringBuilder As New System.Text.StringBuilder
	For Each emailInfo As KeyValuePair(Of String, DateTime) In emailInfos
		stringBuilder.AppendLine(String.Join(";", emailInfo.Key, emailInfo.Value.ToString()))
	Next
	System.IO.File.WriteAllText(emailInfosFile, stringBuilder.ToString(), encoding)
End Sub

 

Message 3 of 6

SevInventor
Advocate
Advocate

Hello Michael,

 

thanks a lot for the code. It looks more like a final version than a good starting point!

Perfect!

0 Likes
Message 4 of 6

SevInventor
Advocate
Advocate

works as expected if i comment out this line for testing

	RemoveEmailInfo(emailInfos, fileName2)

 

0 Likes
Message 5 of 6

Michael.Navara
Advisor
Advisor

My recommendations for production version:

  1. Keep emailInfos.csv file as small as possible. Large file can be a performance issue.
  2. Use different identifier that the email was sent. Useful is for example FileSaveCounter (instead of timeStamp). Because until the FileSaveCounter is not increased, the file is the same (issues is not fixed yet).
Message 6 of 6

SevInventor
Advocate
Advocate

@Michael.Navara ,

Thanks again,

Your Feedback 2.Use different identifier that the email was sent brought me to use a combined string of version and revision of the .idw. The Version String for a specific filename must be different from the one in the csv file otherwise exit sub.

It assures that each file is only processed once and is much easier than finding a solid time comparison solution. 

 

Sub Main()
WorkWithTextFileSample()
End Sub

Private emailInfosFile As String = "C:\Temp\emailInfos.csv"
Private encoding As System.Text.Encoding = System.Text.Encoding.UTF8

Private Sub WorkWithTextFileSample()

	'Read data from file
	Dim emailInfos As Dictionary(Of String, String) = ReadEmailInfosFromTextFile(emailInfosFile)
	Logger.Debug(emailInfos.Count)

	'get The Filename
	Dim fileName As String = ThisDoc.PathAndFileName(False)

	Logger.Debug(emailInfos.Count)
	
		'Get Version/Revision String from emailInfos by file name
	Dim VersString3 As String = GetVersString(emailInfos, fileName)
	Logger.Debug(timeStamp3)
	
	Dim VersString As String = iProperties.Value("Custom", "AIMD_VERSION_INDEX") & iProperties.Value("Custom", "AIMD_REVISION_INDEX")
	AppendEmailInfo(emailInfos, fileName, VersString)
	Logger.Debug(emailInfos.Count)

	
	If VersString = VersString3 Then
		Exit Sub
	End If

	'Save data to file
	SaveEmailInfosToTextFile(emailInfos)
	Logger.Debug("Saved")
End Sub


Private Function GetVersString(emailInfos As Dictionary(Of String, String), fileName As String) As String
	If emailInfos.ContainsKey(fileName) Then Return emailInfos(fileName)

	'fileName not found
	Return "???"

End Function

Private Sub RemoveEmailInfo(emailInfos As Dictionary(Of String, String), fileName As String)
	If emailInfos.ContainsKey(fileName) Then
		emailInfos.Remove(fileName)
	End If
End Sub


Private Function ReadEmailInfosFromTextFile(emailInfosFile As String) As Dictionary(Of String, String)

	'File is missing
	If Not System.IO.File.Exists(emailInfosFile) Then Return New Dictionary(Of String, String)

	'Load file content
	Dim emailInfos As New Dictionary(Of String, String)
	Dim lines As String() = System.IO.File.ReadAllLines(emailInfosFile, encoding)
	For Each line As String In lines
		Dim strings As String() = Line.Split(";")
		emailInfos.Add(strings(0), strings(1))
	Next
	Return emailInfos
End Function

Private Sub AppendEmailInfo(emailInfos As Dictionary(Of String, String), fileName As String, VersString As String)
	If (emailInfos.ContainsKey(fileName)) Then

		emailInfos(fileName) = VersString

	Else
		emailInfos.Add(fileName, VersString)
	End If

End Sub

Private Sub SaveEmailInfosToTextFile(emailInfos As Dictionary(Of String, String))
	Dim stringBuilder As New System.Text.StringBuilder
	For Each emailInfo As KeyValuePair(Of String, String) In emailInfos
		stringBuilder.AppendLine(String.Join(";", emailInfo.Key, emailInfo.Value.ToString()))
	Next
	System.IO.File.WriteAllText(emailInfosFile, stringBuilder.ToString(), encoding)
End Sub

 

0 Likes