Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sharing my "Time Counter" iLogic Code

0 REPLIES 0
Reply
Message 1 of 1
mecanicu
266 Views, 0 Replies

Sharing my "Time Counter" iLogic Code

Hello everyone,

I wanted to share with you all a piece of iLogic code that I've been working on. It's a "time counter" 
that keeps track of the time spent working on a project. Here's the code:

 

 

 

 

 

Sub Main()
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oDocName As String = System.IO.Path.GetFileName(oDoc.FullFileName) 
	MsgBox(oDocName,,"Good Document!!!")
	SharedVariable("docName") = oDocName
	Trace.WriteLine("Start Data")
	Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
    AddHandler oAppEvents.OnOpenDocument, AddressOf ApplicationEvents_OnOpenDocument
    AddHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
End Sub

Sub ApplicationEvents_OnOpenDocument(DocumentObject As _Document, FullDocumentName As String, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	If BeforeOrAfter = EventTimingEnum.kAfter Then
		Dim sName As String = System.IO.Path.GetFileName(FullDocumentName) 
		Dim aName As String = SharedVariable("docName")
			If sName = aName Then
				MsgBox(aName & " = " & sName,,"Same Name!!!")	
			    SharedVariable("Start") = Date.Now
			    Trace.WriteLine("onOpen Data")
			End If
        SharedVariable.Remove("docName")
		Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
        RemoveHandler oAppEvents.OnOpenDocument, AddressOf ApplicationEvents_OnOpenDocument
		oAppEvents = Nothing
    End If
End Sub

Sub ApplicationEvents_OnCloseDocument(DocumentObject As _Document, FullDocumentName As String, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
    If BeforeOrAfter = EventTimingEnum.kBefore Then
        Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
        Dim sName As String = System.IO.Path.GetFileName(FullDocumentName)
        Dim timeS As Date = SharedVariable("Start")
        Dim ts As TimeSpan = Date.Now - timeS
        Dim elapsedTime As String = ts.ToString("dd' Days, 'hh' Hours, 'mm' Min, 'ss'.'ff' Sec'")
        Dim result = MsgBox(elapsedTime, MessageBoxButtons.YesNo, "Time Worked on Project:  " & sName)
        Trace.WriteLine("onClose Data")
        If (result = MsgBoxResult.No) Then
            SharedVariable.Remove("Start")
            RemoveHandler oAppEvents.OnOpenDocument, AddressOf ApplicationEvents_OnOpenDocument
            RemoveHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
            oAppEvents = Nothing
            MsgBox("No Export!!!", , "Quit")
            Trace.WriteLine("No Export")
            Exit Sub
        End If
        GoExcel.Open("C:\TEMP\1.xlsx", "Sheet1")
        Trace.WriteLine("Export Excel") 
        Dim RowStart As Integer = 2
        Dim RowEnd As Integer = 1000
        Dim rowFound As Integer = 0
        For count = RowStart To RowEnd
            If GoExcel.CellValue("A" & count) = sName Then
                rowFound = count
                Exit For
            End If
        Next
        Dim i As Integer = 0
        For count = RowStart To RowEnd
            If String.IsNullOrEmpty(GoExcel.CellValue("A" & count)) Then 
                i = i + 1
            End If
        Next
        Dim Row As Integer = RowEnd - i + 1
        If rowFound > 0 Then
            MsgBox("Add Data on Row " & rowFound, , "Export to Excel")
	    Dim oldElapsedTime As String = GoExcel.CellValue("C" & rowFound)
            Dim oldTimeSpan As TimeSpan = TimeSpan.ParseExact(oldElapsedTime, "dd' Days, 'hh' Hours, 'mm' Min, 'ss'.'ff' Sec'", Nothing)
            Dim newTimeSpan As TimeSpan = oldTimeSpan.Add(ts)
            Dim newElapsedTime As String = newTimeSpan.ToString("dd' Days, 'hh' Hours, 'mm' Min, 'ss'.'ff' Sec'")
            GoExcel.CellValue("B" & rowFound) = DateString &" "& TimeString
	    GoExcel.CellValue("C" & rowFound) = newElapsedTime
			
        Else
            MsgBox("Inserting Data on Row " & Row, , "Export to Excel")
            GoExcel.CellValue("A" & Row) = sName
	    GoExcel.CellValue("B" & Row) = DateString &" "& TimeString
            GoExcel.CellValue("C" & Row) = elapsedTime
        End If
        GoExcel.Save()
        GoExcel.Close()
        SharedVariable.Remove("Start")
        RemoveHandler oAppEvents.OnOpenDocument, AddressOf ApplicationEvents_OnOpenDocument
        RemoveHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
        oAppEvents = Nothing
    End If
End Sub

 

 

 

 

To use it, simply add the code as an external rule and set it as an event trigger on all documents, 
after open document. You must have an Excel file at "C:\TEMP\1.xlsx". Please note that there is a known
issue where the first time you start Inventor and open a document, it doesn't trigger. It only works on
the second try. I'd like to give a shoutout to this forum and Bing chat for their help in developing this code. Their
guidance and suggestions were invaluable. This post was written entirely by Bing. I hope this is helpful to some of you. Let me know if you have any questions or suggestions for
improvement. Best,
Bing Chat
P.S. I know iLogic 😉
 

 

0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report