| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**
- * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
- *
- * This file is part of the QCAD project.
- *
- * QCAD is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * QCAD is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with QCAD.
- */
- include("scripts/EAction.js");
- include("scripts/simple.js");
- include("scripts/File/File.js");
- include("scripts/Common/DrawPM.js")
- /**
- * \ingroup ecma_misc_examples_ioexamples
- * \class ExTxtImport
- * This action reads an XML file and outputs information to stderr.
- * This can be used as starting point for XML importers.
- */
- function PMAdjust(guiAction) {
- EAction.call(this, guiAction);
- this.ratio = 1.0;
- this.dialog = undefined;
- }
- PMAdjust.prototype = new EAction();
- PMAdjust.includeBasePath = includeBasePath;
- PMAdjust.prototype.beginEvent = function () {
- try {
- EAction.prototype.beginEvent.call(this);
- var appWin = EAction.getMainWindow();
- if (RSettings.getStringValue("OpenProject/Current", "") == "") {
- QMessageBox.critical(appWin, qsTr("提示"), qsTr("请先选择工程路径"));
- return;
- }
- this.dialog = this.createWidget(PMAdjust.includeBasePath + "/PMAdjustDialog.ui", appWin);
- // 恢复默认
- var resetBtn = this.dialog.findChild("resetBtn");
- resetBtn.clicked.connect(this, this.resetClicked);
- // 恢复默认
- var calBtn = this.dialog.findChild("calBtn");
- calBtn.clicked.connect(this, this.calClicked);
- // var lastOpenFile = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation());
- var lastOpenFileDir = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation()) + "/JD_R_ls.csv";
- // var lastOpenFileDir = PMAdjust.includeBasePath + "/../../../project/" + lastOpenFile + "/JD_R_ls.csv";
- // EAction.handleUserMessage("lastOpenPmFileDir" + lastOpenFileDir);
- this.pmCsvPath = lastOpenFileDir.substring(0, lastOpenFileDir.lastIndexOf(".")) + ".csv";
- this.setTable(this.pmCsvPath);
- } catch (error) {
- EAction.handleUserMessage("Interaction/PMAdjust/PMAdjust.js error: " + error.toString());
- }
- };
- PMAdjust.prototype.setTable = function (fileName) {
- var tableWidget = this.dialog.findChild("tableWidget");
- tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch);//布局
- var file = new QFile(fileName);
- file.open(makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text));
- var stream = new QTextStream(file);
- var row = 0;
- while (!stream.atEnd()) {
- var line = stream.readLine().trim();
- var fields = line.split(",");//按照","分割字符,因为csv文件是以逗号间隔数据的
- var rowCount = tableWidget.rowCount;
- if (row == 0) {
- tableWidget.setHorizontalHeaderLabels(fields);
- }
- else {
- tableWidget.insertRow(rowCount);
- for (var i = 0; i < 4; i++) {
- tableWidget.setItem(rowCount, i, new QTableWidgetItem(fields[i]));
- }
- }
- row++;
- }
- if (!this.dialog.exec()) {
- EAction.handleUserMessage("clicked~");
- destr(this.dialog);
- EAction.activateMainWindow();
- return undefined;
- }
- }
- PMAdjust.prototype.resetClicked = function () {
- fileName = this.pmCsvPath;
- var tableWidget = this.dialog.findChild("tableWidget");
- var rowNum = tableWidget.rowCount;
- EAction.handleUserMessage("rowCount" + rowNum);
- for (var i = 0; i < rowNum; i++) {
- tableWidget.removeRow(0);
- }
- var file = new QFile(fileName);
- file.open(makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text));
- var stream = new QTextStream(file);
- var row = 0;
- while (!stream.atEnd()) {
- var line = stream.readLine().trim();
- var fields = line.split(",");//按照","分割字符,因为csv文件是以逗号间隔数据的
- var rowCount = tableWidget.rowCount;
- if (row == 0) {
- tableWidget.setHorizontalHeaderLabels(fields);
- }
- else {
- tableWidget.insertRow(rowCount);
- for (var i = 0; i < 4; i++) {
- tableWidget.setItem(rowCount, i, new QTableWidgetItem(fields[i]));
- }
- }
- row++;
- }
- }
- PMAdjust.prototype.calClicked = function () {
- var tableWidget = this.dialog.findChild("tableWidget");
- var lastOpenFileDir = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation());
- //首先获取表格内容,先试着保存到csv
- var tmpStr = lastOpenFileDir + "//JD_R_ls_aj.csv";
- var fileName = tmpStr;//"D:/Project/JD_R_ls_aj.csv";
- var file = new QFile(fileName);
- file.open(makeQIODeviceOpenMode(QIODevice.ReadWrite, QIODevice.Text));
- var stream = new QTextStream(file);
- var conTents = "";
- // 写入头
- var header = tableWidget.horizontalHeader();
- if (null != header) {
- for (var i = 0; i < header.count(); i++) {
- var item = tableWidget.horizontalHeaderItem(i);
- conTents += item.text();
- if (i < header.count() - 1) {
- conTents += ",";
- }
- }
- conTents += "\n";
- }
- // 写内容
- for (var row = 0; row < tableWidget.rowCount; row++) {
- for (var column = 0; column < tableWidget.columnCount; column++) {
- var item = tableWidget.item(row, column);
- var str = item.text();
- conTents += str;
- if (column < tableWidget.columnCount - 1) {
- conTents += ",";
- }
- }
- conTents += "\n";
- }
- stream.writeString(conTents);
- file.close();
- //调用
- var ok = this.convert(fileName);
- }
- PMAdjust.prototype.convert = function (filename) {
- var process = new QProcess;
- EAction.handleUserMessage(PMAdjust.includeBasePath + "/csv2pm.exe" + filename);
- process.setProgram(PMAdjust.includeBasePath + "/csv2pm.exe");
- var args = [];
- // var filename = this.pmCsvPath.substring(0,this.pmCsvPath.lastIndexOf("."))+"_aj.csv";//"D:/Project/JD_R_ls_aj.csv";
- args.push(filename);
- process.setArguments(args);
- process.start();
- process.waitForStarted(); //等待程序启动
- process.waitForFinished(-1);//等待程序关闭
- var fileName = filename.substring(0, filename.lastIndexOf(".")) + ".pm";
- // 绘制PM
- var filePM = new DrawPM(fileName, this.ratio, 3);
- filePM.applyDrawing();
- if (true) {
- var tmp = RSettings.getStringValue("OpenDisplayFile/PMFile", "");
- RSettings.setValue("OpenDisplayFile/PMFile", tmp + "," + fileName);
- }
- if(this.dialog){
- destr(this.dialog);
- }
- return true;
- };
|