1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
28
#include <QTextStream>
31
#include "rs_fileio.h"
34
RS_FileIO::RS_FileIO()
41
* Calls the import method of the filter responsible for the format
44
* @param graphic The container to which we will add
45
* entities. Usually that's an RS_Graphic entity but
46
* it can also be a polyline, text, ...
47
* @param file Path and name of the file to import.
49
bool RS_FileIO::fileImport(RS_Graphic& graphic, const QString& file,
50
RS2::FormatType type) {
52
RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
55
if (type == RS2::FormatUnknown) {
56
t = detectFormat(file);
62
if (RS2::FormatUnknown != t) {
63
std::unique_ptr<RS_FilterInterface> filter(getImportFilter(file, t));
64
if (filter.get() != NULL ){
65
return filter->fileImport(graphic, file, t);
67
RS_DEBUG->print(RS_Debug::D_WARNING,
68
"RS_FileIO::fileImport: failed to import file: %s",
69
file.toLatin1().data());
72
RS_DEBUG->print(RS_Debug::D_WARNING,
73
"RS_FileIO::fileImport: failed to detect file format: %s",
74
file.toLatin1().data());
83
* Calls the export method of the object responsible for the format
86
* @param file Path and name of the file to import.
88
bool RS_FileIO::fileExport(RS_Graphic& graphic, const QString& file,
89
RS2::FormatType type) {
91
RS_DEBUG->print("RS_FileIO::fileExport");
92
//RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
94
if (type==RS2::FormatUnknown) {
96
extension = QFileInfo(file).suffix().toLower();
98
if (extension=="dxf") {
99
type = RS2::FormatDXFRW;
101
else if (extension=="cxf") {
102
type = RS2::FormatCXF;
104
else if (extension=="lff") {
105
type = RS2::FormatLFF;
109
std::unique_ptr<RS_FilterInterface> filter(getExportFilter(file, type));
110
if (filter.get() != NULL ){
111
return filter->fileExport(graphic, file, type);
113
RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
120
* Detects and returns the file format of the given file.
122
RS2::FormatType RS_FileIO::detectFormat(const QString& file) {
123
RS2::FormatType type = RS2::FormatUnknown;
126
QString ext = fi.suffix().toLower();
128
type = RS2::FormatLFF;
129
} else if (ext=="cxf") {
130
type = RS2::FormatCXF;
132
} else if (ext=="dwg") {
133
type = RS2::FormatDWG;
135
} else if (ext=="dxf") {
136
type = RS2::FormatDXF1;
139
if (!f.open(QIODevice::ReadOnly)) {
140
// Error opening file:
141
RS_DEBUG->print(RS_Debug::D_WARNING,
142
"RS_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
143
type = RS2::FormatUnknown;
145
RS_DEBUG->print("RS_FileIO::detectFormat: "
146
"Successfully opened DXF file: %s",
147
file.toLatin1().data());
152
while (!ts.atEnd() && ++c<100) {
153
line = ts.readLine();
154
if (line=="$ACADVER" || line=="ENTITIES") {
155
type = RS2::FormatDXFRW;
158
// very simple reduced DXF:
159
// if (line=="ENTITIES" && c<10) {
160
// type = RS2::FormatDXFRW;