/** * 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/Edit/Paste/Paste.js"); include("scripts/EAction.js"); include("scripts/simple.js"); /** * \class ImportFile * \brief Inserts a file into the drawing. * The target point is specified by the user. * \ingroup ecma_edit */ function DXFImport(guiAction, fileName) { Paste.call(this, guiAction); this.sourceDocument = new RDocument(new RMemoryStorage(), createSpatialIndex()); this.sourceDi = new RDocumentInterface(this.sourceDocument); this.sourceDi.setNotifyListeners(false); // unit tests pass file name directly to action: this.fileName = fileName; this.blockName = undefined; this.asBlock = false; this.newLayerData = new Array(); this.newPointEntityData = new Array(); this.newLineEntityData = new Array(); this.newArcEntityData = new Array(); this.newCircleEntityData = new Array(); this.newPolylineEntityData = new Array(); this.newBlockEntityData = new Array(); this.newTextEntityData = new Array(); this.ratio = 1.0; // this.setUiOptions(DXFImport.includeBasePath + "/DXFImport.ui"); } DXFImport.prototype = new Paste(); DXFImport.includeBasePath = includeBasePath; DXFImport.prototype.beginEvent = function() { Paste.prototype.beginEvent.call(this); var fileDialogInfo; if (!isNull(this.fileName)) { fileDialogInfo = [ this.fileName, "" ]; } else { fileDialogInfo = this.getFileName(); } // workaround for Qt keyboard focus bug: var appWin = RMainWindowQt.getMainWindow(); if (!isNull(appWin)) { appWin.activateWindow(); appWin.raise(); appWin.setFocus(Qt.OtherFocusReason); } if (isNull(fileDialogInfo)) { destr(this.sourceDi); this.sourceDi = undefined; this.terminate(); return; } EAction.handleUserWarning("=="); // var ok = this.readTxt(fileDialogInfo[0]); if (this.sourceDi.importFile(fileDialogInfo[0], fileDialogInfo[1], false)!==RDocumentInterface.IoErrorNoError) { EAction.handleUserWarning(qsTr("Error while importing file %1").arg(fileDialogInfo[0])); if (!isNull(this.sourceDi)) { destr(this.sourceDi); this.sourceDi = undefined; } this.terminate(); return; } }; DXFImport.prototype.finishEvent = function() { if (!isNull(this.sourceDi)) { destr(this.sourceDi); } Paste.prototype.finishEvent.call(this); }; DXFImport.prototype.initOperation = function(op) { if (this.asBlock && !isNull(this.blockName) && this.blockName.length>0) { op.setBlockName(this.blockName); } // op.setCopyAllLayers(true); // op.setCopyEmptyBlocks(true); }; DXFImport.prototype.getFileName = function() { var appWin = EAction.getMainWindow(); // if(RSettings.getStringValue("OpenProject/Current","") == ""){ // QMessageBox.critical(appWin, // qsTr("提示"), // qsTr("请先选择工程路径")); // return; // } var lastOpenFileDir = RSettings.getStringValue("OpenProject/Current", RSettings.getDocumentsLocation()); var filterAll = qsTr("地形文件") + " (*.dxf);"; var fileDialog = new QFileDialog(appWin, qsTr("请选择地形文件"), lastOpenFileDir, ""); fileDialog.setNameFilters([filterAll]); fileDialog.selectNameFilter(filterAll); fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog()); if (!isNull(QFileDialog.DontUseCustomDirectoryIcons)) { fileDialog.setOption(QFileDialog.DontUseCustomDirectoryIcons, true); } fileDialog.fileMode = QFileDialog.ExistingFiles; fileDialog.setLabelText(QFileDialog.FileType, qsTr("Format:")); if (!fileDialog.exec()) { destr(fileDialog); EAction.activateMainWindow(); return undefined; } var fileNames = fileDialog.selectedFiles(); if (fileNames.length===0) { destr(fileDialog); EAction.activateMainWindow(); return undefined; } var fileName = fileNames[0]; var suffix = new QFileInfo(fileName).suffix(); suffix = suffix.toUpperCase(); var imageFormats = QImageReader.supportedImageFormats(); for (var i=0; i0) { this.newPolylineEntityData.push([TempPolyLineData, Poly_Layer]); // EAction.handleUserWarning(TempPolyLineData.toString()); break; } } while (!ts.atEnd() && code != '0' && value != 'ENDSEC' && value != 'EOF'); } //读取块数据 liyz if (code == '0' && value == 'BLOCK') { // EAction.handleUserWarning('Block Info...............................'); var BlockName = ''; var Block_X = ''; var Block_Y = ''; var BlockLayer = ''; do { code = ts.readLine().trim(); value = ts.readLine().trim(); code = code.replace(/[\t]+/g, " ").replace(/[\s]+/g, " "); code = code.replace(/\r\n/g, ""); value = value.replace(/[\t]+/g, " ").replace(/[\s]+/g, " "); value = value.replace(/\r\n/g, ""); if (code == '8') { BlockLayer = value; } else if (code == '2') { BlockName = value; } else if (code == '10') { Block_X = value; } else if (code == '20') { Block_Y = value; this.newBlockEntityData.push([BlockName, parseFloat(Block_X), parseFloat(Block_Y), BlockLayer]); //EAction.handleUserWarning(BlockName + ', ' + Block_X + ', ' + Block_Y+ ', ' +BlockLayer); } } while (!ts.atEnd() && value != 'ENDBLK' && value != 'ENDSEC' && value != 'EOF'); } //读文本数据 liyz if (code == '0' && value == 'TEXT') { EAction.handleUserWarning('TEXT Info...............................'); var TextName = ''; var Text_X = ''; var Text_Y = ''; var Text_Hight = 0.0; // var Text_angle = 0.0; //var Text_font = ''; var TextLayer = ''; do { code = ts.readLine().trim(); value = ts.readLine().trim(); code = code.replace(/[\t]+/g, " ").replace(/[\s]+/g, " "); code = code.replace(/\r\n/g, ""); value = value.replace(/[\t]+/g, " ").replace(/[\s]+/g, " "); value = value.replace(/\r\n/g, ""); if (code == '8') { TextLayer = value; } else if (code == '10') { Text_X = value; } else if (code == '20') { Text_Y = value; } else if (code == '40') { Text_Hight = value; } else if (code == '1') { TextName = value; this.newTextEntityData.push([TextName, parseFloat(Text_X), parseFloat(Text_Y), parseFloat(Text_Hight), TextLayer]); EAction.handleUserWarning(TextName + ', ' + Text_X + ', ' + Text_Y + ', ' + Text_Hight + ', ' + TextLayer); break; } } while (!ts.atEnd() && value != 'ENDBLK' && value != 'ENDSEC' && value != 'EOF'); } pLineNo++; } while (!ts.atEnd()); //EAction.handleUserMessage(this.newLayerData.toString()); //EAction.handleUserMessage(this.newPointEntityData.toString()); //EAction.handleUserMessage(this.newLineEntityData.toString()); //EAction.handleUserMessage(this.newCircleEntityData.toString()); //EAction.handleUserMessage(this.newArcEntityData.toString()); this.applyOperation(); reader.close(); return true; } DXFImport.prototype.applyOperation = function (preview) { var LineColor = new RColor(105, 105, 105); var PointColor = new RColor(255, 0, 0); var CircleColor = new RColor(255, 255, 255); var ArcColor = new RColor(0, 255, 0); //添加图层 liyz for (var i = 0; i < this.newLayerData.length;i++) { addLayerToView(this.newLayerData[i][0], null/*this.newLayerData[i][1]*/, this.newLayerData[i][2], parseInt(this.newLayerData[i][3]), 1); //addLayerToView(this.newLayerData[i][0], null, null, null, 0); } //绘制点 liyz for (var i = 0; i < this.newPointEntityData.length; i++) { addPointToView(this.newPointEntityData[i][0], this.newPointEntityData[i][1], 1); } //绘制线 liyz for (var i = 0; i < this.newLineEntityData.length; i++) { addLineToView(this.newLineEntityData[i][0], this.newLineEntityData[i][1], this.newLineEntityData[i][2], this.newLineEntityData[i][3], 1); } //绘制多线段 liyz for (var i = 0; i < this.newPolylineEntityData.length; i++) { addPolylinetoView(this.newPolylineEntityData[i][0], false, false, 1, LineColor); } //绘制圆 liyz for (var i = 0; i < this.newCircleEntityData.length; i++) { addCircleToView(this.newCircleEntityData[i][0], this.newCircleEntityData[i][1], this.newCircleEntityData[i][2], 1, CircleColor); } //绘制弧 liyz for (var i = 0; i < this.newArcEntityData.length; i++) { addArctoView(this.newArcEntityData[i][0], this.newArcEntityData[i][1], this.newArcEntityData[i][2], this.newArcEntityData[i][3], this.newArcEntityData[i][4], false, 1, ArcColor); } //绘制文字 liyz for (var i = 0; i < this.newTextEntityData.length; i++) { addSimpleTextToView(this.newTextEntityData[i][0], this.newTextEntityData[i][1], this.newTextEntityData[i][2], this.newTextEntityData[i][3], null, null, null, null, null, null,1); } var di = getAssignDocumentInterface(1); if (!isNull(di)) { // di.regenerateScenes(); di.autoZoom(); } return true; }