Multiline Note from copied text

I am using following code to read lines from text file and drooping note inside drafting.. not sure what I am doing wrong


Sub Main()
Dim path As String = "mypath\file.txt"
Dim i As Integer = 1
Dim line(i) As String
Dim objReader As New System.IO.StreamReader(path)
Do While objReader.Peek() <> -1
line(i) = objReader.ReadLine()
i = i + 1
Loop
objReader.Close()
Call Drop_Text(line) ' SHOWING ERROR IN THIS LINE
End Sub
Public Sub Drop_Text(TextToDrop As String())
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display

Dim nullNXOpen_Annotations_SimpleDraftingAid As NXOpen.Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As NXOpen.Annotations.DraftingNoteBuilder = Nothing

draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(nullNXOpen_Annotations_SimpleDraftingAid)
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)
draftingNoteBuilder1.Style.LetteringStyle.GeneralTextSize = 0.1

Dim nullNXOpen_View As NXOpen.View = Nothing
Dim point1 As NXOpen.Point3d = New NXOpen.Point3d(1, 3.5, 0.0)
Dim nXObject1 As NXOpen.NXObject = Nothing

draftingNoteBuilder1.Text.TextBlock.SetText(TextToDrop)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullNXOpen_View, point1)
nXObject1 = draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()

End Sub

<\code>

I have tried this below:

Dim path As String = "\\ad001.siemens.net\dfs001\File\US\Systems_Development\0_Installers\7_NX_SpeedBar\archive\MEX R&D NOtes\SDR NOTES.txt"

Dim stringA As String = System.IO.File.ReadAllText(path)
Dim stringB() As String = stringA.Split(vbCrLf)

lw.Open()
For Each stringX As String In stringB
lw.WriteLine(stringX)

Next
'Loop
'objReader.Close()

Drop_Text(stringB)

Which is returning the texts from the text file, but cutting 7 spaces starting from 2nd line till the last line. Very surprising! Wonder why it would happen? Anyone have seen this issue?

Regards,
MFJ

In the code below, I used a stream reader to read the text file line by line. I added those lines to a list and passed it in to your Drop_text subroutine. It works for me (NX 1919).

If you are seeing characters of the string cut off, it might be a text file encoding issue. The stream reader has options for differently encoded files.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports NXOpen

Module insert_text_file

Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

lw.Open()

Dim txtFilePath As String = "C:\temp\test.txt"

Dim sr As StreamReader = New StreamReader(txtFilePath)
Dim line As String
Dim txtLines As New List(Of String)

Try
Line = sr.ReadLine()
While Not line Is Nothing
txtLines.Add(line)
line = sr.ReadLine()
End While
Finally
sr.Close()
End Try

For Each temp As String In txtLines
lw.WriteLine(temp)
Next

Drop_Text(txtLines.ToArray)

End Sub

Public Sub Drop_Text(TextToDrop As String())
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim nullNXOpen_Annotations_SimpleDraftingAid As NXOpen.Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As NXOpen.Annotations.DraftingNoteBuilder = Nothing

draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(nullNXOpen_Annotations_SimpleDraftingAid)
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)
draftingNoteBuilder1.Style.LetteringStyle.GeneralTextSize = 0.1

Dim nullNXOpen_View As NXOpen.View = Nothing
Dim point1 As NXOpen.Point3d = New NXOpen.Point3d(1, 3.5, 0.0)
Dim nXObject1 As NXOpen.NXObject = Nothing

draftingNoteBuilder1.Text.TextBlock.SetText(TextToDrop)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullNXOpen_View, point1)
nXObject1 = draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()

End Sub

End Module

Thanks. Just before you posted this, I figured an alternative way using ReadallLines Method, using a suggestion from a friend. Very straightforward with same dropText sub. The code is below:


Dim stringA As String() = System.IO.File.ReadAllLines(path)
For Each stringX As String In stringA
lw.Open()
lw.WriteLine(stringX)

Next
Call Drop_Text(stringA)

Regards,
MFJ