| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #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" |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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(); |
| |
|
| | |
| | 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(); |
| |
|
| | 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; |
| | } |
| | } |
| | } |
| | 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; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | RS2::FormatType RS_FileIO::detectFormat(QString const& file, bool forRead) |
| | { |
| | |
| | std::map<QString, RS2::FormatType> list{ |
| | {"dxf", RS2::FormatDXFRW}, |
| | {"cxf", RS2::FormatCXF}, |
| | {"lff", RS2::FormatLFF} |
| | }; |
| | |
| | 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; |
| |
|
| | |
| | if (forRead && type==RS2::FormatDXFRW) { |
| | type = RS2::FormatDXFRW; |
| | QFile f(file); |
| |
|
| | if (!f.open(QIODevice::ReadOnly)) { |
| | |
| | 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; |
| | } |
| | |
| | |
| | |
| | |
| | } |
| | f.close(); |
| | } |
| | } |
| | return type; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | bool RS_FileIO::fileExport(RS_Graphic& graphic, const QString& file, |
| | RS2::FormatType type) { |
| |
|
| | RS_DEBUG->print("RS_FileIO::fileExport"); |
| | |
| |
|
| | 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; |
| | } |
| |
|
| | |
| | |
| | |
| | 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; |
| | } |
| |
|
| | |
| | |
| | |
| | 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 |
| | }; |
| | } |