[SOLVED] how to import ASCII file with coordinates ?
Posted: Fri Nov 02, 2007 2:09 pm
is it possible to import a geometry from a (ascii) file with x,y coordinates ?
https://www.ribbonsoft.com/rsforum/
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
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
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
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.
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");
}
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 !