Salome HOME
2c9edffe4e80cc1f774c5119f111aec30c768837
[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_Attribute;
26 class ModelAPI_AttributeBoolean;
27 class ModelAPI_AttributeDouble;
28 class ModelAPI_AttributeInteger;
29 class ModelAPI_AttributeRefAttr;
30 class ModelAPI_AttributeRefAttrList;
31 class ModelAPI_AttributeReference;
32 class ModelAPI_AttributeRefList;
33 class ModelAPI_AttributeSelection;
34 class ModelAPI_AttributeSelectionList;
35 class ModelAPI_AttributeString;
36 class ModelAPI_CompositeFeature;
37 class ModelAPI_Document;
38 class ModelAPI_Entity;
39 class ModelAPI_Feature;
40 class ModelAPI_Object;
41
42 typedef std::shared_ptr<ModelAPI_Entity>  EntityPtr;
43 typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
44
45 /**\class ModelHighAPI_Dumper
46  * \ingroup CPPHighAPI
47  * \brief Dump engine for the model
48  */
49 class ModelHighAPI_Dumper
50 {
51 public:
52   /// Default constructor
53   MODELHIGHAPI_EXPORT
54   ModelHighAPI_Dumper();
55
56   /// Sets instance of a dumper
57   MODELHIGHAPI_EXPORT
58   static void setInstance(ModelHighAPI_Dumper* theDumper);
59
60   /// Returns instance of a dumper
61   MODELHIGHAPI_EXPORT
62   static ModelHighAPI_Dumper* getInstance();
63
64   /// Destructor
65   virtual ~ModelHighAPI_Dumper() {}
66
67   /// Dump given document into the file
68   /// \return \c true, if succeed
69   MODELHIGHAPI_EXPORT
70   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
71
72   /// Add module to list of imported modules
73   /// \param theModuleName  name of the module to be imported
74   /// \param theObject      name of the entity to be imported from the module (if empty, while module will be imported)
75   MODELHIGHAPI_EXPORT
76   void importModule(const std::string& theModuleName,
77                     const std::string& theObject = std::string());
78
79   /// Returns name of specified entity
80   MODELHIGHAPI_EXPORT
81   const std::string& name(const EntityPtr& theEntity);
82
83   /// Returns name of parent composite feature for specified entity
84   MODELHIGHAPI_EXPORT
85   const std::string& parentName(const FeaturePtr& theFeature);
86
87   /// Dump given feature
88   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
89
90   /// Return name of getter for corresponding attribute
91   virtual std::string attributeGetter(const FeaturePtr& theFeature,
92                                       const std::string& theAttrName) const = 0;
93
94   /// Save all dumps into specified file
95   MODELHIGHAPI_EXPORT
96   bool exportTo(const std::string& theFileName);
97
98   /// Dump character
99   MODELHIGHAPI_EXPORT
100   ModelHighAPI_Dumper& operator<<(const char theChar);
101   /// Dump string
102   MODELHIGHAPI_EXPORT
103   ModelHighAPI_Dumper& operator<<(const char* theString);
104   /// Dump string
105   MODELHIGHAPI_EXPORT
106   ModelHighAPI_Dumper& operator<<(const std::string& theString);
107   /// Dump boolean
108   MODELHIGHAPI_EXPORT
109   ModelHighAPI_Dumper& operator<<(const bool theValue);
110   /// Dump integer
111   MODELHIGHAPI_EXPORT
112   ModelHighAPI_Dumper& operator<<(const int theValue);
113   /// Dump real
114   MODELHIGHAPI_EXPORT
115   ModelHighAPI_Dumper& operator<<(const double theValue);
116   /// Dump std::endl
117   friend
118   MODELHIGHAPI_EXPORT
119   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
120                                   std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
121
122   /// Dump GeomAPI_Pnt in the following form:
123   /// "GeomAPI_Pnt(X, Y, Z)"
124   MODELHIGHAPI_EXPORT
125   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
126   /// Dump GeomAPI_Dir
127   /// "GeomAPI_Dir(X, Y, Z)"
128   MODELHIGHAPI_EXPORT
129   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
130
131   /// Dump GeomDataAPI_Dir in the following form:
132   /// "X, Y, Z"
133   MODELHIGHAPI_EXPORT
134   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
135   /// Dump GeomDataAPI_Point in the following form:
136   /// "X, Y, Z"
137   MODELHIGHAPI_EXPORT
138   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
139   /// Dump GeomDataAPI_Point2D in the following form:
140   /// "X, Y"
141   MODELHIGHAPI_EXPORT
142   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
143
144   /// Dump AttributeBoolean
145   MODELHIGHAPI_EXPORT
146   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
147   /// Dump AttributeInteger
148   MODELHIGHAPI_EXPORT
149   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
150   /// Dump AttributeDouble
151   MODELHIGHAPI_EXPORT
152   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
153   /// Dump AttributeString
154   MODELHIGHAPI_EXPORT
155   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
156   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
157   MODELHIGHAPI_EXPORT
158   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
159
160   /// Dump Attribute
161   MODELHIGHAPI_EXPORT
162   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
163   /// Dump Object
164   MODELHIGHAPI_EXPORT
165   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
166
167   /// Dump AttributeRefAttr
168   MODELHIGHAPI_EXPORT
169   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
170   /// Dump AttributeRefAttrList as follows:
171   /// "[obj1, obj2, obj3, ...]"
172   MODELHIGHAPI_EXPORT
173   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
174   /// Dump AttributeRefList as follows:
175   /// "[obj1, obj2, obj3, ...]"
176   MODELHIGHAPI_EXPORT
177   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
178   /// Dump AttributeSelection
179   MODELHIGHAPI_EXPORT
180   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
181   /// Dump AttributeSelectionList
182   MODELHIGHAPI_EXPORT
183   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
184   /// Dump AttributeReference
185   MODELHIGHAPI_EXPORT
186   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
187
188   /// Clear dump buffer
189   MODELHIGHAPI_EXPORT
190   void clear(bool bufferOnly = false);
191   /// clear list of not dumped entities
192   MODELHIGHAPI_EXPORT void clearNotDumped();
193
194 protected:
195   /// Dump "setName" command if last entity had user-defined name
196   MODELHIGHAPI_EXPORT void dumpEntitySetName();
197
198 private:
199   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
200   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
201
202   /// Iterate all features in document and dump them into intermediate buffer
203   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
204
205   /// Iterate all features in composite feature and dump them into intermediate buffer
206   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite);
207
208   /// Check the entity is already dumped
209   bool isDumped(const EntityPtr& theEntity) const;
210
211 private:
212   typedef std::map<EntityPtr, std::pair<std::string, bool> > EntityNameMap;
213   typedef std::map<std::string, std::set<std::string> >      ModulesMap;
214
215   static ModelHighAPI_Dumper* mySelf;
216
217   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
218   std::ostringstream  myFullDump;           ///< full buffer of dumped data
219
220   ModulesMap          myModules;            ///< modules and entities to be imported
221   EntityNameMap       myNames;              ///< names of the entities
222   EntityPtr           myLastEntityWithName; ///< not null, if last dumped entity had user defined name
223
224 protected:
225   std::set<EntityPtr> myNotDumpedEntities;  ///< list of entities, used by other features but not dumped yet
226 };
227
228 #endif