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