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