ZDMDialogInput.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/Misc/MiscIO/MiscIO.js");
  22. //include("scripts/File/File.js");
  23. /**
  24. * SAX style XML handler.
  25. */
  26. /**
  27. * \ingroup ecma_misc_examples_ioexamples
  28. * \class ExTxtImport
  29. * This action reads an XML file and outputs information to stderr.
  30. * This can be used as starting point for XML importers.
  31. */
  32. function ZDMDialogInput(guiAction) {
  33. EAction.call(this, guiAction);
  34. this.row = 0;
  35. this.newEntityData = new Array();
  36. this.newFZEntityData = new Array();
  37. this.newJPointData = new Array();
  38. this.csvCoor = new Array();
  39. this.allR = new Array();
  40. this.ratio = 10.0;
  41. // this.setUiOptions("ZDMDialogInput.ui");
  42. }
  43. ZDMDialogInput.prototype = new EAction();
  44. ZDMDialogInput.includeBasePath = includeBasePath;
  45. ZDMDialogInput.prototype.beginEvent = function() {
  46. EAction.prototype.beginEvent.call(this);
  47. var appWin = EAction.getMainWindow();
  48. this.dialog = this.createWidget(ZDMDialogInput.includeBasePath + "/ZDMDialogInput.ui", appWin);
  49. var tableWidget = this.dialog.findChild("tableWidget");
  50. tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch);//布局
  51. tableWidget.setHorizontalHeaderLabels(["K", "H", "R"]);
  52. tableWidget.cellPressed.connect(this, this.selectRow);
  53. // 添加一行
  54. var addBtn = this.dialog.findChild("addBtn");
  55. addBtn.clicked.connect(this, this.addClicked);
  56. // 删除一行
  57. var delBtn = this.dialog.findChild("delBtn");
  58. delBtn.clicked.connect(this, this.delClicked);
  59. // 计算绘图
  60. var calBtn = this.dialog.findChild("calBtn");
  61. calBtn.clicked.connect(this, this.calClicked);
  62. if (!this.dialog.exec()) {
  63. EAction.handleUserMessage("clicked~");
  64. destr(this.dialog);
  65. EAction.activateMainWindow();
  66. return undefined;
  67. }
  68. };
  69. // 添加行
  70. ZDMDialogInput.prototype.addClicked = function(){
  71. var tableWidget = this.dialog.findChild("tableWidget");
  72. tableWidget.insertRow(tableWidget.rowCount);
  73. };
  74. // 删除行
  75. ZDMDialogInput.prototype.delClicked = function(){
  76. var tableWidget = this.dialog.findChild("tableWidget");
  77. if (this.curRow == -1) {
  78. // EAction.handleUserMessage("111~");
  79. return;
  80. }else{
  81. tableWidget.model().removeRow(this.curRow);
  82. }
  83. };
  84. // 选择行
  85. ZDMDialogInput.prototype.selectRow = function(currentRow, curCol){
  86. this.curRow = currentRow;
  87. }
  88. // 计算绘图
  89. ZDMDialogInput.prototype.calClicked = function(){
  90. //首先获取表格内容,先试着保存到csv
  91. var tmpStr = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation()) + "/k_E_R_input.csv";
  92. var fileName = tmpStr;
  93. var file = new QFile(fileName);
  94. file.open(makeQIODeviceOpenMode(QIODevice.ReadWrite, QIODevice.Text));
  95. var stream = new QTextStream(file);
  96. var conTents="";
  97. // 写入头
  98. var tableWidget = this.dialog.findChild("tableWidget");
  99. var header = tableWidget.horizontalHeader();
  100. if (null != header)
  101. {
  102. for (var i = 0; i < header.count(); i++)
  103. {
  104. var item = tableWidget.horizontalHeaderItem(i);
  105. conTents += item.text();
  106. if(i < header.count() - 1){
  107. conTents += ",";
  108. }
  109. }
  110. conTents += "\n";
  111. }
  112. // 写内容
  113. for (var row = 0; row < tableWidget.rowCount; row++)
  114. {
  115. for (var column = 0; column < tableWidget.columnCount; column++)
  116. {
  117. var item = tableWidget.item(row, column);
  118. var str = item.text();
  119. conTents += str;
  120. if(column < tableWidget.columnCount - 1){
  121. conTents += ",";
  122. }
  123. }
  124. conTents += "\n";
  125. }
  126. stream.writeString(conTents);
  127. file.close();
  128. //调用
  129. var ok = this.convert(fileName);
  130. }
  131. ZDMDialogInput.prototype.convert = function(filename) {
  132. var process = new QProcess;
  133. EAction.handleUserMessage(ZDMDialogInput.includeBasePath + "/csv2zdm.exe");
  134. process.setProgram(ZDMDialogInput.includeBasePath + "/csv2zdm.exe");
  135. var args = [];
  136. args.push(filename);
  137. process.setArguments(args);
  138. process.start();
  139. process.waitForStarted(); //等待程序启动
  140. process.waitForFinished(-1);//等待程序关闭
  141. this.sFile = filename.substring(0, filename.lastIndexOf(".")) + ".zdm";
  142. var ok = this.readTxt(filename.substring(0, filename.lastIndexOf(".")) + ".zdm");
  143. if(ok){
  144. var tmp = RSettings.getStringValue("OpenDisplayFile/ZDMFile", "");
  145. RSettings.setValue("OpenDisplayFile/ZDMFile", tmp + "," + this.sFile);
  146. }
  147. return true;
  148. };
  149. ZDMDialogInput.prototype.applyOperation = function (preview) {
  150. // 添加图层
  151. addLayerToView(this.sFile, "white", "CONTINUOUS", RLineweight.Weight025,0);
  152. setCurrentLayer(this.sFile);
  153. var colorReady = new Array(
  154. [
  155. new RColor(255, 0, 0),
  156. new RColor(160, 32, 240),
  157. new RColor(0, 255, 0),
  158. ]
  159. ,
  160. [
  161. new RColor(128, 0, 128),
  162. new RColor(200, 96, 255),
  163. new RColor(160, 32, 240),
  164. ],
  165. [
  166. new RColor(255, 153, 77),
  167. new RColor(128, 255, 255),
  168. new RColor(0, 255, 255),
  169. ],
  170. [
  171. new RColor(77, 255, 255),
  172. new RColor(255, 255, 128),
  173. new RColor(255, 255, 0),
  174. ],
  175. [
  176. new RColor(148, 121, 253),
  177. new RColor(255, 192, 128),
  178. new RColor(255, 165, 0),
  179. ]
  180. );
  181. var colorIndex = Math.floor(Math.random() * colorReady.length);
  182. var selectedArray = colorReady[colorIndex];
  183. var PDColor = selectedArray[0];
  184. var SQXColor = selectedArray[1];
  185. var FZXColor = selectedArray[2];
  186. //绘制坡道及竖曲线,1为坡道,2为竖曲线
  187. for (var i = 0; i < this.newEntityData.length-1; i++) {
  188. var TempLineData = new Array();
  189. TempLineData.push([this.newEntityData[i][0], this.newEntityData[i][1]]);
  190. TempLineData.push([this.newEntityData[i+1][0], this.newEntityData[i+1][1]]);
  191. if (this.newEntityData[i][2] == 1) {
  192. addPolylinetoView(TempLineData, false, false, 0, PDColor);
  193. // 计算斜率
  194. var pdAngle = (this.newEntityData[i+1][1]-this.newEntityData[i][1])/(this.newEntityData[i+1][0]-this.newEntityData[i][0]);
  195. // 计算长度
  196. var pdLen = sqrt((this.newEntityData[i+1][1]-this.newEntityData[i][1])*(this.newEntityData[i+1][1]-this.newEntityData[i][1])+(this.newEntityData[i+1][0]-this.newEntityData[i][0])*(this.newEntityData[i+1][0]-this.newEntityData[i][0]));
  197. addSimpleTextToView((round(pdAngle*1000)/1000).toString(),[(this.newEntityData[i+1][0]+this.newEntityData[i][0])/2-2, (this.newEntityData[i+1][1]+this.newEntityData[i][1])/2+9],
  198. 7,rad2deg(pdAngle),"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  199. addSimpleTextToView((round(pdLen*1000)/1000).toString(),[(this.newEntityData[i+1][0]+this.newEntityData[i][0])/2-2, (this.newEntityData[i+1][1]+this.newEntityData[i][1])/2-2],
  200. 7,rad2deg(pdAngle),"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  201. }
  202. else if (this.newEntityData[i][2] == 2){
  203. addPolylinetoView(TempLineData, false, false, 0, SQXColor);
  204. }
  205. }
  206. var min_y = this.newEntityData[0][1];
  207. // 绘制竖曲线
  208. if (this.newEntityData.length > 0) {
  209. for(var i = 0; i < this.newEntityData.length;i++){
  210. if(min_y > this.newEntityData[i][1]){
  211. min_y = this.newEntityData[i][1];
  212. }
  213. }
  214. var firstItem = this.newEntityData[0];
  215. var lastItem = this.newEntityData[this.newEntityData.length - 1];
  216. addLineToView([firstItem[0] - 100, min_y - 50], [lastItem[0], min_y - 50], 0);//横坐标
  217. addLineToView([firstItem[0], min_y - 110], [firstItem[0], min_y + 150], 0); //纵坐标
  218. addLineToView([firstItem[0] - 100, min_y - 110], [lastItem[0], min_y - 110], 0); //格线
  219. addSimpleTextToView(qsTr("R"),[firstItem[0] - 80, min_y - 70],
  220. 12,0,"Standard",RS.VAlignTop,RS.HAlignLeft,true,false,0);
  221. }
  222. //绘制辅助线,1为竖曲线切线起点,2为变坡点,3为竖曲线切线终点
  223. for (var i = 0; i < this.newFZEntityData.length; ) {
  224. var TempLineData = new Array();
  225. if (i < this.newFZEntityData.length && this.newFZEntityData[i][2] == 1) {
  226. TempLineData.push([this.newFZEntityData[i][0], this.newFZEntityData[i][1]]);
  227. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]-1.5],[this.newFZEntityData[i][0], this.newFZEntityData[i][1]+1.5],0);
  228. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]-1.5],[this.newFZEntityData[i][0]+0.5, this.newFZEntityData[i][1]-1.5],0);
  229. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]+1.5],[this.newFZEntityData[i][0]+0.5, this.newFZEntityData[i][1]+1.5],0);
  230. }
  231. if (i + 1 < this.newFZEntityData.length && this.newFZEntityData[i+1][2] == 2) {
  232. TempLineData.push([this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1]]);
  233. addCircleToView(this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1], 0.5,0, FZXColor);
  234. addLineToView([this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1]+1.5],[this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1]+50],0);
  235. addSimpleTextToView("K"+(round(this.newFZEntityData[i + 1][0]*1000*this.ratio)/1000).toString(),[this.newFZEntityData[i + 1][0]-9, this.newFZEntityData[i + 1][1]+2.5],7,90,"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  236. addSimpleTextToView(this.newFZEntityData[i + 1][1].toString(),[this.newFZEntityData[i + 1][0]+2, this.newFZEntityData[i + 1][1]+3],7,90,"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  237. // EAction.handleUserMessage("start point end point:" + this.newFZEntityData[i + 1][0] + "," + (this.newFZEntityData[i + 1][1]+1)+";"+this.newFZEntityData[i + 1][0]+","+ (this.newFZEntityData[i + 1][1]+10));
  238. }
  239. if (i + 2 < this.newFZEntityData.length && this.newFZEntityData[i + 2][2] == 3) {
  240. TempLineData.push([this.newFZEntityData[i + 2][0], this.newFZEntityData[i + 2][1]]);
  241. addLineToView([this.newFZEntityData[i+2][0], this.newFZEntityData[i+2][1]-1.5],[this.newFZEntityData[i + 2][0], this.newFZEntityData[i + 2][1]+1.5],0);
  242. addLineToView([this.newFZEntityData[i+2][0]-0.5, this.newFZEntityData[i+2][1]-1.5],[this.newFZEntityData[i + 2][0], this.newFZEntityData[i + 2][1]-1.5],0);
  243. addLineToView([this.newFZEntityData[i+2][0]-0.5, this.newFZEntityData[i+2][1]+1.5],[this.newFZEntityData[i + 2][0], this.newFZEntityData[i + 2][1]+1.5],0);
  244. addSimpleTextToView("R-" + this.allR[i%3].toString(), [this.newFZEntityData[i][0] , min_y - 70], 12,0,"Standard",RS.VAlignTop,RS.HAlignLeft,true,false,0);
  245. addLineToView([this.newFZEntityData[i][0], min_y - 90],[this.newFZEntityData[i + 2][0], min_y - 90],0);
  246. }
  247. addPolylinetoView(TempLineData, false, false, 0, FZXColor);
  248. i = i + 3;
  249. }
  250. EAction.handleUserMessage("load success!");
  251. var di = getAssignDocumentInterface(0);
  252. if (!isNull(di)) {
  253. // di.regenerateScenes();
  254. di.autoZoom();
  255. }
  256. return true;
  257. };
  258. ZDMDialogInput.prototype.readTxt = function(file) {
  259. var reader = new QFile(file);
  260. var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
  261. if (!reader.open(flags)) {
  262. EAction.handleUserWarning("cannot open file: " + file);
  263. return;
  264. }
  265. //每次都去文件中的一行,然后输出读取到的字符串
  266. var ts = new QTextStream(reader);
  267. setUtf8Codec(ts);
  268. var TempEntityData = new Array();
  269. //当读取出现错误(返回 -1)或者读取到的字符数为 0 时,结束读取
  270. do {
  271. var textLine = ts.readLine().trim();
  272. // EAction.handleUserWarning(textLine);
  273. textLine= textLine.replace(/[\t]+/g, " ").replace(/[\s]+/g, " ");
  274. textLine = textLine.replace(/[ABC]/g, "").replace(/\r\n/g, "");
  275. // EAction.handleUserMessage(textLine2);
  276. var fields = textLine.split(" "); // Reserved: ',&[' & ';&['
  277. if (fields.length !== 5) { // With two extra fields >
  278. continue;
  279. } // End with two extra
  280. qDebug(parseFloat(fields[0]));
  281. qDebug(parseFloat(fields[1]));
  282. qDebug(parseFloat(fields[2]));
  283. qDebug(parseFloat(this.ratio));
  284. TempEntityData.push([parseFloat(fields[0]), parseFloat(fields[1]), parseFloat(fields[2])]);
  285. this.csvCoor.push([parseFloat(fields[0]), parseFloat(fields[1]), parseFloat(fields[2])]);
  286. }
  287. while (!ts.atEnd());
  288. var CurRampZH = 0.0;
  289. var CurRampBG = 0.0;
  290. var CurRampR = 0.0;
  291. var PreRampZH = 0.0;
  292. var PreRampBG = 0.0;
  293. var PreRampR = 0.0;
  294. var NextRampZH = 0.0;
  295. var NextRampBG = 0.0;
  296. var NextRampR = 0.0;
  297. var CurLineType = 1;
  298. var DeltaS = 10.0;//竖曲线取样点间隔
  299. for (var i = 0; i < TempEntityData.length; i++) {
  300. CurRampZH = TempEntityData[i][0];
  301. CurRampBG = TempEntityData[i][1];
  302. CurRampR = TempEntityData[i][2];
  303. if (CurRampR < 1.0 || i == (TempEntityData.length - 1) || i == 0) {
  304. CurLineType = 1;
  305. this.newEntityData.push([CurRampZH / parseFloat(this.ratio), CurRampBG, CurLineType]);
  306. }
  307. else {
  308. var i_1 = 0.0;//前坡坡度 lyz
  309. var i_2 = 0.0;//后坡坡度 lyz
  310. var CurL = 0.0;//竖曲线长 lyz
  311. var CurT = 0.0;//竖曲线切线长 lyz
  312. var CurE = 0.0;//竖曲线外距 lyz
  313. var CurQD = 0.0;//竖曲线起点桩号 lyz
  314. var CurZD = 0.0;//竖曲线终点桩号 lyz
  315. PreRampZH = TempEntityData[i - 1][0];
  316. PreRampBG = TempEntityData[i - 1][1];
  317. PreRampR = TempEntityData[i - 1][2];
  318. NextRampZH = TempEntityData[i + 1][0];
  319. NextRampBG = TempEntityData[i + 1][1];
  320. NextRampR = TempEntityData[i + 1][2];
  321. i_1 = (CurRampBG - PreRampBG) / (CurRampZH - PreRampZH);
  322. i_2 =(NextRampBG - CurRampBG) / (NextRampZH - CurRampZH);
  323. CurL = CurRampR * Math.abs(i_2 - i_1);
  324. CurT = CurL / 2;
  325. CurE = (CurT * CurT) / (2 * CurRampR);
  326. CurQD = CurRampZH - CurT;
  327. CurZD = CurRampZH + CurT;
  328. // EAction.handleUserMessage("CurL: " + CurL.toString());
  329. for (var j = CurQD; j < CurZD; j = j + DeltaS) {
  330. var jQBG = 0.0; //j点切线标高 lyz
  331. var jE = 0.0; //j点外距 lyz
  332. var jBG = 0.0; //j点标高 lyz
  333. if (j <= CurRampZH) {
  334. jQBG = CurRampBG - (CurRampZH - j) * i_1;
  335. jE = (j - CurQD) * (j - CurQD) / (2 * CurRampR);
  336. if ((i_1 - i_2) >= 0) {//确定是凸曲线还是凹曲线 lyz
  337. jBG = jQBG - jE;
  338. }
  339. else {
  340. jBG = jQBG + jE;
  341. }
  342. }
  343. else {
  344. jQBG = CurRampBG - (CurRampZH - j) * i_2;
  345. jE = (CurZD - j) * (CurZD - j) / (2 * CurRampR);
  346. if ((i_1 - i_2) >= 0) {//确定是凸曲线还是凹曲线 lyz
  347. jBG = jQBG - jE;
  348. }
  349. else {
  350. jBG = jQBG + jE;
  351. }
  352. }
  353. if (j + DeltaS >= CurZD) {
  354. CurLineType = 1;
  355. }
  356. else CurLineType = 2;
  357. this.newEntityData.push([j / parseFloat(this.ratio), jBG, CurLineType]);
  358. //插入辅助线数据
  359. if (j == CurQD) {
  360. this.newFZEntityData.push([j / parseFloat(this.ratio), jBG, 1]);
  361. this.newFZEntityData.push([CurRampZH / parseFloat(this.ratio), CurRampBG, 2]);
  362. }
  363. if (j + DeltaS >= CurZD){
  364. this.newFZEntityData.push([j / parseFloat(this.ratio), jBG, 3]);
  365. }
  366. this.allR.push(CurRampR);
  367. }
  368. }
  369. }
  370. this.applyOperation();
  371. reader.close();
  372. return true;
  373. };