Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for vb journal critique

Status
Not open for further replies.

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
 
Replies continue below

Recommended for you

Not a comment on your journal itself, but you should use the Code box function on this forum. Easier to read for the users.. :)

code_lrfqfu.png



Code:
'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

Ronald van den Broek
Senior Application Engineer
Winterthur Gas & Diesel Ltd
NX9 / TC10.1.2

Building new PLM environment from Scratch using NX12 / TC11
 
Thanks NutAce, wasn't aware of that.

NX 12.0.1.7 Windows 10
 
Your code looks mostly OK, though I didn't read it very carefully. A few comments
(1) Use Option Infer On. It helps a lot.
(2) Don't use ReDim. It's very old-fashioned VB. Use lists, instead.
(3) Using List.AddRange would shorten your code in a couple of places
(4) It's usually a good idea to declare a variable and give it its initial value in the same line of code.

If you don't use an IDE, you're missing out on "intellisense", which is a *HUGE* benefit of VB versus GRIP.

There is a special manual to help people transition from GRIP to SNAP or NX/Open. You might find it useful. It's part of the standard NX doc collection.
 
Thanks BubbaK, just got through looking at examples of all your suggestions and I'm impressed. As a self taught programmer these are jewels to me.

As far as an IDE goes, I've tried to get one but the company won't shell out two bits to help me, but not automating is not an option so I teach myself and shrug off the inconvenience, after all, the best tool is the brain.

Thanks to all for the inputs.

NX 12.0.1.7 Windows 10
 
Thanks cowski, what version of visual studio is compatible with NX12 and are there instructions on integrating it with NX?

NX 12.0.1.7 Windows 10
 
That information can be found in the NX help product notes:

For NX 12 and a .net language, you will need the .net 4.5.1 and 4.6 framework installed. If you are running the code as a journal (uncompiled text file), it doesn't really matter what IDE you use as NX will compile the code right before running it.

C/C++ code is pretty picky about which compilers are compatible; .net languages much less so. Looks like Visual Studio 2015 is required for C/C++; you might want to stick with that for .net development just to be safe (assuming you are compiling your code to .dll or .exe files).

www.nxjournaling.com
 
You should probably read this post.

The two "Getting Started" guides mentioned there will give you a good introduction to SNAP or NX/Open. These guides contain tutorials that use the built-in Journal Editor and also Visual Studio (the free edition).

> the best tool is the brain
Debatable. For mundane rote tasks that don't require any creative thinking, a computer program like an IDE is arguably better than your brain, and is certainly better than my brain.

 
Awesome post to link to Bubbak. IDE's are great for the mundane tasks, but understanding the mundane tasks helps in trouble shooting. Going to read through the nxopen getting started before asking for vs express though.

Thanks for the inputs.

NX 12.0.1.7 Windows 10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor