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