' Journal created NX 10.0.2.6
'
Option Strict Off
Imports System
Imports NXOpen
Imports System.IO
Imports System.Windows.Forms

' -- Close NX Session
' https://community.sw.siemens.com/s/question/0D54O000061x3csSAA/how-to-terminate-nx-75-teamcenter-4-tier-session-from-net-dll

Module NXJournal
 Sub Main (ByVal args() As String)

  Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
  Dim NxSyslog As LogFile = theSession.LogFile
  Dim MainAssemblyIR as String = "#ItemNameRev#"
  Dim MainAssemblyCLI as String = "#ObjectCliName#"
  Dim PLMPerfSW As New System.Diagnostics.Stopwatch()
  Dim ParWinHndl as Integer

  '-- #Doc: get NX Window Handel
  ParWinHndl = NXOpenUI.FormUtilities.GetDefaultParentWindowHandle()

  ' ----------------------------------------------
  '  Menu: File ->open...
  ' ----------------------------------------------
  Dim markId1 As NXOpen.Session.UndoMarkId
  markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Start")

  theSession.SetUndoMarkName(markId1, "Part open")
  theSession.DeleteUndoMark(markId1, Nothing)

  NxSyslog.WriteLine("##PLMPerfTimeStartLoad: " + Now.ToString())
  NxSyslog.WriteLine("##PLMPerfLoadTopLevelAP: " + MainAssemblyIR)
  PLMPerfSW.Start()

  theSession.Parts.SetNonmasterSeedPartData(MainAssemblyCLI)

  Dim basePart1 As NXOpen.BasePart
  Dim partLoadStatus1 As NXOpen.PartLoadStatus = nothing
  basePart1 = theSession.Parts.OpenBaseDisplay(MainAssemblyCLI, partLoadStatus1)

  Dim workPart As NXOpen.Part = theSession.Parts.Work
  Dim displayPart As NXOpen.Part = theSession.Parts.Display

  partLoadStatus1.Dispose()

  ' stop perf-stopwatch and write measured time in NX-Syslog file
  PLMPerfSW.Stop()
  '-- 15.10.2020 changed key from ##PLMPerfTime to ##PLMPerfDuration
  NxSyslog.WriteLine("##PLMPerfTDS_KEY1_#ObjectCliName#:#Time:"+ Now.ToString() + "#Dura:"+ PLMPerfSW.Elapsed.TotalSeconds.ToString().Replace(",", ".") + " sec" + " #Description: DurationToLoadTopLevel #ObjectCliName#")
  NxSyslog.WriteLine("##PLMPerfDuration: " + PLMPerfSW.Elapsed.TotalSeconds.ToString().Replace(",", ".") + " sec")
  NxSyslog.WriteLine("##PLMPerfTimeEndLoad2: " + Now.ToString())

  ' ----------------------------------------------
  '  Menu: Datei->close->all parts
  ' ----------------------------------------------
  theSession.Parts.CloseAll(NXOpen.BasePart.CloseModified.CloseModified, Nothing)

  workPart = Nothing
  displayPart = Nothing

  '-- #Step: Finish NX (ugraf.exe)
  '-- #Doc: wait 0.5 second
  NxSyslog.WriteLine("##PLMPerfEnd:1/4: wait 0.5 sec")
  System.Threading.Thread.Sleep (500)
  '-- #Doc: lets do finish Events
  NxSyslog.WriteLine("##PLMPerfEnd:2/4 System.Windows.Forms.Application.DoEvents()")
  System.Windows.Forms.Application.DoEvents()
  '-- #Doc: wait 0.5 second
  NxSyslog.WriteLine("##PLMPerfEnd:3/4: wait 0.5 sec")
  System.Threading.Thread.Sleep (500)

  '-- #Doc: send exit to ugraf.exe
  NxSyslog.WriteLine("##PLMPerfEnd:4/4: exit NX (ugraf.exe) ParWinHndl:" + ParWinHndl.ToString + " via SendMessage:WM_CLOSE (" + WM_CLOSE.ToString + ")" )
  SendMessage(ParWinHndl, WM_CLOSE, 0, 0)

 End Sub


 '-- Send close message to ugraf.exe via Handel
 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
 Public Const WM_CLOSE as long = &H10



End Module
