Can Use Some Help with Journal Code
Can Use Some Help with Journal Code
(OP)
I have always worked with Macros in the past. Unfortunately, Macros dont seem to want to record going into Preferences - Drafting. I found I can record a Journal and make a change in Preferences - Drafting.
Now - When I recorded Macros in the past, I found it was too easy to make a mistake and forget something if you tried to record all of the setting changes in one recording. So I would make a few smaller macros and combine them. If I missed something, I could record what I missed and combine it to the main macro. Unfortunately, I have no idea how to do this with Journal code. I tried a few things but got errors. Is there a certain section of Journal I can remove from the end of one and the beginning of another to make them work together?
By the way, thanks for the help I have been getting here. NX9 has been a big change over and this forum has been a great help.
Now - When I recorded Macros in the past, I found it was too easy to make a mistake and forget something if you tried to record all of the setting changes in one recording. So I would make a few smaller macros and combine them. If I missed something, I could record what I missed and combine it to the main macro. Unfortunately, I have no idea how to do this with Journal code. I tried a few things but got errors. Is there a certain section of Journal I can remove from the end of one and the beginning of another to make them work together?
By the way, thanks for the help I have been getting here. NX9 has been a big change over and this forum has been a great help.





RE: Can Use Some Help with Journal Code
Using the journal recorder is a great way to build up a useful journal. Unfortunately, combining multiple journals is not as simple as copy & paste with macro code. In general, you can copy & paste the code inside the main subroutine (or function depending on the language used) then look for & fix any duplicate variable names. Depending on what action was recorded, there may be a lot of 'fluff' generated by the journal recorder that can be eliminated entirely.
If you can post two of your journals along with the desired output of each, I'd be glad to step you through the process.
www.nxjournaling.com
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
www.nxjournaling.com
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
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 ModuleNX 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
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")CODE
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)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 ModuleThis 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
RE: Can Use Some Help with Journal Code
EXAMPLE.....
-----------------------------------------------------------------------------------------
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
Dim displayPart As Part = theSession.Parts.Display
' ----------------------------------------------
' 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.ViewStyle.ViewStyleGeneral.Silhouettes = True
preferencesBuilder1.ViewStyle.ViewStyleGeneral.Centerlines = False
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
--------------------------------------------------------------------------------------
Thanks Cowski. I do appreciate the help, and your time. Especially the length of detail you went into. I will work on recording a few and seeing how well I do putting them all together. lol
RE: Can Use Some Help with Journal Code
Happy coding!
www.nxjournaling.com
RE: Can Use Some Help with Journal Code
Is there something in the Journal's writing that makes it only good for the computer that created it? Do I need to add/change something to make it run on other UG sessions?
RE: Can Use Some Help with Journal Code
Can you post a screenshot of the error message that appears?
www.nxjournaling.com
RE: Can Use Some Help with Journal Code
I tried it on three others computers and came up with two different errors. In the picture the top error came up twice and the bottom error was on one computer.
In another post, I will also upload the Journal in case that will help.
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
http://www.microsoft.com/en-us/download/details.as...
The other computer can't seem to find the color specified; perhaps it is using a custom color palette? There are some functions for getting the closest color in the CDF to the one specified, I'll see if I can dig those up...
www.nxjournaling.com
RE: Can Use Some Help with Journal Code
By chance, would NX have a spacific pallet that all computers would have? If so, is it possible to have the Journal choose that pallet to use before doing anything else?
RE: Can Use Some Help with Journal Code
CODE
Option Strict Off Imports System Imports NXOpen Imports NXOpen.UF Module NXJournal Sub Main(ByVal args() As String) Dim theSession As Session = Session.GetSession() Dim theUfSession As UFSession = UFSession.GetUFSession() If IsNothing(theSession.Parts.Work) Then 'active part required Return End If Dim workPart As Part = theSession.Parts.Work Dim markId1 As Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start") Dim preferencesBuilder1 As Drafting.PreferencesBuilder preferencesBuilder1 = workPart.SettingsManager.CreatePreferencesBuilder() preferencesBuilder1.ViewStyle.ViewStyleGeneral.Silhouettes = True preferencesBuilder1.ViewStyle.ViewStyleGeneral.Centerlines = False 'find closest NX color in display part color table 'Iron Gray (R,G,B) = 76, 76, 76 Dim colorValues(2) As Double colorValues(0) = 76 / 255 colorValues(1) = 76 / 255 colorValues(2) = 76 / 255 Dim closeColor As Integer theUfSession.Disp.AskClosestColor(UFConstants.UF_DISP_rgb_model, colorValues, UFConstants.UF_DISP_CCM_EUCLIDEAN_DISTANCE, closeColor) Dim myBorderColor As NXColor myBorderColor = workPart.Colors.Find(closeColor) 'preferencesBuilder1.ViewWorkflow.BorderColor = workPart.Colors.Find("Iron Gray") preferencesBuilder1.ViewWorkflow.BorderColor = myBorderColor Dim fontIndex1 As Integer fontIndex1 = workPart.Fonts.AddFont("leroy", FontCollection.Type.Nx) preferencesBuilder1.ViewStyle.ViewStyleVisibleLines.VisibleColor = workPart.Colors.Find(-1) preferencesBuilder1.ViewStyle.ViewStyleVisibleLines.VisibleColor = workPart.Colors.Find("Background") preferencesBuilder1.ViewStyle.ViewStyleHiddenLines.Color = workPart.Colors.Find(-1) preferencesBuilder1.ViewStyle.ViewStyleHiddenLines.Color = workPart.Colors.Find("Background") preferencesBuilder1.ViewStyle.ViewStyleHiddenLines.EdgesHiddenByEdges = True preferencesBuilder1.ViewStyle.ViewStyleSmoothEdges.Color = workPart.Colors.Find(-1) preferencesBuilder1.ViewStyle.ViewStyleSmoothEdges.Color = workPart.Colors.Find("Background") preferencesBuilder1.ViewStyle.ViewStyleSmoothEdges.SmoothEdge = False preferencesBuilder1.ViewStyle.ViewProjectedViewSettings.DisplayArrowOnParentView = Drawings.ViewProjectedViewSettingsBuilder.DisplayArrowOnParentViewType.No preferencesBuilder1.ViewStyle.ViewStyleSection.SheetBodies = True preferencesBuilder1.ViewStyle.ViewStyleSection.Background = True preferencesBuilder1.AnnotationStyle.DimensionStyle.ChamferSeparator = Annotations.ChamferSeparatorType.UppercaseX preferencesBuilder1.AnnotationStyle.OrdinateStyle.PositiveDirection = Annotations.OrdinatePositiveDirection.UpperRight preferencesBuilder1.AnnotationStyle.OrdinateStyle.DisplayNameStyle = Annotations.OrdinateOriginDisplayOption.NoText preferencesBuilder1.AnnotationStyle.OrdinateStyle.OrdinateTextOrientation = Annotations.TextOrientation.Aligned preferencesBuilder1.AnnotationStyle.LetteringStyle.AppendedTextSize = 2.5 preferencesBuilder1.AnnotationStyle.LetteringStyle.DimensionTextSize = 2.5 preferencesBuilder1.AnnotationStyle.LetteringStyle.ToleranceTextSize = 2.5 preferencesBuilder1.AnnotationStyle.HoleCalloutSettings.SetLeaderAttachment(Annotations.HoleCalloutSettingsBuilder.LeaderAttachment.Top) Dim nXObject1 As NXObject nXObject1 = preferencesBuilder1.Commit() theSession.SetUndoMarkName(markId1, "Drafting Preferences") preferencesBuilder1.Destroy() End Sub End Modulewww.nxjournaling.com
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
RE: Can Use Some Help with Journal Code
www.nxjournaling.com