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