hexagon logo

Import static data from textfile (fast hack)

Proof of concept.

Contents of data.txt:
Article: 988893
Description: pistons
Nr. Lot: 838384


How pc-dimis can import only the data "988893" , "pistons" and "838384
" from this file??


Code for reading:
FPTR       =FILE/OPEN,C:\Documents and Settings\CS-FC-SDE-S-PCID254\Desktop\data.txt,READ
            ASSIGN/ROW1=""
            ASSIGN/ROW2=""
            ASSIGN/ROW3=""
V1         =FILE/READLINE,FPTR,{ROW1}
V1         =FILE/READLINE,FPTR,{ROW2}
V1         =FILE/READLINE,FPTR,{ROW3}
            FILE/CLOSE,FPTR,KEEP
            ASSIGN/ROW1=ROW1-"Article: "
            ASSIGN/ROW2=ROW2-"Description: "
            ASSIGN/ROW3=ROW3-"Nr. Lot: "


Note that the contents of the three rows must match the static string that we are deleting from the line we read, otherwise it will not work. If you change the "Article: " to anything else, you must also change the "ROW1-"Article: " to match your change. This applies of course to the ROW2 and ROW3 contents as well.

An easy hack with no string verification or any of the fancy stuff. It builds on the fact that the source file "data.txt" only contains three rows with data that is interesting for us and these are the three first rows in the source file.
  • I think there is an even more direct way to do this:

    FPTR       =FILE/OPEN,C:\Documents and Settings\CS-FC-SDE-S-PCID254\Desktop\data.txt
    V1         =FILE/READLINE,FPTR,"Article:"+" "+{ROW1}
    V1         =FILE/READLINE,FPTR,"Description:"+" "+{ROW2}
    V1         =FILE/READLINE,FPTR,"Nr. Lot:"+" "+{ROW3}
                FILE/CLOSE,FPTR,KEEP


    This should read over the unwanted text and only store the desired characters in the variable.

    I'll double check it when I get to work but I'm pretty sure this will do the same thing. I haven't tried it with the desirable text at the beginning, or in the middle, of the line. VPT's method should work no matter where the text is located.

    UPDATE: Syntax above adjusted a bit. The static text that you want to ignore needs to match the text in the file exactly and it must be in "quotes". If there is a space in the string that needs to be ignored that needs to be separately defined as shown above.
  • You can also use this "ignore" technique to easily read in data separated by some delimiter. In this case the file is comma delimited:
    FPTR       =FILE/OPEN,ROOT + PN + "\\PROGRAMS\\" + OP + "\\" + PN + "_" + OP + "_" + "DATUM_C" + ".TXT",READ
    DTM_C      =FILE/READLINE,FPTR,{DTMCX} + "," + {DTMCY} + "," + {DTMCZ} + "," + {DTMCI} + "," + {DTMCJ} + "," + {DTMCK} 
                FILE/CLOSE,FPTR,KEEP
    $$ NO,
                DTMCX
                DTMCY
                DTMCZ
                DTMCI
                DTMCJ
                DTMCK
    DTM_C3     =GENERIC/PLANE,DEPENDENT,CARTESIAN,$
                NOM/XYZ,<69.046,1.908,234.581>,$
                MEAS/XYZ,<DTMCX,DTMCY,DTMCZ>,$
                NOM/IJK,<-1,0,0>,$
                MEAS/IJK,<DTMCI,DTMCJ,DTMCK>
    DIM ROOT_LNGTH= 2D DISTANCE FROM PLANE LE_BF_PLN TO PLANE DTM_C3 PAR TO   XAXIS,NO_RADIUS  UNITS=MM,$
    GRAPH=OFF  TEXT=OFF  MULT=50.00  OUTPUT=BOTH
    AX    NOMINAL       MEAS        DEV
    M      237.900    237.900      0.000
  • Awesome, I did not know about the Ignore handling. Thanks guys!