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