Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

"NXOpen.NXException.Invalid file name" error message

Status
Not open for further replies.

NXProf

Mechanical
Nov 28, 2012
45
0
0
US
Hello,

I've completed VB code that properly creates html files from drawing sheets, in which it throws out an error message after creating the files and states the following:

"NXOpen.NXException.Invalid file name
at NXOpen.Builder.Commit()
at NXJournal.Main() in T:\Temp\NXJournals912\journal.vb:line193"

Lines 192 and 193 are as follows:
Code:
        Dim nXObject1 As NXObject
        nXObject1 = plotBuilder1.Commit()

Is there any way to identify the "NXJournals912" error message?

Since the output is exactly what I desire with no errors, is there a method to bypass this error message and continue with the .NET code? ...possibly having the program hit the OK button to the error message without user input.

Thank you ahead of time for your help.

Regards,
NX Prof
 
Replies continue below

Recommended for you

The PlotBuilder uses several files to work its magic; based on the error description, NXOpen.NXException.Invalid file name, I'd guess it doesn't like one or more of the file names used. If you can post the section of code relating to the plotBuilder, we might be able to pinpoint the problem.

All Builder objects have a [tt].Validate[/tt] method that will return either [tt]True[/tt] or [tt]False[/tt] and indicates if the builder has enough information to successfully complete. As a test, I'd try calling this method and logging the response before calling the [tt].Commit[/tt] method.

To help catch, analyze, and potentially bypass errors; you can use the [tt]Try Catch[/tt] block. It is similar to an [tt]If Else[/tt] construct, only it works with errors. It would look something like this:

Code:
'plot builder code

Try
    plotBuilder1.Commit
Catch ex as NXException
    'add code to inspect and handle error
    'we'll just show a message box
    msgbox(ex.errorcode & ": " & ex.message)
Finally
    'any cleanup code you need to run whether there is an error or not
    plotBuilder1.Destroy
End Try

For more info on Try Catch:

www.nxjournaling.com
 
Thank you cowski for your response.

I merged your code, which provided me with a journal message "1020005: Invalid file name".

The code (stripped down) I am currently using is as follows:

Code:
' NX 7.5.5.4
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Windows.Forms.MessageBox
Imports NXOpen
Imports NXOpen.UF
Imports System.Diagnostics
Imports NXOpenUI


Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

      Dim dwgs As Drawings.DrawingSheetCollection
      dwgs = workPart.DrawingSheets
      Dim sheet As Drawings.DrawingSheet

      Dim currentFile As String
      Dim fileAndRev As String

      Dim strRevision As String

      currentFile = workPart.GetStringAttribute("PARTNUMBER")
      strRevision = workPart.GetStringAttribute("DB_PART_REV")
      fileAndRev = currentFile & "/" & strRevision
      '____________________________________________________________________________________

      Dim theUISession As UI = UI.GetUI

      Dim sheetName As String
      Dim sheetNameString As String
      Dim sheetNameInteger As Integer
      Dim sheetNameIntegerMinusOne As Integer
      Dim totalNumberOfSheets As Integer = 0
      Dim lengthOfSheetName As Integer

      ' ----------------------------------------------
      '   Menu: File->Plot...
      ' ----------------------------------------------

      Dim plotBuilder1 As PlotBuilder
      plotBuilder1 = workPart.PlotManager.CreatePlotBuilder()
      plotBuilder1.Copies = 1
      plotBuilder1.Tolerance = 0.001
      plotBuilder1.RasterImages = True
      plotBuilder1.XDisplay = PlotBuilder.XdisplayOption.Right
      plotBuilder1.XOffset = 0.15
      plotBuilder1.CharacterSize = 0.06
      plotBuilder1.Rotation = PlotBuilder.RotationOption.Degree90

      plotBuilder1.JobName = fileAndRev

      '______________________________________________
      For Each sheet In workPart.DrawingSheets
         totalNumberOfSheets += 1  'the final totalNumberOfSheets count = the total number of sheets (including the INFO sheet)
      Next
      totalNumberOfSheets = totalNumberOfSheets - 1  'the final totalNumberOfSheets count = the total number of sheets (NOT including the INFO sheet)

      Dim sheets1() As NXObject
      Dim filenames1() As String

      For Each sheet In workPart.DrawingSheets
         sheetName = sheet.Name
         lengthOfSheetName = sheetName.Length

            If sheetName <> "INFO" Then
                If sheetName.Substring(0, 2) = "SH" Then 'test to see if 1st 2 characters = "SH" & bypass "INFO" sheet
                    If lengthOfSheetName = 3 Then
                        sheetNameString = sheetName.Substring(2, 1)
                    End If
                    If lengthOfSheetName = 4 Then
                        sheetNameString = sheetName.Substring(2, 2)
                    End If
                    If lengthOfSheetName > 3 Then
                        If sheetName.Substring(3, 1) = "_" Then
                            sheetNameString = sheetName.Substring(2, 1)
                        End If
                    End If
                    If lengthOfSheetName > 4 Then
                        If sheetName.Substring(4, 1) = "_" Then
                            sheetNameString = sheetName.Substring(2, 2)
                        End If
                    End If
                End If

                sheetNameInteger = CInt(sheetNameString)

                sheetNameIntegerMinusOne = sheetNameInteger - 1
                
                Dim drawingSheet1 As Drawings.DrawingSheet = CType(workPart.DrawingSheets.FindObject(sheetName), Drawings.DrawingSheet)

                ReDim Preserve sheets1(totalNumberOfSheets)
                sheets1(sheetNameIntegerMinusOne) = drawingSheet1

                ReDim Preserve filenames1(totalNumberOfSheets)
                filenames1(sheetNameIntegerMinusOne) = "T:\TEMP\DWG_" & currentFile & strRevision & sheetNameString & ".cgm"

            End If
        Next

      '______________________________________________
      plotBuilder1.SourceBuilder.SetSheets(sheets1)
      plotBuilder1.PlotterText = "HPGL2_TO_DISK"
      plotBuilder1.ProfileText = "compare_tiff_E-J_size"
      plotBuilder1.ColorsWidthsBuilder.Colors = PlotColorsWidthsBuilder.Color.BlackOnWhite
      plotBuilder1.ColorsWidthsBuilder.Widths = PlotColorsWidthsBuilder.Width.StandardWidths
      plotBuilder1.PrinterGroupText = "Plot_Files"

      plotBuilder1.SetFilenames(filenames1)

        '_________________________________________________________________________
        'plot builder code
        Try
            plotBuilder1.Commit()
        Catch ex As NXException
            'add code to inspect and handle error
            'we'll just show a message box
            msgbox(ex.errorcode & ": " & ex.message)
        Finally
            'any cleanup code you need to run whether there is an error or not
            plotBuilder1.Destroy()
        End Try
        '_________________________________________________________________________

        Dim nXObject1 As NXObject
        nXObject1 = plotBuilder1.Commit()

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

   End Sub

End Module

Thank you again for your help.

Regards,
NX Prof
 
Thank you cowski.

But what I am considering is that there will always exist one more sheet in each file. Please see the example below, where the actual number of sheets = 5 and the first sheet “INFO” is to be ignored for the final output:

totalNumberOfSheets = 4
The sheet names are: “INFO” (to be overlooked), “SH1_B”, “SH2_A”, “SH3”, and “SH4_B”
[ul]
[li]sheets1(0) = 1[/li]
[li]sheets1(1) = 2[/li]
[li]sheets1(2) = 3[/li]
[li]sheets1(3) = 4[/li]
[/ul]
And when the currentFile = “0123456” and strRevision = “B”,
[ul]
[li]filenames1(0) = "T:\TEMP\DWG_0123456BSH1_B.cgm"[/li]
[li]filenames1(1) = "T:\TEMP\DWG_0123456BSH2_B.cgm"[/li]
[li]filenames1(2) = "T:\TEMP\DWG_0123456BSH3_B.cgm"[/li]
[li]filenames1(3) = "T:\TEMP\DWG_0123456BSH4_B.cgm"[/li]
[/ul]

Later, I will add code that uses a batch routine to merge all of the files, while converting them to a single Multi_page.tif file where the final name would be 0123456_B.tif from the above example. So the name is not terribly important. But wouldn’t the sheets1 and filenames1 have an "off by one" condition to take care of the “INFO” sheet that is to be disregarded?

Thank you greatly for your help.

Regards,
NX Prof
 
First you loop through the sheets incrementing the totalNumberOfSheets variable. In your example above totalNumberOfSheets now has the value of 5. Next you subtract one to account for the INFO sheet
totalNumberOfSheets = 4
Later, you redim your array (which doesn't need to be in the loop , btw):
ReDim Preserve filenames1(totalNumberOfSheets {value = 4})

This gives you
filenames1(0)
filenames1(1)
filenames1(2)
filenames1(3)
filenames1(4)
Your array can now hold 5 sheet objects, but you are only interested in 4. When counting the sheets, you start with one (as normal humans do), but the Computer Science types like to start counting with zero (arrays are zero based); therefore you end up with one more element in your array than you need. The journal then tries to use one of the filenames twice, which leads to the "invalid file name" error.

MSDN help said:
Array Bounds. Each entry in boundlist can specify the lower and upper bounds of that dimension. The lower bound is always 0 (zero). The upper bound is the highest possible index value for that dimension, not the length of the dimension (which is the upper bound plus one). The index for each dimension can vary from 0 through its upper bound value.
(emphasis mine)

www.nxjournaling.com
 
Rather than mucking about with arrays, I strongly suggest using a list object (System.Collections.Generic.List). You can use its .Add method to add an object to the list, it resizes itself as needed. It can be used in a For Each construct and you can use the .ToArray method to convert it to an array when needed (such as the plotBuilder1.SourceBuilder.SetSheets method, which only accepts an array). In short, the list object will take care of the details behind the scenes and make your life easier.

I'll try to post an example tomorrow, time permitting.

www.nxjournaling.com
 
Here's some modified code:

Code:
' NX 7.5.5.4
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Windows.Forms.MessageBox
Imports NXOpen
Imports NXOpen.UF
Imports System.Diagnostics
Imports NXOpenUI


Module plot_drawings
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work

        Dim displayPart As Part = theSession.Parts.Display

        'Dim dwgs As Drawings.DrawingSheetCollection
        'dwgs = workPart.DrawingSheets
        'Dim sheet As Drawings.DrawingSheet

        Dim currentFile As String
        Dim fileAndRev As String

        Dim strRevision As String

        currentFile = workPart.GetStringAttribute("PARTNUMBER")
        strRevision = workPart.GetStringAttribute("DB_PART_REV")
        fileAndRev = currentFile & "/" & strRevision
        '____________________________________________________________________________________

        Dim theUISession As UI = UI.GetUI

        Dim sheetName As String
        Dim sheetNameString As String
        Dim sheetNameInteger As Integer
        Dim sheetNameIntegerMinusOne As Integer
        Dim totalNumberOfSheets As Integer = 0
        Dim lengthOfSheetName As Integer

        ' ----------------------------------------------
        '   Menu: File->Plot...
        ' ----------------------------------------------

        Dim plotBuilder1 As PlotBuilder
        plotBuilder1 = workPart.PlotManager.CreatePlotBuilder()
        plotBuilder1.Copies = 1
        plotBuilder1.Tolerance = 0.001
        plotBuilder1.RasterImages = True
        plotBuilder1.XDisplay = PlotBuilder.XdisplayOption.Right
        plotBuilder1.XOffset = 0.15
        plotBuilder1.CharacterSize = 0.06
        plotBuilder1.Rotation = PlotBuilder.RotationOption.Degree90

        plotBuilder1.JobName = fileAndRev

        '______________________________________________
        'For Each sheet In workPart.DrawingSheets
        '    totalNumberOfSheets += 1  'the final totalNumberOfSheets count = the total number of sheets (including the INFO sheet)
        'Next
        'totalNumberOfSheets = totalNumberOfSheets - 1  'the final totalNumberOfSheets count = the total number of sheets (NOT including the INFO sheet)
        ''msgbox("total number of sheets: " & totalNumberOfSheets)
        'Dim sheets1() As NXObject
        'Dim filenames1() As String

        Dim mySheets As New List(Of NXObject)
        Dim fileNames As New List(Of String)

        For Each sheet As Drawings.DrawingSheet In workPart.DrawingSheets
            sheetName = sheet.Name
            lengthOfSheetName = sheetName.Length

            If sheetName.ToLower = "info" Then
                'skip to next sheet
                Continue For
            End If

            If sheetName.Substring(0, 2) = "SH" Then 'test to see if 1st 2 characters = "SH" & bypass "INFO" sheet
                If lengthOfSheetName = 3 Then
                    sheetNameString = sheetName.Substring(2, 1)
                End If
                If lengthOfSheetName = 4 Then
                    sheetNameString = sheetName.Substring(2, 2)
                End If
                If lengthOfSheetName > 3 Then
                    If sheetName.Substring(3, 1) = "_" Then
                        sheetNameString = sheetName.Substring(2, 1)
                    End If
                End If
                If lengthOfSheetName > 4 Then
                    If sheetName.Substring(4, 1) = "_" Then
                        sheetNameString = sheetName.Substring(2, 2)
                    End If
                End If
            End If
            sheetNameInteger = CInt(sheetNameString)

            sheetNameIntegerMinusOne = sheetNameInteger - 1

            'Dim drawingSheet1 As Drawings.DrawingSheet = CType(workPart.DrawingSheets.FindObject(sheetName), Drawings.DrawingSheet)

            'ReDim Preserve sheets1(totalNumberOfSheets)
            'sheets1(sheetNameIntegerMinusOne) = drawingSheet1

            'ReDim Preserve filenames1(totalNumberOfSheets)
            'filenames1(sheetNameIntegerMinusOne) = "T:\TEMP\DWG_" & currentFile & strRevision & sheetNameString & ".cgm"

            mySheets.Add(sheet)
            fileNames.Add("T:\TEMP\DWG_" & currentFile & strRevision & sheetNameString & ".cgm")

        Next

        '______________________________________________
        plotBuilder1.SourceBuilder.SetSheets(mySheets.ToArray)
        plotBuilder1.PlotterText = "HPGL2_TO_DISK"
        plotBuilder1.ProfileText = "compare_tiff_E-J_size"
        plotBuilder1.ColorsWidthsBuilder.Colors = PlotColorsWidthsBuilder.Color.BlackOnWhite
        plotBuilder1.ColorsWidthsBuilder.Widths = PlotColorsWidthsBuilder.Width.StandardWidths
        plotBuilder1.PrinterGroupText = "Plot_Files"

        plotBuilder1.SetFilenames(fileNames.ToArray)

        '_________________________________________________________________________
        'plot builder code
        Try
            plotBuilder1.Commit()
        Catch ex As NXException
            'add code to inspect and handle error
            'we'll just show a message box
            msgbox(ex.errorcode & ": " & ex.message)
        Finally
            'any cleanup code you need to run whether there is an error or not
            plotBuilder1.Destroy()
        End Try
        '_________________________________________________________________________

    End Sub

End Module

www.nxjournaling.com
 
Thank you cowski for your tremendous help, sound advice, and educating me to lists as well as the Microsoft link on ReDim Statements.

Tomorrow, I'll let you know how it works out.

Thank you again!

Regards,
NX Prof
 
cowski, your revised code worked like a charm!

Thank you very much for your incredible guidance, code, and suggestions which I cannot thank you enough! I'll definitely have to purchase another .NET book since the one I have (even though it is good) lacks many of the moderate to advance techniques of VB coding.

Best regards,
NX Prof
 
Glad that I could help.

I have a few book recommendations here; but the page is a few years old so the books reference VB 2010. Perhaps the authors have put out new versions...

If you find a book that is helpful, please let me know and I will add it to the recommendation list.

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