CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
OdUnitsFormatter.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, Open Design Alliance (the "Alliance").
3// All rights reserved.
4//
5// This software and its documentation and related materials are owned by
6// the Alliance. The software may only be incorporated into application
7// programs owned by members of the Alliance, subject to a signed
8// Membership Agreement and Supplemental Software License Agreement with the
9// Alliance. The structure and organization of this software are the valuable
10// trade secrets of the Alliance and its suppliers. The software is also
11// protected by copyright law and international treaty provisions. Application
12// programs incorporating this software must include the following statement
13// with their copyright notices:
14//
15// This application incorporates Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 by Open Design Alliance.
18// All rights reserved.
19//
20// By use of this software, its documentation or related materials, you
21// acknowledge and accept the above terms.
23
24#ifndef _OD_UNITSFORMATTER_H_INCLUDED_
25#define _OD_UNITSFORMATTER_H_INCLUDED_
26
27#include "OdPlatform.h"
28#include "RxObject.h"
29#include "OdString.h"
30#include "OdDToStr.h"
31#include "SharedPtr.h"
32
33class OdCmColorBase;
34class OdGePoint3d;
35
42{
43public:
45
46 enum LUnits
47 {
48 kScientific = 1,
49 kDecimal = 2,
50 kEngineering = 3, // feet and decimal inches
51 kArchitectural = 4, // feet and fractional inches
52 kFractional = 5,
53 kWindowsDesktop = 6
54 };
55
57 {
58 kSuppressesZeroFeetAndPreciselyZeroInches = 0,
59 kIncludesZeroFeetAndPreciselyZeroInches = 1,
60 kIncludesZeroFeetAndSuppressesZeroInches = 2,
61 kIncludesZeroInchesAndSuppressesZeroFeet = 3,
62 // for dimentions
63 kSuppressesLeadingZeros = 4,
64 kSuppressesTrailingZeros = 8,
65 kSuppressesBothLeadingAndTrailingZeros = 12
66 };
67
72 virtual OdString formatCmColor(const OdCmColorBase& value) const = 0;
73
78 virtual OdSharedPtr<OdCmColorBase> unformatCmColor(const OdString& string) const = 0;
79
86 virtual OdString formatLinear(double value) const = 0;
87
94 virtual double unformatLinear(const OdString& string) const = 0;
95
104 virtual OdString formatAngle(double value) const = 0;
105
114 virtual double unformatAngle(const OdString& string) const = 0;
115
116 static bool isZeroFeetSuppressed(int dimzin);
117 static bool isZeroInchesSuppressed(int dimzin);
118
132 static OdString formatL(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
133 static OdString formatArea(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
134
139 static double unformatL(const OdString& string);
140
142 {
143 kDegrees = 0,
144 kDegMinSec = 1,
145 kGrads = 2,
146 kRadians = 3,
147 kSurveyors = 4
148 };
149
150
164 static OdString formatA(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0);
165 static OdString formatA_unnorm(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0);
166
173 static double unformatA(const OdString& string);
174 static double unformatA_unnorm(const OdString& string, bool refuseDots = false);
175
176private:
177 static OdString _formatL(double value, bool isItArea, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
178 static OdString _formatA(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0);
179};
180
185
186
193{
194public:
197
198 static OdString formatDecimal(double value, int precision, int dimzin, const OdChar* decsep, const OdChar* thsep = 0);
199 static double angle(const OdChar*& buf, bool refuseDots = false);
200 static int toInt(const OdString& sValue, int nMinValid = INT_MIN, int nMaxValid = INT_MAX);
201 static bool isZero(double v, int precision);
202 static OdString formatArchitectural(bool isNegative, int feet, int entier, int numerator, int denominator, int dimzin, int mode);
203 static bool negative(const OdChar*& buf);
204 static double integer(const OdChar*& buf);
205 static double number(const OdChar*& buf, bool* pHasDot = 0, bool* pHasExponent = 0);
206 static void fraction(double value, int& entier, int& numerator, int& denominator, int precision)
207 {
208 denominator = 1 << precision;
209 value = double(floor(value * denominator + 0.5)) / double(denominator);
210 entier = int(floor(value));
211 numerator = int((value - entier) * double(denominator));
212 while((numerator % 2) == 0 && (numerator != 0))
213 {
214 numerator /= 2;
215 denominator /= 2;
216 }
217 }
218
219 static double base_denominator(int prec, double base)
220 {
221 double denom = 1.;
222 for(int i = 0; i < prec; ++i)
223 denom *= base;
224 return denom;
225 }
226
227 static double linear_denominator(int prec)
228 {
229 return base_denominator(prec, 10.);
230 }
231
232 static bool digit(OdChar c)
233 {
234 return (c >= '0' && c <= '9');
235 }
236
237 static double denominator(int prec)
238 {
239 double denom = 1.;
240 switch(prec)
241 {
242 default:
243 {
244 for(int i = prec; i >= 5; i--)
245 denom *= 10.;
246 }
247 //case 8: // 19d58'58.8889"
248 // denom *= 10;
249 //case 7: // 19d58'58.889"
250 // denom *= 10;
251 //case 6: // 19d58'58.89"
252 // denom *= 10;
253 //case 5: // 19d58'58.9"
254 // denom *= 10;
255 case 4: // 19d58'59"
256 case 3:
257 denom *= 60;
258 case 2: // 19d59'
259 case 1:
260 denom *= 60;
261 case 0: // 20d
262 break;
263 }
264 return denom;
265 }
266
267 static void decomp(double v, int& degs, int& mins, double& secs, int prec);
268
269 static OdString format(int degs, int mins, double secs, int prec)
270 {
271 OdString res;
272 switch (prec)
273 {
274 case 0:
275 res.format(OD_T("%dd"), degs);
276 break;
277 case 1:
278 case 2:
279 res.format(OD_T("%dd%d'"), degs, mins);
280 break;
281 case 3:
282 prec = 4;
283 // no break
284 default:
285 prec -= 4;
286 OdString s = odDToStr(secs, 'f', prec);
287 res.format(OD_T("%dd%d'%ls\""), degs, mins, s.c_str());
288 break;
289 }
290
291 return res;
292 }
293
294 static OdString next(OdString& list, const OdChar* delim = OD_T(","))
295 {
296 OdString res = list.spanExcluding(delim);
297 if(res.getLength() != list.getLength())
298 {
299 list = list.mid(res.getLength()+1);
300 }
301 else
302 {
303 list.empty();
304 }
305 return res;
306 }
307
308 static int countOccurences(const OdString& string, OdChar delim = ',')
309 {
310 int nOccurences = 0;
311 int nDigits = 0;
312 int nSpec = 0;
313 const OdChar *pBuffer = string.c_str();
314 while (*pBuffer)
315 {
316 if (*pBuffer >= '0' && *pBuffer <= '9')
317 nDigits++;
318 else if (*pBuffer == '+' || *pBuffer == '-')
319 {
320 nSpec++;
321 if (nDigits > 0)
322 break;
323 }
324 else if (*pBuffer == delim && nDigits > 0 && nSpec <= 1)
325 {
326 nDigits = nSpec = 0;
327 nOccurences++;
328 }
329 else
330 break;
331 pBuffer++;
332 }
333 return nOccurences;
334 }
335};
336
337#endif //#ifndef _OD_UNITSFORMATTER_H_INCLUDED_
FIRSTDLL_EXPORT void odDToStr(char *dst, double val, char fmt, int prec, int cropzeros=0)
#define OD_T(x)
#define ODRX_ABSTRACT
wchar_t OdChar
OdSmartPtr< OdUnitsFormatter > OdUnitsFormatterPtr
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
OdString mid(int startIndex, int length) const
OdString & format(const OdChar *formatString,...)
void empty()
const OdChar * c_str() const
Definition: OdString.h:203
OdString spanExcluding(const OdChar *charSet) const
int getLength() const
Definition: OdString.h:133
virtual OdSharedPtr< OdCmColorBase > unformatCmColor(const OdString &string) const =0
virtual OdString formatLinear(double value) const =0
virtual double unformatAngle(const OdString &string) const =0
static OdString formatArea(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar *decsep=0, const OdChar *thsep=0)
virtual OdString formatCmColor(const OdCmColorBase &value) const =0
static OdString formatA_unnorm(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar *decsep=0)
static double unformatA_unnorm(const OdString &string, bool refuseDots=false)
static OdString formatA(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar *decsep=0)
static OdString formatL(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar *decsep=0, const OdChar *thsep=0)
static bool isZeroFeetSuppressed(int dimzin)
static double unformatA(const OdString &string)
virtual double unformatLinear(const OdString &string) const =0
ODRX_DECLARE_MEMBERS(OdUnitsFormatter)
virtual OdString formatAngle(double value) const =0
static double unformatL(const OdString &string)
static bool isZeroInchesSuppressed(int dimzin)
static OdString formatArchitectural(bool isNegative, int feet, int entier, int numerator, int denominator, int dimzin, int mode)
static double integer(const OdChar *&buf)
static int countOccurences(const OdString &string, OdChar delim=',')
static void fraction(double value, int &entier, int &numerator, int &denominator, int precision)
static OdString next(OdString &list, const OdChar *delim=OD_T(","))
static double number(const OdChar *&buf, bool *pHasDot=0, bool *pHasExponent=0)
static OdString formatDecimal(double value, int precision, int dimzin, const OdChar *decsep, const OdChar *thsep=0)
static double denominator(int prec)
static double base_denominator(int prec, double base)
static void decomp(double v, int &degs, int &mins, double &secs, int prec)
static bool digit(OdChar c)
static double linear_denominator(int prec)
static OdString format(int degs, int mins, double secs, int prec)
static bool isZero(double v, int precision)
static int toInt(const OdString &sValue, int nMinValid=INT_MIN, int nMaxValid=INT_MAX)
static bool negative(const OdChar *&buf)
static double angle(const OdChar *&buf, bool refuseDots=false)
const GLfloat * v
Definition: gles2_ext.h:315
GLenum GLint GLint * precision
Definition: gles2_ext.h:563
GLsizei const GLfloat * value
Definition: gles2_ext.h:302