1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
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.
13 **
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.
18 **
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
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 
28 #include "rs_pattern.h"
29 
30 #include "rs_system.h"
31 #include "rs_fileio.h"
32 #include "rs_layer.h"
33 #include "rs_debug.h"
34 
35 
36 /**
37  * Constructor.
38  *
39  * @param fileName File name of a DXF file defining the pattern
40  */
RS_Pattern(const QString & fileName)41 RS_Pattern::RS_Pattern(const QString& fileName)
42 		: RS_EntityContainer(NULL)
43 		,fileName(fileName)
44 		,loaded(false)
45 {
46 	RS_DEBUG->print("RS_Pattern::RS_Pattern() ");
47 }
48 
49 
50 /**
51  * Loads the given pattern file into this pattern.
52  * Entities other than lines are ignored.
53  *
54  * @param filename File name of the pattern file (without path and
55  * extension or full path.
56  */
loadPattern()57 bool RS_Pattern::loadPattern() {
58     if (loaded) {
59         return true;
60     }
61 
62     RS_DEBUG->print("RS_Pattern::loadPattern");
63 
64     QString path;
65 
66     // Search for the appropriate pattern if we have only the name of the pattern:
67     if (!fileName.toLower().contains(".dxf")) {
68         QStringList patterns = RS_SYSTEM->getPatternList();
69         QFileInfo file;
70         for (QStringList::Iterator it = patterns.begin();
71                 it!=patterns.end();
72                 it++) {
73 
74             if (QFileInfo(*it).baseName().toLower()==fileName.toLower()) {
75                 path = *it;
76                 RS_DEBUG->print("Pattern found: %s", path.toLatin1().data());
77                 break;
78             }
79         }
80     }
81 
82     // We have the full path of the pattern:
83     else {
84         path = fileName;
85     }
86 
87     // No pattern paths found:
88     if (path.isEmpty()) {
89         RS_DEBUG->print("No pattern \"%s\"available.", fileName.toLatin1().data());
90         return false;
91     }
92 
93 	RS_Graphic gr;
94 	RS_FileIO::instance()->fileImport(gr, path);
95 	for(auto e: gr){
96 		if (e->rtti()==RS2::EntityLine ||
97 				e->rtti()==RS2::EntityArc||
98 				e->rtti()==RS2::EntityEllipse
99 				) {
100             RS_Layer* l = e->getLayer();
101             RS_Entity* cl = e->clone();
102             cl->reparent(this);
103 			if (l) {
104                 cl->setLayer(l->getName());
105             }
106             addEntity(cl);
107         }
108 	}
109 
110     loaded = true;
111     RS_DEBUG->print("RS_Pattern::loadPattern: OK");
112 
113     return true;
114 }
115 
getFileName() const116 QString RS_Pattern::getFileName() const {
117 	return fileName;
118 }
119 
120