PMAdjust.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/EAction.js");
  20. include("scripts/simple.js");
  21. include("scripts/File/File.js");
  22. include("scripts/Common/DrawPM.js")
  23. /**
  24. * \ingroup ecma_misc_examples_ioexamples
  25. * \class ExTxtImport
  26. * This action reads an XML file and outputs information to stderr.
  27. * This can be used as starting point for XML importers.
  28. */
  29. function PMAdjust(guiAction) {
  30. EAction.call(this, guiAction);
  31. this.ratio = 1.0;
  32. this.dialog = undefined;
  33. }
  34. PMAdjust.prototype = new EAction();
  35. PMAdjust.includeBasePath = includeBasePath;
  36. PMAdjust.prototype.beginEvent = function () {
  37. try {
  38. EAction.prototype.beginEvent.call(this);
  39. var appWin = EAction.getMainWindow();
  40. if (RSettings.getStringValue("OpenProject/Current", "") == "") {
  41. QMessageBox.critical(appWin, qsTr("提示"), qsTr("请先选择工程路径"));
  42. return;
  43. }
  44. this.dialog = this.createWidget(PMAdjust.includeBasePath + "/PMAdjustDialog.ui", appWin);
  45. // 恢复默认
  46. var resetBtn = this.dialog.findChild("resetBtn");
  47. resetBtn.clicked.connect(this, this.resetClicked);
  48. // 恢复默认
  49. var calBtn = this.dialog.findChild("calBtn");
  50. calBtn.clicked.connect(this, this.calClicked);
  51. // var lastOpenFile = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation());
  52. var lastOpenFileDir = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation()) + "/JD_R_ls.csv";
  53. // var lastOpenFileDir = PMAdjust.includeBasePath + "/../../../project/" + lastOpenFile + "/JD_R_ls.csv";
  54. // EAction.handleUserMessage("lastOpenPmFileDir" + lastOpenFileDir);
  55. this.pmCsvPath = lastOpenFileDir.substring(0, lastOpenFileDir.lastIndexOf(".")) + ".csv";
  56. this.setTable(this.pmCsvPath);
  57. } catch (error) {
  58. EAction.handleUserMessage("Interaction/PMAdjust/PMAdjust.js error: " + error.toString());
  59. }
  60. };
  61. PMAdjust.prototype.setTable = function (fileName) {
  62. var tableWidget = this.dialog.findChild("tableWidget");
  63. tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch);//布局
  64. var file = new QFile(fileName);
  65. file.open(makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text));
  66. var stream = new QTextStream(file);
  67. var row = 0;
  68. while (!stream.atEnd()) {
  69. var line = stream.readLine().trim();
  70. var fields = line.split(",");//按照","分割字符,因为csv文件是以逗号间隔数据的
  71. var rowCount = tableWidget.rowCount;
  72. if (row == 0) {
  73. tableWidget.setHorizontalHeaderLabels(fields);
  74. }
  75. else {
  76. tableWidget.insertRow(rowCount);
  77. for (var i = 0; i < 4; i++) {
  78. tableWidget.setItem(rowCount, i, new QTableWidgetItem(fields[i]));
  79. }
  80. }
  81. row++;
  82. }
  83. if (!this.dialog.exec()) {
  84. EAction.handleUserMessage("clicked~");
  85. destr(this.dialog);
  86. EAction.activateMainWindow();
  87. return undefined;
  88. }
  89. }
  90. PMAdjust.prototype.resetClicked = function () {
  91. fileName = this.pmCsvPath;
  92. var tableWidget = this.dialog.findChild("tableWidget");
  93. var rowNum = tableWidget.rowCount;
  94. EAction.handleUserMessage("rowCount" + rowNum);
  95. for (var i = 0; i < rowNum; i++) {
  96. tableWidget.removeRow(0);
  97. }
  98. var file = new QFile(fileName);
  99. file.open(makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text));
  100. var stream = new QTextStream(file);
  101. var row = 0;
  102. while (!stream.atEnd()) {
  103. var line = stream.readLine().trim();
  104. var fields = line.split(",");//按照","分割字符,因为csv文件是以逗号间隔数据的
  105. var rowCount = tableWidget.rowCount;
  106. if (row == 0) {
  107. tableWidget.setHorizontalHeaderLabels(fields);
  108. }
  109. else {
  110. tableWidget.insertRow(rowCount);
  111. for (var i = 0; i < 4; i++) {
  112. tableWidget.setItem(rowCount, i, new QTableWidgetItem(fields[i]));
  113. }
  114. }
  115. row++;
  116. }
  117. }
  118. PMAdjust.prototype.calClicked = function () {
  119. var tableWidget = this.dialog.findChild("tableWidget");
  120. var lastOpenFileDir = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation());
  121. //首先获取表格内容,先试着保存到csv
  122. var tmpStr = lastOpenFileDir + "//JD_R_ls_aj.csv";
  123. var fileName = tmpStr;//"D:/Project/JD_R_ls_aj.csv";
  124. var file = new QFile(fileName);
  125. file.open(makeQIODeviceOpenMode(QIODevice.ReadWrite, QIODevice.Text));
  126. var stream = new QTextStream(file);
  127. var conTents = "";
  128. // 写入头
  129. var header = tableWidget.horizontalHeader();
  130. if (null != header) {
  131. for (var i = 0; i < header.count(); i++) {
  132. var item = tableWidget.horizontalHeaderItem(i);
  133. conTents += item.text();
  134. if (i < header.count() - 1) {
  135. conTents += ",";
  136. }
  137. }
  138. conTents += "\n";
  139. }
  140. // 写内容
  141. for (var row = 0; row < tableWidget.rowCount; row++) {
  142. for (var column = 0; column < tableWidget.columnCount; column++) {
  143. var item = tableWidget.item(row, column);
  144. var str = item.text();
  145. conTents += str;
  146. if (column < tableWidget.columnCount - 1) {
  147. conTents += ",";
  148. }
  149. }
  150. conTents += "\n";
  151. }
  152. stream.writeString(conTents);
  153. file.close();
  154. //调用
  155. var ok = this.convert(fileName);
  156. }
  157. PMAdjust.prototype.convert = function (filename) {
  158. var process = new QProcess;
  159. EAction.handleUserMessage(PMAdjust.includeBasePath + "/csv2pm.exe" + filename);
  160. process.setProgram(PMAdjust.includeBasePath + "/csv2pm.exe");
  161. var args = [];
  162. // var filename = this.pmCsvPath.substring(0,this.pmCsvPath.lastIndexOf("."))+"_aj.csv";//"D:/Project/JD_R_ls_aj.csv";
  163. args.push(filename);
  164. process.setArguments(args);
  165. process.start();
  166. process.waitForStarted(); //等待程序启动
  167. process.waitForFinished(-1);//等待程序关闭
  168. var fileName = filename.substring(0, filename.lastIndexOf(".")) + ".pm";
  169. // 绘制PM
  170. var filePM = new DrawPM(fileName, this.ratio, 3);
  171. filePM.applyDrawing();
  172. if (true) {
  173. var tmp = RSettings.getStringValue("OpenDisplayFile/PMFile", "");
  174. RSettings.setValue("OpenDisplayFile/PMFile", tmp + "," + fileName);
  175. }
  176. if(this.dialog){
  177. destr(this.dialog);
  178. }
  179. return true;
  180. };