Page 1 of 1

[SOLVED] how to import ASCII file with coordinates ?

Posted: Fri Nov 02, 2007 2:09 pm
by pipo
is it possible to import a geometry from a (ascii) file with x,y coordinates ?

Posted: Fri Nov 02, 2007 4:20 pm
by michael
Hmm... good question.

There are solutions I think. Lets see.

A) Use the "Scripting Language". See examples in "scripts" of your QCad installation.

B) It is planned to have a multi-line-input command line. But this does not work right now.
Then, it will be possible to enter
line 0,0
@10,0
...
from a plain text editor, to copy&past directly from an file with comments on it.


But right now, A) will be the solution.
So your script can be used by directly enter some values there, stored in a variable/array list. Or, more comfortable, to script an file-open-dialog box, then read the xy-Values in an array, to conversion (add "line", or whatever needed), etc.

Posted: Thu Nov 08, 2007 3:21 pm
by pipo
good answer - thanks a lot.

The example scripts demonstrate nicely how to open files and draw things.
Since I'm not proficient in C I'd need another hint on how to access/read (all) the data in the file.
TIA

Posted: Thu Nov 08, 2007 5:36 pm
by michael
(NOTE: Customer programming and specific customer support is basically not included by this low price of QCad Professional. Or made a donation so that we can support you for your application as needed.

Basically there is a fist-come first-serve. If you pay for a feature, it will be implemented first. Or if you are a VIP customer, with a 10 million dollar business critical application, please contact us.

Or if you have an superb application value and usage for the feature, that pushes QCad Prof that everybody must have and pay it... :lol:

A good starting point is for example theCadTrain.com for basic drafting with QCad Prof. (But scripting is not included there right now.)

We know AutoCAD ... and we like it ... but I found out that QCad can do many 2D things much more easier then AutoCAD... 8) QCad is in fact easier and faster. But it is not AutoCAD... sure.

-- The drawing code crashes here. Sure you have to read the values in x and y values. Now it uses strings. But the file will be read line by line and printed out correctly.)


My input file was:
-------------
0,0
10,10
0,100
-----------
(no newline at the end. No "---", ascii.)

--> Maybe you have an more interesting example files, online? XY-Maps, etc. I cant find anything with google.com


You do not have to by a C/C++ specialist, it is anyway more like JavaScript.

Take a look here:
http://www.ribbonsoft.com/qcad/manual_s ... index.html

And here:
http://doc.trolltech.com/qsa-1.2.2/language.html
http://doc.trolltech.com/qsa-1.2.2/getting-started.html


Code: Select all

/**
 *
 *  Read from a ASCII text file xy values.
 */
function main() {
    var doc;       // current document
    var view;      // current document view
    var line;      // line to add
    var i;         // counter
    var factor;    // scaling factor
    var lx = -1.0;
    var ly = -1.0;
    var x;
    var y;
        
    var file;  
    
    
    factor = inputData();
  
    doc = new Document;
    view = new View;
    

    try {
      var file;  
      var filename = "support_inputxy.txt";
      var aLine;
        file = new File( filename);
         file.open(1);


while  (aLine = file.readLine() ) {
  print("line: " + aLine);
//              var line = new Line(doc, lx*factor.x, ly*factor.y,
//                                x*factor.x, y*factor.y);
//            doc.addEntity(line);
}
   //process( file );

       }
     finally {
       // file.close(1);   
     }
     

    view.redraw();
}



/**
 * Presents a dialog to input an X and Y factor for the sine.
 */
function inputData() {
    var dialog = new Dialog;
    dialog.caption = "Factor for sine curve";
    dialog.okButtonText = "OK";
    dialog.cancelButtonText = "Abort";

    var xfact = new LineEdit;
    xfact.label = "X Factor: ";
    xfact.text = "1";
    dialog.add(xfact);

    var yfact = new LineEdit;
    yfact.label = "Y Factor: ";
    yfact.text = "1";
    dialog.add(yfact);

    if (dialog.exec()) {
        print("Factors: " + xfact.text + "/" + yfact.text);
    }

    var ret = new Vector(xfact.text, yfact.text);

    return ret;
}


pipo wrote:good answer - thanks a lot.

The example scripts demonstrate nicely how to open files and draw things.
Since I'm not proficient in C I'd need another hint on how to access/read (all) the data in the file.
TIA

Posted: Fri Nov 09, 2007 10:45 am
by pipo
great - thanks for your valuable help.

Your script does not seem to work yet because the coordinates x,y are not extracted from the line 'aline' (the plotted entity also shows that)

A last question: why did you comment out process.file and file.close ? Because if I remove the comment the interpreter complains about file.close not being a process.

Posted: Fri Nov 09, 2007 11:56 am
by pipo
you were asking for a more interesting data file. What I'd like to do is read airfoil geometries, I attach an example (they are normalized to length 1)

Code: Select all

     1.000000    0.000514
     0.992836    0.001482
     0.981052    0.003074
     0.967821    0.004862
     0.952989    0.006866
     0.936635    0.009075
     0.919167    0.011435
     0.901089    0.013878
     0.882691    0.016364
     0.864046    0.018883
     0.845194    0.021430
     0.826166    0.024001
     0.806981    0.026593
     0.787653    0.029205
     0.768198    0.031833
     0.748657    0.034473
     0.729107    0.037115
     0.709615    0.039748
     0.690213    0.042370
     0.670897    0.044980
     0.651662    0.047578
     0.632502    0.050167
     0.613412    0.052746
     0.586682    0.056358
     0.575422    0.057858
     0.556515    0.060277
     0.537674    0.062553
     0.518907    0.064675
     0.500218    0.066628
     0.481603    0.068403
     0.463056    0.069989
     0.444575    0.071377
     0.426162    0.072557
     0.407822    0.073518
     0.389566    0.074250
     0.371400    0.074742
     0.353304    0.074984
     0.335230    0.074967
     0.317128    0.074695
     0.298991    0.074175
     0.280853    0.073413
     0.262759    0.072409
     0.244730    0.071158
     0.226769    0.069658
     0.208892    0.067903
     0.191146    0.065894
     0.173594    0.063629
     0.156282    0.061103
     0.139232    0.058308
     0.122517    0.055237
     0.106377    0.051917
     0.091149    0.048425
     0.077162    0.044857
     0.064657    0.041316
     0.053755    0.037903
     0.044442    0.034699
     0.036594    0.031691
     0.030029    0.028852
     0.024523    0.026166
     0.019869    0.023613
     0.015917    0.021168
     0.012543    0.018812
     0.009649    0.016530
     0.007169    0.014305
     0.005064    0.012113
     0.003329    0.009916
     0.001962    0.007699
     0.000967    0.005466
     0.000333    0.003244
     0.000035    0.001062
     0.000000    0.000000
     0.000035   -0.001062
     0.000333   -0.003244
     0.000967   -0.005466
     0.001962   -0.007699
     0.003329   -0.009916
     0.005064   -0.012113
     0.007169   -0.014305
     0.009649   -0.016530
     0.012543   -0.018812
     0.015917   -0.021168
     0.019869   -0.023613
     0.024523   -0.026166
     0.030029   -0.028852
     0.036594   -0.031691
     0.044442   -0.034699
     0.053755   -0.037903
     0.064657   -0.041316
     0.077162   -0.044857
     0.091149   -0.048425
     0.106377   -0.051917
     0.122517   -0.055237
     0.139232   -0.058308
     0.156282   -0.061103
     0.173594   -0.063629
     0.191146   -0.065894
     0.208892   -0.067903
     0.226769   -0.069658
     0.244730   -0.071158
     0.262759   -0.072409
     0.280853   -0.073413
     0.298991   -0.074175
     0.317128   -0.074695
     0.335230   -0.074967
     0.353304   -0.074984
     0.371400   -0.074742
     0.389566   -0.074250
     0.407822   -0.073518
     0.426162   -0.072557
     0.444575   -0.071377
     0.463056   -0.069989
     0.481603   -0.068403
     0.500218   -0.066628
     0.518907   -0.064675
     0.537674   -0.062553
     0.556515   -0.060277
     0.575422   -0.057858
     0.586682   -0.056358
     0.613412   -0.052746
     0.632502   -0.050167
     0.651662   -0.047578
     0.670897   -0.044980
     0.690213   -0.042370
     0.709615   -0.039748
     0.729107   -0.037115
     0.748657   -0.034473
     0.768198   -0.031833
     0.787653   -0.029205
     0.806981   -0.026593
     0.826166   -0.024001
     0.845194   -0.021430
     0.864046   -0.018883
     0.882691   -0.016364
     0.901089   -0.013878
     0.919167   -0.011435
     0.936635   -0.009075
     0.952989   -0.006866
     0.967821   -0.004862
     0.981052   -0.003074
     0.992836   -0.001482
     1.000000   -0.000514

Posted: Fri Nov 09, 2007 2:38 pm
by michael
Check this:

You do not have to by a C/C++ specialist, it is anyway more like JavaScript.

Take a look here:
http://www.ribbonsoft.com/qcad/manual_s ... index.html

And here:
http://doc.trolltech.com/qsa-1.2.2/language.html
http://doc.trolltech.com/qsa-1.2.2/getting-started.html


--> Maybe you have an more example files online?

* XY-Maps
* Survivor Maps
* etc.

I cant find anything with google.com

pipo wrote:you were asking for a more interesting data file. What I'd like to do is read airfoil geometries, I attach an example (they are normalized to length 1)

Code: Select all

     1.000000    0.000514
     0.992836    0.001482
     0.981052    0.003074
     0.967821    0.004862
     0.952989    0.006866
     0.936635    0.009075
     0.919167    0.011435
     0.901089    0.013878
     0.882691    0.016364
     0.864046    0.018883
     0.845194    0.021430
     0.826166    0.024001
     0.806981    0.026593
     0.787653    0.029205
     0.768198    0.031833
     0.748657    0.034473
     0.729107    0.037115
     0.709615    0.039748
     0.690213    0.042370
     0.670897    0.044980
     0.651662    0.047578
     0.632502    0.050167
     0.613412    0.052746
     0.586682    0.056358
     0.575422    0.057858
     0.556515    0.060277
     0.537674    0.062553
     0.518907    0.064675
     0.500218    0.066628
     0.481603    0.068403
     0.463056    0.069989
     0.444575    0.071377
     0.426162    0.072557
     0.407822    0.073518
     0.389566    0.074250
     0.371400    0.074742
     0.353304    0.074984
     0.335230    0.074967
     0.317128    0.074695
     0.298991    0.074175
     0.280853    0.073413
     0.262759    0.072409
     0.244730    0.071158
     0.226769    0.069658
     0.208892    0.067903
     0.191146    0.065894
     0.173594    0.063629
     0.156282    0.061103
     0.139232    0.058308
     0.122517    0.055237
     0.106377    0.051917
     0.091149    0.048425
     0.077162    0.044857
     0.064657    0.041316
     0.053755    0.037903
     0.044442    0.034699
     0.036594    0.031691
     0.030029    0.028852
     0.024523    0.026166
     0.019869    0.023613
     0.015917    0.021168
     0.012543    0.018812
     0.009649    0.016530
     0.007169    0.014305
     0.005064    0.012113
     0.003329    0.009916
     0.001962    0.007699
     0.000967    0.005466
     0.000333    0.003244
     0.000035    0.001062
     0.000000    0.000000
     0.000035   -0.001062
     0.000333   -0.003244
     0.000967   -0.005466
     0.001962   -0.007699
     0.003329   -0.009916
     0.005064   -0.012113
     0.007169   -0.014305
     0.009649   -0.016530
     0.012543   -0.018812
     0.015917   -0.021168
     0.019869   -0.023613
     0.024523   -0.026166
     0.030029   -0.028852
     0.036594   -0.031691
     0.044442   -0.034699
     0.053755   -0.037903
     0.064657   -0.041316
     0.077162   -0.044857
     0.091149   -0.048425
     0.106377   -0.051917
     0.122517   -0.055237
     0.139232   -0.058308
     0.156282   -0.061103
     0.173594   -0.063629
     0.191146   -0.065894
     0.208892   -0.067903
     0.226769   -0.069658
     0.244730   -0.071158
     0.262759   -0.072409
     0.280853   -0.073413
     0.298991   -0.074175
     0.317128   -0.074695
     0.335230   -0.074967
     0.353304   -0.074984
     0.371400   -0.074742
     0.389566   -0.074250
     0.407822   -0.073518
     0.426162   -0.072557
     0.444575   -0.071377
     0.463056   -0.069989
     0.481603   -0.068403
     0.500218   -0.066628
     0.518907   -0.064675
     0.537674   -0.062553
     0.556515   -0.060277
     0.575422   -0.057858
     0.586682   -0.056358
     0.613412   -0.052746
     0.632502   -0.050167
     0.651662   -0.047578
     0.670897   -0.044980
     0.690213   -0.042370
     0.709615   -0.039748
     0.729107   -0.037115
     0.748657   -0.034473
     0.768198   -0.031833
     0.787653   -0.029205
     0.806981   -0.026593
     0.826166   -0.024001
     0.845194   -0.021430
     0.864046   -0.018883
     0.882691   -0.016364
     0.901089   -0.013878
     0.919167   -0.011435
     0.936635   -0.009075
     0.952989   -0.006866
     0.967821   -0.004862
     0.981052   -0.003074
     0.992836   -0.001482
     1.000000   -0.000514

possible solution to import XY data

Posted: Mon Nov 12, 2007 11:04 pm
by michael
The Question was: "Is it possible to import a geometry from a (ascii) file with x,y coordinates?"

My answer: Yes, it is. Sure it is possible. 8)

Need to know how? Read on.

Feel the power of QCad Professional integrated scripting language.


Here is a possible solution to import XY data to QCad Professional:

The input file format must by plain text file, ASCII with TAB or COMMA separated value pairs whitch are XY values.

1.234, 2.324
3.234, 4.234

or:

1.234<TAB> 2.324
3.234<TAB> 4.234


Happy hacking! 8)

Hint: Just implement LOGO now, HPGL, Surviver Coordinates, Country boundary or whatever!

For more information about LOGO please visit http://en.wikipedia.org/wiki/Logo_programming_language
For more information about HPGL please visit
HPGL: http://en.wikipedia.org/wiki/HPGL

Do it and post the code here. (I did not found any country border xy data. So if you know where to find such data, or any public available xy data, please let us know.)



file: support_inputxy.qs
---------------------------

Code: Select all

/**
 *  Status: works just fine.
 *  Read from a ASCII text file xy values.
 * 1. lower this window.
 * 2. Click on it (make active) so that the QCad 
 *    windows is shown.
 * 3. Hit F5 key in this lowered windows.
 * 4. Nice watching!
 *
 */

function main() {

  var filename = "support_inputxy.txt";    

  var doc;       // current document
  var view;      // current document view
  var line;      // line to add
  var i;         // counter
  var factor;    // scaling factor
  var lx = -1.0;
  var ly = -1.0;
  var x;
  var y;
        
  var file;  

  var aLine;
  var bLine;
  
  filename = inputFileName();
  factor = inputData();

  doc = new Document;
  view = new View;

  try {
    file = new File( filename);
    file.open(1);

    var re = /(.*)[,|\t](.*)/;  // comma separated list 
    var num_x; // 1st point x
    var num_y; // 1st point y
    var num_bx;  // 2nd point x
    var num_by;  // 2nd point y
    var line;

    
    // --- line 1 and 2
    aLine = file.readLine();
    bLine = file.readLine();

    aLine.match(re);
    num_x = parseFloat( re.capturedTexts[1] );
    num_y = parseFloat( re.capturedTexts[2] );

    bLine.match(re);
    num_bx = parseFloat( re.capturedTexts[1] );
    num_by = parseFloat( re.capturedTexts[2] );
    
    lx = num_x;   
    ly = num_y;   
  
    x = num_bx;   
    y = num_by;   

    line = new Line(doc, lx*factor.x, ly*factor.y,
                          x*factor.x,  y*factor.y);
    doc.addEntity(line);

    // line 2 and 3 ...
    while  ( aLine = file.readLine() ) {
      //print("DEBUG: raw input data line: \"" + aLine + "\"");

      aLine.match(re);
      num_x = parseFloat( re.capturedTexts[1] );
      num_y = parseFloat( re.capturedTexts[2] );

      //print("DEBUG: x y: \'" + num_x + "\'   \'" + num_y + "\'");
      //print("DEBUG: bx by: \'" + num_bx + "\'   \'" + num_by + "\'");
    
      lx = num_bx;   
      ly = num_by;   
  
      x = num_x;   
      y = num_y;   

      line = new Line(doc, lx*factor.x, ly*factor.y,
                            x*factor.x,  y*factor.y);
      doc.addEntity(line);
      view.redraw();
      
      num_bx = num_x;
      num_by = num_y;
    }

  }
  finally {
    file.close();
  }

  print("DEBUG: End of File reached.");     

  view.redraw();

  print("All done.");
}


/**
 * Presents a dialog to input file name.
 */
function inputFileName() {
    var dialog = new Dialog;
    dialog.caption = "Input filename";
    dialog.okButtonText = "OK";
    dialog.cancelButtonText = "Abort";

    var fn = new LineEdit;
    fn.label = "Filename: ";
    fn.text = "support_inputxy.txt";
    dialog.add(fn);

    if (dialog.exec()) {
        print("filename: " + fn.text);
    }

    var ret = fn.text;

    return ret;
}

/**
 * Presents a dialog to input an X and Y factor for the sine.
 */
function inputData() {
    var dialog = new Dialog;
    dialog.caption = "Factor for sine curve";
    dialog.okButtonText = "OK";
    dialog.cancelButtonText = "Abort";

    var xfact = new LineEdit;
    xfact.label = "X Factor: ";
    xfact.text = "100";
    dialog.add(xfact);

    var yfact = new LineEdit;
    yfact.label = "Y Factor: ";
    yfact.text = "100";
    dialog.add(yfact);

    if (dialog.exec()) {
        print("Factors: " + xfact.text + "/" + yfact.text);
    }

    var ret = new Vector(xfact.text, yfact.text);

    return ret;
}

// EOF.
---------------------------





A word about autostart.qs

------------------------

Code: Select all

//
// autostart.qs
//
// 1. Place this file into a directory location where QCad can find it
//
//   Replace <USERNAME> with your user login name on your system.
//
//   On a Windows XP system this will be:
//     C:/Documents and Settings/<USERNAME>/.qcad/autostart.qs
//
//   On a Unix/Linux system this will be:
//     /home/<USERNAME>/.qcad/autostart.qs
//
// 2. Restart QCad and check if it loads your autostart.qs file
//   You should see in the command line something like this:
//   Running script 'C:/Documents and Settings/<USERNAME>/.qcad/autostart.qs'
//
// 3. Once this works, place all neede files in your autostart directory
//    For example demo01.qs and demo01.png.
//
// 4. Optionally add customized icons for your script
//    If your sctipt is demo01.qs then the icon will be demo01.png.
//    Just a normal PNG file, 16x16 pixels in size, 72 dpi.
//
// 5. Press the button
//    You should see something like this in the command line:
// 
//    Running script 'C:/Documents and Settings/<USERNAME>/.qcad/demo01.qs'
//
//
// NOTE: You can also load and run this script directly within the
//       scripting IDE for testing purpose.
//
// For more information about this file see:
// http://www.ribbonsoft.com/qcad/manual_scripting/contents.html
//

function main() {

  // -- add a toolbar with some script buttons
  var tbScripts = new ToolBar("Scripts Tool Bar");
  tbScripts.addScriptButton("demo01.qs");   // -- icon demo01.png
  tbScripts.addScriptButton("entities01.qs");
  tbScripts.addScriptButton("layer01.qs");
  tbScripts.addScriptButton("math01.qs");
  tbScripts.addScriptButton("plot01.qs");
  tbScripts.addScriptButton("spiral.qs");

  // -- add a toolbar with some block buttons
  var tbBlocks = new ToolBar("Blocks Tool Bar");
  tbBlocks.addBlockButton("misc/t-part.dxf");
  tbBlocks.addBlockButton("misc/screw.dxf");

}


---------------

Posted: Sun Nov 18, 2007 9:35 am
by pipo
had to wait for the week end to proceed. But Michael already provided a script that works like a charm. Thanks a lot.

Qcad and it's people are Qool !

Posted: Sun Nov 18, 2007 11:35 am
by michael
Thanks! 8) If you have a script that works too, please don't hesitate to publish it here.

Maybe add a dialog box who ask if you want to trance the drawing while read the file ... or even open and import different file formats or whatever.
pipo wrote:had to wait for the week
end to proceed. But Michael already provided a script that works like a charm. Thanks a lot.

Qcad and it's people are Qool !