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