Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

How to add error handling to partSaveStatus1 = workPart.SaveAs("C:\some.prt")

Status
Not open for further replies.

CADMANSOUTH

Mechanical
Aug 2, 2006
43
Hi
I want to add a save as to the end of an NX OPEN vb.net .dll
How to I add error handling to this journal? Also how would I add a user interactive save as if I get an error? I want to have a defult value of "file1" in the user interactive SaveAs.

I am using NX6 in native.
'++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: File->Save As...
' ----------------------------------------------
Dim partSaveStatus1 As PartSaveStatus
Dim file1 as string = "C:\some.prt"
partSaveStatus1 = workPart.SaveAs(file1)

partSaveStatus1.Dispose()
' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

 
Replies continue below

Recommended for you


you could use:

Code:
If partSaveStatus1.NumberUnsavedParts > 0 Then

'add error catchement here

Else

'carry on with your code

End If
or
Code:
        Try
            partSaveStatus1 = workPart.SaveAs(file1)
 
        Catch exc As NXException
            'add error catchement here, you can use exc.message to get the reason for faliure

        End Try

If you are feeling brave and don't want to (or can't) compile you can add a second module to your code and invoke a form in your journal.
This is a cut down version of a very old journal I made when I was learning how to write nxopen scripts (forgive the 'goto' statements, I'm sure with a little thought you can replace these with some until statements)


Code:
Option Strict Off
Imports System
Imports NXOpen
Imports System.Windows.Forms

Module NXJournal

    Public inputdesc As String
    Public input As String
    Public form_Desc As String
    Public button_result As Integer = -1
    Public msg As String 'Main Message string decleration to share with form and main module
    Sub Main()

        

        

            ' ----------------------------------------------
            '   User input of Description
            ' ----------------------------------------------
            Dim desc As String

desc_input:
            Dim Input_Box As Form1
            inputdesc = "Label Text"
            form_Desc = "Form Text"
            Input_Box = New Form1

            Input_Box.ShowDialog()

            If button_result = 0 Then
                
                Input_Box.Dispose()

                If input = "" Then


                    MsgBox("User Must Enter a Description")

                    'msgbox("User Must Enter a Description")
                    GoTo desc_input
                End If
                

            ElseIf button_result = 2 Then
                Input_Box.Dispose()
                'call dispose_new(filename,saveDialog.FileName)
                GoTo end_sub
            ElseIf button_result = -1 Then
                Input_Box.Dispose()
                GoTo end_sub
            End If
        End If




end_sub:
    End Sub

    
   
End Module
Public Structure DimensionData

    ''''''''''''
    ' Fields used in the form UI and the IShapeCreator
    ''''''''''''

    ' name for the dimension
    Dim name As String

    ''''''''''''
    ' Fields used only in the form UI
    ''''''''''''

End Structure

Module Input_Box

    ' Form that asks the user to select a shape
    Public Class Form1
        Inherits System.Windows.Forms.Form

        ''''''''''
        '      PUBLIC METHODS
        ''''''''''


#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()


            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub


        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer



        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents m_OKButton As System.Windows.Forms.Button

        Friend WithEvents m_cancelButton As System.Windows.Forms.Button
        Friend WithEvents m_inputlabel As System.Windows.Forms.label
        Friend WithEvents m_inputTextBox As System.Windows.Forms.TextBox





        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))





            Me.m_OKButton = New System.Windows.Forms.Button
            Me.m_CancelButton = New System.Windows.Forms.Button
            m_inputlabel = New System.Windows.Forms.label
            m_inputTextBox = New System.Windows.Forms.TextBox

            Me.SuspendLayout()


            '
            'm_OKButton
            '
            Me.m_OKButton.Location = New System.Drawing.Point(122, 105)
            Me.m_OKButton.Name = "m_OKButton"
            Me.m_OKButton.TabIndex = 2
            Me.m_OKButton.Text = "OK"

            '
            'm_CancelButton
            '
            Me.m_CancelButton.Location = New System.Drawing.Point(208, 105)
            Me.m_CancelButton.Name = "m_CancelButton"
            Me.m_CancelButton.TabIndex = 4
            Me.m_CancelButton.Text = "Cancel"

            '
            'm_inputTextBox
            '
            Me.m_inputTextBox.Location = New System.Drawing.Point(32, 50)
            Me.m_inputTextBox.Name = "m_inputTextBox"
            Me.m_inputTextBox.Size = New System.Drawing.Size(250, 60)
            Me.m_inputTextBox.TabIndex = 1
            Me.m_inputTextBox.Text = "file1"    '<-------------- The initial filename displayed
            '
            'm_inputLabel
            '
            Me.m_inputlabel.Location = New System.Drawing.Point(32, 30)
            Me.m_inputlabel.Name = "m_inputlabel"
            Me.m_inputlabel.Size = New System.Drawing.Size(120, 16)
            Me.m_inputlabel.TabIndex = 5
            Me.m_inputlabel.Text = inputdesc
            'Me.m_inputlabel.font.bold = true
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(320, 140)
            Me.Controls.Add(Me.m_OKButton)

            Me.Controls.Add(Me.m_CancelButton)
            Me.Controls.Add(Me.m_inputTextBox)
            Me.Controls.Add(Me.m_inputlabel)
            Me.Name = "Form1"
            Me.Text = form_desc
            Me.ResumeLayout(False)
            nxopenui.formutilities.setapplicationicon(Me)
        End Sub

#End Region

        ''''''''''
        '      PRIVATE METHODS ADDED BY HAND
        ''''''''''



        Public Sub m_OKButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_OKButton.Click

            'msgbox(Me.m_inputTextBox.Text)

            input = Me.m_inputTextBox.text
            button_result = 0
            MyBase.dispose()

        End Sub


        Private Sub m_CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_CancelButton.Click
            button_result = 2
            MyBase.dispose()

        End Sub
        Private Sub txt_keypress(ByVal system As System.Object, ByVal e As KeyPressEventArgs) Handles m_inputTextBox.KeyPress

            If e.KeyChar = Chr(Keys.Enter) Then
                Call m_OKButton_Click("enter", e)
            End If
        End Sub
        ''''''''''
        '      PRIVATE DATA
        ''''''''''





    End Class





End Module

Mark Benson
Aerodynamic Model Designer

To a Designer, the glass was right on CAD.
 
Hi MSPBenson,
Thank you for your help. I will try to use the
Code:
        Try
            partSaveStatus1 = workPart.SaveAs(file1)
 
        Catch exc As NXException
            'add error catchement here, you can use exc.message to get the reason for faliure

        End Try

That looks like what I need.

Your post with the windows forms is also very interesting. I am using an inputbox for each variable. I think it is much easier for the used to enter everything in one from. I have not gotten windows forms to work.

I can compile .dll. I am used sharp develop. I have not found good instructions on how to add Windows forms to NXOpen in sharpdevelop (# develop). I will start with your method.
Much Thanks

 
Here's a quick example of how to mimic the NX save-as dialog behavior:

Code:
Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen

Module Module1

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

    Sub Main()

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        SaveAs()

    End Sub


    Sub SaveAs()

        theUISession.LockAccess()

        Dim saveFileDialog1 As New SaveFileDialog()
        Dim messages(1) As String
        messages(0) = "This file already exists."
        messages(1) = "Enter a different name."
        Dim partSaveStatus1 As PartSaveStatus

        Dim directoryInfo As System.IO.DirectoryInfo
        directoryInfo = System.IO.Directory.GetParent(workPart.FullPath)

        With saveFileDialog1
            .Title = "Save As"
            .InitialDirectory = directoryInfo.FullName
            .Filter = "Part files (*.prt)|*.prt"
            .FilterIndex = 1
            .RestoreDirectory = True
            .AddExtension = True
            .DefaultExt = "prt"
            .OverwritePrompt = False
        End With

        Do
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                If Not IO.File.Exists(saveFileDialog1.FileName) Then
                    'save file

                    Try

                        partSaveStatus1 = workPart.SaveAs(saveFileDialog1.FileName)

                    Catch ex As Exception
                        MessageBox.Show("Save-As Error: " & ex.Message)
                    Finally
                        partSaveStatus1.Dispose()
                    End Try


                    Exit Do
                Else
                    'ok was pressed, but file already exists
                    theUISession.NXMessageBox.Show("Save As", NXMessageBox.DialogType.Error, messages)
                End If

            Else
                'cancel was pressed
                Exit Do
            End If

        Loop

        theUISession.UnlockAccess()

    End Sub

End Module

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

Part and Inventory Search

Sponsor