zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/////////////////////////////////////////////////////////////////////////////// 
// Copyright (C) 2002-2016, Open Design Alliance (the "Alliance"). 
// All rights reserved. 
// 
// This software and its documentation and related materials are owned by 
// the Alliance. The software may only be incorporated into application 
// programs owned by members of the Alliance, subject to a signed 
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable  
// trade secrets of the Alliance and its suppliers. The software is also 
// protected by copyright law and international treaty provisions. Application  
// programs incorporating this software must include the following statement 
// with their copyright notices:
//   
//   This application incorporates Teigha(R) software pursuant to a license 
//   agreement with Open Design Alliance.
//   Teigha(R) Copyright (C) 2002-2016 by Open Design Alliance. 
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you 
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
 
#ifndef _OD_UNITSFORMATTER_H_INCLUDED_
#define _OD_UNITSFORMATTER_H_INCLUDED_
 
#include "OdPlatform.h"
#include "DbExport.h"
#include "RxObject.h"
#include "OdString.h"
#include "OdDToStr.h"
#include "SharedPtr.h"
 
class OdCmColorBase;
class OdGePoint3d;
 
/** \details
    This class defines the interface for a Units Formatter.
 
    <group Other_Classes>
*/
class ODRX_ABSTRACT FIRSTDLL_EXPORT OdUnitsFormatter : public OdRxObject
public:
  ODRX_DECLARE_MEMBERS(OdUnitsFormatter);
 
  enum LUnits
  {
    kScientific    = 1,
    kDecimal       = 2,
    kEngineering   = 3, // feet and decimal inches
    kArchitectural = 4, // feet and fractional inches
    kFractional    = 5,
    kWindowsDesktop = 6
  };
 
  enum DimzinValues
  {
    kSuppressesZeroFeetAndPreciselyZeroInches = 0,
    kIncludesZeroFeetAndPreciselyZeroInches   = 1,
    kIncludesZeroFeetAndSuppressesZeroInches  = 2,
    kIncludesZeroInchesAndSuppressesZeroFeet  = 3,
    // for dimentions
    kSuppressesLeadingZeros                   = 4,
    kSuppressesTrailingZeros                  = 8,
    kSuppressesBothLeadingAndTrailingZeros    = 12
  };
 
  /** \details
    Returns the specified color value or OdCmColorBase object as a string.
    \param value [in]  Value to format.
  */
  virtual OdString formatCmColor(const OdCmColorBase& value) const = 0;
 
  /** \details
    Returns the specified string as an wrapped OdCmColorBase object.
    \param string [in]  String to unformat.
  */
  virtual OdSharedPtr<OdCmColorBase> unformatCmColor(const OdString& string) const = 0;
 
  /** \details
    Returns the specified distance value as a string.
    \param value [in]  Value to format.
    \remarks
    The returned value depends on the DIMZIN, LUNITS, LUPREC, and UNITMODE system variables.
  */
  virtual OdString formatLinear(double value) const = 0;
 
  /** \details
    Returns the specified string as a distance value.
    \param string [in]  String to unformat.
    \remarks
    The returned value depends on the UNITMODE system variable.
  */
  virtual double unformatLinear(const OdString& string) const = 0;
 
  /** \details
    Returns the specified angle value as a string.
    \param value [in]  Value to format.
    \remarks
    The returned value depends on the ANGBASE, AUNITS, AUPREC, DIMZIN, and UNITMODE system variables.
    \note
    All angles are expressed in radians.
  */
  virtual OdString formatAngle(double value) const = 0;
 
  /** \details
    Returns the specified string as an angle value.
    \param string [in]  String to unformat.
    \remarks
    The returned value depends on the ANGBASE system variable.
    \note
    All angles are expressed in radians.
  */
  virtual double unformatAngle(const OdString& string) const = 0;
 
  static bool isZeroFeetSuppressed(int dimzin);
  static bool isZeroInchesSuppressed(int dimzin);
 
  /** \details
    Returns the specified distance value as a string.
    \param value [in]  Value to format.
    \param lUnits [in]  Length units. 
    \param precision [in]  Number of decimal places.
    \param dimzin [in]  A combination of ZeroSuppressionFlags.
    \param unitMode [in]  UNITMODE value.
    \param decsep [in]  Decimal separator.
    \param thsep [in]  Thousands separator.
    \remarks
    * A zero value for decsep specifies the default seoarator is to be used.
    * A zero value for thsep specifies that no separator is to be used.
  */
  static OdString formatL(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
  static OdString formatArea(double value, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
 
  /** \details
    Returns the specified length string as a length value.
    \param string [in]  String to unformat.
  */
  static double unformatL(const OdString& string);
 
  enum AUnits
  {
    kDegrees    = 0,
    kDegMinSec  = 1,
    kGrads      = 2,
    kRadians    = 3,
    kSurveyors  = 4
  };
 
 
  /** \details
    Returns the specified angular value as a string.
    \param value [in]  Value to format.
    \param aUnits [in]  Angle units. 
    \param precision [in]  Number of decimal places.
    \param dimzin [in]  A combination of ZeroSuppressionFlags.
    \param unitMode [in]  UNITMODE value.
    \param decsep [in]  Decimal separator.
    \remarks
    * A zero value for decsep specifies the default separator is to be used.
    \note
    All angles are expressed as radians.
  */
  static OdString formatA(double value, AUnits aUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0);
 
  /** \details
    Returns the specified string as an angular value.
    \param string [in]  String to unformat.
    \note
    All angles are expressed as radians.
  */
  static double unformatA(const OdString& string);
  static double unformatA_unnorm(const OdString& string, bool refuseDots = false);
 
private:
  static OdString _formatL(double value, bool isItArea, LUnits lUnits, int precision, int dimzin, int unitMode, const OdChar* decsep = 0, const OdChar* thsep = 0);
};
 
/** \details
  This template class is a specialization of the OdSmartPtr class for OdDbUnitsFormatter object pointers.
*/
typedef OdSmartPtr<OdUnitsFormatter> OdUnitsFormatterPtr;
 
 
/** \details
    This class defines the interface for a Units Formatter.
 
    <group Other_Classes>
*/
class ODRX_ABSTRACT FIRSTDLL_EXPORT OdUnitsFormatterTool
public:
  OdUnitsFormatterTool()  {}
  ~OdUnitsFormatterTool() {}
 
  static OdString formatDecimal(double value, int precision, int dimzin, const OdChar* decsep, const OdChar* thsep = 0);
  static double angle(const OdChar*& buf, bool refuseDots = false);
  static int toInt(const OdString& sValue, int nMinValid = INT_MIN, int nMaxValid = INT_MAX);
  static bool isZero(double v, int precision);
  static OdString formatArchitectural(bool isNegative, int feet, int entier, int numerator, int denominator, int dimzin, int mode);
  static bool negative(const OdChar*& buf);
  static double integer(const OdChar*& buf);
  static void fraction(double value, int& entier, int& numerator, int& denominator, int precision)
  {
    denominator = 1 << precision;
    value = double(floor(value * denominator + 0.5)) / double(denominator);
    entier = int(floor(value));
    numerator = int((value - entier) * double(denominator));
    while((numerator % 2) == 0 && (numerator != 0))
    {
      numerator   /= 2;
      denominator /= 2;
    }
  }
 
  static double base_denominator(int prec, double base)
  {
    double denom = 1.;
    for(int i = 0; i < prec; ++i)
      denom *= base;
    return denom;
  }
 
  static double linear_denominator(int prec)
  {
    return base_denominator(prec, 10.);
  }
 
  static bool digit(OdChar c)
  {
    return (c >= '0' && c <= '9');
  }
 
  static double denominator(int prec)
  {
    double denom = 1.;
    switch(prec)
    {
    default:
    {
      for(int i = prec; i >= 5; i--)
        denom *= 10.;
    }
      //case 8: // 19d58'58.8889"
      //  denom *= 10;
      //case 7: // 19d58'58.889"
      //  denom *= 10;
      //case 6: // 19d58'58.89"
      //  denom *= 10;
      //case 5: // 19d58'58.9"
      //  denom *= 10;
    case 4: // 19d58'59"
    case 3:
      denom *= 60;
    case 2: // 19d59'
    case 1:
      denom *= 60;
    case 0: // 20d
      break;
    }
    return denom;
  }
 
  static void decomp(double v, int& degs, int& mins, double& secs, int prec);
 
  static OdString format(int degs, int mins, double secs, int prec)
  {
      OdString res;
      switch (prec)
      {
      case 0:
        res.format(OD_T("%dd"), degs);
        break;
      case 1:
      case 2:
        res.format(OD_T("%dd%d'"), degs, mins);
        break;
      case 3:
        prec = 4;
        // no break
      default:
        prec -= 4;
        OdString s = odDToStr(secs, 'f', prec);
        res.format(OD_T("%dd%d'%ls\""), degs, mins, s.c_str());
        break;
      }
 
    return res;
  }
 
  static OdString next(OdString& list, const OdChar* delim = OD_T(","))
  {
    OdString res = list.spanExcluding(delim);
    if(res.getLength() != list.getLength())
    {
      list = list.mid(res.getLength()+1);
    }
    else
    {
      list.empty();
    }
    return res;
  }
 
  static int countOccurences(const OdString& string, OdChar delim = ',')
  {
    int nOccurences = 0;
    int nDigits = 0;
    int nSpec = 0;
    const OdChar *pBuffer = string.c_str();
    while (*pBuffer)
    {
      if (*pBuffer >= '0' && *pBuffer <= '9')
        nDigits++;
      else if (*pBuffer == '+' || *pBuffer == '-')
      {
        nSpec++;
        if (nDigits > 0)
          break;
        }
      else if (*pBuffer == delim && nDigits > 0 && nSpec <= 1)
      {
        nDigits = nSpec = 0;
        nOccurences++;
      }
      else
        break;
      pBuffer++;
    } 
    return nOccurences;
  }
};
 
#endif //#ifndef _OD_UNITSFORMATTER_H_INCLUDED_