]> Shamusworld >> Repos - architektonas/blob - src/base/rs_pattern.cpp
GPL compliance check...
[architektonas] / src / base / rs_pattern.cpp
1 // rs_pattern.cpp
2 //
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
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/01/2010  Added this text. :-)
15 //
16
17 #include "rs_pattern.h"
18
19 #include <QtCore>
20 #include "rs_system.h"
21 #include "rs_fileio.h"
22 #include "drawing.h"
23
24 /**
25  * Constructor.
26  *
27  * @param fileName File name of a DXF file defining the pattern
28  */
29 RS_Pattern::RS_Pattern(const QString & fileName): RS_EntityContainer(NULL)
30 {
31         RS_DEBUG->print("RS_Pattern::RS_Pattern() ");
32
33         this->fileName = fileName;
34         loaded = false;
35 }
36
37 /**
38  * Constructor.
39  *
40  * @param fileName File name of a PAT file which defines this
41  *         pattern among others.
42  * @param name Pattern name.
43  *
44  */
45 /*RS_Pattern::RS_Pattern(const QString& fileName, const QString& name)
46         : RS_EntityContainer(NULL) {
47         this->fileName = fileName;
48         this->name = name;
49         loaded = false;
50 }*/
51
52 RS_Pattern::~RS_Pattern()
53 {
54 }
55
56 /**
57  * Loads the given pattern file into this pattern.
58  * Entities other than lines are ignored.
59  *
60  * @param filename File name of the pattern file (without path and
61  * extension or full path.
62  */
63 bool RS_Pattern::loadPattern()
64 {
65         if (loaded)
66                 return true;
67
68         RS_DEBUG->print("RS_Pattern::loadPattern");
69
70         QString path;
71
72         // Search for the appropriate pattern if we have only the name of the pattern:
73         if (!fileName.toLower().contains(".dxf"))
74         {
75                 QStringList patterns = RS_SYSTEM->getPatternList();
76                 QFileInfo file;
77
78                 for(QStringList::Iterator it=patterns.begin(); it!=patterns.end(); it++)
79                 {
80                         if (QFileInfo(*it).baseName().toLower() == fileName.toLower())
81                         {
82                                 path = *it;
83                                 RS_DEBUG->print("Pattern found: %s", path.toLatin1().data());
84                                 break;
85                         }
86                 }
87         }
88         // We have the full path of the pattern:
89         else
90         {
91                 path = fileName;
92         }
93
94         // No pattern paths found:
95         if (path.isEmpty())
96         {
97                 RS_DEBUG->print("No pattern \"%s\"available.", fileName.toLatin1().data());
98                 return false;
99         }
100
101         Drawing * gr = new Drawing();
102
103         // TODO: Find out why the new dxf filter doesn't work for patterns:
104         RS_FILEIO->fileImport(*gr, path, RS2::FormatDXF1);
105
106         for(RS_Entity * e=gr->firstEntity(); e!=NULL; e=gr->nextEntity())
107         {
108                 if (e->rtti() == RS2::EntityLine || e->rtti() == RS2::EntityArc)
109                 {
110                         RS_Layer * l = e->getLayer();
111                         RS_Entity * cl = e->clone();
112                         cl->reparent(this);
113
114                         if (l != NULL)
115                         {
116                                 cl->setLayer(l->getName());
117                         }
118
119                         addEntity(cl);
120                 }
121         }
122
123         delete gr;
124         loaded = true;
125         RS_DEBUG->print("RS_Pattern::loadPattern: OK");
126
127         return true;
128 }
129
130 /** @return the fileName of this pattern. */
131 QString RS_Pattern::getFileName() const
132 {
133         return fileName;
134 }