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"
20 #include "rs_filterinterface.h"
22 RS_FileIO * RS_FileIO::uniqueInstance = NULL;
25 RS_FileIO::RS_FileIO()
30 * @return Instance to the unique import object.
32 /*static*/ RS_FileIO * RS_FileIO::instance()
34 if (uniqueInstance == NULL)
35 uniqueInstance = new RS_FileIO();
37 return uniqueInstance;
41 * Registers a new import filter.
43 void RS_FileIO::registerFilter(RS_FilterInterface * f)
49 * @return List of registered filters.
51 QList<RS_FilterInterface *> RS_FileIO::getFilterList()
57 * @return Filter which can import the given file type.
59 RS_FilterInterface * RS_FileIO::getImportFilter(RS2::FormatType t)
61 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
62 for(int i=0; i<filterList.size(); i++)
64 RS_FilterInterface * f = filterList[i];
74 * @return Filter which can export the given file type.
76 RS_FilterInterface * RS_FileIO::getExportFilter(RS2::FormatType t)
78 // for(RS_FilterInterface * f=filterList.first(); f!=NULL; f=filterList.next())
79 for(int i=0; i<filterList.size(); i++)
81 RS_FilterInterface * f = filterList[i];
91 * Calls the import method of the filter responsible for the format
94 * @param graphic The container to which we will add
95 * entities. Usually that's an Drawing entity but
96 * it can also be a polyline, text, ...
97 * @param file Path and name of the file to import.
99 bool RS_FileIO::fileImport(Drawing & graphic, const QString & file, RS2::FormatType type)
101 RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
103 RS_FilterInterface * filter = NULL;
106 if (type == RS2::FormatUnknown)
107 t = detectFormat(file);
111 filter = getImportFilter(t);
116 filter = new RS_FilterCXF(graphic);
119 case RS2::FormatDXF1:
120 filter = new RS_FilterDXF1(graphic);
124 filter = new RS_FilterDXF(graphic);
133 return filter->fileImport(graphic, file, t);
136 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_FileIO::fileImport: failed to import file: %s",
137 file.toLatin1().data());
144 * Calls the export method of the object responsible for the format
147 * @param file Path and name of the file to import.
149 bool RS_FileIO::fileExport(Drawing & graphic, const QString & file, RS2::FormatType type)
151 RS_DEBUG->print("RS_FileIO::fileExport");
152 //RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
154 if (type == RS2::FormatUnknown)
157 // extension = QFileInfo(file).extension(false).toLower();
158 extension = QFileInfo(file).suffix().toLower();
160 if (extension == "dxf")
161 type = RS2::FormatDXF;
162 else if (extension == "cxf")
163 type = RS2::FormatCXF;
166 RS_FilterInterface * filter = getExportFilter(type);
169 return filter->fileExport(graphic, file, type);
171 RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
177 * Detects and returns the file format of the given file.
179 RS2::FormatType RS_FileIO::detectFormat(const QString & file)
181 RS2::FormatType type = RS2::FormatUnknown;
184 // QString ext = f.extension(false).toLower();
185 QString ext = f.suffix().toLower();
189 type = RS2::FormatCXF;
191 else if (ext == "dxf")
193 type = RS2::FormatDXF1;
196 if (!f.open(QIODevice::ReadOnly))
198 // Error opening file:
199 RS_DEBUG->print(RS_Debug::D_WARNING,
200 "RS_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
201 type = RS2::FormatUnknown;
205 RS_DEBUG->print("RS_FileIO::detectFormat: Successfully opened DXF file: %s",
206 file.toLatin1().data());
212 while (!f.atEnd() && ++c < 100)
214 line = ts.readLine();
216 if (line == "$ACADVER")
217 type = RS2::FormatDXF;
219 // very simple reduced DXF:
220 if (line == "ENTITIES" && c < 10)
221 type = RS2::FormatDXF;