Salome HOME
fe17ca832bf4f38beb4612a2b06764755cfd52e1
[modules/shaper.git] / src / XAO / XAO_XaoUtils.cxx
1 // Copyright (C) 2013-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Frederic Pons (OpenCascade)
20
21 #include <sstream>
22
23 #include "XAO_Exception.hxx"
24 #include "XAO_Xao.hxx"
25 #include "XAO_XaoUtils.hxx"
26
27 using namespace XAO;
28
29
30 std::string XaoUtils::intToString(int value)
31 {
32     std::ostringstream str;
33     str << value;
34     return str.str();
35 }
36
37 int XaoUtils::stringToInt(const std::string& value)
38 {
39     int res;
40     std::istringstream convert(value);
41     if ( !(convert >> res) )
42         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to integer.");
43     return res;
44 }
45
46 std::string XaoUtils::doubleToString(double value)
47 {
48     std::ostringstream str;
49     str << value;
50     return str.str();
51 }
52
53 double XaoUtils::stringToDouble(const std::string& value)
54 {
55     double res;
56     std::istringstream convert(value);
57     if ( !(convert >> res) )
58         throw XAO_Exception(MsgBuilder() << "Cannot convert '" << value << "' to double.");
59     return res;
60 }
61
62 std::string XaoUtils::booleanToString(bool value)
63 {
64     return value ? "true" : "false";
65 }
66
67 bool XaoUtils::stringToBoolean(const std::string& value)
68 {
69     if (value == "true" || value == "1")
70         return true;
71     if (value == "false" || value == "0")
72         return false;
73
74     throw XAO_Exception(MsgBuilder() << "Invalid boolean value: " << value);
75 }
76
77 std::string XaoUtils::dimensionToString(XAO::Dimension dimension)
78 {
79     if (dimension == XAO::VERTEX)
80         return "vertex";
81     if (dimension == XAO::EDGE)
82         return "edge";
83     if (dimension == XAO::FACE)
84         return "face";
85     if (dimension == XAO::SOLID)
86         return "solid";
87     if (dimension == XAO::WHOLE)
88         return "whole";
89
90     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
91 }
92
93 XAO::Dimension XaoUtils::stringToDimension(const std::string& dimension)
94 {
95     if (dimension == "vertex")
96         return XAO::VERTEX;
97     if (dimension == "edge")
98         return XAO::EDGE;
99     if (dimension == "face")
100         return XAO::FACE;
101     if (dimension == "solid")
102         return XAO::SOLID;
103     if (dimension == "whole")
104         return XAO::WHOLE;
105
106     throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension);
107 }
108
109 std::string XaoUtils::fieldTypeToString(XAO::Type type)
110 {
111     if (type ==XAO:: BOOLEAN)
112         return "boolean";
113     if (type == XAO::INTEGER)
114         return "integer";
115     if (type == XAO::DOUBLE)
116         return "double";
117     if (type == XAO::STRING)
118         return "string";
119
120     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
121 }
122
123 XAO::Type XaoUtils::stringToFieldType(const std::string& type)
124 {
125     if (type == "boolean")
126         return XAO::BOOLEAN;
127     if (type == "integer")
128         return XAO::INTEGER;
129     if (type == "double")
130         return XAO::DOUBLE;
131     if (type == "string")
132         return XAO::STRING;
133
134     throw XAO_Exception(MsgBuilder() << "Bad type: " << type);
135 }
136
137 std::string XaoUtils::shapeFormatToString(XAO::Format format)
138 {
139     if (format == XAO::BREP)
140         return "BREP";
141     if (format == XAO::STEP)
142         return "STEP";
143
144     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
145 }
146
147 XAO::Format XaoUtils::stringToShapeFormat(const std::string& format)
148 {
149     if (format == "BREP")
150         return XAO::BREP;
151     if (format == "STEP")
152         return XAO::STEP;
153
154     throw XAO_Exception(MsgBuilder() << "Bad format: " << format);
155 }