Salome HOME
61ed607039eb1be2ae67335b6d68f2c5e7e594ae
[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 <string>
18
19 class GeomAPI_Pnt;
20 class GeomAPI_Dir;
21
22 class GeomDataAPI_Dir;
23 class GeomDataAPI_Point;
24 class GeomDataAPI_Point2D;
25
26 class ModelAPI_Attribute;
27 class ModelAPI_AttributeBoolean;
28 class ModelAPI_AttributeDouble;
29 class ModelAPI_AttributeInteger;
30 class ModelAPI_AttributeRefAttr;
31 class ModelAPI_AttributeRefAttrList;
32 class ModelAPI_AttributeReference;
33 class ModelAPI_AttributeRefList;
34 class ModelAPI_AttributeSelection;
35 class ModelAPI_AttributeSelectionList;
36 class ModelAPI_AttributeString;
37 class ModelAPI_CompositeFeature;
38 class ModelAPI_Document;
39 class ModelAPI_Entity;
40 class ModelAPI_Feature;
41 class ModelAPI_Object;
42 class ModelAPI_Result;
43
44 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
45 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
46 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
47 typedef std::shared_ptr<ModelAPI_Result> ResultPtr;
48
49 /**\class ModelHighAPI_Dumper
50  * \ingroup CPPHighAPI
51  * \brief Dump engine for the model
52  */
53 class ModelHighAPI_Dumper
54 {
55 public:
56   /// Default constructor
57   MODELHIGHAPI_EXPORT
58   ModelHighAPI_Dumper();
59
60   /// Sets instance of a dumper
61   MODELHIGHAPI_EXPORT
62   static void setInstance(ModelHighAPI_Dumper* theDumper);
63
64   /// Returns instance of a dumper
65   MODELHIGHAPI_EXPORT
66   static ModelHighAPI_Dumper* getInstance();
67
68   /// Destructor
69   virtual ~ModelHighAPI_Dumper() {}
70
71   /// Dump given document into the file
72   /// \return \c true, if succeed
73   MODELHIGHAPI_EXPORT
74   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
75
76   /// Add module to list of imported modules
77   /// \param theModuleName  name of the module to be imported
78   /// \param theObject      name of the entity to be imported from the module (if empty, while module will be imported)
79   MODELHIGHAPI_EXPORT
80   void importModule(const std::string& theModuleName,
81                     const std::string& theObject = std::string());
82
83   /// Returns name of specified entity
84   /// \param theEntity        [in] named entity
85   /// \param theSaveNotDumped [in] if \c true, the entity should be stored as not dumped (will be dumped automatically)
86   /// \return name of the entity
87   MODELHIGHAPI_EXPORT
88   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true);
89
90   /// Returns name of parent composite feature for specified entity
91   MODELHIGHAPI_EXPORT
92   const std::string& parentName(const FeaturePtr& theFeature);
93
94   /// Dump parameter feature only
95   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
96   /// Dump given feature
97   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
98
99   /// Return name of getter for corresponding attribute
100   virtual std::string attributeGetter(const FeaturePtr& theFeature,
101                                       const std::string& theAttrName) const = 0;
102
103   /// Save all dumps into specified file
104   MODELHIGHAPI_EXPORT
105   bool exportTo(const std::string& theFileName);
106
107   /// Dump character
108   MODELHIGHAPI_EXPORT
109   ModelHighAPI_Dumper& operator<<(const char theChar);
110   /// Dump string
111   MODELHIGHAPI_EXPORT
112   ModelHighAPI_Dumper& operator<<(const char* theString);
113   /// Dump string
114   MODELHIGHAPI_EXPORT
115   ModelHighAPI_Dumper& operator<<(const std::string& theString);
116   /// Dump boolean
117   MODELHIGHAPI_EXPORT
118   ModelHighAPI_Dumper& operator<<(const bool theValue);
119   /// Dump integer
120   MODELHIGHAPI_EXPORT
121   ModelHighAPI_Dumper& operator<<(const int theValue);
122   /// Dump real
123   MODELHIGHAPI_EXPORT
124   ModelHighAPI_Dumper& operator<<(const double theValue);
125   /// Dump std::endl
126   friend
127   MODELHIGHAPI_EXPORT
128   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
129                                   std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
130
131   /// Dump GeomAPI_Pnt in the following form:
132   /// "GeomAPI_Pnt(X, Y, Z)"
133   MODELHIGHAPI_EXPORT
134   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
135   /// Dump GeomAPI_Dir
136   /// "GeomAPI_Dir(X, Y, Z)"
137   MODELHIGHAPI_EXPORT
138   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
139
140   /// Dump GeomDataAPI_Dir in the following form:
141   /// "X, Y, Z"
142   MODELHIGHAPI_EXPORT
143   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
144   /// Dump GeomDataAPI_Point in the following form:
145   /// "X, Y, Z"
146   MODELHIGHAPI_EXPORT
147   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
148   /// Dump GeomDataAPI_Point2D in the following form:
149   /// "X, Y"
150   MODELHIGHAPI_EXPORT
151   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
152
153   /// Dump AttributeBoolean
154   MODELHIGHAPI_EXPORT
155   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
156   /// Dump AttributeInteger
157   MODELHIGHAPI_EXPORT
158   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
159   /// Dump AttributeDouble
160   MODELHIGHAPI_EXPORT
161   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
162   /// Dump AttributeString
163   MODELHIGHAPI_EXPORT
164   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
165   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
166   MODELHIGHAPI_EXPORT
167   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
168
169   /// Dump result
170   MODELHIGHAPI_EXPORT
171   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
172
173   /// Dump Attribute
174   MODELHIGHAPI_EXPORT
175   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
176   /// Dump Object
177   MODELHIGHAPI_EXPORT
178   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
179
180   /// Dump AttributeRefAttr
181   MODELHIGHAPI_EXPORT
182   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
183   /// Dump AttributeRefAttrList as follows:
184   /// "[obj1, obj2, obj3, ...]"
185   MODELHIGHAPI_EXPORT
186   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
187   /// Dump AttributeRefList as follows:
188   /// "[obj1, obj2, obj3, ...]"
189   MODELHIGHAPI_EXPORT
190   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
191   /// Dump AttributeSelection
192   MODELHIGHAPI_EXPORT
193   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
194   /// Dump AttributeSelectionList
195   MODELHIGHAPI_EXPORT
196   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
197   /// Dump AttributeReference
198   MODELHIGHAPI_EXPORT
199   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
200
201   /// Clear dump buffer
202   MODELHIGHAPI_EXPORT
203   void clear(bool bufferOnly = false);
204   /// clear list of not dumped entities
205   MODELHIGHAPI_EXPORT void clearNotDumped();
206
207 protected:
208   /// Dump "setName" command if last entity had user-defined name
209   MODELHIGHAPI_EXPORT void dumpEntitySetName();
210
211 private:
212   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
213   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
214
215   /// Iterate all features in document and dump them into intermediate buffer
216   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
217
218   /// Dump composite feature and all it sub-features
219   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce = false);
220
221   /// Iterate all features in composite feature and dump them into intermediate buffer
222   /// \param theComposite   [in] parent composite feature
223   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
224   MODELHIGHAPI_EXPORT
225   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool theDumpModelDo = false);
226
227   /// Check the entity is already dumped
228   bool isDumped(const EntityPtr& theEntity) const;
229
230 private:
231   typedef std::map<EntityPtr, std::pair<std::string, bool> > EntityNameMap;
232   typedef std::map<std::string, std::set<std::string> >      ModulesMap;
233   typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
234
235   static ModelHighAPI_Dumper* mySelf;
236
237   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
238   std::ostringstream  myFullDump;           ///< full buffer of dumped data
239
240   ModulesMap          myModules;            ///< modules and entities to be imported
241   EntityNameMap       myNames;              ///< names of the entities
242   EntityPtr           myLastEntityWithName; ///< not null, if last dumped entity had user defined name
243
244   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
245
246 protected:
247   std::set<EntityPtr> myNotDumpedEntities;  ///< list of entities, used by other features but not dumped yet
248
249   friend class SketchAPI_Sketch;
250 };
251
252 #endif