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