TexGen
TexGen.cpp
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2006 Martin Sherburn
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18=============================================================================*/
19
20#include "PrecompiledHeaders.h"
21#include "TexGen.h"
22
23using namespace TexGen;
24
25// Create the single instance on program start
27
28CTexGen::CTexGen(void)
29: m_pLogger(NULL)
30, m_pTextileCallback(NULL)
31, m_iMajorVersion(3)
32, m_iMinorVersion(12)
33, m_iRevision(2)
34, m_bMessagesOn(true)
35{
37 AddNewUnits("tex", "g/km");
38 AddNewUnits("denier", "g / 9 km");
39}
40
42{
44 if (m_pLogger)
45 delete m_pLogger;
46}
47
48string CTexGen::GetName(const CTextile* pTextile) const
49{
50 map<string, CTextile*>::const_iterator itTextile;
51 for (itTextile = m_Textiles.begin(); itTextile != m_Textiles.end(); ++itTextile)
52 {
53 if (itTextile->second == pTextile)
54 return itTextile->first;
55 }
56 return "";
57}
58
59CTextile* CTexGen::GetTextile(string TextileName)
60{
61 if (TextileName.empty())
62 {
63 if (m_Textiles.empty())
64 {
65 TGERROR("Unable to get textile, no textiles have been created");
66 }
67 else
68 {
69 return m_Textiles.begin()->second;
70 }
71 }
72 else
73 {
74 map<string, CTextile*>::iterator itTextile;
75 itTextile = m_Textiles.find(TextileName);
76 if (itTextile != m_Textiles.end())
77 return itTextile->second;
78 TGERROR("Unable to get textile, textile does not exist: " + TextileName);
79 }
80 return NULL;
81}
82
83bool CTexGen::AddTextile(string TextileName, const CTextile &Textile, bool bOverwrite)
84{
85 // Check that the name given is not blank
86 if (TextileName.empty())
87 {
88 TGERROR("Unable to add textile, given textile name is empty");
89 return false;
90 }
91 // Check that the entry does not exist
92 if (m_Textiles.find(TextileName) != m_Textiles.end())
93 {
94 if (!bOverwrite)
95 {
96 TGERROR("Unable to add textile, textile with that name already exists: " + TextileName);
97 return false;
98 }
99 else
100 {
101 delete m_Textiles[TextileName];
102 m_Textiles[TextileName] = Textile.Copy();
103 TGLOG("Replaced textile \"" << TextileName << "\"");
105 m_pTextileCallback(TextileName, true);
106 return true;
107 }
108 }
109 // Add the section to the list using the name as the key
110 m_Textiles[TextileName] = Textile.Copy();
111 TGLOG("Added textile \"" << TextileName << "\"");
113 m_pTextileCallback(TextileName, true);
114 return true;
115}
116
117string CTexGen::AddTextile(const CTextile &Textile)
118{
119 string DefaultName = Textile.GetDefaultName();
120 int iNumber = 1;
121 // Check that the entry does not exist, and the name given is not blank
122 while (m_Textiles.find(DefaultName) != m_Textiles.end())
123 {
124 DefaultName = Textile.GetDefaultName() + "-" + stringify(++iNumber);
125 }
126 // Add the section to the list using the generated name as the key
127 m_Textiles[DefaultName] = Textile.Copy();
128 TGLOG("Added textile \"" << DefaultName << "\"");
130 m_pTextileCallback(DefaultName, true);
131 return DefaultName;
132}
133
134bool CTexGen::DeleteTextile(string TextileName)
135{
136 map<string, CTextile*>::iterator itTextile;
137 itTextile = m_Textiles.find(TextileName);
138 if (itTextile != m_Textiles.end())
139 {
140 delete itTextile->second;
141 m_Textiles.erase(itTextile);
142 TGLOG("Deleted textile \"" << TextileName << "\"");
144 m_pTextileCallback(TextileName, false);
145 return true;
146 }
147 TGERROR("Unable to delete textile, textile does not exist: " + TextileName);
148 return false;
149}
150
151void CTexGen::PopulateTiXmlElement(TiXmlElement &Element, string TextileName, OUTPUT_TYPE OutputType)
152{
153 if (TextileName.empty())
154 {
155 map<string, CTextile*>::const_iterator itTextile;
156 for (itTextile = m_Textiles.begin(); itTextile != m_Textiles.end(); ++itTextile)
157 {
158 TiXmlElement Textile("Textile");
159 Textile.SetAttribute("name", itTextile->first);
160 itTextile->second->PopulateTiXmlElement(Textile, OutputType);
161 Element.InsertEndChild(Textile);
162 }
163 }
164 else
165 {
166 CTextile *pTextile = GetTextile(TextileName);
167 if (pTextile)
168 {
169 TiXmlElement Textile("Textile");
170 Textile.SetAttribute("name", TextileName);
171 pTextile->PopulateTiXmlElement(Textile, OutputType);
172 Element.InsertEndChild(Textile);
173 }
174 }
175}
176
177bool CTexGen::LoadTiXmlElement(TiXmlElement &Element)
178{
179 bool bOverwrite = false;
180 FOR_EACH_TIXMLELEMENT(pTextile, Element, "Textile")
181 {
182 string Name = pTextile->Attribute("name");
183 const string* pType = pTextile->Attribute(string("type"));
184 if (pType)
185 {
186 if (*pType == "CTextileWeave2D")
187 AddTextile(Name, CTextileWeave2D(*pTextile), bOverwrite);
188 else if (*pType == "CTextileWeave3D")
189 AddTextile(Name, CTextileWeave3D(*pTextile), bOverwrite);
190 else if (*pType == "CTextileWeave")
191 AddTextile(Name, CTextileWeave(*pTextile), bOverwrite);
192 else if (*pType == "CTextileOrthogonal")
193 AddTextile(Name, CTextileOrthogonal(*pTextile), bOverwrite);
194 else if (*pType == "CTextileAngleInterlock")
195 AddTextile(Name, CTextileAngleInterlock(*pTextile), bOverwrite);
196 else if (*pType == "CTextileOffsetAngleInterlock")
197 AddTextile(Name, CTextileOffsetAngleInterlock(*pTextile), bOverwrite);
198 else if (*pType == "CTextileLayerToLayer")
199 AddTextile(Name, CTextileLayerToLayer(*pTextile), bOverwrite);
200 else if (*pType == "CTextileDecoupledLToL")
201 AddTextile(Name, CTextileDecoupledLToL(*pTextile), bOverwrite);
202 else if (*pType == "CTextile3DWeave")
203 AddTextile(Name, CTextile3DWeave(*pTextile), bOverwrite);
204 else if (*pType == "CShearedTextileWeave2D")
205 AddTextile(Name, CShearedTextileWeave2D(*pTextile), bOverwrite);
206 else if (*pType == "CTextileLayered")
207 AddTextile(Name, CTextileLayered(*pTextile), bOverwrite);
208 else
209 AddTextile(Name, CTextile(*pTextile), bOverwrite);
210 }
211 }
212 return true;
213}
214
215void CTexGen::SaveToXML(string FileName, string TextileName, OUTPUT_TYPE OutputType)
216{
217 TiXmlDocument doc(FileName);
218 TiXmlDeclaration Declaration("1.0", "", "");
219 doc.InsertEndChild(Declaration);
220 TiXmlElement Root("TexGenModel");
221 Root.SetAttribute("version", GetVersion());
222 PopulateTiXmlElement(Root, TextileName, OutputType);
223 doc.InsertEndChild(Root);
224 if (doc.SaveFile())
225 {
226 TGLOG("XML file saved to \"" << FileName << "\"");
227 }
228 else
229 {
230 TGERROR("Error saving XML file to \"" << FileName << "\"");
231 }
232}
233
234bool CTexGen::ReadFromXML(string FileName)
235{
236 TGLOGINDENT("Loading XML file: \"" << FileName << "\"");
237 TiXmlDocument doc(FileName);
238 if (doc.LoadFile())
239 {
240 TiXmlElement* pRoot = doc.RootElement();
241 if (pRoot)
242 {
243 string Version = pRoot->Attribute("version");
244 if (Version != GetVersion())
245 {
246 TGERROR("Warning: File was created with version " << Version << ", current version is " << GetVersion());
247 }
248 if (LoadTiXmlElement(*pRoot))
249 {
250 TGLOG("XML file loaded from \"" << FileName << "\"");
251 return true;
252 }
253 }
254 }
255
256 TGERROR("Error loading XML file from \"" << FileName << "\"");
257
258 return false;
259}
260
262{
263 while (!m_Textiles.empty())
264 {
265 DeleteTextile(m_Textiles.begin()->first);
266 }
267}
268
269void CTexGen::SetLogger(const CLogger &Logger)
270{
271 if (m_pLogger)
272 delete m_pLogger;
273 m_pLogger = Logger.Copy();
274}
275
276void CTexGen::SetTextileCallback(void (*pTextileCallback)(string TextileName, bool bAdded))
277{
278 m_pTextileCallback = pTextileCallback;
279}
280
282{
284}
285
286void CTexGen::SetMessages( bool bMessagesOn, const CLogger &Logger )
287{
288 m_bMessagesOn = bMessagesOn;
289 SetLogger( Logger );
290}
291
292void CTexGen::SetMessages( bool bMessagesOn )
293{
294 m_bMessagesOn = bMessagesOn;
295 if ( m_bMessagesOn )
297 else
299}
300
301void CTexGen::GetTextileNames( vector<string> &Names )
302{
303 map<string, CTextile*>::iterator itTextile;
304 Names.clear();
305
306 for ( itTextile = m_Textiles.begin(); itTextile != m_Textiles.end(); ++itTextile )
307 {
308 Names.push_back( itTextile->first );
309 }
310}
311
312
313
314
#define TGLOGINDENT(MESSAGE)
Combines the TGLOG macro and TGLOGAUTOINDENT macro in one.
Definition: Logger.h:68
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
#define TGLOG(MESSAGE)
Definition: Logger.h:36
#define FOR_EACH_TIXMLELEMENT(CHILDELEMENT, PARENTELEMENT, ELEMENTNAME)
Macro to enable looping over tinyxml easier.
Definition: Misc.h:45
#define CLASS_DECLSPEC
Definition: Misc.h:35
#define NULL
Definition: ShinyConfig.h:50
Abstract base class to act as an interface between texgen and the logger.
Definition: Logger.h:85
virtual CLogger * Copy() const =0
Logger used to send all log and error messages into a black hole.
Definition: Logger.h:113
Logger used to print all log and error messages to the screen.
Definition: Logger.h:102
Respresents a 2d woven textile with shear applied.
Template used as a base class for creating singletons.
Definition: Singleton.h:32
Singleton class holding the Textiles in a database.
Definition: TexGen.h:86
bool LoadTiXmlElement(TiXmlElement &Element)
Used for loading data from XML.
Definition: TexGen.cpp:177
void PopulateTiXmlElement(TiXmlElement &Element, string TextileName="", OUTPUT_TYPE OutputType=OUTPUT_STANDARD)
Used for saving data to XML.
Definition: TexGen.cpp:151
string GetName(const CTextile *pTextile) const
Get the name of the textile with given pointer.
Definition: TexGen.cpp:48
map< string, CTextile * > m_Textiles
List of textiles created.
Definition: TexGen.h:159
CLogger * m_pLogger
Logger used to keep track of how error messages and log messages displayed or stored.
Definition: TexGen.h:160
int m_iRevision
Definition: TexGen.h:165
string GetVersion() const
Get the version of TexGen.
Definition: TexGen.cpp:281
int m_iMinorVersion
Definition: TexGen.h:164
void SetMessages(bool bMessagesOn, const CLogger &Logger)
Switch messages on/off.
Definition: TexGen.cpp:286
void SetTextileCallback(void(*pTextileCallback)(string TextileName, bool bAdded))
Set the callback function when a textile is added or deleted.
Definition: TexGen.cpp:276
void DeleteTextiles()
Clear Textiles.
Definition: TexGen.cpp:261
void SetLogger(const CLogger &Logger)
Set the logger.
Definition: TexGen.cpp:269
void(* m_pTextileCallback)(string TextileName, bool bAdded)
Definition: TexGen.h:162
bool ReadFromXML(string FileName)
Read TexGen XML file.
Definition: TexGen.cpp:234
CTextile * GetTextile(string TextileName="")
Get a textile with given name.
Definition: TexGen.cpp:59
bool AddTextile(string TextileName, const CTextile &Textile, bool bOverwrite=false)
Add Textile.
Definition: TexGen.cpp:83
int m_iMajorVersion
Definition: TexGen.h:163
void GetTextileNames(vector< string > &Names)
Get list of textile names.
Definition: TexGen.cpp:301
bool m_bMessagesOn
Definition: TexGen.h:161
~CTexGen(void)
Definition: TexGen.cpp:41
bool DeleteTextile(string TextileName)
Delete a textile.
Definition: TexGen.cpp:134
void SaveToXML(string FileName, string TextileName="", OUTPUT_TYPE OutputType=OUTPUT_STANDARD)
Save TexGen XML file.
Definition: TexGen.cpp:215
Represents a 3D woven textile.
Represents a 3D angle interlock woven textile.
Represents a textile cell containing yarns.
Definition: Textile.h:39
virtual string GetDefaultName() const
Get the default name to assign to a textile.
Definition: Textile.h:162
virtual void PopulateTiXmlElement(TiXmlElement &Element, OUTPUT_TYPE OutputType)
Used for saving data to XML.
Definition: Textile.cpp:87
virtual CTextile * Copy() const
Definition: Textile.h:48
Represents a textile made up from several layers of weaves.
Represents a 3D angle interlock woven textile.
Represents a 3D orthogonal woven textile.
Respresents a 2d woven textile.
Respresents a 3d woven textile.
Represents a woven textile.
Definition: TextileWeave.h:41
Namespace containing a series of customised math operations not found in the standard c++ library.
OUTPUT_TYPE
Definition: Misc.h:105
void AddNewUnits(std::string NewUnit, std::string BaseUnits)
Definition: Misc.cpp:98
std::string stringify(const T &x, int iPrecision=12, bool bScientific=true)
Function to convert a value (e.g. int, double, etc...) to a string.
Definition: Misc.h:50