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