Time measurement of design activities

Hello! I would like to know if someone has an idea of how measure time per events, for example, run an infinite loop where the script is looking for a particular builder (let's say the Extrude builder), if this builder is present so initialize a timer and stop it till it is closed, then print the total time the builder was open.

Thank you in advance for your comments.

Are you looking to time how long it takes a journal to perform an operation or do you want something running in the background monitoring an end user?

Exactly the second one! I would like to detect how long an activity is, for example reallocate a component, or extrude a sketch, in order to obtain data an try to apply methodologies to improve design timing, as you said monitoring an end user.

I'm not sure where to start for such a data gathering program. I have a feeling that it is beyond what can be done with plain journal code running within NX...

Well, I have been thinking in how the builders for every tool (like extrude) works, for example you need to call the NX builder object and select the open status to make it visible, I suppose, you can monitoring for that change of state, what are your thoughts?

You need 3 steps:
1° you must have a nxopen author license to compile a dll and put this in a startup folder
2° You need to create .men file to run the dll before and after each command
3° set UGII_TMP_LOG to your log file

modify as you want the code above

I use this code to log time of each file created or modified, not to each command


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

' INSTRUÇÕES
' Crie um arquivo .men com o conteudo abaixo sem a apóstrofe e salve na pasta Startup
' Crie uma variável de ambiente apontando para o arquivo de log com o nome UGII_TMP_LOG
'
'
'VERSION 130
'
' EDIT UG_GATEWAY_MAIN_MENUBAR
'
' MODIFY
' BUTTON UG_FILE_SAVE_PART
' ACTIONS/PRE path\Time_log.dll
'
' BUTTON UG_FILE_SAVE_AS
' ACTIONS/PRE path\Time_log.dll
'
' BUTTON UG_FILE_SAVE_ALL
' ACTIONS/PRE path\Time_log.dll
'
' END_OF_MODIFY

Module Log_2017_

Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Public registered As Integer = 0
Dim idPartCreated1 As Integer = 0
Dim idPartOpened1 As Integer = 0
Dim idPartSaved1 As Integer = 0
Dim idPartSavedAs1 As Integer = 0
Dim idPartClosed1 As Integer = 0
Dim idPartModified1 As Integer = 0
Dim idPartRenamed1 As Integer = 0
Dim idWorkPartChanged1 As Integer = 0
Dim theSession As Session = Session.GetSession()
Dim t_inicio As DateTime
Dim t_fim As DateTime
Dim t_diferenca As TimeSpan
Dim Tempo As String
Dim fechado As String = Nothing
Dim arquivo_log As String = theSession.GetEnvironmentVariableValue("UGII_TMP_LOG")

Sub PartSaved11(ByVal p As BasePart)
' lw.Open()
' lw.WriteLine(" VB saved: " & p.FullPath)
comando(p)
End Sub

Sub PartSavedAs11(ByVal p As BasePart)
' lw.Open()
' lw.WriteLine(" VB saved as: " & p.FullPath)
comando(p)

End Sub

Sub WorkPartChanged11(ByVal p As BasePart)

comando(p)

End Sub

Sub PartClosed11(ByVal p As BasePart)

fechado = 1

End Sub

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

If Not My.Computer.FileSystem.FileExists(arquivo_log) Then Return

If Environ("TEMPONOARQUIVO") = 1 Then Return

If registered = 0 Then
Startup()
Else
comando(workPart)
End If

End Sub

Public Function Startup() As Integer
Dim theUI As UI = UI.GetUI()

If registered = 0 Then
' idPartCreated1 = theSession.Parts.AddPartCreatedHandler(AddressOf PartCreated1)
' idPartOpened1 = theSession.Parts.AddPartOpenedHandler(AddressOf PartOpened1)
idPartSaved1 = theSession.Parts.AddPartSavedHandler(AddressOf PartSaved11)
idPartSavedAs1 = theSession.Parts.AddPartSavedAsHandler(AddressOf PartSavedAs11)
idPartClosed1 = theSession.Parts.AddPartClosedHandler(AddressOf PartClosed11)
'idPartModified1 = theSession.Parts.AddPartModifiedHandler(AddressOf PartModified1)
' idPartRenamed1 = theSession.Parts.AddPartRenamedHandler(AddressOf PartRenamed1)
idWorkPartChanged1 = theSession.Parts.AddWorkPartChangedHandler(AddressOf WorkPartChanged11)

registered = 1
End If

Startup = 0

End Function

Sub comando(ByVal p As BasePart)

If Environ("TEMPONOARQUIVO") = 1 Then Return

If fechado = 1 Then
fechado = 0
Return
End If

If Tempo = Nothing Then Tempo = 0

If p Is Nothing Then
Tempo = 0
t_inicio = Now
Return
Else
Try
Tempo = p.GetUserAttributeAsString("T_" & System.Environment.UserName, NXObject.AttributeType.String, -1)
Catch ex As Exception
Tempo = 0
End Try
End If

t_fim = Now
t_diferenca = t_fim.Subtract(t_inicio)
Tempo = Decimal.Add(Tempo, 0) + Decimal.Add((t_diferenca.TotalSeconds.ToString("0")), 0)

Try
p.SetUserAttributeLock("T_" & System.Environment.UserName, NXObject.AttributeType.Any, False)
Catch ex As Exception
End Try

Try
p.SetUserAttribute("T_" & System.Environment.UserName, -1, Tempo, Update.Option.Now)
p.SetUserAttributeLock("T_" & System.Environment.UserName, NXObject.AttributeType.Any, True)
ufs.UF.PrintSyslog("1Set attribute: " & "T_" & System.Environment.UserName & " " & p.Leaf & " " & Tempo & " Segundos" & vbCrLf, False)
Catch ex As Exception
ufs.UF.PrintSyslog("1Can not Set attribute: " & "T_" & System.Environment.UserName & " " & p.Leaf & " " & Tempo & " Segundos" & ex.ToString & vbCrLf, False)
End Try

Dim atributosx As String = Nothing
ler_atributos(p, atributosx)
log(p, atributosx)

t_inicio = Now

End Sub

Sub ler_atributos(ByVal attr_part As Part, ByRef atributos As String)

Dim workpart As Part = attr_part
Dim title As String = ""
Dim value As String = ""
Dim inx As Integer = 0
Dim attributo_de_tempo As String = Nothing

Try
Dim attr_info() As NXObject.AttributeInformation
attr_info = workpart.GetUserAttributes(NXObject.AttributeType.String)
Dim count As Integer = attr_info.Length()

Do Until inx = count
title = attr_info(inx).Title.ToString

Dim index As Integer
inx = inx + 1

If title.StartsWith("T_") Then
value = workpart.GetUserAttribute(title, NXObject.AttributeType.String, index).StringValue
attributo_de_tempo = attributo_de_tempo + title + ";" + value + ";"
End If
Loop

Catch ex As Exception

End Try

atributos = (attr_part.Leaf & ";" & "TIME" & ";" & attributo_de_tempo)

Return

End Sub

Sub log(ByVal p As BasePart, ByVal atributos As String)

'Dim arquivo_log As String = theSession.GetEnvironmentVariableValue("UGII_AERO_TMP_LOG")
'Dim arquivo_log As String = "C:\Temp\Novo_Log.txt"
Dim computername As String = System.Environment.MachineName
Dim username As String = System.Environment.UserName

Try
Dim dataAtual As DateTime = DateTime.Now
Dim dia As String = FormatDateTime(dataAtual, DateFormat.ShortDate)
Dim hora As String = FormatDateTime(dataAtual, DateFormat.ShortTime)

Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(arquivo_log, True)
outFile.WriteLine(dia & ";" & hora & ";" & username & ";" & computername & ";" & p.FullPath & ";" & "Atributos" & ";" & atributos)
outFile.Close()

Catch ex As Exception
End Try

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
End Function

End Module

Gelson Nicoletto

Hello Gelson,

This is a very advanced code for me, I'm still figuring out the code, thank you for your response, I will be back to you after a deep review and try out of my quest, with more doubts for sure.

:)

Thanks again.