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!

Journal to edit multiple attributes using Windows Forms

Status
Not open for further replies.

Techomick

Mechanical
Jun 21, 2011
46
0
0
US
Here is a journal to edit a few attributes using a windows form. I created this because the new attribute editor is cumbersome.


Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Private _formTitle As String = ""
    Public ReadOnly Property FormTitle() As String
        Get
            Return _formTitle
        End Get
    End Property

    Private _attributeTitle As String = ""
    Public ReadOnly Property AttributeTitle() As String
        Get
            Return _attributeTitle
        End Get
    End Property

    Private _attributeName As String = ""
    Public Property AttributeName() As String
        Get
            Return _attributeName
        End Get
        Set(ByVal value As String)
            _attributeName = value
        End Set
    End Property

    Private _attributePartNo As String = ""
    Public Property AttributePartNo() As String
        Get
            Return _attributePartNo
        End Get
        Set(ByVal value As String)
            _attributePartNo = value
        End Set
    End Property

    Private _attributeShape As String = ""
    Public Property AttributeShape() As String
        Get
            Return _attributeShape
        End Get
        Set(ByVal value As String)
            _attributeShape = value
        End Set
    End Property

    Private _attributeWeight As String = ""
    Public Property AttributeWeight() As String
        Get
            Return _attributeWeight
        End Get
        Set(ByVal value As String)
            _attributeWeight = value
        End Set
    End Property

    Private _attributeProc As String = ""
    Public Property AttributeProc() As String
        Get
            Return _attributeProc
        End Get
        Set(ByVal value As String)
            _attributeProc = value
        End Set
    End Property


    Sub Main()
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim lg As LogFile = theSession.LogFile

        Const undoMarkName As String = "NXJ form journal"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim myAttributeInfo As NXObject.AttributeInformation
        _attributeTitle = "PARTNUMBER"
        Try
            myAttributeInfo = workPart.GetUserAttribute(_attributeTitle, NXObject.AttributeType.String, -1)
            _attributePartNo = myAttributeInfo.StringValue

        Catch ex As NXException
            If ex.ErrorCode = 512008 Then
                'attribute not found
            Else
                'unexpected error: show error message, undo, and exit journal
                MsgBox(ex.ErrorCode & ": " & ex.Message)
                theSession.UndoToMark(markId1, undoMarkName)
                Return
            End If

        Finally

        End Try

        _attributeTitle = "NAME"
        Try
            myAttributeInfo = workPart.GetUserAttribute(_attributeTitle, NXObject.AttributeType.String, -1)
            _attributeName = myAttributeInfo.StringValue

        Catch ex As NXException
            If ex.ErrorCode = 512008 Then
                'attribute not found
            Else
                'unexpected error: show error message, undo, and exit journal
                MsgBox(ex.ErrorCode & ": " & ex.Message)
                theSession.UndoToMark(markId1, undoMarkName)
                Return
            End If

        Finally

        End Try

        _attributeTitle = "WEIGHT"
        Try
            myAttributeInfo = workPart.GetUserAttribute(_attributeTitle, NXObject.AttributeType.Real, -1)
            _attributeWeight = myAttributeInfo.StringValue

        Catch ex As NXException
            If ex.ErrorCode = 512008 Then
                'attribute not found
            Else
                'unexpected error: show error message, undo, and exit journal
                MsgBox(ex.ErrorCode & ": " & ex.Message)
                theSession.UndoToMark(markId1, undoMarkName)
                Return
            End If

        Finally

        End Try

        _attributeTitle = "SHAPE"
        Try
            myAttributeInfo = workPart.GetUserAttribute(_attributeTitle, NXObject.AttributeType.String, -1)
            _attributeShape = myAttributeInfo.StringValue

        Catch ex As NXException
            If ex.ErrorCode = 512008 Then
                'attribute not found
            Else
                'unexpected error: show error message, undo, and exit journal
                MsgBox(ex.ErrorCode & ": " & ex.Message)
                theSession.UndoToMark(markId1, undoMarkName)
                Return
            End If

        Finally

        End Try

        _attributeTitle = "PROCUREMENTCODE"
        Try
            myAttributeInfo = workPart.GetUserAttribute(_attributeTitle, NXObject.AttributeType.String, -1)
            _attributeProc = myAttributeInfo.StringValue

        Catch ex As NXException
            If ex.ErrorCode = 512008 Then
                'attribute not found
            Else
                'unexpected error: show error message, undo, and exit journal
                MsgBox(ex.ErrorCode & ": " & ex.Message)
                theSession.UndoToMark(markId1, undoMarkName)
                Return
            End If

        Finally

        End Try

        _formTitle = _attributePartNo & " - " & _attributeName & " - Attributes"
        Dim myForm As New Form1
        'set form object properties (current part attribute title and value)
        myForm.AttributePartNo = _attributePartNo
        myForm.AttributeName = _attributeName
        myForm.AttributeWeight = _attributeWeight
        myForm.AttributeShape = _attributeShape
        myForm.AttributeProc = _attributeProc
        myForm.FormTitle = _formTitle
        'display our form
        ufs.Abort.DisableAbort()
        myForm.ShowDialog()

        If myForm.Canceled Then
            ufs.Abort.EnableAbort()
            'user pressed cancel, exit journal
            Return
        Else
            'user pressed OK, assign value from form to part attribute
            ufs.Abort.EnableAbort()
            Try
                workPart.SetUserAttribute("PARTNUMBER", -1, myForm.AttributePartNo, Update.Option.Later)
            Catch ex As Exception
                lg.WriteLine(ex.ToString)
            End Try
            Try
                workPart.SetUserAttribute("NAME", -1, myForm.AttributeName, Update.Option.Later)
            Catch ex As Exception
                lg.WriteLine(ex.ToString)
            End Try
            Try
                workPart.SetUserAttribute("SHAPE", -1, myForm.AttributeShape, Update.Option.Later)
            Catch ex As Exception
                lg.WriteLine(ex.ToString)
            End Try
            Try
                workPart.SetUserAttribute("PROCUREMENTCODE", -1, myForm.AttributeProc, Update.Option.Later)
            Catch ex As Exception
                lg.WriteLine(ex.ToString)
            End Try
            Try
                workPart.SetUserAttribute("WEIGHT", -1, CType(myForm.AttributeWeight, Double), Update.Option.Later)
            Catch ex As Exception
                lg.WriteLine(ex.ToString)
            End Try
        End If

    End Sub


End Module

Public Class Form1
    Private _frmTitle As String
    Public Property FormTitle() As String
        Get
            Return _frmTitle
        End Get
        Set(ByVal value As String)
            _frmTitle = value
        End Set
    End Property

    Private _frmAttributeTitle As String
    Public Property AttributeTitle() As String
        Get
            Return _frmAttributeTitle
        End Get
        Set(ByVal value As String)
            _frmAttributeTitle = value
        End Set
    End Property

    Private _frmAttributeName As String
    Public Property AttributeName() As String
        Get
            Return _frmAttributeName
        End Get
        Set(ByVal value As String)
            _frmAttributeName = value
        End Set
    End Property

    Private _frmAttributePartNo As String
    Public Property AttributePartNo() As String
        Get
            Return _frmAttributePartNo
        End Get
        Set(ByVal value As String)
            _frmAttributePartNo = value
        End Set
    End Property

    Private _frmAttributeProc As String
    Public Property AttributeProc() As String
        Get
            Return _frmAttributeProc
        End Get
        Set(ByVal value As String)
            _frmAttributeProc = value
        End Set
    End Property

    Private _frmAttributeShape As String
    Public Property AttributeShape() As String
        Get
            Return _frmAttributeShape
        End Get
        Set(ByVal value As String)
            _frmAttributeShape = value
        End Set
    End Property

    Private _frmAttributeWeight As String
    Public Property AttributeWeight() As String
        Get
            Return _frmAttributeWeight
        End Get
        Set(ByVal value As String)
            _frmAttributeWeight = value
        End Set
    End Property



    Private _canceled As Boolean = False
    Public ReadOnly Property Canceled() As Boolean
        Get
            Return _canceled
        End Get
    End Property
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = _frmTitle
        TextBox_Name.Text = _frmAttributeName
        TextBox_PartNo.Text = _frmAttributePartNo
        TextBox_Proc.Text = _frmAttributeProc
        TextBox_Shape.Text = _frmAttributeShape
        TextBox_Weight.Text = _frmAttributeWeight
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _canceled = True
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        _frmAttributeName = TextBox_Name.Text.ToUpper
        _frmAttributePartNo = TextBox_PartNo.Text.ToUpper
        _frmAttributeProc = TextBox_Proc.Text.ToUpper
        _frmAttributeShape = TextBox_Shape.Text.ToUpper
        _frmAttributeWeight = TextBox_Weight.Text.ToUpper
        Me.Close()
    End Sub

End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.TextBox_Name = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.TextBox_PartNo = New System.Windows.Forms.TextBox()
        Me.TextBox_Shape = New System.Windows.Forms.TextBox()
        Me.TextBox_Weight = New System.Windows.Forms.TextBox()
        Me.TextBox_Proc = New System.Windows.Forms.TextBox()
        Me.SHAPE_CALC = New System.Windows.Forms.Button()
        Me.WEIGHT_CALC = New System.Windows.Forms.Button()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(11, 12)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(38, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "NAME"
        Me.Label1.UseMnemonic = False
        '
        'btnOK
        '
        Me.btnOK.Location = New System.Drawing.Point(82, 174)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 6
        Me.btnOK.Text = "OK"
        Me.btnOK.UseVisualStyleBackColor = True
        '
        'TextBox_Name
        '
        Me.TextBox_Name.Location = New System.Drawing.Point(82, 12)
        Me.TextBox_Name.Name = "TextBox_Name"
        Me.TextBox_Name.Size = New System.Drawing.Size(149, 20)
        Me.TextBox_Name.TabIndex = 1
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(12, 38)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(58, 13)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "PART NO."
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(12, 64)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(43, 13)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "SHAPE"
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(12, 89)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(51, 13)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "WEIGHT"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(12, 115)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(37, 13)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "PROC"
        '
        'TextBox_PartNo
        '
        Me.TextBox_PartNo.Location = New System.Drawing.Point(82, 38)
        Me.TextBox_PartNo.Name = "TextBox_PartNo"
        Me.TextBox_PartNo.Size = New System.Drawing.Size(149, 20)
        Me.TextBox_PartNo.TabIndex = 2
        '
        'TextBox_Shape
        '
        Me.TextBox_Shape.Location = New System.Drawing.Point(82, 64)
        Me.TextBox_Shape.Name = "TextBox_Shape"
        Me.TextBox_Shape.Size = New System.Drawing.Size(149, 20)
        Me.TextBox_Shape.TabIndex = 3
        '
        'TextBox_Weight
        '
        Me.TextBox_Weight.Location = New System.Drawing.Point(82, 89)
        Me.TextBox_Weight.Name = "TextBox_Weight"
        Me.TextBox_Weight.Size = New System.Drawing.Size(149, 20)
        Me.TextBox_Weight.TabIndex = 4
        '
        'TextBox_Proc
        '
        Me.TextBox_Proc.Location = New System.Drawing.Point(82, 115)
        Me.TextBox_Proc.Name = "TextBox_Proc"
        Me.TextBox_Proc.Size = New System.Drawing.Size(149, 20)
        Me.TextBox_Proc.TabIndex = 5
        '
        'SHAPE_CALC
        '
        Me.SHAPE_CALC.Location = New System.Drawing.Point(247, 64)
        Me.SHAPE_CALC.Name = "SHAPE_CALC"
        Me.SHAPE_CALC.Size = New System.Drawing.Size(75, 23)
        Me.SHAPE_CALC.TabIndex = 0
        Me.SHAPE_CALC.Text = "Calculate"
        Me.SHAPE_CALC.UseVisualStyleBackColor = True
        Me.SHAPE_CALC.Visible = False
        '
        'WEIGHT_CALC
        '
        Me.WEIGHT_CALC.Location = New System.Drawing.Point(247, 89)
        Me.WEIGHT_CALC.Name = "WEIGHT_CALC"
        Me.WEIGHT_CALC.Size = New System.Drawing.Size(75, 23)
        Me.WEIGHT_CALC.TabIndex = 0
        Me.WEIGHT_CALC.Text = "Calculate"
        Me.WEIGHT_CALC.UseVisualStyleBackColor = True
        Me.WEIGHT_CALC.Visible = False
        '
        'btnCancel
        '
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Location = New System.Drawing.Point(177, 174)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(75, 23)
        Me.btnCancel.TabIndex = 7
        Me.btnCancel.Text = "Cancel"
        Me.btnCancel.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AcceptButton = Me.btnOK
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CancelButton = Me.btnCancel
        Me.ClientSize = New System.Drawing.Size(337, 205)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.WEIGHT_CALC)
        Me.Controls.Add(Me.SHAPE_CALC)
        Me.Controls.Add(Me.TextBox_Proc)
        Me.Controls.Add(Me.TextBox_Weight)
        Me.Controls.Add(Me.TextBox_Shape)
        Me.Controls.Add(Me.TextBox_PartNo)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.TextBox_Name)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.Label1)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
        Me.Text = "Menu"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents TextBox_Name As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents TextBox_PartNo As System.Windows.Forms.TextBox
    Friend WithEvents TextBox_Shape As System.Windows.Forms.TextBox
    Friend WithEvents TextBox_Weight As System.Windows.Forms.TextBox
    Friend WithEvents TextBox_Proc As System.Windows.Forms.TextBox
    Friend WithEvents SHAPE_CALC As System.Windows.Forms.Button
    Friend WithEvents WEIGHT_CALC As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button

End Class

Design Engineer, NX 8.5
 
Status
Not open for further replies.
Back
Top