These are good examples of the journal recorder giving you more than what you really need. I have a feeling they will be short & simple journals when we are done cleaning them up.
The journal language that was used is Visual Basic .net (VB.net); some of the comments following are specific to VB, the syntax of C# is slightly different (and C++ is quite different). The single quote mark, or apostrophe, (') is used to denote the following text as a comment. Comments in the code are not executed as commands, they are generally used as a programmer's note to him/her self or to temporarily disable a line of code that you do not want to delete just yet. After a few comments at the top of the file, you'll see a line that starts with the "Option" keyword and a few that start with "Imports". We won't need to change these for what we are doing, but when combining journals, be aware that these lines will only appear once and they need to be outside of the "Module" declaration. Which brings us to the Module: all the code we write (subroutines and functions) must be contained in either a module or class. The journal recorder creates a module and subroutine (Sub Main()) for us. The journal 'skeleton' looks like this:
Code:
Option ...
Imports ...
Module {module name}
Sub Main(ByVal args() as string)
'journal recorder code
End Sub
End Module
NX looks for and executes Sub Main when you start the journal; Sub Main must exist in the journal. We can write other subroutines or functions to make our lives easier, but they are optional.
The "Dim" keyword means a variable is being created; it is followed by the name of the variable, the type of the variable, and optionally the value we want to assign to the variable. In recorded journals, the first two lines in Sub Main are:
Code:
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Most of the variables in journals will refer to objects created by the developers of the NXOpen API. The varaible "theSession" refers to the NX session and gives us access to its functionality, we won't get far without a reference to the NX session. An "object" type of variable is a way for the developer to encapsulate related code and variables; we can access this functionality through the dot operator. The NX session contains a collection of every part you currently have open, only one of which can be the work part. To get a reference to the current work part (for our convenience), we define the "workPart" variable as above.
Ok, on with the practical stuff. Let's start by looking at the border color journal. After recording a journal, the first thing I look for is the specific value that I set when recording the journal. You changed the border color to gray, which can be found in line 146:
Code:
preferencesBuilder1.ViewWorkflow.BorderColor = workPart.Colors.Find("Iron Gray")
From this line, we immediately know that
preferencesBuilder1 and
workPart are essential for the working of this journal. Scroll back up near the top to see the order of operations:
Code:
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim preferencesBuilder1 As Drafting.PreferencesBuilder
preferencesBuilder1 = workPart.SettingsManager.CreatePreferencesBuilder()
The journal starts by creating an "undo mark"; this is good, it will allow you to undo any changes the journal makes if you so desire. After than we see preferencesBuilder1, which we now know is necessary for what we want to do. Next, things go a bit off track...
Code:
Dim origin1 As Point3d = New Point3d(0.0, 0.0, 0.0)
Dim normal1 As Vector3d = New Vector3d(0.0, 0.0, 1.0)
Dim plane1 As Plane
plane1 = workPart.Planes.CreatePlane(origin1, normal1, SmartObject.UpdateOption.WithinModeling)
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
Dim expression1 As Expression
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1)
Now the journal is creating other objects such as points, vectors, planes, and expressions that don't seem to be necessary to change the color of a view border... All of these lines of code are suspect. If you search the file for
origin1, you'll see that it is used in the definition of
plane1, but is not referenced anywhere else in the code. When we search for
plane1, we see that it is created and near the end of the file it is destroyed, no other code in the file references
plane1.
plane1,
origin1, and
normal1 are good candidates for deletion; before we delete them, let's comment out those code lines and run the journal again to make sure it still works as intended without errors.
Keep working through the code, disabling lines that don't seem to be necessary. Expressions? probably don't need those to change the border color. Sheet metal flat pattern callout builder? I don't see how that's necessary... If the journal still works after testing, delete the lines you previously commented out and continue cleaning as necessary. If the journal no longer works, re-enable the lines you commented out and try to figure out why they are needed.
I boiled the border color journal down to the following code:
Code:
Option Strict Off
Imports System
Imports NXOpen
Module NXJournal
Sub Main (ByVal args() As String)
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
' ----------------------------------------------
' Menu: Preferences->Drafting...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim preferencesBuilder1 As Drafting.PreferencesBuilder
preferencesBuilder1 = workPart.SettingsManager.CreatePreferencesBuilder()
preferencesBuilder1.ViewWorkflow.BorderColor = workPart.Colors.Find("Iron Gray")
Dim nXObject1 As NXObject
nXObject1 = preferencesBuilder1.Commit()
theSession.SetUndoMarkName(markId1, "Drafting Preferences")
preferencesBuilder1.Destroy()
End Sub
End Module
This is a short & simple code listing that will prove to be much easier to combine with other code than the original recorded journal. Try the process out on the other journal to see what you come up with.
www.nxjournaling.com