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

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 [highlight #FCE94F]& ".prt"[/highlight], 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
 
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

 
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
 
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?

 
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
 
Ok so trying to remember how to use/call a function. I want to add something like below?

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

 
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
 
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...
 
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
 
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'
 
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
 
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.
 
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
 
Thanks again for the help. I'll look into how to do this and hopefully figure it out.
 
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
 
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
 
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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor