]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Dumper.h
Salome HOME
11b6775a8e7f4fd481a78219bfac9c7b0a5fdae2
[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   /// \return name of the entity
88   MODELHIGHAPI_EXPORT
89   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true);
90
91   /// Returns name of parent composite feature for specified entity
92   MODELHIGHAPI_EXPORT
93   const std::string& parentName(const FeaturePtr& theFeature);
94
95   /// Dump parameter feature only
96   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
97   /// Dump given feature
98   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
99
100   /// Return name of getter for corresponding attribute
101   virtual std::string attributeGetter(const FeaturePtr& theFeature,
102                                       const std::string& theAttrName) const = 0;
103
104   /// Save all dumps into specified file
105   MODELHIGHAPI_EXPORT
106   bool exportTo(const std::string& theFileName);
107
108   /// Dump character
109   MODELHIGHAPI_EXPORT
110   ModelHighAPI_Dumper& operator<<(const char theChar);
111   /// Dump string
112   MODELHIGHAPI_EXPORT
113   ModelHighAPI_Dumper& operator<<(const char* theString);
114   /// Dump string
115   MODELHIGHAPI_EXPORT
116   ModelHighAPI_Dumper& operator<<(const std::string& theString);
117   /// Dump boolean
118   MODELHIGHAPI_EXPORT
119   ModelHighAPI_Dumper& operator<<(const bool theValue);
120   /// Dump integer
121   MODELHIGHAPI_EXPORT
122   ModelHighAPI_Dumper& operator<<(const int theValue);
123   /// Dump real
124   MODELHIGHAPI_EXPORT
125   ModelHighAPI_Dumper& operator<<(const double theValue);
126   /// Dump std::endl
127   friend
128   MODELHIGHAPI_EXPORT
129   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
130                                   std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
131
132   /// Dump GeomAPI_Pnt in the following form:
133   /// "GeomAPI_Pnt(X, Y, Z)"
134   MODELHIGHAPI_EXPORT
135   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
136   /// Dump GeomAPI_Dir
137   /// "GeomAPI_Dir(X, Y, Z)"
138   MODELHIGHAPI_EXPORT
139   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
140
141   /// Dump GeomDataAPI_Dir in the following form:
142   /// "X, Y, Z"
143   MODELHIGHAPI_EXPORT
144   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
145   /// Dump GeomDataAPI_Point in the following form:
146   /// "X, Y, Z"
147   MODELHIGHAPI_EXPORT
148   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
149   /// Dump GeomDataAPI_Point2D in the following form:
150   /// "X, Y"
151   MODELHIGHAPI_EXPORT
152   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
153
154   /// Dump AttributeBoolean
155   MODELHIGHAPI_EXPORT
156   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
157   /// Dump AttributeInteger
158   MODELHIGHAPI_EXPORT
159   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
160   /// Dump AttributeDouble
161   MODELHIGHAPI_EXPORT
162   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
163   /// Dump AttributeString
164   MODELHIGHAPI_EXPORT
165   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
166   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
167   MODELHIGHAPI_EXPORT
168   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
169
170   /// Dump result
171   MODELHIGHAPI_EXPORT
172   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
173
174   /// Dump Attribute
175   MODELHIGHAPI_EXPORT
176   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
177   /// Dump Object
178   MODELHIGHAPI_EXPORT
179   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
180
181   /// Dump AttributeRefAttr
182   MODELHIGHAPI_EXPORT
183   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
184   /// Dump AttributeRefAttrList as follows:
185   /// "[obj1, obj2, obj3, ...]"
186   MODELHIGHAPI_EXPORT
187   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
188   /// Dump AttributeRefList as follows:
189   /// "[obj1, obj2, obj3, ...]"
190   MODELHIGHAPI_EXPORT
191   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
192   /// Dump AttributeSelection
193   MODELHIGHAPI_EXPORT
194   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
195   /// Dump AttributeSelectionList
196   MODELHIGHAPI_EXPORT
197   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
198   /// Dump AttributeReference
199   MODELHIGHAPI_EXPORT
200   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
201
202   /// Clear dump buffer
203   MODELHIGHAPI_EXPORT
204   void clear(bool bufferOnly = false);
205   /// clear list of not dumped entities
206   MODELHIGHAPI_EXPORT void clearNotDumped();
207
208 protected:
209   /// Dump "setName" command if last entity had user-defined name
210   MODELHIGHAPI_EXPORT void dumpEntitySetName();
211
212 private:
213   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
214   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
215
216   /// Iterate all features in document and dump them into intermediate buffer
217   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
218
219   /// Dump composite feature and all it sub-features
220   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
221
222   /// Iterate all features in composite feature and dump them into intermediate buffer
223   /// \param theComposite   [in] parent composite feature
224   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
225   MODELHIGHAPI_EXPORT
226   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
227
228   /// Check the entity is already dumped
229   bool isDumped(const EntityPtr& theEntity) const;
230
231   /// Stores names of results for the given feature
232   void saveResultNames(const FeaturePtr& theFeature);
233
234 private:
235   typedef std::map<EntityPtr, std::pair<std::string, std::string> > EntityNameMap;
236   typedef std::map<std::string, std::set<std::string> >             ModulesMap;
237   typedef std::map<DocumentPtr, std::map<std::string, int> >        NbFeaturesMap;
238
239   struct LastDumpedEntity {
240     EntityPtr            myEntity; // last dumped entity
241     bool                 myUserName; // the entity hase user-defined name
242     std::list<ResultPtr> myResultsWithName; // results of this entity, which has user-defined names
243
244     LastDumpedEntity(EntityPtr theEntity, bool theUserName, const std::list<ResultPtr>& theResults)
245       : myEntity(theEntity), myUserName(theUserName), myResultsWithName(theResults)
246     {}
247   };
248   typedef std::stack<LastDumpedEntity>                              DumpStack;
249
250   static ModelHighAPI_Dumper* mySelf;
251
252   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
253   std::ostringstream  myFullDump;           ///< full buffer of dumped data
254
255   ModulesMap          myModules;            ///< modules and entities to be imported
256   EntityNameMap       myNames;              ///< names of the entities
257   DumpStack           myEntitiesStack;      ///< stack of dumped entities
258
259   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
260
261 protected:
262   std::set<EntityPtr> myNotDumpedEntities;  ///< list of entities, used by other features but not dumped yet
263
264   friend class SketchAPI_Sketch;
265 };
266
267 #endif