Parse a text file to create PVectors

edited February 2014 in Questions about Code

I am working on a project and I need some help to get it started. I want to create a program that will parse text from a text file and create an arrayList of PVector points from the file.

Here is an example of the text I will use as input:

...
LOAD/TOOL,1240,ADJUST,1240
PAINT/COLOR,186
RAPID
GOTO/50.9361188,22.1399311,66.0000000,0.0000000,0.0000000,1.0000000
PAINT/COLOR,211
RAPID
GOTO/50.9361188,22.1399311,44.8782394
PAINT/COLOR,6
FEDRAT/IPM,50.0000000

GOTO/50.9361188,22.1399311,41.8782394 // First point 

PAINT/COLOR,31
FEDRAT/100.000000  // This is an indicator line to start recording the following points

// I also want to use the point before the following as the first point

GOTO/135.032223,22.1399311,41.8782394
GOTO/135.643971,26.8821142,43.6767850
GOTO/135.655965,26.9743716,43.7117751
GOTO/135.668047,27.0666190,43.7467613
GOTO/135.681138,27.1656088,43.7843047
...

After I create the arrayList of 3D Vectors, I want to calculate the sum of the distances traveled from point to point in sequence.

There is a lot more I want to do with this after, but this will get me started.

Answers

  • edited February 2014

    I used one of the examples that came with Processing and came up with this so far:

    String[] lines;
    int index = 0;
    ArrayList<PVector> pointList;
    
    void setup() {
      size(200, 200);
      background(0);
      stroke(254);
      lines = loadStrings("file.txt");
      pointList = new ArrayList<PVector>();
    }
    
    void draw() {
      if (index < lines.length) {
        String[] pieces = splitTokens(lines[index], "/,");
        for (int i = 0; i<pieces.length; i=i+1) {
          if (pieces[i].equals("GOTO")) {
            println("X=",pieces[i+1],"Y=",pieces[i+2],"Z=",pieces[i+3]);
            pointList.add(new PVector(float(pieces[i+1]),float(pieces[i+2]),float(pieces[i+3])));
          }
        }
      }
      index = index + 1;
    }
    

    Now I just need to put some logic in there somewhere to select only the points I want, then calculate the sum of the distances, which will be easy with the PVectors.

    Any refinements or pointers are welcome.

  • edited February 2014 Answer ✓

    first I recommend to check pointList

    e.g. use

    println (pointList.size());

    if the number of items is correct approx.

    then you can println a few items / PVectors to see if they are as expected

    then to add up dist use a for-loop i to size()-1 (or -2 ?)

    and say

    PVector currentPoint1 = pointList.get(i);
    
    PVector currentPoint2 = pointList.get(i+1);
    

    then say

    myDist = myDist + currentPoint1.dist(currentPoint2);
    println ( currentPoint1.dist(currentPoint2) ); 
    

    Greetings, Chrisir

  • why does line 5 have 6 arguments when all the other GOTOs have 3?

  • edited February 2014 Answer ✓

    well spotted!

    also, why use a for-loop in line 16 of the code?

    Just say

    // first exclude line 5 of the text file 
    if (pieces.length == 4) {
     // then check GOTO 
     if (pieces[0].equals("GOTO")) {
        int i=0; 
        println("X=",pieces[i+1],"Y=",pieces[i+2],"Z=",pieces[i+3]);
        pointList.add(new PVector(float(pieces[i+1]),float(pieces[i+2]),float(pieces[i+3])));
      } // if 
    }  if 
    

    Greetings, Chrisir

  • Answer ✓

    or

    Just say

    // first exclude line 5 of the text file 
    if (pieces.length == 4) {
     // then check GOTO 
     if (pieces[0].equals("GOTO")) {
        println("X=",pieces[1],"Y=",pieces[2],"Z=",pieces[3]);
        pointList.add(new PVector(float(pieces[1]),float(pieces[2]),float(pieces[3])));
      } // if 
    }  if 
    

    Greetings, Chrisir

  • edited February 2014

    @Chrisir Thanks for all the points! I will go through and do some refactoring. That's the fun part. I don't think a for-loop is necessary. I'll make some changes.

    @koogs This is code that is used to generate G Code for machine programming. The first 3 numbers are for X, Y, and Z position of the tool tip. The last 3 are I, J, and K, which indicate the tool's orientation. With 0,0,1, as above, the tool is oriented straight down. This is necessary info for 5 axis mills. If the orientation of the tool doesn't change, then the IJK numbers are dropped until (or if) they change.

Sign In or Register to comment.