/** * 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; } // var ok = this.readTxt(fileDialogInfo[0]); try { // 只再平面上加载 var di = getAssignDocumentInterface(1); // this.sourceDi; // this.getDocumentInterface(); var file = di.importFile(fileDialogInfo[0], fileDialogInfo[1], false); // di.regenerateScenes(); // di.autoZoom(); if (file !== 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(); } var entities = this.sourceDocument.queryAllEntities(false, false, RS.EntityImage); for (var i = 0; i < entities.length; i++) { var entityId = entities[i]; var direct = this.sourceDocument.queryEntityDirect(entityId); var scale = direct.scale(new RVector(0.001, 0.001), new RVector(0, 0)); } return; } catch (error) { EAction.handleUserWarning("error: " + error.toString()); } }; 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; i < imageFormats.length; i++) { var format = imageFormats[i]; if (suffix === format.toString().toUpperCase()) { include("scripts/Draw/Image/Image.js"); var imageAction = new Image(); imageAction.fileName = fileName; this.getDocumentInterface(0).queueAction(imageAction); this.terminate(); return; } } var nameFilter = fileDialog.selectedNameFilter(); destr(fileDialog); EAction.activateMainWindow(); //this.blockName = new QFileInfo(fileNames[0]).baseName(); //RSettings.setValue("ImportFile/BlockName", new QFileInfo(fileNames[0]).baseName()); var optionsToolBar = EAction.getOptionsToolBar(); if (!isNull(optionsToolBar)) { var leBlockName = optionsToolBar.findChild("BlockName"); if (!isNull(leBlockName)) { var blockName = new QFileInfo(fileNames[0]).completeBaseName(); blockName = RDxfServices.getSafeBlockName(blockName); leBlockName.text = blockName; } } return [fileNames[0], nameFilter]; }; DXFImport.prototype.slotAsBlockChanged = function (value) { this.asBlock = value; this.updatePreview(true); }; DXFImport.prototype.slotBlockNameChanged = function (value) { this.blockName = value; this.updatePreview(true); }; DXFImport.prototype.readTxt = function (file) { var reader = new QFile(file); var flags = makeQIODeviceOpenMode(QIODevice.ReadOnly, QIODevice.Text); if (!reader.open(flags)) { EAction.handleUserWarning("cannot open file: " + file); return false; } var pLineNo = 0; //每次都去文件中的一行,然后输出读取到的字符串lyz var ts = new QTextStream(reader); setUtf8Codec(ts); do { var code = ts.readLine().trim(); var value = ts.readLine().trim(); //var textLine = ts.readLine().trim(); // EAction.handleUserWarning(textLine); 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, ""); // EAction.handleUserMessage(textLine2); // var code = parseInt(textLine.substring(0, 3)); // var value = textLine.substring(3).trim(); //读取图层数据 liyz if (code == '2' && value == 'LAYER') { //EAction.handleUserWarning('Layer Info...............................'); var LayerName = ''; var LayerColor = ''; var LayerLineType = ''; var LayerLineWidth = ''; 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 == '2') { LayerName = value; } else if (code == '62') { LayerColor = value; } else if (code == '6') { LayerLineType = value; } else if (code == '370') { LayerLineWidth = value; this.newLayerData.push([LayerName, LayerColor, LayerLineType, LayerLineWidth]); // EAction.handleUserWarning(LayerName + ', ' + LayerColor + ', ' + LayerLineType+ ', ' +LayerLineWidth); } } while (!ts.atEnd() && value != 'ENDTAB' && value != 'ENDSEC' && value != 'EOF'); } //读取点数据 liyz if (code == '0' && value == 'POINT') { //EAction.handleUserWarning('Point Info...............................'); var POINT_X = ''; var POINT_Y = ''; var POINT_Layer = ''; 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') { POINT_Layer = value; } else if (code == '10') { POINT_X = value; } else if (code == '20') { POINT_Y = value; this.newPointEntityData.push([parseFloat(POINT_X), parseFloat(POINT_Y), POINT_Layer]); // EAction.handleUserWarning(POINT_X + ', ' + POINT_Y + ', ' + POINT_Layer); break; } } while (!ts.atEnd() && code != '0' && value != 'ENDSEC' && value != 'EOF'); } //读取线数据 liyz if (code == '0' && value == 'LINE') { // EAction.handleUserWarning('Line Info...............................'); var StartPOINT_X = ''; var StartPOINT_Y = ''; var EndPOINT_X = ''; var EndPOINT_Y = ''; var Line_Layer = ''; 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, ""); //EAction.handleUserMessage(code); //EAction.handleUserMessage(value); if (code == '8') { Line_Layer = value; } else if (code == '10') { StartPOINT_X = value; } else if (code == '20') { StartPOINT_Y = value; } else if (code == '11') { EndPOINT_X = value; } else if (code == '21') { EndPOINT_Y = value; this.newLineEntityData.push([parseFloat(StartPOINT_X), parseFloat(StartPOINT_Y), parseFloat(EndPOINT_X), parseFloat(EndPOINT_Y), Line_Layer]); //EAction.handleUserWarning(StartPOINT_X + ', ' + StartPOINT_Y + ', ' + EndPOINT_X + ', ' + EndPOINT_Y + ', ' + Line_Layer); break; } } while (!ts.atEnd() && code != '0' && value != 'ENDSEC' && value != 'EOF'); } //读取圆数据 liyz if (code == '0' && value == 'CIRCLE') { // EAction.handleUserWarning('Circle Info...............................'); var CirclePOINT_X = ''; var CirclePOINT_Y = ''; var Circle_R = ''; var Circle_Layer = ''; 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, ""); //EAction.handleUserMessage(code); //EAction.handleUserMessage(value); if (code == '8') { Circle_Layer = value; } else if (code == '10') { CirclePOINT_X = value; } else if (code == '20') { CirclePOINT_Y = value; } else if (code == '40') { Circle_R = value; this.newCircleEntityData.push([parseFloat(CirclePOINT_X), parseFloat(CirclePOINT_Y), parseFloat(Circle_R), Circle_Layer]); //EAction.handleUserWarning(CirclePOINT_X + ', ' + CirclePOINT_Y + ', ' + Circle_R + ', ' + Circle_Layer); break; } } while (!ts.atEnd() && code != '0' && value != 'ENDSEC' && value != 'EOF'); } //读取弧数据 liyz if (code == '0' && value == 'ARC') { // EAction.handleUserWarning('Arc Info...............................'); var ArcPOINT_X = ''; var ArcPOINT_Y = ''; var Arc_R = ''; var Arc_StartAngle = ''; var Arc_EndAngle = ''; var Arc_Layer = ''; 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, ""); //EAction.handleUserMessage(code); //EAction.handleUserMessage(value); if (code == '8') { Arc_Layer = value; } else if (code == '10') { ArcPOINT_X = value; } else if (code == '20') { ArcPOINT_Y = value; } else if (code == '40') { Arc_R = value; } else if (code == '50') { Arc_StartAngle = value; } else if (code == '51') { Arc_EndAngle = value; this.newArcEntityData.push([parseFloat(ArcPOINT_X), parseFloat(ArcPOINT_Y), parseFloat(Arc_R), parseFloat(Arc_StartAngle), parseFloat(Arc_EndAngle), Arc_Layer]); //EAction.handleUserWarning(ArcPOINT_X + ', ' + ArcPOINT_Y + ', ' + Arc_R + ', ' + Arc_StartAngle + ', ' + Arc_EndAngle + ', ' + Arc_Layer); break; } } while (!ts.atEnd() && code != '0' && value != 'ENDSEC' && value != 'EOF'); } //读取多线段数据 liyz if (code == '0' && value == 'LWPOLYLINE') { // EAction.handleUserWarning('Polyline Info...............................'); var PolyPOINT_X = ''; var PolyPOINT_Y = ''; var PolyPOINT_Num = 0; var Poly_Layer = ''; var TempPolyLineData = new Array(); 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, ""); //EAction.handleUserWarning(code); //EAction.handleUserWarning(value); if (code == '8') { Poly_Layer = value; } else if (code == '90') { PolyPOINT_Num = parseInt(value); //EAction.handleUserWarning(PolyPOINT_Num.toString()); } else if (code == '10') { PolyPOINT_X = value; } else if (code == '20') { PolyPOINT_Y = value; TempPolyLineData.push([parseFloat(PolyPOINT_X), parseFloat(PolyPOINT_Y)]); } if (TempPolyLineData.length == PolyPOINT_Num && PolyPOINT_Num > 0) { 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; }