FileDMX.js 2.9 KB

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