Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeInfo.h
1 // Copyright (C) 2014-2023  CEA, EDF
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
20 #ifndef GeomAlgoAPI_ShapeInfo_H_
21 #define GeomAlgoAPI_ShapeInfo_H_
22
23 #include "GeomAlgoAPI.h"
24
25 #include <GeomAPI_Shape.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Pnt.h>
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Face.h>
30
31 #include <string>
32
33 /// Keeps values of different possible types, used in python shapeInfo.
34 class GeomAlgoAPI_InfoValue
35 {
36   int myType;        //< type stored in this Value
37   std::string myStr; //< type 0
38   double myDouble;   //< type 1
39   bool myBool;       //< type 2
40
41 public:
42   GeomAlgoAPI_InfoValue() : myType(-1) {} // invalid case
43   GeomAlgoAPI_InfoValue(const char* theVal) : myType(0), myStr(theVal) {}
44   GeomAlgoAPI_InfoValue(const std::string theVal) : myType(0), myStr(theVal) {}
45   GeomAlgoAPI_InfoValue(const double theVal) : myType(1), myDouble(theVal) {}
46   GeomAlgoAPI_InfoValue(const bool theVal) : myType(2), myBool(theVal) {}
47
48   const int& type() const { return myType; }
49   const std::string& string() const { return myStr; }
50   const double& real() const { return myDouble; }
51   const bool& boolean() const { return myBool; }
52 };
53
54 /// \class GeomAlgoAPI_ShapeInfo
55 /// \ingroup DataAlgo
56 /// \brief Provides information about shapes in textual (HTML for Information Panel)  or
57 ///        a list of parameters order (for python "shapeInfo" command).
58 ///
59 /// At least the following geometrical items are supported:
60 /// 0D: Vertex,
61 /// 1D : Line segment, Polygon, Circle, Arc, Ellipse, Arc of ellipse,
62 /// 2D : Rectangle, Polygon, Disc with circular or elliptical base, Sphere, Cylinder, Cone, Torus
63 ///
64 /// It is initialized by a shape and fors the needed output depending on the requested format.
65 class GeomAlgoAPI_ShapeInfo
66 {
67   /// Keeps the shape, source of the produced information.
68   std::shared_ptr<GeomAPI_Shape> myShape;
69   std::string myShapeType; //< detected shape type
70 public:
71
72   /// Initializes a parameters input by an input shape.
73   GEOMALGOAPI_EXPORT GeomAlgoAPI_ShapeInfo(const std::shared_ptr<GeomAPI_Shape> theShape)
74    : myShape(theShape) {}
75
76   /// A translator to the current language is needed for HTML export.
77   /// It is provided by a caller of this class.
78   class Translator
79   {
80   public:
81     Translator() {}
82     GEOMALGOAPI_EXPORT virtual std::string translate(const char* theSource) { return theSource; }
83   };
84
85   /// Returns an HTML text of a shape info.
86   GEOMALGOAPI_EXPORT std::string html(Translator* theTranslator);
87
88   /// Returns the detected shape type, or empty string.
89   GEOMALGOAPI_EXPORT const std::string& shapeType() { return myShapeType; }
90
91   /// Sets the shape type
92   void setShapeType(const std::string theType) { myShapeType = theType; }
93
94   /// Returns a list of shape information values.
95   GEOMALGOAPI_EXPORT std::list<GeomAlgoAPI_InfoValue> values();
96
97 private:
98   /// Possible types of export values
99   enum InfoType {
100     TYPE_HTML,
101     TYPE_VALS
102   };
103
104   /// Allows to input values in a defined format, then - to export them.
105   class Values
106   {
107     /// A type of stored shape info.
108     InfoType myType;
109     /// An HTML string container.
110     std::string myStr;
111     Translator* myTr; //< an external translator
112     /// A container of values: type of the value -> value
113     std::list<GeomAlgoAPI_InfoValue> myVals;
114   public:
115
116     /// Initialization of an empty container, default translator
117     Values(const InfoType theType);
118
119     /// Returns HTML representation of values
120     const std::string& html() const { return myStr; }
121
122     void setTranslator(Translator* theTranslator) { myTr = theTranslator; }
123     Translator* translator() { return myTr; }
124
125     /// Returns list of values representation.
126     const std::list<GeomAlgoAPI_InfoValue>& values() const { return myVals; }
127
128     /// Adds next value
129     Values& operator<< (double theValue);
130     /// Adds next value
131     Values& operator<< (bool theValue);
132     /// Adds string
133     Values& operator<< (const char* theStr);
134     /// Adds point coordinates
135     void addPoint(const char* theTitle, const GeomPointPtr& thePoint);
136     /// Adds directon coordinates
137     void addDirection(const char* theTitle, const GeomDirPtr& thePoint);
138     /// Adds named value
139     void addNamedValue(const char* theName, const double theValue);
140     /// Adds named value
141     void addNamedValue(const char* theName, const bool theValue);
142     /// Adds a name (title) of the group
143     void addGroupName(const char* theName);
144   };
145
146   /// Gets information from the shape
147   void processShape(Values& theVals);
148
149   /// Gets particual information from the edge
150   void processEdge(Values& theVals, GeomEdgePtr theEdge);
151
152   /// Gets particual information from face
153   void processFace(Values& theVals, GeomFacePtr theFace);
154 };
155
156 #endif