Salome HOME
e029335983ea3c6e71ef5e769b3c6ba73a3ec85f
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef ModelHighAPI_Dumper_H_
22 #define ModelHighAPI_Dumper_H_
23
24 #include "ModelHighAPI.h"
25
26 #include <list>
27 #include <map>
28 #include <memory>
29 #include <set>
30 #include <sstream>
31 #include <stack>
32 #include <string>
33
34 class GeomAPI_Pnt;
35 class GeomAPI_Dir;
36
37 class GeomDataAPI_Dir;
38 class GeomDataAPI_Point;
39 class GeomDataAPI_Point2D;
40
41 class ModelAPI_Attribute;
42 class ModelAPI_AttributeBoolean;
43 class ModelAPI_AttributeDouble;
44 class ModelAPI_AttributeInteger;
45 class ModelAPI_AttributeRefAttr;
46 class ModelAPI_AttributeRefAttrList;
47 class ModelAPI_AttributeReference;
48 class ModelAPI_AttributeRefList;
49 class ModelAPI_AttributeSelection;
50 class ModelAPI_AttributeSelectionList;
51 class ModelAPI_AttributeString;
52 class ModelAPI_AttributeStringArray;
53 class ModelAPI_CompositeFeature;
54 class ModelAPI_Document;
55 class ModelAPI_Entity;
56 class ModelAPI_Feature;
57 class ModelAPI_Folder;
58 class ModelAPI_Object;
59 class ModelAPI_Result;
60
61 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
62 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
63 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
64 typedef std::shared_ptr<ModelAPI_Folder>   FolderPtr;
65 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
66
67 /**\class ModelHighAPI_Dumper
68  * \ingroup CPPHighAPI
69  * \brief Dump engine for the model
70  */
71 class ModelHighAPI_Dumper
72 {
73 public:
74   /// Default constructor
75   MODELHIGHAPI_EXPORT
76   ModelHighAPI_Dumper();
77
78   /// Sets instance of a dumper
79   MODELHIGHAPI_EXPORT
80   static void setInstance(ModelHighAPI_Dumper* theDumper);
81
82   /// Returns instance of a dumper
83   MODELHIGHAPI_EXPORT
84   static ModelHighAPI_Dumper* getInstance();
85
86   /// Destructor
87   virtual ~ModelHighAPI_Dumper() {}
88
89   /// Dump given document into the file
90   /// \return \c true, if succeed
91   MODELHIGHAPI_EXPORT
92   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
93
94   /// Add module to list of imported modules
95   /// \param theModuleName  name of the module to be imported
96   /// \param theObject      name of the entity to be imported
97   ///                       from the module (if empty, while module will be imported)
98   MODELHIGHAPI_EXPORT
99   void importModule(const std::string& theModuleName,
100                     const std::string& theObject = std::string());
101
102   /// Returns name of specified entity
103   /// \param theEntity        [in] named entity
104   /// \param theSaveNotDumped [in]
105   ///    if \c true, the entity should be stored as not dumped (will be dumped automatically)
106   /// \param theUseEntityName [in]
107   ///    if \c true, the entity name should be used "as is" without changing default name
108   /// \return name of the entity
109   MODELHIGHAPI_EXPORT
110   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
111                           bool theUseEntityName = false);
112
113   /// Returns name of parent composite feature for specified entity
114   MODELHIGHAPI_EXPORT
115   const std::string& parentName(const FeaturePtr& theFeature);
116
117   /// Dump parameter feature only
118   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
119   /// Dump given feature
120   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
121   /// Dump folder
122   virtual void dumpFolder(const FolderPtr& theFolder) = 0;
123
124   /// Set a feature that should not be dumped anyway
125   MODELHIGHAPI_EXPORT
126   void doNotDumpFeature(const FeaturePtr& theFeature)
127   { myFeaturesToSkip.insert(theFeature); }
128
129   /// Dump sub-feature name and color, without dumping feature creation.
130   /// Used for features which creates sub-features in their execute method.
131   /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
132   /// \param theSubFeature    [in] sub-feature
133   MODELHIGHAPI_EXPORT
134   void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
135                                   const FeaturePtr& theSubFeature);
136
137   /// Return name of getter for corresponding attribute
138   virtual std::string attributeGetter(const FeaturePtr& theFeature,
139                                       const std::string& theAttrName) const = 0;
140
141   /// Return name of wrapper feature
142   virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
143
144   /// Save all dumps into specified file
145   MODELHIGHAPI_EXPORT
146   bool exportTo(const std::string& theFileName);
147
148   /// Dump character
149   MODELHIGHAPI_EXPORT
150   ModelHighAPI_Dumper& operator<<(const char theChar);
151   /// Dump string
152   MODELHIGHAPI_EXPORT
153   ModelHighAPI_Dumper& operator<<(const char* theString);
154   /// Dump string
155   MODELHIGHAPI_EXPORT
156   ModelHighAPI_Dumper& operator<<(const std::string& theString);
157   /// Dump boolean
158   MODELHIGHAPI_EXPORT
159   ModelHighAPI_Dumper& operator<<(const bool theValue);
160   /// Dump integer
161   MODELHIGHAPI_EXPORT
162   ModelHighAPI_Dumper& operator<<(const int theValue);
163   /// Dump real
164   MODELHIGHAPI_EXPORT
165   ModelHighAPI_Dumper& operator<<(const double theValue);
166   /// Dump std::endl
167   friend
168   MODELHIGHAPI_EXPORT
169   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
170                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
171
172   /// Dump GeomAPI_Pnt in the following form:
173   /// "GeomAPI_Pnt(X, Y, Z)"
174   MODELHIGHAPI_EXPORT
175   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
176   /// Dump GeomAPI_Dir
177   /// "GeomAPI_Dir(X, Y, Z)"
178   MODELHIGHAPI_EXPORT
179   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
180
181   /// Dump GeomDataAPI_Dir in the following form:
182   /// "X, Y, Z"
183   MODELHIGHAPI_EXPORT
184   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
185   /// Dump GeomDataAPI_Point in the following form:
186   /// "X, Y, Z"
187   MODELHIGHAPI_EXPORT
188   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
189   /// Dump GeomDataAPI_Point2D in the following form:
190   /// "X, Y"
191   MODELHIGHAPI_EXPORT
192   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
193
194   /// Dump AttributeBoolean
195   MODELHIGHAPI_EXPORT
196   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
197   /// Dump AttributeInteger
198   MODELHIGHAPI_EXPORT
199   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
200   /// Dump AttributeDouble
201   MODELHIGHAPI_EXPORT
202   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
203   /// Dump AttributeString
204   MODELHIGHAPI_EXPORT
205   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
206   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
207   MODELHIGHAPI_EXPORT
208   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
209
210   /// Dump folder
211   MODELHIGHAPI_EXPORT
212   ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
213
214   /// Dump result
215   MODELHIGHAPI_EXPORT
216   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
217
218   /// Dump Attribute
219   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
220     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
221   /// Dump Object
222   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
223     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
224
225   /// Dump AttributeRefAttr
226   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
227     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
228   /// Dump AttributeRefAttrList as follows:
229   /// "[obj1, obj2, obj3, ...]"
230   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
231     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
232   /// Dump AttributeRefList as follows:
233   /// "[obj1, obj2, obj3, ...]"
234   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
235     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
236   /// Dump AttributeSelection
237   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
238     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
239   /// Dump AttributeSelectionList
240   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
241     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
242   /// Dump AttributeReference
243   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
244   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
245   /// Dump AttributeStringArray
246   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
247     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
248
249   /// Clear dump buffer
250   MODELHIGHAPI_EXPORT
251   void clear(bool bufferOnly = false);
252   /// clear list of not dumped entities
253   MODELHIGHAPI_EXPORT void clearNotDumped();
254
255 protected:
256   /// Dump "setName" command if last entity had user-defined name
257   MODELHIGHAPI_EXPORT void dumpEntitySetName();
258
259 private:
260   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
261   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
262
263   /// Iterate all features in document and dump them into intermediate buffer
264   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
265
266   /// Dump composite feature and all it sub-features
267   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
268                bool isForce = false);
269
270   /// Iterate all features in composite feature and dump them into intermediate buffer
271   /// \param theComposite   [in] parent composite feature
272   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
273   MODELHIGHAPI_EXPORT
274   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
275                    bool theDumpModelDo = false);
276
277   /// Check the entity is already dumped
278   bool isDumped(const EntityPtr& theEntity) const;
279
280   /// Stores names of results for the given feature
281   void saveResultNames(const FeaturePtr& theFeature);
282
283   /// Check the result feature has default color
284   bool isDefaultColor(const ResultPtr& theResult) const;
285
286   /// Check the result feature has default deflection
287   bool isDefaultDeflection(const ResultPtr& theResult) const;
288
289   /// Check the result feature has default transparency
290   bool isDefaultTransparency(const ResultPtr& theResult) const;
291
292   /// Dump stored folders if possible
293   void dumpFolders();
294
295 private:
296   struct EntityName {
297     std::string myCurrentName; ///< default name of current feature
298     std::string myUserName;    ///< user-defined name
299     bool        myIsDefault;   ///< \c true if the name is default
300     bool        myIsDumped;    ///< shows that the names of the feature are already stored
301
302     EntityName() {}
303
304     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
305       : myCurrentName(theCurName),
306         myUserName(theUserName),
307         myIsDefault(theDefault),
308         myIsDumped(false)
309     {}
310   };
311
312   typedef std::map<EntityPtr, EntityName>                     EntityNameMap;
313   typedef std::map<std::string, std::set<std::string> >       ModulesMap;
314   typedef std::map<DocumentPtr, std::map<std::string, int> >  NbFeaturesMap;
315
316   struct LastDumpedEntity {
317     EntityPtr            myEntity;   ///< last dumped entity
318     bool                 myUserName; ///< the entity hase user-defined name
319     /// results of this entity, which has user-defined names or specific colors
320     std::list<ResultPtr> myResults;
321
322     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
323       const std::list<ResultPtr>& theResults)
324       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
325     {}
326   };
327   typedef std::stack<LastDumpedEntity>                              DumpStack;
328
329   static ModelHighAPI_Dumper* mySelf;
330
331   std::ostringstream  myDumpBuffer;         ///< intermediate buffer to store dumping data
332   std::ostringstream  myFullDump;           ///< full buffer of dumped data
333
334   ModulesMap          myModules;            ///< modules and entities to be imported
335   EntityNameMap       myNames;              ///< names of the entities
336   DumpStack           myEntitiesStack;      ///< stack of dumped entities
337
338   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
339
340   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
341   std::set<FeaturePtr> myFeaturesToSkip;
342
343 protected:
344   /// list of entities, used by other features but not dumped yet
345   std::set<EntityPtr> myNotDumpedEntities;
346   /// list of folders which do not dumped yet
347   std::set<FolderPtr> myNotDumpedFolders;
348
349   friend class SketchAPI_Sketch;
350   friend class ModelHighAPI_Folder;
351 };
352
353 #endif