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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/28/2010 Added this text. :-)
15 #include "rs_fileio.h"
17 #include "rs_filtercxf.h"
18 #include "rs_filterdxf.h"
19 #include "rs_filterdxf1.h"
21 RS_FileIO * RS_FileIO::uniqueInstance = NULL;
24 RS_FileIO::RS_FileIO()
29 * @return Instance to the unique import object.
31 /*static*/ RS_FileIO * RS_FileIO::instance()
33 if (uniqueInstance == NULL)
34 uniqueInstance = new RS_FileIO();
36 return uniqueInstance;
40 * Registers a new import filter.
42 void RS_FileIO::registerFilter(RS_FilterInterface * f)
48 * @return List of registered filters.
50 QList<RS_FilterInterface *> RS_FileIO::getFilterList()
56 * @return Filter which can import the given file type.
58 RS_FilterInterface * RS_FileIO::getImportFilter(RS2::FormatType t)
60 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
61 for(int i=0; i<filterList.size(); i++)
63 RS_FilterInterface * f = filterList[i];
73 * @return Filter which can export the given file type.
75 RS_FilterInterface * RS_FileIO::getExportFilter(RS2::FormatType t)
77 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
78 for(int i=0; i<filterList.size(); i++)
80 RS_FilterInterface * f = filterList[i];
90 * Calls the import method of the filter responsible for the format
93 * @param graphic The container to which we will add
94 * entities. Usually that's an RS_Graphic entity but
95 * it can also be a polyline, text, ...
96 * @param file Path and name of the file to import.
98 bool RS_FileIO::fileImport(RS_Graphic & graphic, const QString & file, RS2::FormatType type)
100 RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
102 RS_FilterInterface * filter = NULL;
105 if (type == RS2::FormatUnknown)
106 t = detectFormat(file);
110 filter = getImportFilter(t);
115 filter = new RS_FilterCXF(graphic);
118 case RS2::FormatDXF1:
119 filter = new RS_FilterDXF1(graphic);
123 filter = new RS_FilterDXF(graphic);
132 return filter->fileImport(graphic, file, t);
135 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_FileIO::fileImport: failed to import file: %s",
136 file.toLatin1().data());
143 * Calls the export method of the object responsible for the format
146 * @param file Path and name of the file to import.
148 bool RS_FileIO::fileExport(RS_Graphic & graphic, const QString & file, RS2::FormatType type)
150 RS_DEBUG->print("RS_FileIO::fileExport");
151 //RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
153 if (type == RS2::FormatUnknown)
156 // extension = QFileInfo(file).extension(false).toLower();
157 extension = QFileInfo(file).suffix().toLower();
159 if (extension == "dxf")
160 type = RS2::FormatDXF;
161 else if (extension == "cxf")
162 type = RS2::FormatCXF;
165 RS_FilterInterface * filter = getExportFilter(type);
168 return filter->fileExport(graphic, file, type);
170 RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
176 * Detects and returns the file format of the given file.
178 RS2::FormatType RS_FileIO::detectFormat(const QString & file)
180 RS2::FormatType type = RS2::FormatUnknown;
183 // QString ext = f.extension(false).toLower();
184 QString ext = f.suffix().toLower();
188 type = RS2::FormatCXF;
190 else if (ext == "dxf")
192 type = RS2::FormatDXF1;
195 if (!f.open(QIODevice::ReadOnly))
197 // Error opening file:
198 RS_DEBUG->print(RS_Debug::D_WARNING,
199 "RS_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
200 type = RS2::FormatUnknown;
204 RS_DEBUG->print("RS_FileIO::detectFormat: Successfully opened DXF file: %s",
205 file.toLatin1().data());
211 while (!f.atEnd() && ++c < 100)
213 line = ts.readLine();
215 if (line == "$ACADVER")
216 type = RS2::FormatDXF;
218 // very simple reduced DXF:
219 if (line == "ENTITIES" && c < 10)
220 type = RS2::FormatDXF;