Salome HOME
Fix the problem of not previewed results of constraints applied. Flash Delete (for...
[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 <stack>
18 #include <string>
19
20 class GeomAPI_Pnt;
21 class GeomAPI_Dir;
22
23 class GeomDataAPI_Dir;
24 class GeomDataAPI_Point;
25 class GeomDataAPI_Point2D;
26
27 class ModelAPI_Attribute;
28 class ModelAPI_AttributeBoolean;
29 class ModelAPI_AttributeDouble;
30 class ModelAPI_AttributeInteger;
31 class ModelAPI_AttributeRefAttr;
32 class ModelAPI_AttributeRefAttrList;
33 class ModelAPI_AttributeReference;
34 class ModelAPI_AttributeRefList;
35 class ModelAPI_AttributeSelection;
36 class ModelAPI_AttributeSelectionList;
37 class ModelAPI_AttributeString;
38 class ModelAPI_AttributeStringArray;
39 class ModelAPI_CompositeFeature;
40 class ModelAPI_Document;
41 class ModelAPI_Entity;
42 class ModelAPI_Feature;
43 class ModelAPI_Object;
44 class ModelAPI_Result;
45
46 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
47 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
48 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
49 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
50
51 /**\class ModelHighAPI_Dumper
52  * \ingroup CPPHighAPI
53  * \brief Dump engine for the model
54  */
55 class ModelHighAPI_Dumper
56 {
57 public:
58   /// Default constructor
59   MODELHIGHAPI_EXPORT
60   ModelHighAPI_Dumper();
61
62   /// Sets instance of a dumper
63   MODELHIGHAPI_EXPORT
64   static void setInstance(ModelHighAPI_Dumper* theDumper);
65
66   /// Returns instance of a dumper
67   MODELHIGHAPI_EXPORT
68   static ModelHighAPI_Dumper* getInstance();
69
70   /// Destructor
71   virtual ~ModelHighAPI_Dumper() {}
72
73   /// Dump given document into the file
74   /// \return \c true, if succeed
75   MODELHIGHAPI_EXPORT
76   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
77
78   /// Add module to list of imported modules
79   /// \param theModuleName  name of the module to be imported
80   /// \param theObject      name of the entity to be imported
81   ///                       from the module (if empty, while module will be imported)
82   MODELHIGHAPI_EXPORT
83   void importModule(const std::string& theModuleName,
84                     const std::string& theObject = std::string());
85
86   /// Returns name of specified entity
87   /// \param theEntity        [in] named entity
88   /// \param theSaveNotDumped [in]
89   ///    if \c true, the entity should be stored as not dumped (will be dumped automatically)
90   /// \param theUseEntityName [in]
91   ///    if \c true, the entity name should be used "as is" without changing default name
92   /// \return name of the entity
93   MODELHIGHAPI_EXPORT
94   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
95                           bool theUseEntityName = false);
96
97   /// Returns name of parent composite feature for specified entity
98   MODELHIGHAPI_EXPORT
99   const std::string& parentName(const FeaturePtr& theFeature);
100
101   /// Dump parameter feature only
102   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
103   /// Dump given feature
104   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
105
106   /// Set a feature that should not be dumped anyway
107   MODELHIGHAPI_EXPORT
108   void doNotDumpFeature(const FeaturePtr& theFeature)
109   { myFeaturesToSkip.insert(theFeature); }
110
111   /// Dump sub-feature name and color, without dumping feature creation.
112   /// Used for features which creates sub-features in their execute method.
113   /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
114   /// \param theSubFeature    [in] sub-feature
115   MODELHIGHAPI_EXPORT
116   void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
117                                   const FeaturePtr& theSubFeature);
118
119   /// Return name of getter for corresponding attribute
120   virtual std::string attributeGetter(const FeaturePtr& theFeature,
121                                       const std::string& theAttrName) const = 0;
122
123   /// Return name of wrapper feature
124   virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
125
126   /// Save all dumps into specified file
127   MODELHIGHAPI_EXPORT
128   bool exportTo(const std::string& theFileName);
129
130   /// Dump character
131   MODELHIGHAPI_EXPORT
132   ModelHighAPI_Dumper& operator<<(const char theChar);
133   /// Dump string
134   MODELHIGHAPI_EXPORT
135   ModelHighAPI_Dumper& operator<<(const char* theString);
136   /// Dump string
137   MODELHIGHAPI_EXPORT
138   ModelHighAPI_Dumper& operator<<(const std::string& theString);
139   /// Dump boolean
140   MODELHIGHAPI_EXPORT
141   ModelHighAPI_Dumper& operator<<(const bool theValue);
142   /// Dump integer
143   MODELHIGHAPI_EXPORT
144   ModelHighAPI_Dumper& operator<<(const int theValue);
145   /// Dump real
146   MODELHIGHAPI_EXPORT
147   ModelHighAPI_Dumper& operator<<(const double theValue);
148   /// Dump std::endl
149   friend
150   MODELHIGHAPI_EXPORT
151   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
152                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
153
154   /// Dump GeomAPI_Pnt in the following form:
155   /// "GeomAPI_Pnt(X, Y, Z)"
156   MODELHIGHAPI_EXPORT
157   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
158   /// Dump GeomAPI_Dir
159   /// "GeomAPI_Dir(X, Y, Z)"
160   MODELHIGHAPI_EXPORT
161   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
162
163   /// Dump GeomDataAPI_Dir in the following form:
164   /// "X, Y, Z"
165   MODELHIGHAPI_EXPORT
166   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
167   /// Dump GeomDataAPI_Point in the following form:
168   /// "X, Y, Z"
169   MODELHIGHAPI_EXPORT
170   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
171   /// Dump GeomDataAPI_Point2D in the following form:
172   /// "X, Y"
173   MODELHIGHAPI_EXPORT
174   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
175
176   /// Dump AttributeBoolean
177   MODELHIGHAPI_EXPORT
178   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
179   /// Dump AttributeInteger
180   MODELHIGHAPI_EXPORT
181   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
182   /// Dump AttributeDouble
183   MODELHIGHAPI_EXPORT
184   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
185   /// Dump AttributeString
186   MODELHIGHAPI_EXPORT
187   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
188   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
189   MODELHIGHAPI_EXPORT
190   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
191
192   /// Dump result
193   MODELHIGHAPI_EXPORT
194   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
195
196   /// Dump Attribute
197   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
198     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
199   /// Dump Object
200   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
201     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
202
203   /// Dump AttributeRefAttr
204   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
205     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
206   /// Dump AttributeRefAttrList as follows:
207   /// "[obj1, obj2, obj3, ...]"
208   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
209     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
210   /// Dump AttributeRefList as follows:
211   /// "[obj1, obj2, obj3, ...]"
212   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
213     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
214   /// Dump AttributeSelection
215   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
216     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
217   /// Dump AttributeSelectionList
218   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
219     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
220   /// Dump AttributeReference
221   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
222   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
223   /// Dump AttributeStringArray
224   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
225     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
226
227   /// Clear dump buffer
228   MODELHIGHAPI_EXPORT
229   void clear(bool bufferOnly = false);
230   /// clear list of not dumped entities
231   MODELHIGHAPI_EXPORT void clearNotDumped();
232
233 protected:
234   /// Dump "setName" command if last entity had user-defined name
235   MODELHIGHAPI_EXPORT void dumpEntitySetName();
236
237 private:
238   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
239   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
240
241   /// Iterate all features in document and dump them into intermediate buffer
242   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
243
244   /// Dump composite feature and all it sub-features
245   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
246                bool isForce = false);
247
248   /// Iterate all features in composite feature and dump them into intermediate buffer
249   /// \param theComposite   [in] parent composite feature
250   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
251   MODELHIGHAPI_EXPORT
252   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
253                    bool theDumpModelDo = false);
254
255   /// Check the entity is already dumped
256   bool isDumped(const EntityPtr& theEntity) const;
257
258   /// Stores names of results for the given feature
259   void saveResultNames(const FeaturePtr& theFeature);
260
261   /// Check the result feature has default color
262   bool isDefaultColor(const ResultPtr& theResult) const;
263
264   /// Check the result feature has default deflection
265   bool isDefaultDeflection(const ResultPtr& theResult) const;
266
267 private:
268   struct EntityName {
269     std::string myCurrentName; ///< default name of current feature
270     std::string myUserName;    ///< user-defined name
271     bool        myIsDefault;   ///< \c true if the name is default
272     bool        myIsDumped;    ///< shows that the names of the feature are already stored
273
274     EntityName() {}
275
276     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
277       : myCurrentName(theCurName),
278         myUserName(theUserName),
279         myIsDefault(theDefault),
280         myIsDumped(false)
281     {}
282   };
283
284   typedef std::map<EntityPtr, EntityName>                     EntityNameMap;
285   typedef std::map<std::string, std::set<std::string> >       ModulesMap;
286   typedef std::map<DocumentPtr, std::map<std::string, int> >  NbFeaturesMap;
287
288   struct LastDumpedEntity {
289     EntityPtr            myEntity;   ///< last dumped entity
290     bool                 myUserName; ///< the entity hase user-defined name
291     /// results of this entity, which has user-defined names or specific colors
292     std::list<ResultPtr> myResults;
293
294     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
295       const std::list<ResultPtr>& theResults)
296       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
297     {}
298   };
299   typedef std::stack<LastDumpedEntity>                              DumpStack;
300
301   static ModelHighAPI_Dumper* mySelf;
302
303   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
304   std::ostringstream  myFullDump;           ///< full buffer of dumped data
305
306   ModulesMap          myModules;            ///< modules and entities to be imported
307   EntityNameMap       myNames;              ///< names of the entities
308   DumpStack           myEntitiesStack;      ///< stack of dumped entities
309
310   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
311
312   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
313   std::set<FeaturePtr> myFeaturesToSkip;
314
315 protected:
316    /// list of entities, used by other features but not dumped yet
317   std::set<EntityPtr> myNotDumpedEntities;
318
319   friend class SketchAPI_Sketch;
320 };
321
322 #endif