NX Opening a text file from Post Builder and storing each line as an array element.

2019-05-15T12:14:30Z
NX for Manufacturing

Summary


Details

A data file was created with the same name as the part file except it had a txt extension instead of a prt . This file contains pertinent information to the part and needs to be included in the posted output.  How can this text file be read and then each line be stored as an array element?


Solution

A Custom Command is created in Post Builder and the TCL code seen below is added. This Custom Command is then included in the Start of Program event. When the post is executed Post Builder will verify that the text file exists and then store each line in an array named mom_text_file(n) . This array variable could than be called at any time when posting. In the sample code each line of data will be stored in the array and also listed using a MOM_output_literal command.


global mom_event_handler_file_name mom_text_file mom_part_name

regsub -all {.prt} $mom_part_name ".txt" lookup_tbl

if [catch {open $lookup_tbl r} fileId] {

   set response [MOM_display_message "$fileId" "File error:" Q OK]

   return

} else {

   set i 0

   foreach line [split [read $fileId] \n] {

          set mom_text_file($i) "[lindex $line ] "

          MOM_output_literal "(mom_text_file($i)=$mom_text_file($i))"

   incr i

   }

   close $fileId

}


Notes

Please be aware that any code provided by GTAC is intended to be used for sample purposes only. It is the user's responsibility to determine whether the code is suitable for the purpose.

KB Article ID# PL8006863

Contents

SummaryDetails

Associated Components

Post Builder