×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Recorded Journal Help

Recorded Journal Help

Recorded Journal Help

(OP)
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.

RE: Recorded Journal Help

If the variable answer contains the value "atb12345", you will need to add the ".prt" file extension to open the part.

CODE

basePart1 = theSession.Parts.OpenBase("K:\Library\Standard_Parts\Eckhart_Parts_UG\ATB\" & answer & ".prt", partLoadStatus1) 

Rather than blindly adding ".prt" as the above code does, I suggest checking the answer variable for a file extension first and adding it only if needed.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
The whole file name goes like atb12345.xx.xxxx.prt where the other characters denote stuff like Revision and what not.

So my first thought was to add the extension, but it will not always be the same.

would I check the answer variable, by doing something like:

If answer = "atb12345"
Then answer = answer & "xx.xxxx.prt"
End If

RE: Recorded Journal Help

CODE

if answer.SubString(answer.Length - 4).ToLower = ".prt" then
    'input contains file extension
else
    'does not contain file extension
end if 

The code above checks to see if the last characters in the entry are ".prt". If the "other stuff" in the file name is important, you may want to add other checks for those as well.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
That worked perfectly, once I added the else condition of appending the .prt I ran it several times and I don't seem to be getting any errors anymore.

The "other stuff" is important, but how would I go about checking for it? And honestly I would rather the user not have to worry about it, and the journal check the folder for the latest part file.

quickly googling code to search a file name , because I have never tried doing it before, I found the GetFiles, am I going down the right path or do you know of a better way?

RE: Recorded Journal Help

I'd recommend using the version of GetFiles that allows you to specify a search option. This should return all the files similar to the user input.

If the journal is going to search for files matching user input, you can ignore my comment about validating the "other stuff"; as it shouldn't be needed in this case.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
Ok so trying to remember how to use/call a function. I want to add something like below?

CODE --> vb

...
Imports System.IO
...
Public Shared Function GetFiles (
	path As String,
	searchPattern As String
) As String()
...
answer = answer & GetFiles("directory path", answer & "*")
... 

RE: Recorded Journal Help

Something like the following:

CODE

Dim files() As String

If includeSubDirs Then
    files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.AllDirectories)
Else
    files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.TopDirectoryOnly)
End If 

Not shown in the code above, includeSubDirs is a declared as a boolean variable.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
I still need to have the function GetFiles though right?

and where you have directoryPath that should be the file path written out?

I feel like I am in way over my head here from a programming standpiont...

RE: Recorded Journal Help

The GetFiles method is part of the .net framework. You do not need to write the GetFiles method, it is provided for you "behind the scenes" in the .net framework. Your previous post shows the "Imports System.IO" call, so I wrote my code assuming the System.IO namespace had been imported. If you omit the "Imports System.IO" line, you should fully qualify the method call.

CODE

Dim files() As String

If includeSubDirs Then
    files = System.IO.Directory.GetFiles(directoryPath, "*.prt", SearchOption.AllDirectories)
Else
    files = System.IO.Directory.GetFiles(directoryPath, "*.prt", SearchOption.TopDirectoryOnly)
End If 

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
Ok that makes sense now. Thanks for helping me out again.

I am now getting some errors when I try to declare DirectoryPath as a string = network location.

The error I'm given is "Value of type 'String" cannot be converted to a '1-dimensional array of String'

RE: Recorded Journal Help

Can you post the exact code that you have tried?

www.nxjournaling.com

RE: Recorded Journal Help

I see the issue. The DirectoryPath variable is currently declared as a string array. If you change it to a single string variable, it should work.

Change this:

CODE

Dim DirectoryPath As String() = ("K:\Library\Standard_Parts\parts_UG\ATB\") 

to this:

CODE

Dim DirectoryPath As String = ("K:\Library\Standard_Parts\parts_UG\ATB\") 

Note the removal of the parentheses after the String type.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
Thanks Cowski!

although now it's giving me an erro about the file name. I presume I need to do modify the answer string with search results, but I don't know what the GetFiles returned. I am guessing its returning files as a string and then I need to change answer to the value of files? How would I do that, because one has the () type and the other does not.

RE: Recorded Journal Help

The GetFiles method will return a string array of file names. If it returns more than one match, you will need to look through the results and find the appropriate file or allow the user to choose from the returned results.

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
Thanks again for the help. I'll look into how to do this and hopefully figure it out.

RE: Recorded Journal Help

(OP)
It seems I am stuck again and get past this.

I am using the line

CODE -->

Dim partNumber() As String = Array.FindAll(files, Function(s) s.Contains(answer)) 

After the search if statement to find the match, however I am left with the error of converting an Array to a String. So I tried to match the types, but then I get an error at this line saying that it can't be an array

CODE -->

basePart1 = theSession.Parts.OpenBase("Network Path" & partNumber, partLoadStatus1) 

I suppose the best thing to do would bring up a message box with the match and have the user select OK to add?

Or am I out wandering in left field

RE: Recorded Journal Help

If the variable userInput represents the part number the user types, you can use that as part of the file search filter; something like the following:

CODE

files = System.IO.Directory.GetFiles(directoryPath, userInput & "*.prt", SearchOption.TopDirectoryOnly) 

This should return a string array of all the part files that start with the user input and contain any number of characters following the input pattern. There may be any number of files matching the user input. You can find out how many were returned with the .Length method.

CODE

dim numFiles as integer
numFiles = files.Length

if numFiles =0 then
    'inform user, take necessary steps for no matches found
end if

if numFiles = 1 then
    'one file found, it is item zero in the array (arrays are zero based)
    openFile(files(0))
else
    'more than one file found
    'present user with choices or take some other action
end if 

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
Awesome. The user will be inputting at a minimum a portion of the file name.

However what I couldn't get past over the weekend was that files is an array and NX then throws an error when I put files into the add part line of code.

The error being that the function is undefined for the type array

This of course is all dependent upon files only containing one element.

CODE -->

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

Maybe I missed it in all my searching but I couldn't find an easy way to extract a string element from an array...

RE: Recorded Journal Help

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:
https://msdn.microsoft.com/en-us/library/fyy7a5kt(...

www.nxjournaling.com

RE: Recorded Journal Help

(OP)
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?

RE: Recorded Journal Help

(OP)
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?

RE: Recorded Journal Help

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

RE: Recorded Journal Help

(OP)
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() 

RE: Recorded Journal Help

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: Import assembly from excel/text file 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources