File size: 10,616 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2021 A. Stebich (librecad@mail.lordofbikes.de)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include <cstddef>
#include <QFileInfo>
#include <QTextStream>
#ifdef DWGSUPPORT
#include <QMessageBox>
#include <QApplication>
#endif
#include "rs_fileio.h"
#include "rs_filtercxf.h"
#include "rs_filterdxf1.h"
#include "rs_filterjww.h"
#include "rs_filterlff.h"
#include "rs_filterdxfrw.h"
#include "rs_debug.h"
/**
* Calls the import method of the filter responsible for the format
* of the given file.
*
* @param graphic The container to which we will add
* entities. Usually that's an RS_Graphic entity but
* it can also be a polyline, text, ...
* @param file Path and name of the file to import.
*/
bool RS_FileIO::fileImport(RS_Graphic& graphic, const QString& file,
RS2::FormatType type) {
RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
RS2::FormatType t;
if (type == RS2::FormatUnknown) {
t = detectFormat(file);
}
else {
t = type;
}
if (RS2::FormatUnknown != t) {
std::unique_ptr<RS_FilterInterface>&& filter(getImportFilter(file, t));
if (filter){
#ifdef DWGSUPPORT
bool isDwg {file.endsWith( ".dwg", Qt::CaseInsensitive)};
if (isDwg) {
QApplication::restoreOverrideCursor(); // disable WaitCursor for massagebox
// use QStringList to avoid "\n" in translation strings
QStringList info { QObject::tr("DWG support is not complete!"),
"",
QObject::tr("If this file fails to open try an older DWG format"),
QObject::tr("or try to find a converter to make it a DXF file.") };
QMessageBox::information( qApp->activeWindow(),
QObject::tr("Information"),
info.join( "\n"),
QMessageBox::Ok,
QMessageBox::NoButton);
QApplication::setOverrideCursor( QCursor(Qt::WaitCursor));
}
#endif
bool bImported {filter->fileImport(graphic, file, t)};
if (!bImported) {
QApplication::restoreOverrideCursor(); // disable WaitCursor for massagebox
QString strTitle {QObject::tr("Error", "fileImport")};
QString strError {QObject::tr("Import error:", "fileImport")};
QString strLastError( " %1");
#ifdef DWGSUPPORT
if (isDwg) {
if (graphic.isEmpty()) {
QMessageBox::critical( qApp->activeWindow(),
strTitle,
QStringList( {strError, strLastError}).join( "\n").arg( filter->lastError()),
QMessageBox::Ok,
QMessageBox::NoButton);
}
else {
QStringList message { strError,
strLastError,
"",
QObject::tr("Anyhow, there are some entities identified.", "dwgImport"),
QObject::tr("If you open the file now, the drawing may be not complete or unusable.", "dwgImport"),
"",
QObject::tr("Ignore error and open the file?", "dwgImport"),
};
QMessageBox::StandardButton answer = QMessageBox::warning( qApp->activeWindow(),
QObject::tr("Warning"),
message.join( "\n").arg( filter->lastError()),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::NoButton);
if (QMessageBox::Yes == answer) {
return true; // open the file anyhow
}
}
}
else
#endif
{
QMessageBox::critical( qApp->activeWindow(),
strTitle,
QStringList( {strError, strLastError}).join( "\n").arg( filter->lastError()),
QMessageBox::Ok,
QMessageBox::NoButton);
}
QApplication::setOverrideCursor( QCursor(Qt::WaitCursor));
}
return bImported;
}
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_FileIO::fileImport: failed to import file: %s",
file.toLatin1().data());
}
else {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_FileIO::fileImport: failed to detect file format: %s",
file.toLatin1().data());
}
return false;
}
/** \brief extension2Type convert extension to file format type
* \param file type
* \param verifyRead read the file to verify dxf/dxfrw type, default to false
* \return file format type
*/
RS2::FormatType RS_FileIO::detectFormat(QString const& file, bool forRead)
{
// look up table
std::map<QString, RS2::FormatType> list{
{"dxf", RS2::FormatDXFRW},
{"cxf", RS2::FormatCXF},
{"lff", RS2::FormatLFF}
};
// only read support for dwg
if(forRead) list["dwg"]=RS2::FormatDWG;
QString const extension = QFileInfo(file).suffix().toLower();
RS2::FormatType type=(list.find(extension)!=
list.end()) ? list[extension]:RS2::FormatUnknown;
//only read dxf to verify
if (forRead && type==RS2::FormatDXFRW) {
type = RS2::FormatDXFRW;
QFile f(file);
if (!f.open(QIODevice::ReadOnly)) {
// Error opening file:
RS_DEBUG->print(RS_Debug::D_WARNING,
"%s:"
"Cannot open file: %s",
__func__,
file.toLatin1().data());
type = RS2::FormatUnknown;
} else {
RS_DEBUG->print("%s:"
"Successfully opened DXF file: %s",
__func__,
file.toLatin1().data());
QTextStream ts(&f);
QString line;
int c=0;
while (!ts.atEnd() && ++c<100) {
line = ts.readLine();
if (line=="$ACADVER" || line=="ENTITIES") {
type = RS2::FormatDXFRW;
break;
}
// very simple reduced DXF:
// if (line=="ENTITIES" && c<10) {
// type = RS2::FormatDXFRW;
// }
}
f.close();
}
}
return type;
}
/**
* Calls the export method of the object responsible for the format
* of the given file.
*
* @param file Path and name of the file to import.
*/
bool RS_FileIO::fileExport(RS_Graphic& graphic, const QString& file,
RS2::FormatType type) {
RS_DEBUG->print("RS_FileIO::fileExport");
//RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
if (type==RS2::FormatUnknown) {
type=detectFormat(file, false);
}
std::unique_ptr<RS_FilterInterface>&& filter(getExportFilter(file, type));
if (filter){
return filter->fileExport(graphic, file, type);
}
RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
return false;
}
RS_FileIO* RS_FileIO::instance() {
static RS_FileIO* uniqueInstance=nullptr;
if (!uniqueInstance) {
uniqueInstance = new RS_FileIO();
}
return uniqueInstance;
}
/**
* @return Filter which can import the given file type.
*/
std::unique_ptr<RS_FilterInterface> RS_FileIO::getImportFilter(const QString &fileName,
RS2::FormatType t) const{
for(auto f: getFilters()){
std::unique_ptr<RS_FilterInterface> filter(f());
if(filter &&
filter->canImport(fileName, t)){
return filter;
}
}
return nullptr;
}
/**
* @return Filter which can export the given file type.
*/
std::unique_ptr<RS_FilterInterface> RS_FileIO::getExportFilter(const QString &fileName,
RS2::FormatType t) const{
for(auto f: getFilters()){
std::unique_ptr<RS_FilterInterface> filter(f());
if(filter &&
filter->canExport(fileName, t)){
return filter;
}
}
return nullptr;
}
std::vector<std::function<RS_FilterInterface*()>> RS_FileIO::getFilters(){
return {
RS_FilterLFF::createFilter
,RS_FilterDXFRW::createFilter
,RS_FilterCXF::createFilter
,RS_FilterJWW::createFilter
,RS_FilterDXF1::createFilter
};
} |