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
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