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