Hello,
My name is Olaf I am longtime user of NX ( If 16 years is long) In my career I have written only two VB scripts, both using info of this site. I find it difficult how the NX objects are structured and my programming knowledge is before Object Oriented became mainstream.
Please could you look at my questions.
Start situation:
In NX a face divide into two parts by a plane. The intention of this script is to get the plane at such a position that both sides are equal.
However the code is not made that the left area divided by the right area equals 1, but the total area divided by the left area equals 2.
The result is as expected, the plane is positioned at the correct position.
However I have some messages appearing I don't understand.
Below is the result after running non_gui.bat. In this batch file the part and VisualBasic script is called, area_boundary_ratio.prt and plane_definition.vb.
In the block quote, the output of the script is given, some print statement are shown for debugging purposes and tracking of the program flow. But at the end messages are printed that I don't like or understand.
C:\Users\s021355\Dropbox\Master\afstuderen\VisualBasic\nongui>rem Run non_gui.bat
partLoadStatus1: PartLoadStatus 140699257591264
difference_area : 32.1460183660255
big_step : 5.35766972767092Index : 0
expA.RightHandSide : 55
result total : 2485.84073464102
result left : 1885.84073464102
result division total_area/left_area : 1.3181604835332
Here I am value2 <= two_plus After End If: 1.3181604835332
Here I am value2 <= two_plus In the IF: 1.3181604835332
NOT (value2 < two_plus) AND (value2 > two_minus) : 1.3181604835332
Index third while : 1
expA.RightHandSide third while : 55
expA.RightHandSide third while after assign : 49.6423302723291
expA.RightHandSide third while after update : 49.6423302723291
result division total_area/left_area : 1.48716138328055
Index third while : 2
expA.RightHandSide third while : 49.6423302723291
expA.RightHandSide third while after assign : 44.2846605446582
expA.RightHandSide third while after update : 44.2846605446582
result division total_area/left_area : 1.70587044283165
Index third while : 3
expA.RightHandSide third while : 44.2846605446582
expA.RightHandSide third while after assign : 38.9269908169873
expA.RightHandSide third while after update : 38.9269908169873
result division total_area/left_area : 2
*** ERROR system never initialised, sending output to stderr ***Journal execution results for C:\Users\s021355\Dropbox\Master\afstuderen\VisualB
asic\nongui\plane_definition.vb...
Execution was successful.Journal execution results for C:\Users\s021355\Dropbox\Master\afstuderen\VisualB
asic\nongui\plane_definition.vb...
Execution was successful.Press any key to continue . . .
My questions, how do I get rid of the message:
*** ERROR system never initialised, sending output to stderr ***
Why do I get two times?:
Journal execution results for C:\Users\s021355\Dropbox\Master\afstuderen\VisualB
asic\nongui\plane_definition.vb...
Execution was successful.
How do I code that the necessary expressions: height, left, total and plane_definition are found and when not I get an error message expression property is missing? I know it is something with try and catch
Last question, in what environment can I code the best with NX? Are there environments where you can debug instead of writing everywhere print statements?
What I have coded, thanks to the information at your forum plane_definition.vbs and non_gui.bar:
Thanks for your time.
Regards,
Olaf
rem Run non_gui.bat
@ECHO OFF
ECHO.
call "C:\Program Files\Siemens\NX 12.0\NXBIN\run_journal.exe" plane_definition.vb -args area_boundary_ratio.prt
rem Finished rem Run test_non_gui.bat
ECHO.
PAUSE()
CLS()
EXIT
Option Strict Off
Imports System
Imports NXOpen
Module startup_no_gui
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
Dim workPart As Part
Dim displayedPart As Part
Dim sTextFile As New System.Text.StringBuilder
Dim sFileName As String = "C:\Users\s021355\Dropbox\Master\afstuderen\VisualBasic\test.txt"
Sub Main(args() as string)
lw.Open()
Const two_plus As Double = 2.0001
Const two_minus As Double = 1.9999
Dim big_step As Double = 0.001
Dim small_step As Double
Const undoMarkName As String = "NXJ query expressions"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
OpenPart(args(0))
Dim expA As NXOpen.Expression = workPart.Expressions.FindObject("plane_definition")
' ----------------------------------------------
' Menu: File->Save
' ----------------------------------------------
partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()
Dim height As NXOpen.Expression = workPart.Expressions.FindObject("height")
Dim total As NXOpen.Expression = workPart.Expressions.FindObject("total")
Dim left As NXOpen.Expression = workPart.Expressions.FindObject("left")
Dim left_area as Double
Dim total_area as Double
Dim height_length as Double
Dim value2 as Double
Dim Index as Integer
Dim nErrs1 As Integer
Dim difference_area As Double
total=workPart.Expressions.FindObject("total")
left=workPart.Expressions.FindObject("left")
height=workPart.Expressions.FindObject("height")
left_area = CType(left.value, Double)
total_area = CType(total.value, Double)
height_length=CType(height.value, Double)
difference_area=Math.abs(total_area-2*left_area)/height_length
big_step= difference_area/6
lw.Writeline("difference_area : "& difference_area.ToString)
lw.Writeline("big_step : "& big_step.ToString)
expA = workPart.Expressions.FindObject("plane_definition")
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
value2=total_area/left_area
lw.Writeline(" ")
lw.Writeline("Index : "& Index.ToString)
lw.Writeline("expA.RightHandSide : " & expA.RightHandSide.ToString)
lw.WriteLine("result total : " & total.Value.ToString)
lw.Writeline("result left : " & left.Value.Tostring)
lw.Writeline("result division total_area/left_area : " & value2.ToString)
If NOT((value2> two_minus) AND (value2< two_plus))
If value2 > two_plus Then
while NOT ((value2 <= two_plus) AND (value2 >= two_minus))
While value2 > two_plus
index+=1
lw.Writeline("Index first while : "& Index.ToString)
If index = 50 Then
Exit While
End If
expA.RightHandSide += big_step
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
lw.Writeline("expA.RightHandSide first while : " & expA.RightHandSide.ToString)
total=workPart.Expressions.FindObject("total")
left=workPart.Expressions.FindObject("left")
left_area = CType(left.value, Double)
total_area = CType(total.value, Double)
value2=total_area/left_area
lw.Writeline("result division total_area/left_area : " & value2.ToString)
End While
small_step= big_step/3
While value2 < two_minus
index+=1
lw.Writeline("Index second while : "& Index.ToString)
If index = 50 Then
Exit While
End If
expA.RightHandSide -= small_step
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
lw.Writeline("expA.RightHandSide second while : " & expA.RightHandSide.ToString)
total=workPart.Expressions.FindObject("total")
left=workPart.Expressions.FindObject("left")
left_area = CType(left.value, Double)
total_area = CType(total.value, Double)
value2=total_area/left_area
lw.Writeline("result division total_area/left_area : " & value2.ToString)
End While
big_step=small_step/3
index+=1
lw.Writeline("Index after while : "& Index.ToString)
lw.Writeline("result division total_area/left_area : " & value2.ToString)
lw.Writeline("expA.RightHandSide second while : " & expA.RightHandSide.ToString)
End While
End IF
lw.Writeline("Here I am value2 <= two_plus After End If: " & value2.ToString)
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
If value2 < two_minus Then
lw.Writeline("Here I am value2 <= two_plus In the IF: " & value2.ToString)
while NOT ((value2 <= two_plus) AND (value2 >= two_minus))
lw.Writeline("NOT (value2 < two_plus) AND (value2 > two_minus) : " & value2.ToString)
While value2 < two_minus
index+=1
lw.Writeline("Index third while : "& Index.ToString)
If index = 50 Then
Exit While
End If
lw.Writeline("expA.RightHandSide third while : " & expA.RightHandSide.ToString)
expA.RightHandSide -= big_step
lw.Writeline("expA.RightHandSide third while after assign : " & expA.RightHandSide.ToString)
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
lw.Writeline("expA.RightHandSide third while after update : " & expA.RightHandSide.ToString)
total=workPart.Expressions.FindObject("total")
left=workPart.Expressions.FindObject("left")
left_area = CType(left.value, Double)
total_area = CType(total.value, Double)
value2=total_area/left_area
lw.Writeline("result division total_area/left_area : " & value2.ToString)
End While
small_step=big_step/3
While value2 > two_plus
index+=1
lw.Writeline("Index fourth while : "& Index.ToString)
If index = 50 Then
Exit While
End If
lw.Writeline("expA.RightHandSide fourth while : " & expA.RightHandSide.ToString)
expA.RightHandSide += small_step
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
lw.Writeline("expA.RightHandSide fourth while : " & expA.RightHandSide.ToString)
total=workPart.Expressions.FindObject("total")
left=workPart.Expressions.FindObject("left")
left_area = CType(left.value, Double)
total_area = CType(total.value, Double)
value2=total_area/left_area
lw.Writeline("result division total_area/left_area : " & value2.ToString)
End While
big_step=small_step/3
End While
End If
End if
System.IO.File.AppendAllText(sFileName, sTextFile.ToString)
lw.Close()
partSaveStatus1 = workPart.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()
End Sub
sub OpenPart(byval thePart as string)
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus = Nothing
basePart1 = theSession.Parts.OpenBaseDisplay(thePart, partLoadStatus1)
lw.Writeline("partLoadStatus1: " & partLoadStatus1.ToString)
sTextFile.AppendLine("partLoadStatus1: " & partLoadStatus1.ToString)
displayedPart = theSession.Parts.Display
workPart = theSession.parts.work
partLoadStatus1.Dispose()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
re: run journal in batch mode
When you run a console program in Windows, there are 2 output "streams" available to it: stdout (for normal messages) and stderr (for error messages). When a journal is run in batch mode, any calls to the listing window (lw) are automatically redirected to the stdout stream.
I suspect that the run_journal utility has not done anything with the stderr stream and that is the cause of the line "system not initialized" message. I'd guess that the result message ("journal execution successful") gets sent to both stdout and stderr, but stderr is being sent to stdout; so you see the message twice. I'm not 100% sure of this, but it is my best guess. If you are not interested in seeing error messages, you might try redirecting the stderr output to the null device; something like:
call "C:\Program Files\Siemens\NX 12.0\NXBIN\run_journal.exe" plane_definition.vb -args area_boundary_ratio.prt 2>nul
See the following link for more info (also try a web search on the terms "windows redirect stderr").
https://support.microsoft.com/en-us/help/110930/redirecting-error-messag...
http://nxjournaling.com/content/expressions-creating-expressions-no-units
The article above shows how to create expressions. The code attempts to create an expression with a given name; if the expression already exists, a certain error code is returned and the code uses the .FindObject call to get the existing expression instead of creating a new one. Your code could use the same strategy, or you could turn the logic around and wrap the .FindObject call in a Try block and if the expression isn't found, create one instead.
I use a free version of the MS Visual Studio to write journal code. Like you, I often just print out variables to debug my programs. However, I have read several posts at the Siemens community forum that say you can use the VS debugger when writing NXOpen code. You might need to have an NX author license to do this, I'm not sure.
Here's one thread about debugging:
https://community.sw.siemens.com/s/question/0D54O000061xK8zSAE/how-to-de...
The free version of VS might not have the debugger according to Yamada's post...
run journal in batch mode
Hello,
Thanks for your time and extensive answers. I will try you advice and will have a look at the proposed links. This one http://nxjournaling.com/content/expressions-creating-expressions-no-unit" I probably missed or forgot about the contents.
Again, thanks for your time.
Regards,
Olaf