CFx SDK Documentation  2022 SP0
OdUnitsFormatter.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, 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 Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 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 
33 class OdCmColorBase;
34 class OdGePoint3d;
35 
42 {
43 public:
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 
141  enum AUnits
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 
172  static double unformatA(const OdString& string);
173  static double unformatA_unnorm(const OdString& string, bool refuseDots = false);
174 
175 private:
176  static OdString _formatL(double value, bool isItArea, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
177 };
178 
183 
184 
191 {
192 public:
195 
196  static OdString formatDecimal(double value, int precision, int dimzin, const OdChar* decsep, const OdChar* thsep = 0);
197  static double angle(const OdChar*& buf, bool refuseDots = false);
198  static int toInt(const OdString& sValue, int nMinValid = INT_MIN, int nMaxValid = INT_MAX);
199  static bool isZero(double v, int precision);
200  static OdString formatArchitectural(bool isNegative, int feet, int entier, int numerator, int denominator, int dimzin, int mode);
201  static bool negative(const OdChar*& buf);
202  static double integer(const OdChar*& buf);
203  static void fraction(double value, int& entier, int& numerator, int& denominator, int precision)
204  {
205  denominator = 1 << precision;
206  value = double(floor(value * denominator + 0.5)) / double(denominator);
207  entier = int(floor(value));
208  numerator = int((value - entier) * double(denominator));
209  while((numerator % 2) == 0 && (numerator != 0))
210  {
211  numerator /= 2;
212  denominator /= 2;
213  }
214  }
215 
216  static double base_denominator(int prec, double base)
217  {
218  double denom = 1.;
219  for(int i = 0; i < prec; ++i)
220  denom *= base;
221  return denom;
222  }
223 
224  static double linear_denominator(int prec)
225  {
226  return base_denominator(prec, 10.);
227  }
228 
229  static bool digit(OdChar c)
230  {
231  return (c >= '0' && c <= '9');
232  }
233 
234  static double denominator(int prec)
235  {
236  double denom = 1.;
237  switch(prec)
238  {
239  default:
240  {
241  for(int i = prec; i >= 5; i--)
242  denom *= 10.;
243  }
244  //case 8: // 19d58'58.8889"
245  // denom *= 10;
246  //case 7: // 19d58'58.889"
247  // denom *= 10;
248  //case 6: // 19d58'58.89"
249  // denom *= 10;
250  //case 5: // 19d58'58.9"
251  // denom *= 10;
252  case 4: // 19d58'59"
253  case 3:
254  denom *= 60;
255  case 2: // 19d59'
256  case 1:
257  denom *= 60;
258  case 0: // 20d
259  break;
260  }
261  return denom;
262  }
263 
264  static void decomp(double v, int& degs, int& mins, double& secs, int prec);
265 
266  static OdString format(int degs, int mins, double secs, int prec)
267  {
268  OdString res;
269  switch (prec)
270  {
271  case 0:
272  res.format(OD_T("%dd"), degs);
273  break;
274  case 1:
275  case 2:
276  res.format(OD_T("%dd%d'"), degs, mins);
277  break;
278  case 3:
279  prec = 4;
280  // no break
281  default:
282  prec -= 4;
283  OdString s = odDToStr(secs, 'f', prec);
284  res.format(OD_T("%dd%d'%ls\""), degs, mins, s.c_str());
285  break;
286  }
287 
288  return res;
289  }
290 
291  static OdString next(OdString& list, const OdChar* delim = OD_T(","))
292  {
293  OdString res = list.spanExcluding(delim);
294  if(res.getLength() != list.getLength())
295  {
296  list = list.mid(res.getLength()+1);
297  }
298  else
299  {
300  list.empty();
301  }
302  return res;
303  }
304 
305  static int countOccurences(const OdString& string, OdChar delim = ',')
306  {
307  int nOccurences = 0;
308  int nDigits = 0;
309  int nSpec = 0;
310  const OdChar *pBuffer = string.c_str();
311  while (*pBuffer)
312  {
313  if (*pBuffer >= '0' && *pBuffer <= '9')
314  nDigits++;
315  else if (*pBuffer == '+' || *pBuffer == '-')
316  {
317  nSpec++;
318  if (nDigits > 0)
319  break;
320  }
321  else if (*pBuffer == delim && nDigits > 0 && nSpec <= 1)
322  {
323  nDigits = nSpec = 0;
324  nOccurences++;
325  }
326  else
327  break;
328  pBuffer++;
329  }
330  return nOccurences;
331  }
332 };
333 
334 #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
const OdChar * c_str() const
Definition: OdString.h:200
OdString mid(int startIndex, int length) const
void empty()
OdString & format(const OdChar *formatString,...)
OdString spanExcluding(const OdChar *charSet) const
int getLength() const
Definition: OdString.h:130
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
virtual OdSharedPtr< OdCmColorBase > unformatCmColor(const OdString &string) const =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 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