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