3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/28/2010 Added this text. :-)
17 #include "rs_fileio.h"
19 #include "rs_filtercxf.h"
20 #include "rs_filterdxf.h"
21 #include "rs_filterdxf1.h"
22 #include "rs_filterinterface.h"
24 RS_FileIO * RS_FileIO::uniqueInstance = NULL;
27 RS_FileIO::RS_FileIO()
32 * @return Instance to the unique import object.
34 /*static*/ RS_FileIO * RS_FileIO::instance()
36 if (uniqueInstance == NULL)
37 uniqueInstance = new RS_FileIO();
39 return uniqueInstance;
43 * Registers a new import filter.
45 void RS_FileIO::registerFilter(RS_FilterInterface * f)
51 * @return List of registered filters.
53 QList<RS_FilterInterface *> RS_FileIO::getFilterList()
59 * @return Filter which can import the given file type.
61 RS_FilterInterface * RS_FileIO::getImportFilter(RS2::FormatType t)
63 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
64 for(int i=0; i<filterList.size(); i++)
66 RS_FilterInterface * f = filterList[i];
76 * @return Filter which can export the given file type.
78 RS_FilterInterface * RS_FileIO::getExportFilter(RS2::FormatType t)
80 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
81 for(int i=0; i<filterList.size(); i++)
83 RS_FilterInterface * f = filterList[i];
93 * Calls the import method of the filter responsible for the format
96 * @param graphic The container to which we will add
97 * entities. Usually that's an Drawing entity but
98 * it can also be a polyline, text, ...
99 * @param file Path and name of the file to import.
101 bool RS_FileIO::fileImport(Drawing & graphic, const QString & file, RS2::FormatType type)
103 RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
105 RS_FilterInterface * filter = NULL;
108 if (type == RS2::FormatUnknown)
109 t = detectFormat(file);
113 filter = getImportFilter(t);
118 filter = new RS_FilterCXF(graphic);
121 case RS2::FormatDXF1:
122 filter = new RS_FilterDXF1(graphic);
126 filter = new RS_FilterDXF(graphic);
135 return filter->fileImport(graphic, file, t);
138 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_FileIO::fileImport: failed to import file: %s",
139 file.toLatin1().data());
146 * Calls the export method of the object responsible for the format
149 * @param file Path and name of the file to import.
151 bool RS_FileIO::fileExport(Drawing & graphic, const QString & file, RS2::FormatType type)
153 RS_DEBUG->print("RS_FileIO::fileExport");
154 //RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
156 if (type == RS2::FormatUnknown)
159 // extension = QFileInfo(file).extension(false).toLower();
160 extension = QFileInfo(file).suffix().toLower();
162 if (extension == "dxf")
163 type = RS2::FormatDXF;
164 else if (extension == "cxf")
165 type = RS2::FormatCXF;
168 RS_FilterInterface * filter = getExportFilter(type);
171 return filter->fileExport(graphic, file, type);
173 RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
179 * Detects and returns the file format of the given file.
181 RS2::FormatType RS_FileIO::detectFormat(const QString & file)
183 RS2::FormatType type = RS2::FormatUnknown;
186 // QString ext = f.extension(false).toLower();
187 QString ext = f.suffix().toLower();
191 type = RS2::FormatCXF;
193 else if (ext == "dxf")
195 type = RS2::FormatDXF1;
198 if (!f.open(QIODevice::ReadOnly))
200 // Error opening file:
201 RS_DEBUG->print(RS_Debug::D_WARNING,
202 "RS_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
203 type = RS2::FormatUnknown;
207 RS_DEBUG->print("RS_FileIO::detectFormat: Successfully opened DXF file: %s",
208 file.toLatin1().data());
214 while (!f.atEnd() && ++c < 100)
216 line = ts.readLine();
218 if (line == "$ACADVER")
219 type = RS2::FormatDXF;
221 // very simple reduced DXF:
222 if (line == "ENTITIES" && c < 10)
223 type = RS2::FormatDXF;