FileDMX.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. include("scripts/EAction.js");
  2. include("scripts/simple.js");
  3. function FileDMX() {
  4. // 地面线数据
  5. this.newEntityData = new Array();
  6. this.ratio = 10.0;
  7. }
  8. FileDMX.prototype.getArrayData = function () {
  9. return this.newEntityData;
  10. };
  11. FileDMX.prototype.readTxt = function (file) {
  12. var reader = new QFile(file);
  13. var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
  14. if (!reader.open(flags)) {
  15. EAction.handleUserWarning("cannot open file: " + file);
  16. return false;
  17. }
  18. //每次都去文件中的一行,然后输出读取到的字符串
  19. var ts = new QTextStream(reader);
  20. setUtf8Codec(ts);
  21. //当读取出现错误(返回 -1)或者读取到的字符数为 0 时,结束读取
  22. do {
  23. var textLine = ts.readLine().trim();
  24. textLine = textLine.replace(/[\t]+/g, " ").replace(/[\s]+/g, " ");
  25. textLine = textLine.replace(/[ABC]/g, "").replace(/\r\n/g, "");
  26. var fields = textLine.split(" "); // Reserved: ',&[' & ';&['
  27. if (fields.length !== 2) {
  28. // With two extra fields >
  29. continue;
  30. } // End with two extra
  31. qDebug(parseFloat(fields[0]));
  32. qDebug(parseFloat(fields[1]));
  33. this.newEntityData.push([parseFloat(fields[0]) / this.ratio, parseFloat(fields[1])]);
  34. } while (!ts.atEnd());
  35. this.applyOperation();
  36. EAction.handleUserMessage("DMX end");
  37. reader.close();
  38. return true;
  39. };
  40. FileDMX.prototype.applyOperation = function () {
  41. if (this.newEntityData.length == 0) {
  42. EAction.handleUserMessage("DMX data is empty...");
  43. return false;
  44. }
  45. // 添加图层
  46. var layer = new Date().getTime().toString();
  47. addLayerToView(layer, "white", "CONTINUOUS", RLineweight.Weight025, 0);
  48. setCurrentLayer(layer);
  49. var colorReady = new Array(
  50. [new RColor(255, 0, 0), new RColor(160, 32, 240), new RColor(0, 255, 0)],
  51. [new RColor(128, 0, 128), new RColor(200, 96, 255), new RColor(160, 32, 240)],
  52. [new RColor(255, 153, 77), new RColor(128, 255, 255), new RColor(0, 255, 255)],
  53. [new RColor(77, 255, 255), new RColor(255, 255, 128), new RColor(255, 255, 0)],
  54. [new RColor(148, 121, 253), new RColor(255, 192, 128), new RColor(255, 165, 0)]
  55. );
  56. var colorIndex = Math.floor(Math.random() * colorReady.length);
  57. var selectedArray = colorReady[colorIndex];
  58. var PDColor = selectedArray[0];
  59. var SQXColor = selectedArray[1];
  60. var FZXColor = selectedArray[2];
  61. //绘制多线段
  62. for (var i = 0; i < this.newEntityData.length - 1; i++) {
  63. var TempLineData = new Array();
  64. TempLineData.push([this.newEntityData[i][0], this.newEntityData[i][1]]);
  65. TempLineData.push([this.newEntityData[i + 1][0], this.newEntityData[i + 1][1]]);
  66. addPolylinetoView(TempLineData, false, false, 0, PDColor);
  67. }
  68. return true;
  69. };