Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Convert Units Journal: Executes but fails to convert

Status
Not open for further replies.

3dr

Automotive
Jul 10, 2004
449
0
0
US
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
 
Replies continue below

Recommended for you

Note that this journal only works on the current work part and only if it is a piece part (no components). It uses the current "UGII_BASE_DIR" variable to get the path to the NX CLI conversion program; be careful if you have multiple versions of NX installed.

Before running the journal, go to information -> parts -> loaded parts. This will list all of the parts loaded in session and will tell you what unit system each is using. Run the journal and check again. If the journal didn't report any errors, the file should now be in the other unit system (inch -> mm or mm -> inch).

Also note that this journal does NOT convert the units of existing expressions.

Does this help?

www.nxjournaling.com
 
Thanks Cowski... didn't expect you or anyone else to be on here this week.

Made sure my environment variable "UGII_BASE_DIR" "C:\Program Files\Siemens\NX 11.0 - BASE" is correct

Checked information -> parts -> loaded parts before and after executing.
Same units, no change.

I downloaded this from "
Noticed one post on the thread, (see below) that claims the code was not converting to the correct units.
But rather converting mm to mm and inch to inch.
Doesn't look like the poster included the code tho.

"Comments
Bugfix
NXJournaling - Wed, 06/05/2013 - 12:47
I found and fixed a bug today. The target units were being assigned incorrectly (the journal would have tried to convert a metric part to metric units instead of inch units). If you have downloaded the previous code, please delete it and download this updated journal."




Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
I may have bigger issues... using the UG Convert Part Utility is giving the same results.
No conversion. But it does say it was successful.



Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Well, I ran the code you posted on a test file (NX 1919) and it worked for me. Does your file contain any wave links, promoted bodies, or interpart expressions? I think the code should work even if it does, but it might be affecting the result.

There are a number of lines that would write information to the listing window that are currently commented out (ignored when the journal is run). Try un-commenting these lines to see if anything looks incorrect or suspicious. These lines look like the following:
Code:
'lw.writeline(...

Remove the leading single apostrophe to make the code active and re-run the journal. Then check over the information window to see that everything is correct.
Code:
lw.writeline(...

www.nxjournaling.com
 
3dr said:
I may have bigger issues... using the UG Convert Part Utility is giving the same results.
No conversion. But it does say it was successful.

I didn't see this before my previous post. Yes, it sounds like you have deeper issues here. I'd suggest opening a ticket with your NX support. The journal code is just a fancy wrapper for the ug_convert_part utility; if that isn't working for you, then the journal won't either.

www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top