Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Can Use Some Help with Journal Code 3

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
950
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.
 
Replies continue below

Recommended for you

Kenja824 said:
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?

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
 
I only created two simple journals to test it so far. I recorded turning the border to views to a dark gray and I recorded one going into the view settings and unchecking the "Create with Centerlines" box. Two simple ones that once I saw they both worked in play back I tried to combine them. But if it will be very tough to do it, I wonder if it is best to just make a list of everything I want it to do and try doing it all in one recording. As I wont be able to have someone hold my hand through it every time I do it. lol
 
I'm confident that once you see the process, you will be able to edit future journals you create. I just thought it would be more useful to use two actual journals that you already have rather than some contrived example.

www.nxjournaling.com
 
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
 
Excellent! I did the "No Centerlines" Journal and it worked perfect still. Looking at them, (once they were cleaned up) I could see how identical they were, other than the actual objective of what they were to do. So I copied just the one line of code from the Gray Borders and pasted it into the No Centerlines journal and it worked like a charm.

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
 
Welllll..... I have hit a bump in the road. I created a Journal with a bunch of settings in Drafting Preferences. I run the Journal and it works great. I got out of UG and reloaded it and ran the Journal and it ran great again. However, when I had domeone else try and run the Journal, it errored out right away. I had another person try it and that errored out part way through it.

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?
 
Are you all running the same version of NX? If not, what versions are in use?
Can you post a screenshot of the error message that appears?

www.nxjournaling.com
 
To my knowledge, we all have the same version. NX9. It is entirely possible some computers may not have received one patch at one time or another.

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.
 
 http://files.engineering.com/getfile.aspx?folder=033a6363-2c66-49dc-a251-626ef2606906&file=Journal_Error.jpg
The one computer (the bottom error message) probably needs to have the correct version of the .net framework installed (version 4). It can be found here:

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
 
Yes, I have had problems with Macros in the past for that reason. Forgot about that. It seems some computers have different pallets available or something. I will have to ask I.T. about that. (I'm not I.T., I am just one of the guys who learned some of this so they give me the company toolbars and such to fix when NX revs up like this. lol)

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?
 
Try the following journal, it should get you past the "finding iron gray" error. Rather than replacing someone's favorite color definition file (which is also possible) it will simply use the closest color it can find in that CDF.

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 Module

www.nxjournaling.com
 
Thanks cowski. I'm leaving real soon and wont be in tomorrow. I will get it done Friday and let you know how it works. I really appreciate all the help.
 
The Journal seems to work great Cowski. I see where you even got rid of some waste I left in mine. Thanks again for all of the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor