Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Recorded Journal Help 1

Status
Not open for further replies.

Germfask

Mechanical
Nov 17, 2016
14
Hi I am looking for some help with a journal file that I recorded and modified slightly.

The point of the journal was to enter a part number, in this case atb12345, and then NX would add the file. We are using 9 in Native mode and our part files are organized on the system through various sub folders, so this becomes a pain when I want to add multiple parts that are in multiple directories, so my answer was to create this journal.

I recorded the journal, and then copied some code found on NX Journaling.com to bring up the dialogue box, all that works fine from what I can tell. Also I am not a computer programmer, I am self taught in VB.

When I run the journal I get an error at line 35, which has the file path.

I would also like to make it so that the journal can find the file based on the part number...Essentially I am looking ultimatly for a re-use library button, but without all the different library's.
 
 http://files.engineering.com/getfile.aspx?folder=2819de60-cb38-41cb-b9a7-0f2a98220f4d&file=add_part_test-with_partnumber.txt
Replies continue below

Recommended for you

Code:
basePart1 = theSession.Parts.OpenBase(DirectoryPath & files(0), partLoadStatus1)

To make the code a bit more robust, you can use the Path.Combine method. This method will add a path separator character if one is needed.

Code:
basePart1 = theSession.Parts.OpenBase(System.IO.Path.Combine(DirectoryPath, files(0)), partLoadStatus1)

reference:

www.nxjournaling.com
 
Perfect!!

Once I figured out that I had files returning the full directory path and the part name...

So this:
Code:
basePart1 = theSession.Parts.OpenBase(DirectoryPath & files(0), partLoadStatus1)

Became this:
Code:
basePart1 = theSession.Parts.OpenBase(files(0), partLoadStatus1)


My next step is going to be to try and make it so that this will work with the other parts as well, that start with aXX.

I feel like by taking the user input and setting a search to determine the directory, which would be defined by the first three characters of user input would be the way to go.

I can use something like:
Code:
prefix = answer.SubString(answer.Length - 5).ToLower

Since answer would contain 3 letters and 5 numbers, prefix should contain the first 3 characters of the string and that can then modify directoryPath to search the right folder?

 
I think you are right. At least it's not working as intended...it's not actually working at all since it's throwing me errors on searching.

I have managed to get it to work the way I wanted to though:
Code:
Dim prefix as String = Nothing

prefix = Left(answer, 3)

Dim files() As String

If includeSubDirs Then
	files = Directory.GetFiles(path.combine (DirectoryPath, prefix), answer & "*.prt", SearchOption.AllDirectories)
Else
	files = Directory.GetFiles(path.combine (DirectoryPath, prefix), answer &  "*.prt", SearchOption.TopDirectoryOnly)
End If

where answer is the user input.

Thanks Again for all your help Cowski. I really do appreciate it!

One last question if you don't mind, how is includeSubDirs toggled on and off?
 
In the code that I pulled that from, I had a subroutine that processed the parts in a given directory. The directory to process and the option to include sub-directories were arguments passed into the subroutine.

Code:
Sub processParts(ByVal directoryPath As String, ByVal includeSubDirs As Boolean)
'do stuff
End Sub

'call to the sub elswhere in the code
processParts("C:\temp", True)

www.nxjournaling.com
 
That makes sense now, thanks again for your help.

One last question...I think...

I have been playing around with it and it seems to work fine except for when a part has already been loaded, or is open in another window. Sometimes I have multiple assemblies open at once and it would derail this whole process if I can't add a part to my assembly that is open in another assembly. The error message attached.

Line 44 is:
Code:
basePart1 = theSession.Parts.OpenBase(files(0), partLoadStatus1)

Do you know how I can get around this? This line is from the recorded code, the whole section that has to deal with basePart1 is:
Code:
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = theSession.Parts.OpenBase(files(0), partLoadStatus1)

partLoadStatus1.Dispose()
 
 http://files.engineering.com/getfile.aspx?folder=3d2d1ffe-a35e-4357-8c4c-233d21f804f8&file=Capture.JPG
You can check the NX session's parts collection to see if the part is already loaded or you can wrap the "open part" code in a Try block. Below is a quick example of using a Try block.

Code:
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus = Nothing
Try
    basePart1 = theSession.Parts.OpenBaseDisplay(prt, partLoadStatus1)
Catch ex As NxException
    'code to handle error
Finally
    partLoadStatus1.Dispose()
End Try


Below is a function that takes a part file path string as an argument and looks for the part in the current NX session. If the file is not open, it verifies that the file exists and if so, opens the part. See thread561-350926 for the full journal where this code was used.

Code:
    Function GetPart(ByVal partPath As String) As Part

        Try
            Dim markId1 As Session.UndoMarkId
            markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Change Display Part")

            Dim part1 As Part = CType(theSession.Parts.FindObject(IO.Path.GetFileNameWithoutExtension(partPath)), Part)

            Dim partLoadStatus1 As PartLoadStatus
            Dim status1 As PartCollection.SdpsStatus
            status1 = theSession.Parts.SetDisplay(part1, False, False, partLoadStatus1)

            workPart = theSession.Parts.Work
            displayPart = theSession.Parts.Display
            partLoadStatus1.Dispose()
            theSession.DeleteUndoMark(markId1, "Change Display Part")
            Return part1

        Catch ex As NXException
            If ex.ErrorCode = 3520016 Then
                'part wasn't found in session
                If My.Computer.FileSystem.FileExists(partPath) Then
                    'open part
                    Try
                        Return OpenPart(partPath)
                    Catch ex2 As Exception
                        MessageBox.Show(ex2.Message, "Error opening part", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                Else
                    'part file does not exist
                    Return NewFile(partPath)
                End If
            End If
        End Try

        Return Nothing

    End Function

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor