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