Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Window Forms in NXOpen 2

Status
Not open for further replies.

Zoes

Mechanical
Sep 30, 2011
46
GB
Hi all,

I have written the code bellow but while trying to execute it gives me an error message: "Error in the external library. See syslog for details ... Function name ufusr".
The form is supposed to load the existing expressions of breadth and thickness and assign them to two textboxes expression0 and experssion01.

Does anybody knows how to fix this?

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Windows.Forms

Public Class Form1
Private Shared theSession As Session
Private Shared theUI As UI
Public nErrs1 As Integer
Public id1 As Session.UndoMarkId
Public workPart As Part = theSession.Parts.Work
Public displayPart As Part = theSession.Parts.Display
Dim nxopenSession As NXOpen.UF.UFSession

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()
nxopenSession = NXOpen.UF.UFSession.GetUFSession()

'MeesageWindow
Dim MessageBoxTitle As String = "Track Expression"

'Find Expressions
Dim breadthExp As Expression = CType(theSession.Parts.Work.Expressions.FindObject("Breadth"), Expression)
Dim thicknessExp As Expression = CType(theSession.Parts.Work.Expressions.FindObject("thickness"), Expression)

'Convert Expressions' Values to Strings
Dim breadth As String = breadthExp.Value.ToString
Dim thickness As String = thicknessExp.Value.ToString

If breadthExp IsNot Nothing Then

expression0.Text = breadth
expression01.Text = thickness

If (breadth = 25 And thickness = 6) Then
theUI.NXMessageBox.Show(MessageBoxTitle, NXMessageBox.DialogType.Information, "yeah")

End If

'Force Update and redraw part
nErrs1 = theSession.UpdateManager.DoUpdate(id1)

End If
End Sub

Private Sub Apply_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply_btn.Click

'Force Update and redraw part
nErrs1 = theSession.UpdateManager.DoUpdate(id1)

End Sub
End Class
 
Replies continue below

Recommended for you

Here's some example code (be aware it does no error checking) for one way to do it. Copy and paste it into a text file, change the extension to .vb and run it as a journal. It looks for expressions named "Breadth" and "Thickness".

Code:
[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]
[COLOR=green]'   start of module code[/color]

[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Public[/color] breadthValue [COLOR=blue]As[/color] [COLOR=blue]Double[/color] [COLOR=blue]=[/color] 0  
    [COLOR=blue]Public[/color] thicknessValue [COLOR=blue]As[/color] [COLOR=blue]Double[/color] [COLOR=blue]=[/color] 0  

    [COLOR=blue]Sub[/color] Main()  

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
        [COLOR=blue]Dim[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  
        lw.Open()  

        [COLOR=blue]Dim[/color] markId1 [COLOR=blue]As[/color] Session.UndoMarkId  
        markId1 [COLOR=blue]=[/color] theSession.SetUndoMark(Session.MarkVisibility.Visible, "journal")  

        [COLOR=blue]Dim[/color] myExpressions() [COLOR=blue]As[/color] Expression [COLOR=blue]=[/color] workPart.Expressions.ToArray  

        [COLOR=blue]Dim[/color] breadthExp [COLOR=blue]As[/color] Expression [COLOR=blue]=[/color] [COLOR=blue]Nothing[/color]  
        [COLOR=blue]Dim[/color] thicknessExp [COLOR=blue]As[/color] Expression [COLOR=blue]=[/color] [COLOR=blue]Nothing[/color]  

        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] myExp [COLOR=blue]As[/color] Expression [COLOR=blue]In[/color] myExpressions  
 [COLOR=green]'lw.WriteLine(myExp.Name)[/color]
            [COLOR=blue]If[/color] myExp.Name [COLOR=blue]=[/color] "Breadth" [COLOR=blue]Then[/color]  
                breadthExp [COLOR=blue]=[/color] myExp  
            End [COLOR=blue]If[/color]  
            [COLOR=blue]If[/color] myExp.Name [COLOR=blue]=[/color] "Thickness" [COLOR=blue]Then[/color]  
                thicknessExp [COLOR=blue]=[/color] myExp  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

        [COLOR=blue]Dim[/color] f1 [COLOR=blue]As[/color] [COLOR=blue]New[/color] Form1  
        f1.Label1.Text [COLOR=blue]=[/color] breadthExp.Name.ToString  
        f1.TextBox1.Text [COLOR=blue]=[/color] breadthExp.Value.ToString  

        f1.Label2.Text [COLOR=blue]=[/color] thicknessExp.Name.ToString  
        f1.TextBox2.Text [COLOR=blue]=[/color] thicknessExp.Value.ToString  
        f1.ShowDialog()  

        [COLOR=blue]If[/color] breadthValue <> 0 [COLOR=blue]Then[/color]  
            breadthExp.Value [COLOR=blue]=[/color] breadthValue  
        End [COLOR=blue]If[/color]  

        [COLOR=blue]If[/color] thicknessValue <> 0 [COLOR=blue]Then[/color]  
            thicknessExp.Value [COLOR=blue]=[/color] thicknessValue  
        End [COLOR=blue]If[/color]  

        theSession.UpdateManager.DoUpdate(markId1)  
        theSession.DeleteUndoMark(markId1, [COLOR=blue]Nothing[/color])  

    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]  
[COLOR=green]'[/color]
[COLOR=green]'   end of module code[/color]
[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]

[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]
[COLOR=green]'   start of form class code[/color]

[COLOR=blue]Public[/color] Class Form1  

    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] btnAccept_Click(ByVal sender [COLOR=blue]As[/color] System.Object, [COLOR=blue]ByVal[/color] e [COLOR=blue]As[/color] System.EventArgs) [COLOR=blue]Handles[/color] btnAccept.Click  
        Module1.breadthValue [COLOR=blue]=[/color] Double.Parse(TextBox1.Text)  
        Module1.thicknessValue [COLOR=blue]=[/color] Double.Parse(TextBox2.Text)  
        Me.Close()  
    End [COLOR=blue]Sub[/color]  
End Class  

[COLOR=green]'   end form class code[/color]
[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]

[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]
[COLOR=green]'   form designer code[/color]

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _  
[COLOR=blue]Partial[/color] Class Form1  
    [COLOR=blue]Inherits[/color] System.Windows.Forms.Form  

 [COLOR=green]'Form overrides dispose to clean up the component list.[/color]
    <System.Diagnostics.DebuggerNonUserCode()> _  
    [COLOR=blue]Protected[/color] [COLOR=blue]Overrides[/color] [COLOR=blue]Sub[/color] Dispose(ByVal disposing [COLOR=blue]As[/color] [COLOR=blue]Boolean[/color])  
        [COLOR=blue]Try[/color]  
            [COLOR=blue]If[/color] disposing [COLOR=blue]AndAlso[/color] components [COLOR=blue]IsNot[/color] [COLOR=blue]Nothing[/color] [COLOR=blue]Then[/color]  
                components.Dispose()  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Finally[/color]  
            MyBase.Dispose(disposing)  
        End [COLOR=blue]Try[/color]  
    End [COLOR=blue]Sub[/color]  

 [COLOR=green]'Required by the Windows Form Designer[/color]
    [COLOR=blue]Private[/color] components [COLOR=blue]As[/color] System.ComponentModel.IContainer  

 [COLOR=green]'NOTE: The following procedure is required by the Windows Form Designer[/color]
 [COLOR=green]'It can be modified using the Windows Form Designer.  [/color]
 [COLOR=green]'Do not modify it using the code editor.[/color]
    <System.Diagnostics.DebuggerStepThrough()> _  
    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] InitializeComponent()  
        Me.Label1 [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.Label()  
        Me.TextBox1 [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.TextBox()  
        Me.Label2 [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.Label()  
        Me.TextBox2 [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.TextBox()  
        Me.btnAccept [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.Button()  
        Me.btnCancel [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Windows.Forms.Button()  
        Me.SuspendLayout()  
 [COLOR=green]'[/color]
 [COLOR=green]'Label1[/color]
 [COLOR=green]'[/color]
        Me.Label1.AutoSize [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
        Me.Label1.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(32, 43)  
        Me.Label1.Name [COLOR=blue]=[/color] "Label1"  
        Me.Label1.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(39, 13)  
        Me.Label1.TabIndex [COLOR=blue]=[/color] 0  
        Me.Label1.Text [COLOR=blue]=[/color] "Label1"  
 [COLOR=green]'[/color]
 [COLOR=green]'TextBox1[/color]
 [COLOR=green]'[/color]
        Me.TextBox1.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(102, 40)  
        Me.TextBox1.Name [COLOR=blue]=[/color] "TextBox1"  
        Me.TextBox1.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(154, 20)  
        Me.TextBox1.TabIndex [COLOR=blue]=[/color] 1  
 [COLOR=green]'[/color]
 [COLOR=green]'Label2[/color]
 [COLOR=green]'[/color]
        Me.Label2.AutoSize [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
        Me.Label2.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(32, 89)  
        Me.Label2.Name [COLOR=blue]=[/color] "Label2"  
        Me.Label2.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(39, 13)  
        Me.Label2.TabIndex [COLOR=blue]=[/color] 2  
        Me.Label2.Text [COLOR=blue]=[/color] "Label2"  
 [COLOR=green]'[/color]
 [COLOR=green]'TextBox2[/color]
 [COLOR=green]'[/color]
        Me.TextBox2.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(102, 86)  
        Me.TextBox2.Name [COLOR=blue]=[/color] "TextBox2"  
        Me.TextBox2.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(154, 20)  
        Me.TextBox2.TabIndex [COLOR=blue]=[/color] 3  
 [COLOR=green]'[/color]
 [COLOR=green]'btnAccept[/color]
 [COLOR=green]'[/color]
        Me.btnAccept.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(35, 191)  
        Me.btnAccept.Name [COLOR=blue]=[/color] "btnAccept"  
        Me.btnAccept.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(96, 50)  
        Me.btnAccept.TabIndex [COLOR=blue]=[/color] 4  
        Me.btnAccept.Text [COLOR=blue]=[/color] "Accept"  
        Me.btnAccept.UseVisualStyleBackColor [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
 [COLOR=green]'[/color]
 [COLOR=green]'btnCancel[/color]
 [COLOR=green]'[/color]
        Me.btnCancel.DialogResult [COLOR=blue]=[/color] System.Windows.Forms.DialogResult.Cancel  
        Me.btnCancel.Location [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Point(160, 191)  
        Me.btnCancel.Name [COLOR=blue]=[/color] "btnCancel"  
        Me.btnCancel.Size [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(96, 50)  
        Me.btnCancel.TabIndex [COLOR=blue]=[/color] 5  
        Me.btnCancel.Text [COLOR=blue]=[/color] "Cancel"  
        Me.btnCancel.UseVisualStyleBackColor [COLOR=blue]=[/color] [COLOR=blue]True[/color]  
 [COLOR=green]'[/color]
 [COLOR=green]'Form1[/color]
 [COLOR=green]'[/color]
        Me.AcceptButton [COLOR=blue]=[/color] Me.btnAccept  
        Me.AutoScaleDimensions [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.SizeF(6.0!, 13.0!)  
        Me.AutoScaleMode [COLOR=blue]=[/color] System.Windows.Forms.AutoScaleMode.Font  
        Me.CancelButton [COLOR=blue]=[/color] Me.btnCancel  
        Me.ClientSize [COLOR=blue]=[/color] [COLOR=blue]New[/color] System.Drawing.Size(292, 266)  
        Me.ControlBox [COLOR=blue]=[/color] [COLOR=blue]False[/color]  
        Me.Controls.Add(Me.btnCancel)  
        Me.Controls.Add(Me.btnAccept)  
        Me.Controls.Add(Me.TextBox2)  
        Me.Controls.Add(Me.Label2)  
        Me.Controls.Add(Me.TextBox1)  
        Me.Controls.Add(Me.Label1)  
        Me.FormBorderStyle [COLOR=blue]=[/color] System.Windows.Forms.FormBorderStyle.FixedSingle  
        Me.Name [COLOR=blue]=[/color] "Form1"  
        Me.Text [COLOR=blue]=[/color] "Form1"  
        Me.ResumeLayout(False)  
        Me.PerformLayout()  

    End [COLOR=blue]Sub[/color]  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] Label1 [COLOR=blue]As[/color] System.Windows.Forms.Label  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] TextBox1 [COLOR=blue]As[/color] System.Windows.Forms.TextBox  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] Label2 [COLOR=blue]As[/color] System.Windows.Forms.Label  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] TextBox2 [COLOR=blue]As[/color] System.Windows.Forms.TextBox  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] btnAccept [COLOR=blue]As[/color] System.Windows.Forms.Button  
    [COLOR=blue]Friend[/color] [COLOR=blue]WithEvents[/color] btnCancel [COLOR=blue]As[/color] System.Windows.Forms.Button  
End Class  
[COLOR=green]'[/color]
[COLOR=green]'   end of form designer code[/color]
[COLOR=green]'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[/color]


www.nxjournaling.com
 
Thank you for the reply.
Do you have any idea how we use the debugger of Visual Studio for Nx Open applications?
Also do you know anything about threads in NXOpen? I have the windows forms .exe (instead of using the journal) running and the moment when I change an expression the part changes and a window comes up saying "Work in progress".. Have you come up with a similar proplem before?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NXOpen;
using NXOpen.UF;
using NXOpen.UIStyler;

namespace LoadDLL_20082012
{

public partial class Form1 : Form
{
public Form1()
{

//Load expression
breadthExp = (Expression)theSession.Parts.Work.Expressions.FindObject("Breadth");
thicknessExp = (Expression)theSession.Parts.Work.Expressions.FindObject("thickness");

//Convert Expressions' Values to Strings
String breadth = breadthExp.Value.ToString();
String thickness = thicknessExp.Value.ToString();
this.breadth_box.Text = breadth;
this.thickness_box.Text = thickness;

}

public NXOpen.UI theUI = NXOpen.UI.GetUI();
public Session theSession = Session.GetSession();
public UFSession theUfSession = UFSession.GetUFSession();
public NXOpen.Session.UndoMarkId Id2;
public int nErrs1;
public Expression breadthExp;
public Expression thicknessExp;


private void level1_btn_Click(object sender, EventArgs e)
{
//theUfSession.UF.PrintSyslog("HELLO_Zoe!!", false);
// Load level
level1_groupbox.Visible = true;
}

private void close_btn_Click(object sender, EventArgs e)
{
this.Close();
}

private void Apply_btn_Click(object sender, EventArgs e)
{
// Get values
int br = Convert.ToInt32(breadth_box.Text.Trim());
int th = Convert.ToInt32(thickness_box.Text.Trim());

// Update part
nErrs1 = theSession.UpdateManager.DoUpdate(Id2);

// Load Bitmap Images
if (br == 25 && th == 6)
{

//Change Image Message
MessageBox.Image = Image.FromFile("");
MessageBox2.Image = Image.FromFile("");
}
else
{
//Change Image Message
MessageBox.Image = Image.FromFile("");
MessageBox2.Image = Image.FromFile("");
}

}

private void breadth_box_TextChanged(object sender, EventArgs e)
{
theSession.Parts.Work.Expressions.Edit(breadthExp, this.breadth_box.Text);
}

private void thickness_box_TextChanged(object sender, EventArgs e)
{
theSession.Parts.Work.Expressions.Edit(thicknessExp, this.thickness_box.Text);
}

}
}
 
Regarding debugging, I have seen a post in PLMWorld by Aaron Elliott giving instructions, You may have to sign up for a free account to view it.

I've not (yet) had a need to create new threads, so I won't be of much help there. However, I know there are a few members here that might be able to help; hopefully they will jump in the discussion and teach us something.

www.nxjournaling.com
 
Hi,

I was wondering if any of the members have load two window forms in NX at the same time.
My code loads and shows both forms but then produces and error ufsr. The log file is not very helpful for identifying and fixing the problem.

Thanks in advance
Z.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top