multicaduser
Industrial
- Jan 29, 2013
- 261
Just trying to move from grip to vb journaling. Below is the code for my first try, no error checking yet, just got it functioning first. Looking for comments on the style of vb program writing being used, if there are better ways. No ide, just the journal editor in NX. All constructive criticism appreciated.
'NX 12
'Journal allows selection of file use type,
'rev level of file, and selection of objects
'to output for parasolid translation.
'
'Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module NXJournal
Sub Main ()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim currentPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim currentFile As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim lw As ListingWindow = theSession.ListingWindow
Dim opType As String = ""
Dim opRevLevel As String = ""
Dim opFileName As String = ""
Dim mySelectedObjects() As NXObject
Dim myResponse As Selection.Response
Dim tagList() As NXOpen.Tag
Dim i As Integer = 0
opType = opChoice
opRevLevel = RevLevel
opFileName = (fileNamePieces(opType, opRevLevel, currentPath, currentFile))
myResponse = SelectObjects(mySelectedObjects)
If (myResponse = Selection.Response.Cancel) OrElse (myResponse = Selection.Response.Back) Then
'user canceled selection, exit journal
Exit Sub
End If
ReDim tagList(mySelectedObjects.GetUpperBound(0))
For i = 0 To mySelectedObjects.GetUpperBound(0)
tagList(i) = mySelectedObjects(i).Tag
'lw.WriteLine("object tag is: " & tagList(i))
Next
ufs.Ps.ExportData(tagList, opFileName & ".x_t")
lw.WriteLine("Output parasolid file name = " & opFileName & ".x_t")
ufs.Disp.CreateImage(opFileName & ".png", UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
lw.WriteLine("Output image file name = " & opFileName & ".png")
'theUfSession.Ui.UpdateListingWindow()
'Threading.Thread.Sleep(4000)
'lw.CloseWindow()
End Sub
'select operation type
Function opChoice() As String
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
lw.open()
Dim opList As New List(Of String)
opList.Add("electrode")
opList.Add("engraving")
opList.Add("surface")
opList.Add("wireframe")
Dim retVal As Integer = 0
Dim title As String = "Select file output type"
theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
retVal = theUFSession.Ui.DisplayMenu(title, 1, opList.ToArray, opList.Count)
theUFSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
'1=back
'2=cancel
'5-18=menu item selected
'19=disallowed state, unable to bring up dialog
Select Case retVal
Case Is = 5
Return "electrode"
Case Is = 6
Return "engraving"
Case Is = 7
Return "surface"
Case Is = 8
Return "wireframe"
End Select
End Function
'select revision level
Function RevLevel()
Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim retRev As String = ""
Dim retVal As Integer = 0
Dim RevList As New List(Of String)
revList.Add("None")
revList.Add("A")
revList.Add("B")
revList.Add("C")
revList.Add("D")
revList.Add("E")
revList.Add("F")
revList.Add("G")
Dim title As String = "Select Rev Level"
theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
retVal = theUfSession.Ui.DisplayMenu(title, 1, revList.ToArray, revList.Count)
theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
Select Case retVal
Case 5
retRev = ""
Case 6
retRev = "a"
Case 7
retRev = "b"
Case 8
retRev = "c"
Case 9
retRev = "d"
Case 10
retRev = "e"
Case 11
retRev = "f"
Case 12
retRev = "g"
End Select
If retRev <> "" Then
retRev = ("_rev_" & retRev)
End If
RETURN retRev
End Function
'select objects from the screen
Function SelectObjects(ByRef selobj() As NXObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim prompt as String = "Select Solid Bodies for export"
Dim title As String = "Selection"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = _
Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
Dim tagList() As NXOpen.Tag
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With
Dim resp As Selection.Response = _
theUI.SelectionManager.SelectObjects( _
prompt, title, scope, selAction, _
includeFeatures, keepHighlighted, _
selectionMask_array, selobj)
Return resp
End Function
'file name function
Function fileNamePieces(opType As String, opRev As String, opPath As String, opFile As String)
Dim fullPath as string = ""
Dim pathPosition As Integer = 1
pathPosition = Instr(1,opPath, "CAD")
fullPath = ((Left(opPath, pathPosition-1)) & "CAM\" & opType & "\" & opFile & opRev)
Return fullPath
End Function
End Module
NX 12.0.1.7 Windows 10
'NX 12
'Journal allows selection of file use type,
'rev level of file, and selection of objects
'to output for parasolid translation.
'
'Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module NXJournal
Sub Main ()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim currentPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim currentFile As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim lw As ListingWindow = theSession.ListingWindow
Dim opType As String = ""
Dim opRevLevel As String = ""
Dim opFileName As String = ""
Dim mySelectedObjects() As NXObject
Dim myResponse As Selection.Response
Dim tagList() As NXOpen.Tag
Dim i As Integer = 0
opType = opChoice
opRevLevel = RevLevel
opFileName = (fileNamePieces(opType, opRevLevel, currentPath, currentFile))
myResponse = SelectObjects(mySelectedObjects)
If (myResponse = Selection.Response.Cancel) OrElse (myResponse = Selection.Response.Back) Then
'user canceled selection, exit journal
Exit Sub
End If
ReDim tagList(mySelectedObjects.GetUpperBound(0))
For i = 0 To mySelectedObjects.GetUpperBound(0)
tagList(i) = mySelectedObjects(i).Tag
'lw.WriteLine("object tag is: " & tagList(i))
Next
ufs.Ps.ExportData(tagList, opFileName & ".x_t")
lw.WriteLine("Output parasolid file name = " & opFileName & ".x_t")
ufs.Disp.CreateImage(opFileName & ".png", UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
lw.WriteLine("Output image file name = " & opFileName & ".png")
'theUfSession.Ui.UpdateListingWindow()
'Threading.Thread.Sleep(4000)
'lw.CloseWindow()
End Sub
'select operation type
Function opChoice() As String
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
lw.open()
Dim opList As New List(Of String)
opList.Add("electrode")
opList.Add("engraving")
opList.Add("surface")
opList.Add("wireframe")
Dim retVal As Integer = 0
Dim title As String = "Select file output type"
theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
retVal = theUFSession.Ui.DisplayMenu(title, 1, opList.ToArray, opList.Count)
theUFSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
'1=back
'2=cancel
'5-18=menu item selected
'19=disallowed state, unable to bring up dialog
Select Case retVal
Case Is = 5
Return "electrode"
Case Is = 6
Return "engraving"
Case Is = 7
Return "surface"
Case Is = 8
Return "wireframe"
End Select
End Function
'select revision level
Function RevLevel()
Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim retRev As String = ""
Dim retVal As Integer = 0
Dim RevList As New List(Of String)
revList.Add("None")
revList.Add("A")
revList.Add("B")
revList.Add("C")
revList.Add("D")
revList.Add("E")
revList.Add("F")
revList.Add("G")
Dim title As String = "Select Rev Level"
theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
retVal = theUfSession.Ui.DisplayMenu(title, 1, revList.ToArray, revList.Count)
theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
Select Case retVal
Case 5
retRev = ""
Case 6
retRev = "a"
Case 7
retRev = "b"
Case 8
retRev = "c"
Case 9
retRev = "d"
Case 10
retRev = "e"
Case 11
retRev = "f"
Case 12
retRev = "g"
End Select
If retRev <> "" Then
retRev = ("_rev_" & retRev)
End If
RETURN retRev
End Function
'select objects from the screen
Function SelectObjects(ByRef selobj() As NXObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim prompt as String = "Select Solid Bodies for export"
Dim title As String = "Selection"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = _
Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
Dim tagList() As NXOpen.Tag
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With
Dim resp As Selection.Response = _
theUI.SelectionManager.SelectObjects( _
prompt, title, scope, selAction, _
includeFeatures, keepHighlighted, _
selectionMask_array, selobj)
Return resp
End Function
'file name function
Function fileNamePieces(opType As String, opRev As String, opPath As String, opFile As String)
Dim fullPath as string = ""
Dim pathPosition As Integer = 1
pathPosition = Instr(1,opPath, "CAD")
fullPath = ((Left(opPath, pathPosition-1)) & "CAM\" & opType & "\" & opFile & opRev)
Return fullPath
End Function
End Module
NX 12.0.1.7 Windows 10