Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.h
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModelHighAPI_Dumper.h
4 // Created:     1 August 2016
5 // Author:      Artem ZHIDKOV
6
7 #ifndef ModelHighAPI_Dumper_H_
8 #define ModelHighAPI_Dumper_H_
9
10 #include "ModelHighAPI.h"
11
12 #include <map>
13 #include <memory>
14 #include <set>
15 #include <sstream>
16 #include <string>
17
18 class GeomAPI_Pnt;
19 class GeomAPI_Dir;
20
21 class GeomDataAPI_Dir;
22 class GeomDataAPI_Point;
23 class GeomDataAPI_Point2D;
24
25 class ModelAPI_AttributeBoolean;
26 class ModelAPI_AttributeDouble;
27 class ModelAPI_AttributeInteger;
28 class ModelAPI_AttributeSelection;
29 class ModelAPI_AttributeString;
30 class ModelAPI_CompositeFeature;
31 class ModelAPI_Document;
32 class ModelAPI_Entity;
33 class ModelAPI_Feature;
34
35 typedef std::shared_ptr<ModelAPI_Entity>  EntityPtr;
36 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
37
38 /**\class ModelHighAPI_Dumper
39  * \ingroup CPPHighAPI
40  * \brief Dump engine for the model
41  */
42 class ModelHighAPI_Dumper
43 {
44 public:
45   /// Default constructor
46   MODELHIGHAPI_EXPORT
47   ModelHighAPI_Dumper();
48
49   /// Sets instance of a dumper
50   MODELHIGHAPI_EXPORT
51   static void setInstance(ModelHighAPI_Dumper* theDumper);
52
53   /// Returns instance of a dumper
54   MODELHIGHAPI_EXPORT
55   static ModelHighAPI_Dumper* getInstance();
56
57   /// Destructor
58   virtual ~ModelHighAPI_Dumper() {}
59
60   /// Dump given document into the file
61   /// \return \c true, if succeed
62   MODELHIGHAPI_EXPORT
63   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
64
65   /// Add module to list of imported modules
66   /// \param theModuleName  name of the module to be imported
67   /// \param theObject      name of the entity to be imported from the module (if empty, while module will be imported)
68   MODELHIGHAPI_EXPORT
69   void importModule(const std::string& theModuleName,
70                     const std::string& theObject = std::string());
71
72   /// Returns name of specified entity
73   MODELHIGHAPI_EXPORT
74   const std::string& name(const EntityPtr& theEntity);
75
76   /// Returns name of parent composite feature for specified entity
77   MODELHIGHAPI_EXPORT
78   const std::string& parentName(const FeaturePtr& theFeature);
79
80   /// Dump given feature
81   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
82
83   /// Save all dumps into specified file
84   MODELHIGHAPI_EXPORT
85   bool exportTo(const std::string& theFileName);
86
87   /// Dump character
88   MODELHIGHAPI_EXPORT
89   ModelHighAPI_Dumper& operator<<(const char theChar);
90   /// Dump string
91   MODELHIGHAPI_EXPORT
92   ModelHighAPI_Dumper& operator<<(const char* theString);
93   /// Dump string
94   MODELHIGHAPI_EXPORT
95   ModelHighAPI_Dumper& operator<<(const std::string& theString);
96   /// Dump integer
97   MODELHIGHAPI_EXPORT
98   ModelHighAPI_Dumper& operator<<(const int theValue);
99   /// Dump real
100   MODELHIGHAPI_EXPORT
101   ModelHighAPI_Dumper& operator<<(const double theValue);
102   /// Dump std::endl
103   friend
104   MODELHIGHAPI_EXPORT
105   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
106                                   std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
107
108   /// Dump GeomAPI_Pnt in the following form:
109   /// "GeomAPI_Pnt(X, Y, Z)"
110   MODELHIGHAPI_EXPORT
111   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
112   /// Dump GeomAPI_Dir
113   /// "GeomAPI_Dir(X, Y, Z)"
114   MODELHIGHAPI_EXPORT
115   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
116
117   /// Dump GeomDataAPI_Dir in the following form:
118   /// "X, Y, Z"
119   MODELHIGHAPI_EXPORT
120   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
121   /// Dump GeomDataAPI_Point in the following form:
122   /// "X, Y, Z"
123   MODELHIGHAPI_EXPORT
124   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
125   /// Dump GeomDataAPI_Point2D in the following form:
126   /// "X, Y"
127   MODELHIGHAPI_EXPORT
128   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
129
130   /// Dump AttributeBoolean
131   MODELHIGHAPI_EXPORT
132   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
133   /// Dump AttributeInteger
134   MODELHIGHAPI_EXPORT
135   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
136   /// Dump AttributeDouble
137   MODELHIGHAPI_EXPORT
138   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
139   /// Dump AttributeString
140   MODELHIGHAPI_EXPORT
141   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
142   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
143   MODELHIGHAPI_EXPORT
144   ModelHighAPI_Dumper& operator<<(const EntityPtr& theEntity);
145
146   /// Dump AttributeSelection
147   MODELHIGHAPI_EXPORT
148   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
149
150 protected:
151   /// Dump "setName" command if last entity had user-defined name
152   MODELHIGHAPI_EXPORT void dumpEntitySetName();
153
154   /// Clear dump buffer
155   void clear();
156
157 private:
158   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
159   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
160
161   /// Iterate all features in document and dump them into intermediate buffer
162   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
163
164   /// Iterate all features in composite feature and dump them into intermediate buffer
165   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite);
166
167 private:
168   typedef std::map<EntityPtr, std::pair<std::string, bool> > EntityNameMap;
169   typedef std::map<std::string, std::set<std::string> >      ModulesMap;
170
171   static ModelHighAPI_Dumper* mySelf;
172
173   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
174   ModulesMap          myModules;            ///< modules and entities to be imported
175   EntityNameMap       myNames;              ///< names of the entities
176   EntityPtr           myLastEntityWithName; ///< not null, if last dumped entity had user defined name
177 };
178
179 #endif