TexGen
Property.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 "Property.h"
22using namespace TexGen;
23
24CProperty::CProperty(string SIUnits)
25: m_SIUnits(SIUnits)
26, m_dValue(0)
27, m_bSet(false)
28{
29}
30
31void CProperty::WriteAttribute(TiXmlElement &Element, string AttributeName)
32{
33 if (m_bSet)
34 {
35 Element.SetAttribute(AttributeName+"Value", stringify(m_dValue));
36 Element.SetAttribute(AttributeName+"Units", m_Units);
37 }
38}
39
40void CProperty::ReadAttribute(TiXmlElement &Element, string AttributeName)
41{
42 double dValue = 0;
43 const string* pUnits;
44 if (Element.Attribute(AttributeName+"Value", &dValue) &&
45 (pUnits = Element.Attribute(AttributeName+"Units")))
46 {
47 SetValue(dValue, *pUnits);
48 }
49}
50
52{
53 return stringify(m_dValue) + " " + m_Units;
54}
55
56double CProperty::GetValue(string Units) const
57{
58 return ConvertUnits(m_dValue, m_Units, Units);
59}
60
62{
63 return GetValue(m_SIUnits);
64}
65
66void CProperty::SetValue(double dValue, string Units)
67{
68 string Error;
69 if (CompatibleUnits(Units, m_SIUnits, &Error))
70 {
71 m_dValue = dValue;
72 m_Units = Units;
73 m_bSet = true;
74 }
75 else
76 {
77 Reset();
78 TGERROR("Error setting property: " << Error);
79 }
80}
81
82void CProperty::SetValue( double dValue )
83{
84 m_dValue = dValue;
85 m_bSet = true;
86}
87
88void CProperty::SetUnits( string Units )
89{
90 string Error;
91 if ( CompatibleUnits( Units, m_SIUnits, &Error ) )
92 {
93 m_Units = Units;
94 }
95 else
96 {
97 Reset();
98 TGERROR("Error setting property: " << Error);
99 }
100}
101
103{
104 m_dValue = 0;
105 m_Units.clear();
106 m_bSet = false;
107}
108
110{
111 return m_dValue;
112}
113
115{
116 return m_Units;
117}
118
120{
121 return m_SIUnits;
122}
123
125{
126 return m_bSet;
127}
128
129
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
void SetUnits(string Units)
Definition: Property.cpp:88
double m_dValue
Definition: Property.h:48
string GetUnits() const
Definition: Property.cpp:114
bool IsSet() const
Definition: Property.cpp:124
string m_Units
Definition: Property.h:49
void SetValue(double dValue, string Units)
Definition: Property.cpp:66
string GetString() const
Definition: Property.cpp:51
string GetSIUnits() const
Definition: Property.cpp:119
double GetSIValue() const
Definition: Property.cpp:61
double GetValue() const
Definition: Property.cpp:109
string m_SIUnits
Definition: Property.h:50
void WriteAttribute(TiXmlElement &Element, string AttributeName)
Definition: Property.cpp:31
void ReadAttribute(TiXmlElement &Element, string AttributeName)
Definition: Property.cpp:40
Namespace containing a series of customised math operations not found in the standard c++ library.
bool CompatibleUnits(std::string SourceUnits, std::string TargetUnits, std::string *pErrorMessage)
Definition: Misc.cpp:73
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
double ConvertUnits(double dValue, std::string SourceUnits, std::string TargetUnits)
Definition: Misc.cpp:63