Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

NX Journal read Text File with Numbers....Error Message.

Status
Not open for further replies.

Rick4949

Mechanical
Dec 1, 2016
17
Hello community
I try to read a txt File with numbers, which are seperated with commas
example:
1,2,3,4,5,6
2,3,4,5,6,1

"I get always the Error Message: Object reference not set to an instance of an object."

Can someone help me please ?
I can't find my mistake and falling into despair.

The Code is copied here:


' NX 9.0.0.19
' Journal created by JK_Kirgoer on Tue Jan 31 13:59:16 2017 Mitteleuropäische Zeit
'
Option Strict Off
Imports System
Imports NXOpen
Imports System.IO
Imports System.Windows.Forms
Module NXJournal
Sub Main (ByVal args() As String)
Dim X As Array
Dim Y As Array
Dim Z As Array
Dim VX As Array
Dim VY As Array
Dim VZ As Array
Dim i As Integer
i = "0"
Dim Px1 As Double
Dim Px2 As Double
Dim Py1 As Double
Dim Py2 As Double
Dim Pz1 As Double
Dim Pz2 As Double
Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim line As String
Dim delim As Char() = {","}
Using sr As StreamReader = New StreamReader(openFileDialog1.FileName)
Try
line = sr.ReadLine()
While Not line Is Nothing
line = sr.ReadLine()
Dim strings As String() = line.Split(delim)
X(i) = Double.Parse(strings(0))
Y(i) = Double.Parse(strings(1))
Z(i) = Double.Parse(strings(2))
VX(i) = Double.Parse(strings(3))
VY(i) = Double.Parse(strings(4))
VZ(i) = Double.Parse(strings(5))
i = i + "1"
End While
Catch E As Exception
MessageBox.Show(E.Message)
End Try
End Using
End If

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
For j As Integer = "0" To "10"
Px1 = X(j)
Py1 = Y(j)
Pz1 = Z(j)
Px2 = VX(j)
Py2 = VY(j)
Pz2 = VZ(j)
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Insert Move")
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Create Move")
Dim genericMotionControl1 As CAM.GenericMotionControl = CType(workPart.CAMSetup.CAMOperationCollection.FindObject("GENERIC_MOTION"), CAM.GenericMotionControl)
Dim nullCAM_Move As CAM.Move = Nothing
Dim rotaryPointMoveBuilder1 As CAM.RotaryPointMoveBuilder
rotaryPointMoveBuilder1 = genericMotionControl1.CAMMoveCollection.CreateRotaryPointMoveBuilder(nullCAM_Move)
Dim origin As Point3d = New Point3d(0.0, 0.0, 0.0)
Dim vector As Vector3d = New Vector3d(0.0, 0.0, 1)
Dim direction As Direction
direction = workPart.Directions.CreateDirection(origin, vector, SmartObject.UpdateOption.AfterModeling)
rotaryPointMoveBuilder1.OffsetData.OffsetVector = direction

theSession.SetUndoMarkName(markId2, "Verschiebungsunteroperation erzeugen-Dialogfenster")
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
' ----------------------------------------------
' Dialogfenster Anfang Verschiebungsunteroperation erzeugen
' ----------------------------------------------
rotaryPointMoveBuilder1.MotionType = CAM.MoveBuilder.Motion.Cut
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
theSession.SetUndoMarkName(markId4, "Punkt")
theSession.DeleteUndoMark(markId4, Nothing)
Dim expression36 As Expression
expression36 = workPart.Expressions.CreateSystemExpressionWithUnits(Px1, unit1)
Dim expression37 As Expression
expression37 = workPart.Expressions.CreateSystemExpressionWithUnits(Py1, unit1)
Dim expression38 As Expression
expression38 = workPart.Expressions.CreateSystemExpressionWithUnits(Pz1, unit1)
Dim scalar16 As Scalar
scalar16 = workPart.Scalars.CreateScalarExpression(expression36, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim scalar17 As Scalar
scalar17 = workPart.Scalars.CreateScalarExpression(expression37, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim scalar18 As Scalar
scalar18 = workPart.Scalars.CreateScalarExpression(expression38, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim point6 As Point
point6 = workPart.Points.CreatePoint(scalar16, scalar17, scalar18, SmartObject.UpdateOption.AfterModeling)
rotaryPointMoveBuilder1.Point = point6

Dim markId7 As Session.UndoMarkId
markId7 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
theSession.SetUndoMarkName(markId7, "Vektor-Dialogfenster")
Dim markId8 As Session.UndoMarkId
markId8 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
theSession.SetUndoMarkName(markId8, "Punkt-Dialogfenster")
Dim expression70 As Expression
expression70 = workPart.Expressions.CreateSystemExpressionWithUnits(Px1, unit1)
Dim scalar31 As Scalar
scalar31 = workPart.Scalars.CreateScalarExpression(expression70, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim expression71 As Expression
expression71 = workPart.Expressions.CreateSystemExpressionWithUnits(Py1, unit1)
Dim scalar32 As Scalar
scalar32 = workPart.Scalars.CreateScalarExpression(expression71, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim expression72 As Expression
expression72 = workPart.Expressions.CreateSystemExpressionWithUnits(Pz1, unit1)
Dim scalar33 As Scalar
scalar33 = workPart.Scalars.CreateScalarExpression(expression72, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim point11 As Point
point11 = workPart.Points.CreatePoint(scalar31, scalar32, scalar33, SmartObject.UpdateOption.AfterModeling)
theSession.SetUndoMarkName(markId8, "Punkt")
theSession.DeleteUndoMark(markId8, Nothing)
Dim markId11 As Session.UndoMarkId
markId11 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")
theSession.SetUndoMarkName(markId11, "Punkt-Dialogfenster")
Dim expression102 As Expression
expression102 = workPart.Expressions.CreateSystemExpressionWithUnits(Px2, unit1)
Dim scalar49 As Scalar
scalar49 = workPart.Scalars.CreateScalarExpression(expression102, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim expression103 As Expression
expression103 = workPart.Expressions.CreateSystemExpressionWithUnits(Py2, unit1)
Dim scalar50 As Scalar
scalar50 = workPart.Scalars.CreateScalarExpression(expression103, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim expression104 As Expression
expression104 = workPart.Expressions.CreateSystemExpressionWithUnits(Pz2, unit1)
Dim scalar51 As Scalar
scalar51 = workPart.Scalars.CreateScalarExpression(expression104, Scalar.DimensionalityType.None, SmartObject.UpdateOption.AfterModeling)
Dim point17 As Point
point17 = workPart.Points.CreatePoint(scalar49, scalar50, scalar51, SmartObject.UpdateOption.AfterModeling)
theSession.SetUndoMarkName(markId11, "Punkt")
theSession.DeleteUndoMark(markId11, Nothing)
Dim direction6 As Direction
direction6 = workPart.Directions.CreateDirection(point11, point17, SmartObject.UpdateOption.AfterModeling)
rotaryPointMoveBuilder1.Vector = direction6
Dim nXObject1 As NXObject
nXObject1 = rotaryPointMoveBuilder1.Commit()
Dim manualMove1 As CAM.ManualMove = CType(nXObject1, CAM.ManualMove)
genericMotionControl1.AppendMove(manualMove1)
theSession.SetUndoMarkName(markId2, "Verschiebungsunteroperation erzeugen")
rotaryPointMoveBuilder1.Destroy()
theSession.DeleteUndoMark(markId3, Nothing)
Next
End Sub
End Module
 
Replies continue below

Recommended for you

First issue:
I suggest dimensioning your array variables like this:
Code:
Dim X() as Double
or
Code:
Dim X as Double()
and NOT like:
Code:
Dim X as Array
To add new values into the array, you will need to re-dimension the array so it can hold the new values. Instead of using an array, I would suggest using a list object as it will take care of these operations for you.

The first 2 ways declare an array named X of type double. Declaring a variable as a System.Array creates an object - one that you have not initialized, so you get null reference error.

The counter variable is declared as an Integer, but the code attempts to assign a string value to the variable. This will result in an error; use integer values rather than strings. Alternately, if you use a list object instead of an array of values, you can eliminate this counter value entirely.

Change this:
Code:
Dim i As Integer
i = "0"
...
i = i + "1"

To this:
Code:
Dim i As Integer
i = 0
...
i = i + 1


www.nxjournaling.com
 
Thank you cowski for the the reply.

I have solved my problem with this code.





Option Strict Off

Imports System

Imports NXOpen

Imports System.IO

Imports System.Windows.Forms




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 pointsFile As String

'open csv file

Dim fdlg As OpenFileDialog = New OpenFileDialog()

fdlg.Title = "Choose the points file"

fdlg.InitialDirectory = "c:\"

fdlg.Filter = "All files (*.*)|*.*|TXT files (*.txt)|*.txt"

fdlg.FilterIndex = 2

fdlg.RestoreDirectory = True

If fdlg.ShowDialog() = DialogResult.OK Then

pointsFile = fdlg.FileName

End If




'read the points into an array

Dim num_points As Long

Dim x As Double

Dim y As Double

Dim z As Double

Dim tmpstream As StreamReader = File.OpenText(pointsFile)

Dim strlines() As String

Dim strline() As String

'Load content of file to strLines array

strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)

' Count number of points

num_points = UBound(strlines)

Dim i As Integer

For i = 0 To num_points - 1

strline = strlines(i).Split(",")

x = CDbl(strline(0))

y = CDbl(strline(1))

z = CDbl(strline(2))

Dim coordinates1 As Point3d = New Point3d(x, y, z)

Dim point1 As Point

point1 = workPart.Points.CreatePoint(coordinates1)

point1.SetVisibility(SmartObject.VisibilityOption.Visible)

point1.RedisplayObject()

coordinates1 = Nothing

point1 = Nothing

Next




End Sub

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor