Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.h
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModelHighAPI_Dumper_H_
21 #define ModelHighAPI_Dumper_H_
22
23 #include "ModelHighAPI.h"
24
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <set>
29 #include <sstream>
30 #include <stack>
31 #include <string>
32
33 class GeomAPI_Pnt;
34 class GeomAPI_Dir;
35
36 class GeomDataAPI_Dir;
37 class GeomDataAPI_Point;
38 class GeomDataAPI_Point2D;
39 class GeomDataAPI_Point2DArray;
40
41 class ModelAPI_Attribute;
42 class ModelAPI_AttributeBoolean;
43 class ModelAPI_AttributeDouble;
44 class ModelAPI_AttributeDoubleArray;
45 class ModelAPI_AttributeIntArray;
46 class ModelAPI_AttributeInteger;
47 class ModelAPI_AttributeRefAttr;
48 class ModelAPI_AttributeRefAttrList;
49 class ModelAPI_AttributeReference;
50 class ModelAPI_AttributeRefList;
51 class ModelAPI_AttributeSelection;
52 class ModelAPI_AttributeSelectionList;
53 class ModelAPI_AttributeString;
54 class ModelAPI_AttributeStringArray;
55 class ModelAPI_CompositeFeature;
56 class ModelAPI_Document;
57 class ModelAPI_Entity;
58 class ModelAPI_Feature;
59 class ModelAPI_Folder;
60 class ModelAPI_Object;
61 class ModelAPI_Result;
62
63 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
64 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
65 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
66 typedef std::shared_ptr<ModelAPI_Folder>   FolderPtr;
67 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
68
69 typedef std::set<std::string>                                                ModulesSet;
70 typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > >  NbFeaturesMap;
71
72 /**\class ModelHighAPI_Dumper
73  * \ingroup CPPHighAPI
74  * \brief Dump engine for the model
75  *
76  * The dumper can be customized by the set of storages (DumpStorage), for example to identify
77  * dumped selected objects by their geometrical properties. By default, the dump is executed to
78  * store original names of the selected shapes.
79  */
80 class ModelHighAPI_Dumper
81 {
82 public:
83   /** \class DumpStorage
84    *  \ingroup CPPHighAPI
85    *  \brief Storage for the dumped data. Process selection attributes to be dumped by naming.
86    */
87   class DumpStorage
88   {
89   public:
90     DumpStorage() {}
91     MODELHIGHAPI_EXPORT DumpStorage(const DumpStorage& theOther);
92     MODELHIGHAPI_EXPORT const DumpStorage& operator=(const DumpStorage& theOther);
93
94     void setFilenameSuffix(const std::string& theSuffix) { myFilenameSuffix = theSuffix; }
95
96   protected:
97     std::ostringstream& buffer() { return myDumpBuffer; }
98     std::ostringstream& fullDump() { return myFullDump; }
99
100     MODELHIGHAPI_EXPORT virtual void reserveBuffer();
101     MODELHIGHAPI_EXPORT virtual void restoreReservedBuffer();
102
103     MODELHIGHAPI_EXPORT
104     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
105
106     MODELHIGHAPI_EXPORT
107     virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules);
108
109   private:
110     std::string myFilenameSuffix;
111     std::ostringstream myDumpBuffer;
112     std::ostringstream myFullDump;
113
114     std::stack<std::string> myDumpBufferHideout;
115
116     friend class ModelHighAPI_Dumper;
117   };
118   typedef std::shared_ptr<DumpStorage> DumpStoragePtr;
119
120   /** \class DumpStorageGeom
121    *  \ingroup CPPHighAPI
122    *  \brief Process selection attributes to be dumped by geometric properties.
123    */
124   class DumpStorageGeom : public DumpStorage
125   {
126   protected:
127     MODELHIGHAPI_EXPORT
128     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
129   };
130
131   /** \class DumpStorageWeak
132    *  \ingroup CPPHighAPI
133    *  \brief Process selection attributes to be dumped by weak naming.
134    */
135   class DumpStorageWeak : public DumpStorage
136   {
137   protected:
138     MODELHIGHAPI_EXPORT
139     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
140   };
141
142 public:
143   /// Default constructor
144   MODELHIGHAPI_EXPORT
145   ModelHighAPI_Dumper();
146
147   /// Sets instance of a dumper
148   MODELHIGHAPI_EXPORT
149   static void setInstance(ModelHighAPI_Dumper* theDumper);
150
151   /// Returns instance of a dumper
152   MODELHIGHAPI_EXPORT
153   static ModelHighAPI_Dumper* getInstance();
154
155   /// Destructor
156   MODELHIGHAPI_EXPORT
157   virtual ~ModelHighAPI_Dumper();
158
159   /// Add custom storage to collect corresponding dump
160   MODELHIGHAPI_EXPORT
161   void addCustomStorage(const DumpStoragePtr& theStorage);
162   /// Clear custom storages list
163   MODELHIGHAPI_EXPORT
164   void clearCustomStorage();
165   /// Dump given document into the file
166   /// \return \c true, if succeed
167   MODELHIGHAPI_EXPORT
168   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theFileName);
169
170   /// Add module to list of imported modules
171   /// \param theModuleName  name of the module to be imported
172   MODELHIGHAPI_EXPORT
173   void importModule(const std::string& theModuleName);
174
175   /// Returns name of specified entity
176   /// \param theEntity        [in] named entity
177   /// \param theSaveNotDumped [in]
178   ///    if \c true, the entity should be stored as not dumped (will be dumped automatically)
179   /// \param theUseEntityName [in]
180   ///    if \c true, the entity name should be used "as is" without changing default name
181   /// \return name of the entity
182   MODELHIGHAPI_EXPORT
183   const std::string& name(const EntityPtr& theEntity, bool theSaveNotDumped = true,
184                           bool theUseEntityName = false);
185
186   /// Returns name of parent composite feature for specified entity
187   MODELHIGHAPI_EXPORT
188   const std::string& parentName(const FeaturePtr& theFeature);
189
190   /// Dump parameter feature only
191   virtual void dumpParameter(const FeaturePtr& theFeature) = 0;
192   /// Dump given feature
193   virtual void dumpFeature(const FeaturePtr& theFeature, const bool theForce = false) = 0;
194   /// Dump folder
195   virtual void dumpFolder(const FolderPtr& theFolder) = 0;
196
197   /// Set feature postponed until all its dependencies are not dumped.
198   /// The name of the feature is stored anyway.
199   MODELHIGHAPI_EXPORT
200   void postpone(const EntityPtr& theEntity);
201
202   /// Set a feature that should not be dumped anyway
203   MODELHIGHAPI_EXPORT
204   void doNotDumpFeature(const FeaturePtr& theFeature)
205   { myFeaturesToSkip.insert(theFeature); }
206
207   /// Dump sub-feature name and color, without dumping feature creation.
208   /// Used for features which creates sub-features in their execute method.
209   /// \param theSubFeatureGet [in] method for getting sub-feature (e.g. "Feature_1.subFeature(0)")
210   /// \param theSubFeature    [in] sub-feature
211   MODELHIGHAPI_EXPORT
212   void dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
213                                   const FeaturePtr& theSubFeature);
214
215   /// Return name of getter for corresponding attribute
216   virtual std::string attributeGetter(const FeaturePtr& theFeature,
217                                       const std::string& theAttrName) const = 0;
218
219   /// Return name of wrapper feature
220   virtual std::string featureWrapper(const FeaturePtr& theFeature) const = 0;
221
222   /// Dump character
223   MODELHIGHAPI_EXPORT
224   ModelHighAPI_Dumper& operator<<(const char theChar);
225   /// Dump string
226   MODELHIGHAPI_EXPORT
227   ModelHighAPI_Dumper& operator<<(const char* theString);
228   /// Dump string
229   MODELHIGHAPI_EXPORT
230   ModelHighAPI_Dumper& operator<<(const std::string& theString);
231   /// Dump boolean
232   MODELHIGHAPI_EXPORT
233   ModelHighAPI_Dumper& operator<<(const bool theValue);
234   /// Dump integer
235   MODELHIGHAPI_EXPORT
236   ModelHighAPI_Dumper& operator<<(const int theValue);
237   /// Dump real
238   MODELHIGHAPI_EXPORT
239   ModelHighAPI_Dumper& operator<<(const double theValue);
240   /// Dump std::endl
241   friend
242   MODELHIGHAPI_EXPORT
243   ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
244                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&));
245
246   /// Dump GeomAPI_Pnt in the following form:
247   /// "GeomAPI_Pnt(X, Y, Z)"
248   MODELHIGHAPI_EXPORT
249   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
250   /// Dump GeomAPI_Dir
251   /// "GeomAPI_Dir(X, Y, Z)"
252   MODELHIGHAPI_EXPORT
253   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir);
254
255   /// Dump GeomDataAPI_Dir in the following form:
256   /// "X, Y, Z"
257   MODELHIGHAPI_EXPORT
258   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Dir>& theDir);
259   /// Dump GeomDataAPI_Point in the following form:
260   /// "X, Y, Z"
261   MODELHIGHAPI_EXPORT
262   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point>& thePoint);
263   /// Dump GeomDataAPI_Point2D in the following form:
264   /// "X, Y"
265   MODELHIGHAPI_EXPORT
266   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
267   /// Dump GeomDataAPI_Point2DArray as a list of 2D points
268   MODELHIGHAPI_EXPORT
269   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<GeomDataAPI_Point2DArray>& thePointArray);
270
271   /// Dump AttributeBoolean
272   MODELHIGHAPI_EXPORT
273   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool);
274   /// Dump AttributeInteger
275   MODELHIGHAPI_EXPORT
276   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt);
277   /// Dump AttributeIntArray
278   MODELHIGHAPI_EXPORT
279   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeIntArray>& theArray);
280   /// Dump AttributeDouble
281   MODELHIGHAPI_EXPORT
282   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
283   /// Dump AttributeDoubleArray
284   MODELHIGHAPI_EXPORT
285   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray);
286   /// Dump AttributeString
287   MODELHIGHAPI_EXPORT
288   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
289   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
290   MODELHIGHAPI_EXPORT
291   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
292
293   /// Dump folder
294   MODELHIGHAPI_EXPORT
295   ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
296
297   /// Dump result
298   MODELHIGHAPI_EXPORT
299   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
300
301   /// Dump a list of results
302   MODELHIGHAPI_EXPORT
303   ModelHighAPI_Dumper& operator<<(const std::list<ResultPtr>& theResults);
304
305   /// Dump Attribute
306   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
307     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
308   /// Dump Object
309   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
310     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
311
312   /// Dump AttributeRefAttr
313   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
314     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
315   /// Dump AttributeRefAttrList as follows:
316   /// "[obj1, obj2, obj3, ...]"
317   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
318     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
319   /// Dump AttributeRefList as follows:
320   /// "[obj1, obj2, obj3, ...]"
321   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
322     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
323   /// Dump AttributeSelection
324   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
325     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
326   /// Dump AttributeSelectionList
327   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
328     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
329   /// Dump AttributeReference
330   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
331   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
332   /// Dump AttributeStringArray
333   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
334     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
335
336   /// clear list of not dumped entities
337   MODELHIGHAPI_EXPORT void clearNotDumped();
338
339   /// Check the entity is already dumped
340   MODELHIGHAPI_EXPORT
341   bool isDumped(const EntityPtr& theEntity) const;
342   /// Check theRefAttr is already dumped
343   MODELHIGHAPI_EXPORT
344   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
345   /// Check all objects in theRefList are already dumped
346   MODELHIGHAPI_EXPORT
347   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
348
349   /// Export variables names to another module (calls exportVariable implemented in python)
350   MODELHIGHAPI_EXPORT virtual void exportVariables() const;
351
352   /// Export one variable name to another module (implemented in python)
353   MODELHIGHAPI_EXPORT virtual void exportVariable(
354     const std::string& theEntry, const std::string& theVarName) const {}
355
356 protected:
357   /// Dump "setName" command if last entity had user-defined name
358   MODELHIGHAPI_EXPORT void dumpEntitySetName();
359
360 private:
361   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
362   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
363
364   /// Iterate all features in document and dump them into intermediate buffer
365   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
366
367   /// Dump composite feature and all it sub-features
368   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
369                bool isForce = false,
370                bool isDumpModelDo = true);
371
372   /// Iterate all features in composite feature and dump them into intermediate buffer
373   /// \param theComposite   [in] parent composite feature
374   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
375   MODELHIGHAPI_EXPORT
376   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
377                    bool theDumpModelDo = false);
378
379   /// Stores names of results for the given feature
380   void saveResultNames(const FeaturePtr& theFeature);
381
382   /// Check the result feature has default color
383   bool isDefaultColor(const ResultPtr& theResult) const;
384
385   /// Check the result feature has default deflection
386   bool isDefaultDeflection(const ResultPtr& theResult) const;
387
388   /// Check the result feature has default transparency
389   bool isDefaultTransparency(const ResultPtr& theResult) const;
390
391   /// Dump postponed entities
392   void dumpPostponed(bool theDumpFolders = false);
393
394 private:
395   struct EntityName {
396     std::string myCurrentName; ///< default name of current feature
397     std::string myUserName;    ///< user-defined name
398     bool        myIsDefault;   ///< \c true if the name is default
399     bool        myIsDumped;    ///< shows that the names of the feature are already stored
400
401     EntityName() {}
402
403     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
404       : myCurrentName(theCurName),
405         myUserName(theUserName),
406         myIsDefault(theDefault),
407         myIsDumped(false)
408     {}
409   };
410   typedef std::map<EntityPtr, EntityName> EntityNameMap;
411
412   struct LastDumpedEntity {
413     EntityPtr            myEntity;   ///< last dumped entity
414     bool                 myUserName; ///< the entity hase user-defined name
415     /// results of this entity, which has user-defined names or specific colors
416     std::list<ResultPtr> myResults;
417
418     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
419       const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
420       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
421     {}
422   };
423   typedef std::stack<LastDumpedEntity> DumpStack;
424
425   static ModelHighAPI_Dumper* mySelf;
426
427   class DumpStorageBuffer;
428   DumpStorageBuffer*  myDumpStorage;        ///< storage of dumped data
429
430   ModulesSet          myModules;            ///< modules and entities to be imported
431   EntityNameMap       myNames;              ///< names of the entities
432   DumpStack           myEntitiesStack;      ///< stack of dumped entities
433
434   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
435
436   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
437   std::set<FeaturePtr> myFeaturesToSkip;
438
439   std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
440   bool myDumpPostponedInProgress; ///< processing postponed is in progress
441
442 protected:
443   /// list of entities, used by other features but not dumped yet
444   std::set<EntityPtr> myNotDumpedEntities;
445
446   friend class SketchAPI_Sketch;
447   friend class ModelHighAPI_Folder;
448 };
449
450 #endif