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.
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
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
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"
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 ifThe 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
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
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
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
CODE
Dim files() As String If includeSubDirs Then files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.AllDirectories) Else files = Directory.GetFiles(directoryPath, "*.prt", SearchOption.TopDirectoryOnly) End IfNot shown in the code above, includeSubDirs is a declared as a boolean variable.
www.nxjournaling.com
RE: Recorded Journal Help
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
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 Ifwww.nxjournaling.com
RE: Recorded Journal Help
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
www.nxjournaling.com
RE: Recorded Journal Help
RE: Recorded Journal Help
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
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
www.nxjournaling.com
RE: Recorded Journal Help
RE: Recorded Journal Help
I am using the line
CODE -->
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
CODE
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 ifwww.nxjournaling.com
RE: Recorded Journal Help
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 -->
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
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
reference:
https://msdn.microsoft.com/en-us/library/fyy7a5kt(...
www.nxjournaling.com
RE: Recorded Journal Help
Once I figured out that I had files returning the full directory path and the part name...
So this:
CODE -->
Became this:
CODE -->
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 -->
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
https://msdn.microsoft.com/en-us/library/hxthx5h6(...
www.nxjournaling.com
RE: Recorded Journal Help
I have managed to get it to work the way I wanted to though:
CODE -->
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
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
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 -->
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 -->
RE: Recorded Journal Help
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 TryBelow 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 Functionwww.nxjournaling.com