ZDM.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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("../DMXImport/DMXImport.js")
  22. function ZDM() {
  23. this.newEntityData = new Array();
  24. this.newFZEntityData = new Array();
  25. this.newJPointData = new Array();
  26. this.csvCoor = new Array();
  27. this.allR = new Array();
  28. this.ratio = 10.0;
  29. }
  30. ZDM.includeBasePath = includeBasePath;
  31. ZDM.prototype.interface = function(fileName){
  32. this.fileName = fileName.substring(0,fileName.lastIndexOf("."))+".csv";
  33. var ok = this.convert(this.fileName);
  34. this.cvFile = this.fileName.substring(0,this.fileName.lastIndexOf("."))+"_op.zdm";
  35. var ok = this.readTxt(this.cvFile);
  36. if(ok){
  37. var tmp = RSettings.getStringValue("OpenDisplayFile/ZDMFile", "");
  38. RSettings.setValue("OpenDisplayFile/ZDMFile", tmp + "," + this.cvFile);
  39. var dmx = new DMXImport();
  40. dmx.interface(fileName);
  41. }
  42. }
  43. ZDM.prototype.convert = function (fileName) {
  44. var process = new QProcess;
  45. // 后台运行
  46. // process.start(ZDM.includeBasePath + "/railway_vertical_sw_V1-background.exe -f " + fileName + " -e " + ZDM.includeBasePath);
  47. // 阻塞运行
  48. EAction.handleUserMessage(ZDM.includeBasePath + "/railway_vertical_sw_V1.exe -f " + fileName + " -e " + ZDM.includeBasePath);
  49. process.setProgram(ZDM.includeBasePath + "/railway_vertical_sw_V1.exe");
  50. var args = [];
  51. var input1 = " -f ";
  52. args.push(input1);
  53. var input2 = fileName;
  54. args.push(input2);
  55. var input3 = " -e ";
  56. args.push(input3);
  57. var input4 = ZDM.includeBasePath;
  58. args.push(input4);
  59. process.setArguments(args);
  60. process.start();
  61. process.waitForStarted(); //等待程序启动
  62. process.waitForFinished(-1);//等待程序关闭
  63. return true;
  64. };
  65. ZDM.prototype.applyOperation = function (preview) {
  66. // 添加图层
  67. addLayerToView(this.cvFile, "white", "CONTINUOUS", RLineweight.Weight025,0);
  68. setCurrentLayer(this.cvFile);
  69. var colorReady = new Array(
  70. [
  71. new RColor(255, 0, 0),
  72. new RColor(160, 32, 240),
  73. new RColor(0, 255, 0),
  74. ]
  75. // ,
  76. // [
  77. // new RColor(128, 0, 128),
  78. // new RColor(200, 96, 255),
  79. // new RColor(160, 32, 240),
  80. // ],
  81. // [
  82. // new RColor(255, 153, 77),
  83. // new RColor(128, 255, 255),
  84. // new RColor(0, 255, 255),
  85. // ],
  86. // [
  87. // new RColor(77, 255, 255),
  88. // new RColor(255, 255, 128),
  89. // new RColor(255, 255, 0),
  90. // ],
  91. // [
  92. // new RColor(148, 121, 253),
  93. // new RColor(255, 192, 128),
  94. // new RColor(255, 165, 0),
  95. // ]
  96. );
  97. var colorIndex = Math.floor(Math.random() * colorReady.length);
  98. var selectedArray = colorReady[colorIndex];
  99. var PDColor = selectedArray[0];
  100. var SQXColor = selectedArray[1];
  101. var FZXColor = selectedArray[2];
  102. // // 添加纵横比1:10
  103. // addLineToView([this.newEntityData[0][0]-100, this.newEntityData[0][1]],[this.newEntityData[0][0]-20, this.newEntityData[0][1]],0);
  104. // addLineToView([this.newEntityData[0][0]-100, this.newEntityData[0][1]],[this.newEntityData[0][0]-100, this.newEntityData[0][1]+15],0);
  105. // addLineToView([this.newEntityData[0][0]-20, this.newEntityData[0][1]],[this.newEntityData[0][0]-20, this.newEntityData[0][1]+15],0);
  106. // addSimpleTextToView(("ratio 1:10"),[this.newEntityData[0][0]-80, this.newEntityData[0][1]+10],
  107. // 7,0,"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  108. //绘制坡道及竖曲线,1为坡道,2为竖曲线
  109. for (var i = 0; i < this.newEntityData.length-1; i++) {
  110. var TempLineData = new Array();
  111. TempLineData.push([this.newEntityData[i][0], this.newEntityData[i][1]]);
  112. TempLineData.push([this.newEntityData[i+1][0], this.newEntityData[i+1][1]]);
  113. if (this.newEntityData[i][2] == 1) {
  114. addPolylinetoView(TempLineData, false, false, 0, PDColor);
  115. // 计算斜率
  116. var pdAngle = (this.newEntityData[i+1][1]-this.newEntityData[i][1])/(this.newEntityData[i+1][0]-this.newEntityData[i][0]);
  117. // 计算长度
  118. 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]));
  119. 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],
  120. 7,rad2deg(pdAngle),"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  121. 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],
  122. 7,rad2deg(pdAngle),"Standard",RS.VAlignTop,RS.HAlignLeft,false,false,0);
  123. }
  124. else if (this.newEntityData[i][2] == 2){
  125. addPolylinetoView(TempLineData, false, false, 0, SQXColor);
  126. }
  127. }
  128. var min_y = this.newEntityData[0][1];
  129. // 绘制竖曲线
  130. if (this.newEntityData.length > 0) {
  131. for(var i = 0; i < this.newEntityData.length;i++){
  132. if(min_y > this.newEntityData[i][1]){
  133. min_y = this.newEntityData[i][1];
  134. }
  135. }
  136. var firstItem = this.newEntityData[0];
  137. var lastItem = this.newEntityData[this.newEntityData.length - 1];
  138. addLineToView([firstItem[0] - 100, min_y - 50], [lastItem[0], min_y - 50], 0);//横坐标
  139. addLineToView([firstItem[0], min_y - 110], [firstItem[0], min_y + 150], 0); //纵坐标
  140. addLineToView([firstItem[0] - 100, min_y - 110], [lastItem[0], min_y - 110], 0); //格线
  141. addSimpleTextToView(qsTr("R"),[firstItem[0] - 80, min_y - 70],
  142. 12,0,"Standard",RS.VAlignTop,RS.HAlignLeft,true,false,0);
  143. }
  144. //绘制辅助线,1为竖曲线切线起点,2为变坡点,3为竖曲线切线终点
  145. for (var i = 0; i < this.newFZEntityData.length; ) {
  146. var TempLineData = new Array();
  147. if (i < this.newFZEntityData.length && this.newFZEntityData[i][2] == 1) {
  148. TempLineData.push([this.newFZEntityData[i][0], this.newFZEntityData[i][1]]);
  149. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]-1.5],[this.newFZEntityData[i][0], this.newFZEntityData[i][1]+1.5],0);
  150. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]-1.5],[this.newFZEntityData[i][0]+0.5, this.newFZEntityData[i][1]-1.5],0);
  151. addLineToView([this.newFZEntityData[i][0], this.newFZEntityData[i][1]+1.5],[this.newFZEntityData[i][0]+0.5, this.newFZEntityData[i][1]+1.5],0);
  152. }
  153. if (i + 1 < this.newFZEntityData.length && this.newFZEntityData[i+1][2] == 2) {
  154. TempLineData.push([this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1]]);
  155. addCircleToView(this.newFZEntityData[i + 1][0], this.newFZEntityData[i + 1][1], 0.5,0, FZXColor);
  156. 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);
  157. 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);
  158. 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);
  159. // 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));
  160. }
  161. if (i + 2 < this.newFZEntityData.length && this.newFZEntityData[i + 2][2] == 3) {
  162. TempLineData.push([this.newFZEntityData[i + 2][0], this.newFZEntityData[i + 2][1]]);
  163. 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);
  164. 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);
  165. 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);
  166. addSimpleTextToView("R-" + this.allR[i%3].toString(), [this.newFZEntityData[i][0] , min_y - 70], 12,0,"Standard",RS.VAlignTop,RS.HAlignLeft,true,false,0);
  167. addLineToView([this.newFZEntityData[i][0], min_y - 90],[this.newFZEntityData[i + 2][0], min_y - 90],0);
  168. }
  169. addPolylinetoView(TempLineData, false, false, 0, FZXColor);
  170. i = i + 3;
  171. }
  172. EAction.handleUserMessage("save csv file start!");
  173. var fname = this.fileName.substring(0,this.fileName.lastIndexOf("."))+"_op.csv";
  174. var file = new QFile(fname);
  175. if ( file.exists())
  176. {
  177. //如果文件存在执行的操作,此处为空,因为文件不可能存在
  178. }
  179. file.open( makeQIODeviceOpenMode(QIODevice.WriteOnly, QIODevice.Text));
  180. var out = new QTextStream(file);
  181. //写表头
  182. out.writeString("K,H,R\n");
  183. //写数据
  184. for(i = 0;i < this.csvCoor.length; i++)
  185. {
  186. out.writeString(this.csvCoor[i][0]+","+this.csvCoor[i][1]+","+this.csvCoor[i][2]+"\n");
  187. }
  188. out.flush();
  189. //写完数据需要关闭文件
  190. file.close();
  191. EAction.handleUserMessage("save csv file success!");
  192. var di = getAssignDocumentInterface(0);
  193. if (!isNull(di)) {
  194. // di.regenerateScenes();
  195. di.autoZoom();
  196. }
  197. return true;
  198. };
  199. ZDM.prototype.readTxt = function(file) {
  200. var reader = new QFile(file);
  201. var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text);
  202. if (!reader.open(flags)) {
  203. EAction.handleUserWarning("cannot open file: " + file);
  204. return;
  205. }
  206. //每次都去文件中的一行,然后输出读取到的字符串
  207. var ts = new QTextStream(reader);
  208. setUtf8Codec(ts);
  209. var TempEntityData = new Array();
  210. //当读取出现错误(返回 -1)或者读取到的字符数为 0 时,结束读取
  211. do {
  212. var textLine = ts.readLine().trim();
  213. // EAction.handleUserWarning(textLine);
  214. textLine= textLine.replace(/[\t]+/g, " ").replace(/[\s]+/g, " ");
  215. textLine = textLine.replace(/[ABC]/g, "").replace(/\r\n/g, "");
  216. // EAction.handleUserMessage(textLine2);
  217. var fields = textLine.split(" "); // Reserved: ',&[' & ';&['
  218. if (fields.length !== 5) { // With two extra fields >
  219. continue;
  220. } // End with two extra
  221. qDebug(parseFloat(fields[0]));
  222. qDebug(parseFloat(fields[1]));
  223. qDebug(parseFloat(fields[2]));
  224. qDebug(parseFloat(this.ratio));
  225. TempEntityData.push([parseFloat(fields[0]), parseFloat(fields[1]), parseFloat(fields[2])]);
  226. this.csvCoor.push([parseFloat(fields[0]), parseFloat(fields[1]), parseFloat(fields[2])]);
  227. }
  228. while (!ts.atEnd());
  229. var CurRampZH = 0.0;
  230. var CurRampBG = 0.0;
  231. var CurRampR = 0.0;
  232. var PreRampZH = 0.0;
  233. var PreRampBG = 0.0;
  234. var PreRampR = 0.0;
  235. var NextRampZH = 0.0;
  236. var NextRampBG = 0.0;
  237. var NextRampR = 0.0;
  238. var CurLineType = 1;
  239. var DeltaS = 10.0;//竖曲线取样点间隔
  240. for (var i = 0; i < TempEntityData.length; i++) {
  241. CurRampZH = TempEntityData[i][0];
  242. CurRampBG = TempEntityData[i][1];
  243. CurRampR = TempEntityData[i][2];
  244. if (CurRampR < 1.0 || i == (TempEntityData.length - 1) || i == 0) {
  245. CurLineType = 1;
  246. this.newEntityData.push([CurRampZH / parseFloat(this.ratio), CurRampBG, CurLineType]);
  247. }
  248. else {
  249. var i_1 = 0.0;//前坡坡度 lyz
  250. var i_2 = 0.0;//后坡坡度 lyz
  251. var CurL = 0.0;//竖曲线长 lyz
  252. var CurT = 0.0;//竖曲线切线长 lyz
  253. var CurE = 0.0;//竖曲线外距 lyz
  254. var CurQD = 0.0;//竖曲线起点桩号 lyz
  255. var CurZD = 0.0;//竖曲线终点桩号 lyz
  256. PreRampZH = TempEntityData[i - 1][0];
  257. PreRampBG = TempEntityData[i - 1][1];
  258. PreRampR = TempEntityData[i - 1][2];
  259. NextRampZH = TempEntityData[i + 1][0];
  260. NextRampBG = TempEntityData[i + 1][1];
  261. NextRampR = TempEntityData[i + 1][2];
  262. i_1 = (CurRampBG - PreRampBG) / (CurRampZH - PreRampZH);
  263. i_2 =(NextRampBG - CurRampBG) / (NextRampZH - CurRampZH);
  264. CurL = CurRampR * Math.abs(i_2 - i_1);
  265. CurT = CurL / 2;
  266. CurE = (CurT * CurT) / (2 * CurRampR);
  267. CurQD = CurRampZH - CurT;
  268. CurZD = CurRampZH + CurT;
  269. // EAction.handleUserMessage("CurL: " + CurL.toString());
  270. for (var j = CurQD; j < CurZD; j = j + DeltaS) {
  271. var jQBG = 0.0; //j点切线标高 lyz
  272. var jE = 0.0; //j点外距 lyz
  273. var jBG = 0.0; //j点标高 lyz
  274. if (j <= CurRampZH) {
  275. jQBG = CurRampBG - (CurRampZH - j) * i_1;
  276. jE = (j - CurQD) * (j - CurQD) / (2 * CurRampR);
  277. if ((i_1 - i_2) >= 0) {//确定是凸曲线还是凹曲线 lyz
  278. jBG = jQBG - jE;
  279. }
  280. else {
  281. jBG = jQBG + jE;
  282. }
  283. }
  284. else {
  285. jQBG = CurRampBG - (CurRampZH - j) * i_2;
  286. jE = (CurZD - j) * (CurZD - j) / (2 * CurRampR);
  287. if ((i_1 - i_2) >= 0) {//确定是凸曲线还是凹曲线 lyz
  288. jBG = jQBG - jE;
  289. }
  290. else {
  291. jBG = jQBG + jE;
  292. }
  293. }
  294. if (j + DeltaS >= CurZD) {
  295. CurLineType = 1;
  296. }
  297. else CurLineType = 2;
  298. this.newEntityData.push([j / parseFloat(this.ratio), jBG, CurLineType]);
  299. //插入辅助线数据
  300. if (j == CurQD) {
  301. this.newFZEntityData.push([j / parseFloat(this.ratio), jBG, 1]);
  302. this.newFZEntityData.push([CurRampZH / parseFloat(this.ratio), CurRampBG, 2]);
  303. }
  304. if (j + DeltaS >= CurZD){
  305. this.newFZEntityData.push([j / parseFloat(this.ratio), jBG, 3]);
  306. }
  307. this.allR.push(CurRampR);
  308. }
  309. }
  310. }
  311. // EAction.handleUserMessage(this.newEntityData.toString());
  312. // EAction.handleUserMessage(this.newFZEntityData.toString());
  313. this.applyOperation();
  314. reader.close();
  315. return true;
  316. };