]> Shamusworld >> Repos - architektonas/blob - src/base/rs_pattern.cpp
5952dfae56c0f19034c4e16063b52ed01f2e158d
[architektonas] / src / base / rs_pattern.cpp
1 /****************************************************************************
2 ** $Id: rs_pattern.cpp 1938 2004-12-09 23:09:53Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_pattern.h"
28
29 #include <QtCore>
30 #include "rs_system.h"
31 #include "rs_fileio.h"
32 #include "rs_graphic.h"
33
34 /**
35  * Constructor.
36  *
37  * @param fileName File name of a DXF file defining the pattern
38  */
39 RS_Pattern::RS_Pattern(const QString & fileName): RS_EntityContainer(NULL)
40 {
41         RS_DEBUG->print("RS_Pattern::RS_Pattern() ");
42
43         this->fileName = fileName;
44         loaded = false;
45 }
46
47 /**
48  * Constructor.
49  *
50  * @param fileName File name of a PAT file which defines this
51  *         pattern among others.
52  * @param name Pattern name.
53  *
54  */
55 /*RS_Pattern::RS_Pattern(const QString& fileName, const QString& name)
56         : RS_EntityContainer(NULL) {
57         this->fileName = fileName;
58         this->name = name;
59         loaded = false;
60 }*/
61
62 RS_Pattern::~RS_Pattern()
63 {
64 }
65
66 /**
67  * Loads the given pattern file into this pattern.
68  * Entities other than lines are ignored.
69  *
70  * @param filename File name of the pattern file (without path and
71  * extension or full path.
72  */
73 bool RS_Pattern::loadPattern()
74 {
75         if (loaded)
76                 return true;
77
78         RS_DEBUG->print("RS_Pattern::loadPattern");
79
80         QString path;
81
82         // Search for the appropriate pattern if we have only the name of the pattern:
83         if (!fileName.toLower().contains(".dxf"))
84         {
85                 QStringList patterns = RS_SYSTEM->getPatternList();
86                 QFileInfo file;
87
88                 for(QStringList::Iterator it=patterns.begin(); it!=patterns.end(); it++)
89                 {
90                         if (QFileInfo(*it).baseName().toLower() == fileName.toLower())
91                         {
92                                 path = *it;
93                                 RS_DEBUG->print("Pattern found: %s", path.toLatin1().data());
94                                 break;
95                         }
96                 }
97         }
98         // We have the full path of the pattern:
99         else
100         {
101                 path = fileName;
102         }
103
104         // No pattern paths found:
105         if (path.isEmpty())
106         {
107                 RS_DEBUG->print("No pattern \"%s\"available.", fileName.toLatin1().data());
108                 return false;
109         }
110
111         RS_Graphic * gr = new RS_Graphic();
112
113         // TODO: Find out why the new dxf filter doesn't work for patterns:
114         RS_FILEIO->fileImport(*gr, path, RS2::FormatDXF1);
115
116         for(RS_Entity * e=gr->firstEntity(); e!=NULL; e=gr->nextEntity())
117         {
118                 if (e->rtti() == RS2::EntityLine || e->rtti() == RS2::EntityArc)
119                 {
120                         RS_Layer * l = e->getLayer();
121                         RS_Entity * cl = e->clone();
122                         cl->reparent(this);
123
124                         if (l != NULL)
125                         {
126                                 cl->setLayer(l->getName());
127                         }
128
129                         addEntity(cl);
130                 }
131         }
132
133         delete gr;
134         loaded = true;
135         RS_DEBUG->print("RS_Pattern::loadPattern: OK");
136
137         return true;
138 }
139
140 /** @return the fileName of this pattern. */
141 QString RS_Pattern::getFileName() const
142 {
143         return fileName;
144 }