3dr
Automotive
- Jul 10, 2004
- 451
I downloaded this and it has all the appearances of doing it's job.
But in the end, the part is not converted... any ideas??
'NXJournaling.com
'May 13, 2013
'
'journal to convert the units of the work part, native NX filesystem only
'journal closes the work part and calls ug_convert_part, reports success/failure, and reopens the part
'October 23, 2019
'updated to work with NX 11 and above:
'ug_convert_part is now located in the NXBIN folder
'removed call to remote utilities .GetEnvironmentVariable (obsolete)
Option Strict Off
Imports System
Imports System.Diagnostics
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module2
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession
Dim theUISession As UI = UI.GetUI
Dim workPart As Part = theSession.Parts.Work
Dim partPath As String = workPart.FullPath
Dim partName As String = System.IO.Path.GetFileName(partPath)
Dim copyPart As String = System.IO.Path.Combine(System.IO.Path.GetTempPath, partName)
Const nxjID As String = "12a8872f-0d54-4c56-96b0-4449c512d92d"
theSession.LogFile.WriteLine("NXJID: " & nxjID)
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim IsTcEng As Boolean = False
ufs.UF.IsUgmanagerActive(IsTcEng)
If IsTcEng Then
theUISession.NXMessageBox.Show("Native NX only", NXMessageBox.DialogType.Information, "This journal only works in native NX")
Exit Sub
End If
'lw.WriteLine("user: " & Environment.UserName)
'lw.WriteLine("part path: " & partPath)
'lw.WriteLine("part name: " & partName)
'lw.WriteLine("copy destination: " & copyPart)
'lw.WriteLine("current part units: " & workPart.PartUnits.ToString)
Dim targetUnits As String = ""
Dim unitArg As String = ""
If workPart.PartUnits = BasePart.Units.Millimeters Then
targetUnits = "inch"
unitArg = " -in "
Else
targetUnits = "metric"
unitArg = " -mm "
End If
Dim convertPath As String = theSession.GetEnvironmentVariableValue("UGII_BASE_DIR") & "\NXBIN\ug_convert_part.exe"
'lw.WriteLine("UGII_BASE_DIR: " & convertPath)
Dim c As ComponentAssembly = workPart.ComponentAssembly
If Not IsNothing(c.RootComponent) Then
lw.WriteLine("Assemblies not supported in this version, journal exiting")
Return
Else
lw.WriteLine("Part has no components, units will be converted")
End If
Dim response As Integer
response = theUISession.NXMessageBox.Show("Convert Units?", NXMessageBox.DialogType.Question, "Convert part to " & targetUnits & " units?")
'1 = Yes
'2 = No
If response = 2 Then
Return
End If
If ufs.Part.IsModified(workPart.Tag) Then
response = theUISession.NXMessageBox.Show("Save?", NXMessageBox.DialogType.Question, "Save part before closing?")
If response = 1 Then
'save and close
Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
Else
'just close
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Close Part")
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
'UF_UNDO_delete_all_marks
End If
Else
'just close
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Close Part")
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
'UF_UNDO_delete_all_marks
End If
'save backup copy to temp folder before conversion
Dim tempPath As String = System.IO.Path.GetTempPath
My.Computer.FileSystem.CopyFile(partPath, copyPart, True)
'tell user about backup copy
theUISession.NXMessageBox.Show("Backup Created", NXMessageBox.DialogType.Information, "A backup file has been saved to: " & copyPart)
Dim conversionSuccess As Boolean = False
Try
Dim convertProcess As New Process
Dim myStartInfo As New ProcessStartInfo
myStartInfo.UseShellExecute = False
myStartInfo.CreateNoWindow = True
myStartInfo.FileName = """" & convertPath & """" & unitArg & """" & partPath & """"
myStartInfo.RedirectStandardOutput = True
myStartInfo.RedirectStandardError = True
'lw.WriteLine("""" & convertPath & """" & unitArg & """" & partPath & """")
convertProcess.StartInfo = myStartInfo
convertProcess.Start()
Dim std_out As IO.StreamReader = convertProcess.StandardOutput()
Dim std_err As IO.StreamReader = convertProcess.StandardError()
'lw.WriteLine("")
'lw.WriteLine("stdout:")
Dim stdOutput As String = std_out.ReadToEnd
Dim stdLines() As String = stdOutput.Split(ControlChars.CrLf)
For Each Line As String In stdLines
If Line.ToUpper.Contains("SUCCESS") Then
conversionSuccess = True
End If
'lw.WriteLine(Line)
Next
Dim stdError As String = std_err.ReadToEnd
If Not String.IsNullOrEmpty(stdError) Then
lw.WriteLine("")
lw.WriteLine("stdErr:")
For Each Line As String In stdError
lw.WriteLine(Line)
Next
End If
std_out.Close()
std_err.Close()
convertProcess.Close()
Catch ex As Exception
lw.WriteLine("Error with ug_convert_part.exe")
lw.WriteLine(ex.GetType.ToString & " : " & ex.Message)
End Try
If conversionSuccess Then
theUISession.NXMessageBox.Show("Success", NXMessageBox.DialogType.Information, "File converted successfully, reopening file")
Else
theUISession.NXMessageBox.Show("Failure", NXMessageBox.DialogType.Information, "File failed to convert, reopening file")
End If
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBaseDisplay(partPath, partLoadStatus1)
workPart = theSession.Parts.Work
partLoadStatus1.Dispose()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures
NX11, Win 10 Pro
But in the end, the part is not converted... any ideas??
'NXJournaling.com
'May 13, 2013
'
'journal to convert the units of the work part, native NX filesystem only
'journal closes the work part and calls ug_convert_part, reports success/failure, and reopens the part
'October 23, 2019
'updated to work with NX 11 and above:
'ug_convert_part is now located in the NXBIN folder
'removed call to remote utilities .GetEnvironmentVariable (obsolete)
Option Strict Off
Imports System
Imports System.Diagnostics
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Module Module2
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession
Dim theUISession As UI = UI.GetUI
Dim workPart As Part = theSession.Parts.Work
Dim partPath As String = workPart.FullPath
Dim partName As String = System.IO.Path.GetFileName(partPath)
Dim copyPart As String = System.IO.Path.Combine(System.IO.Path.GetTempPath, partName)
Const nxjID As String = "12a8872f-0d54-4c56-96b0-4449c512d92d"
theSession.LogFile.WriteLine("NXJID: " & nxjID)
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim IsTcEng As Boolean = False
ufs.UF.IsUgmanagerActive(IsTcEng)
If IsTcEng Then
theUISession.NXMessageBox.Show("Native NX only", NXMessageBox.DialogType.Information, "This journal only works in native NX")
Exit Sub
End If
'lw.WriteLine("user: " & Environment.UserName)
'lw.WriteLine("part path: " & partPath)
'lw.WriteLine("part name: " & partName)
'lw.WriteLine("copy destination: " & copyPart)
'lw.WriteLine("current part units: " & workPart.PartUnits.ToString)
Dim targetUnits As String = ""
Dim unitArg As String = ""
If workPart.PartUnits = BasePart.Units.Millimeters Then
targetUnits = "inch"
unitArg = " -in "
Else
targetUnits = "metric"
unitArg = " -mm "
End If
Dim convertPath As String = theSession.GetEnvironmentVariableValue("UGII_BASE_DIR") & "\NXBIN\ug_convert_part.exe"
'lw.WriteLine("UGII_BASE_DIR: " & convertPath)
Dim c As ComponentAssembly = workPart.ComponentAssembly
If Not IsNothing(c.RootComponent) Then
lw.WriteLine("Assemblies not supported in this version, journal exiting")
Return
Else
lw.WriteLine("Part has no components, units will be converted")
End If
Dim response As Integer
response = theUISession.NXMessageBox.Show("Convert Units?", NXMessageBox.DialogType.Question, "Convert part to " & targetUnits & " units?")
'1 = Yes
'2 = No
If response = 2 Then
Return
End If
If ufs.Part.IsModified(workPart.Tag) Then
response = theUISession.NXMessageBox.Show("Save?", NXMessageBox.DialogType.Question, "Save part before closing?")
If response = 1 Then
'save and close
Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = workPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
Else
'just close
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Close Part")
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
'UF_UNDO_delete_all_marks
End If
Else
'just close
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Close Part")
workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
workPart = Nothing
'UF_UNDO_delete_all_marks
End If
'save backup copy to temp folder before conversion
Dim tempPath As String = System.IO.Path.GetTempPath
My.Computer.FileSystem.CopyFile(partPath, copyPart, True)
'tell user about backup copy
theUISession.NXMessageBox.Show("Backup Created", NXMessageBox.DialogType.Information, "A backup file has been saved to: " & copyPart)
Dim conversionSuccess As Boolean = False
Try
Dim convertProcess As New Process
Dim myStartInfo As New ProcessStartInfo
myStartInfo.UseShellExecute = False
myStartInfo.CreateNoWindow = True
myStartInfo.FileName = """" & convertPath & """" & unitArg & """" & partPath & """"
myStartInfo.RedirectStandardOutput = True
myStartInfo.RedirectStandardError = True
'lw.WriteLine("""" & convertPath & """" & unitArg & """" & partPath & """")
convertProcess.StartInfo = myStartInfo
convertProcess.Start()
Dim std_out As IO.StreamReader = convertProcess.StandardOutput()
Dim std_err As IO.StreamReader = convertProcess.StandardError()
'lw.WriteLine("")
'lw.WriteLine("stdout:")
Dim stdOutput As String = std_out.ReadToEnd
Dim stdLines() As String = stdOutput.Split(ControlChars.CrLf)
For Each Line As String In stdLines
If Line.ToUpper.Contains("SUCCESS") Then
conversionSuccess = True
End If
'lw.WriteLine(Line)
Next
Dim stdError As String = std_err.ReadToEnd
If Not String.IsNullOrEmpty(stdError) Then
lw.WriteLine("")
lw.WriteLine("stdErr:")
For Each Line As String In stdError
lw.WriteLine(Line)
Next
End If
std_out.Close()
std_err.Close()
convertProcess.Close()
Catch ex As Exception
lw.WriteLine("Error with ug_convert_part.exe")
lw.WriteLine(ex.GetType.ToString & " : " & ex.Message)
End Try
If conversionSuccess Then
theUISession.NXMessageBox.Show("Success", NXMessageBox.DialogType.Information, "File converted successfully, reopening file")
Else
theUISession.NXMessageBox.Show("Failure", NXMessageBox.DialogType.Information, "File failed to convert, reopening file")
End If
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBaseDisplay(partPath, partLoadStatus1)
workPart = theSession.Parts.Work
partLoadStatus1.Dispose()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures
NX11, Win 10 Pro