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. :-)
15 // JLH 09/07/2010 Fixed file detection algorithm.
20 #include "filtercxf.h"
21 #include "filterdxf.h"
22 #include "filterdxf1.h"
23 #include "filterinterface.h"
25 FileIO * FileIO::uniqueInstance = NULL;
33 * @return Instance to the unique import object.
35 /*static*/ FileIO * FileIO::instance()
38 uniqueInstance = new FileIO();
40 return uniqueInstance;
44 * Registers a new import filter.
46 void FileIO::registerFilter(FilterInterface * f)
52 * @return List of registered filters.
54 QList<FilterInterface *> FileIO::getFilterList()
60 * @return Filter which can import the given file type.
62 FilterInterface * FileIO::getImportFilter(RS2::FormatType t)
64 for(int i=0; i<filterList.size(); i++)
66 FilterInterface * f = filterList[i];
76 * @return Filter which can export the given file type.
78 FilterInterface * FileIO::getExportFilter(RS2::FormatType t)
80 for(int i=0; i<filterList.size(); i++)
82 FilterInterface * f = filterList[i];
92 * Calls the import method of the filter responsible for the format of the
95 * @param drawing The container to which we will add entities. Usually that's
96 * an Drawing entity but it can also be a polyline, text, ...
97 * @param file Path and name of the file to import.
99 bool FileIO::fileImport(Drawing & drawing, const QString & file, RS2::FormatType type)
101 DEBUG->print("Trying to import file '%s'...", file.toAscii().data());
103 FilterInterface * filter = NULL;
104 RS2::FormatType t = (type == RS2::FormatUnknown ? detectFormat(file) : type);
105 filter = getImportFilter(t);
108 return filter->fileImport(drawing, file, t);
111 DEBUG->print(Debug::D_WARNING, "FileIO::fileImport: failed to import file: %s",
112 file.toAscii().data());
119 * Calls the export method of the object responsible for the format of the
122 * @param file Path and name of the file to import.
124 bool FileIO::fileExport(Drawing & drawing, const QString & file, RS2::FormatType type)
126 DEBUG->print("FileIO::fileExport");
128 if (type == RS2::FormatUnknown)
130 QString extension = QFileInfo(file).suffix().toLower();
132 if (extension == "dxf")
133 type = RS2::FormatDXF;
134 else if (extension == "cxf")
135 type = RS2::FormatCXF;
138 FilterInterface * filter = getExportFilter(type);
141 return filter->fileExport(drawing, file, type);
143 DEBUG->print("FileIO::fileExport: no filter found");
149 * Detects and returns the file format of the given file.
151 RS2::FormatType FileIO::detectFormat(const QString & file)
153 RS2::FormatType type = RS2::FormatUnknown;
154 QFileInfo info(file);
155 QString ext = info.suffix().toLower();
159 type = RS2::FormatCXF;
161 else if (ext == "dxf")
163 type = RS2::FormatDXF1;
166 if (!f.open(QIODevice::ReadOnly))
168 // Error opening file:
169 DEBUG->print(Debug::D_WARNING,
170 "FileIO::detectFormat: Cannot open file: %s", file.toAscii().data());
171 type = RS2::FormatUnknown;
175 DEBUG->print("FileIO::detectFormat: Successfully opened DXF file: %s",
176 file.toAscii().data());
181 //I think this is wrong... We're mixing classes here...
182 //AND THAT WAS THE PROBLEM!!!
183 // while (!f.atEnd() && ++c < 100)
184 while (!ts.atEnd() && ++c < 100)
186 QString line = ts.readLine();
188 if (line == "$ACADVER")
189 type = RS2::FormatDXF;
191 // very simple reduced DXF:
192 if (line == "ENTITIES" && c < 10)
193 type = RS2::FormatDXF;