Salome HOME
ed2834f4684f317e6ce8fb87eb11b8ec469d05ef
[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 <list>
13 #include <map>
14 #include <memory>
15 #include <set>
16 #include <sstream>
17 #include <stack>
18 #include <string>
19
20 class GeomAPI_Pnt;
21 class GeomAPI_Dir;
22
23 class GeomDataAPI_Dir;
24 class GeomDataAPI_Point;
25 class GeomDataAPI_Point2D;
26
27 class ModelAPI_Attribute;
28 class ModelAPI_AttributeBoolean;
29 class ModelAPI_AttributeDouble;
30 class ModelAPI_AttributeInteger;
31 class ModelAPI_AttributeRefAttr;
32 class ModelAPI_AttributeRefAttrList;
33 class ModelAPI_AttributeReference;
34 class ModelAPI_AttributeRefList;
35 class ModelAPI_AttributeSelection;
36 class ModelAPI_AttributeSelectionList;
37 class ModelAPI_AttributeString;
38 class ModelAPI_CompositeFeature;
39 class ModelAPI_Document;
40 class ModelAPI_Entity;
41 class ModelAPI_Feature;
42 class ModelAPI_Object;
43 class ModelAPI_Result;
44
45 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
46 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
47 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
48 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
49
50 /**\class ModelHighAPI_Dumper
51  * \ingroup CPPHighAPI
52  * \brief Dump engine for the model
53  */
54 class ModelHighAPI_Dumper
55 {
56 public:
57   /// Default constructor
58   MODELHIGHAPI_EXPORT
59   ModelHighAPI_Dumper();
60
61   /// Sets instance of a dumper
62   MODELHIGHAPI_EXPORT
63   static void setInstance(ModelHighAPI_Dumper* theDumper);
64
65   /// Returns instance of a dumper
66   MODELHIGHAPI_EXPORT
67   static ModelHighAPI_Dumper* getInstance();
68
69   /// Destructor
70   virtual ~ModelHighAPI_Dumper() {}
71
72   /// Dump given document into the file
73   /// \return \c true, if succeed
74   MODELHIGHAPI_EXPORT
75   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
76
77   /// Add module to list of imported modules
78   /// \param theModuleName  name of the module to be imported
79   /// \param theObject      name of the entity to be imported from the module (if empty, while module will be imported)
80   MODELHIGHAPI_EXPORT
81   void importModule(const std::string& theModuleName,
82                     const std::string& theObject = std::string());
83
84   /// Returns name of specified entity
85   /// \param theEntity        [in] named entity
86   /// \param theSaveNotDumped [in] if \c true, the entity should be stored as not dumped (will be dumped automatically)
87   /// \param theUseEntityName [in] if \c true, the entity name should be used "as is" without changing default name
88   /// \return name of the entity
89   MODELHIGHAPI_EXPORT
90   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true, bool theUseEntityName = false);
91
92   /// Returns name of parent composite feature for specified entity
93   MODELHIGHAPI_EXPORT
94   const std::string& parentName(const FeaturePtr& theFeature);
95
96   /// Dump parameter feature only
97   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
98   /// Dump given feature
99   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
100
101   /// Return name of getter for corresponding attribute
102   virtual std::string attributeGetter(const FeaturePtr& theFeature,
103                                       const std::string& theAttrName) const = 0;
104
105   /// Save all dumps into specified file
106   MODELHIGHAPI_EXPORT
107   bool exportTo(const std::string& theFileName);
108
109   /// Dump character
110   MODELHIGHAPI_EXPORT
111   ModelHighAPI_Dumper& operator<<(const char theChar);
112   /// Dump string
113   MODELHIGHAPI_EXPORT
114   ModelHighAPI_Dumper& operator<<(const char* theString);
115   /// Dump string
116   MODELHIGHAPI_EXPORT
117   ModelHighAPI_Dumper& operator<<(const std::string& theString);
118   /// Dump boolean
119   MODELHIGHAPI_EXPORT
120   ModelHighAPI_Dumper& operator<<(const bool theValue);
121   /// Dump integer
122   MODELHIGHAPI_EXPORT
123   ModelHighAPI_Dumper& operator<<(const int theValue);
124   /// Dump real
125   MODELHIGHAPI_EXPORT
126   ModelHighAPI_Dumper& operator<<(const double theValue);
127   /// Dump std::endl
128   friend
129   MODELHIGHAPI_EXPORT
130   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
131                                   std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
132
133   /// Dump GeomAPI_Pnt in the following form:
134   /// "GeomAPI_Pnt(X, Y, Z)"
135   MODELHIGHAPI_EXPORT
136   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
137   /// Dump GeomAPI_Dir
138   /// "GeomAPI_Dir(X, Y, Z)"
139   MODELHIGHAPI_EXPORT
140   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
141
142   /// Dump GeomDataAPI_Dir in the following form:
143   /// "X, Y, Z"
144   MODELHIGHAPI_EXPORT
145   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
146   /// Dump GeomDataAPI_Point in the following form:
147   /// "X, Y, Z"
148   MODELHIGHAPI_EXPORT
149   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
150   /// Dump GeomDataAPI_Point2D in the following form:
151   /// "X, Y"
152   MODELHIGHAPI_EXPORT
153   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
154
155   /// Dump AttributeBoolean
156   MODELHIGHAPI_EXPORT
157   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
158   /// Dump AttributeInteger
159   MODELHIGHAPI_EXPORT
160   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
161   /// Dump AttributeDouble
162   MODELHIGHAPI_EXPORT
163   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
164   /// Dump AttributeString
165   MODELHIGHAPI_EXPORT
166   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
167   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
168   MODELHIGHAPI_EXPORT
169   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
170
171   /// Dump result
172   MODELHIGHAPI_EXPORT
173   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
174
175   /// Dump Attribute
176   MODELHIGHAPI_EXPORT
177   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
178   /// Dump Object
179   MODELHIGHAPI_EXPORT
180   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
181
182   /// Dump AttributeRefAttr
183   MODELHIGHAPI_EXPORT
184   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
185   /// Dump AttributeRefAttrList as follows:
186   /// "[obj1, obj2, obj3, ...]"
187   MODELHIGHAPI_EXPORT
188   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
189   /// Dump AttributeRefList as follows:
190   /// "[obj1, obj2, obj3, ...]"
191   MODELHIGHAPI_EXPORT
192   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
193   /// Dump AttributeSelection
194   MODELHIGHAPI_EXPORT
195   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
196   /// Dump AttributeSelectionList
197   MODELHIGHAPI_EXPORT
198   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
199   /// Dump AttributeReference
200   MODELHIGHAPI_EXPORT
201   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
202
203   /// Clear dump buffer
204   MODELHIGHAPI_EXPORT
205   void clear(bool bufferOnly = false);
206   /// clear list of not dumped entities
207   MODELHIGHAPI_EXPORT void clearNotDumped();
208
209 protected:
210   /// Dump "setName" command if last entity had user-defined name
211   MODELHIGHAPI_EXPORT void dumpEntitySetName();
212
213 private:
214   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
215   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
216
217   /// Iterate all features in document and dump them into intermediate buffer
218   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
219
220   /// Dump composite feature and all it sub-features
221   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
222
223   /// Iterate all features in composite feature and dump them into intermediate buffer
224   /// \param theComposite   [in] parent composite feature
225   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
226   MODELHIGHAPI_EXPORT
227   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
228
229   /// Check the entity is already dumped
230   bool isDumped(const EntityPtr& theEntity) const;
231
232   /// Stores names of results for the given feature
233   void saveResultNames(const FeaturePtr& theFeature);
234
235 private:
236   typedef std::map<EntityPtr, std::pair<std::string, std::string> > EntityNameMap;
237   typedef std::map<std::string, std::set<std::string> >             ModulesMap;
238   typedef std::map<DocumentPtr, std::map<std::string, int> >        NbFeaturesMap;
239
240   struct LastDumpedEntity {
241     EntityPtr            myEntity; // last dumped entity
242     bool                 myUserName; // the entity hase user-defined name
243     std::list<ResultPtr> myResultsWithName; // results of this entity, which has user-defined names
244
245     LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list<ResultPtr>& theResults)
246       : myEntity(theEntity), myUserName(theUserName), myResultsWithName(theResults)
247     {}
248   };
249   typedef std::stack<LastDumpedEntity>                              DumpStack;
250
251   static ModelHighAPI_Dumper* mySelf;
252
253   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
254   std::ostringstream  myFullDump;           ///< full buffer of dumped data
255
256   ModulesMap          myModules;            ///< modules and entities to be imported
257   EntityNameMap       myNames;              ///< names of the entities
258   DumpStack           myEntitiesStack;      ///< stack of dumped entities
259
260   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
261
262 protected:
263   std::set<EntityPtr> myNotDumpedEntities;  ///< list of entities, used by other features but not dumped yet
264
265   friend class SketchAPI_Sketch;
266 };
267
268 #endif