ExTool.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. include("scripts/simple.js");
  2. include("scripts/Tools/arguments.js");
  3. function printHelp() {
  4. print("Usage: " + args[1] + " [OPTION]... <drawing file>");
  5. print();
  6. print("This tool does...");
  7. print();
  8. print(" -f, -force Overwrite existing output file");
  9. print(" -h, -help Display this help");
  10. print(" -o, -outfile=FILE Set output file to FILE");
  11. print(" -p, -parameter Some parameter...");
  12. print(" ...");
  13. printGenericUsage();
  14. print();
  15. }
  16. function main() {
  17. if (testArgument(args, "-h", "-help")) {
  18. printHelp();
  19. return;
  20. }
  21. if (args.length < 3) {
  22. print("No input file given. Try -h for help.");
  23. return;
  24. }
  25. var inFile = args[args.length - 1];
  26. if (inFile.indexOf("-") === 0) {
  27. print("No input file. Try -h for help.");
  28. return;
  29. }
  30. var fi = new QFileInfo(inFile);
  31. if (!fi.isAbsolute()) {
  32. inFile = RSettings.getLaunchPath() + QDir.separator + inFile;
  33. fi = new QFileInfo(inFile);
  34. }
  35. var outFile = getArgument(args, "-o", "-outfile");
  36. if (outFile!==undefined) {
  37. outFile = getAbsolutePathForArg(outFile);
  38. }
  39. else {
  40. print("No output file. Try -h for help.");
  41. }
  42. if (new QFileInfo(outFile).exists() && !testArgument(args, "-f", "-force")) {
  43. print("Output file exists already, not overwriting: ", outFile);
  44. print("Use -f to force overwrite");
  45. return;
  46. }
  47. // create new document (off screen):
  48. var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
  49. var di = new RDocumentInterface(doc);
  50. // import given file:
  51. if (di.importFile(inFile) != RDocumentInterface.IoErrorNoError) {
  52. destr(di);
  53. print("Cannot import file:", inFile);
  54. return;
  55. }
  56. // add line to drawing (usine QCAD Simple API):
  57. startTransaction(di);
  58. addLine(0,0, 100,100);
  59. endTransaction()
  60. // export to given file (-o)
  61. if (!di.exportFile(outFile)) {
  62. destr(di);
  63. print("Cannot export file:", outFile);
  64. return;
  65. }
  66. destr(di);
  67. }
  68. if (typeof(including)=='undefined' || including===false) {
  69. main();
  70. }