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