]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Dumper.h
Salome HOME
Issue #17347: B-Splines in Sketcher
[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_AttributeInteger;
46 class ModelAPI_AttributeRefAttr;
47 class ModelAPI_AttributeRefAttrList;
48 class ModelAPI_AttributeReference;
49 class ModelAPI_AttributeRefList;
50 class ModelAPI_AttributeSelection;
51 class ModelAPI_AttributeSelectionList;
52 class ModelAPI_AttributeString;
53 class ModelAPI_AttributeStringArray;
54 class ModelAPI_CompositeFeature;
55 class ModelAPI_Document;
56 class ModelAPI_Entity;
57 class ModelAPI_Feature;
58 class ModelAPI_Folder;
59 class ModelAPI_Object;
60 class ModelAPI_Result;
61
62 typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
63 typedef std::shared_ptr<ModelAPI_Entity>   EntityPtr;
64 typedef std::shared_ptr<ModelAPI_Feature>  FeaturePtr;
65 typedef std::shared_ptr<ModelAPI_Folder>   FolderPtr;
66 typedef std::shared_ptr<ModelAPI_Result>   ResultPtr;
67
68 typedef std::set<std::string>                                                ModulesSet;
69 typedef std::map<DocumentPtr, std::map<std::string, std::pair<int, int> > >  NbFeaturesMap;
70
71 /**\class ModelHighAPI_Dumper
72  * \ingroup CPPHighAPI
73  * \brief Dump engine for the model
74  *
75  * The dumper can be customized by the set of storages (DumpStorage), for example to identify
76  * dumped selected objects by their geometrical properties. By default, the dump is executed to
77  * store original names of the selected shapes.
78  */
79 class ModelHighAPI_Dumper
80 {
81 public:
82   /** \class DumpStorage
83    *  \ingroup CPPHighAPI
84    *  \brief Storage for the dumped data. Process selection attributes to be dumped by naming.
85    */
86   class DumpStorage
87   {
88   public:
89     DumpStorage() {}
90     MODELHIGHAPI_EXPORT DumpStorage(const DumpStorage& theOther);
91     MODELHIGHAPI_EXPORT const DumpStorage& operator=(const DumpStorage& theOther);
92
93     void setFilenameSuffix(const std::string& theSuffix) { myFilenameSuffix = theSuffix; }
94
95   protected:
96     std::ostringstream& buffer() { return myDumpBuffer; }
97     std::ostringstream& fullDump() { return myFullDump; }
98
99     MODELHIGHAPI_EXPORT virtual void reserveBuffer();
100     MODELHIGHAPI_EXPORT virtual void restoreReservedBuffer();
101
102     MODELHIGHAPI_EXPORT
103     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
104
105     MODELHIGHAPI_EXPORT
106     virtual bool exportTo(const std::string& theFilename, const ModulesSet& theUsedModules);
107
108   private:
109     std::string myFilenameSuffix;
110     std::ostringstream myDumpBuffer;
111     std::ostringstream myFullDump;
112
113     std::stack<std::string> myDumpBufferHideout;
114
115     friend class ModelHighAPI_Dumper;
116   };
117   typedef std::shared_ptr<DumpStorage> DumpStoragePtr;
118
119   /** \class DumpStorageGeom
120    *  \ingroup CPPHighAPI
121    *  \brief Process selection attributes to be dumped by geometric properties.
122    */
123   class DumpStorageGeom : public DumpStorage
124   {
125   protected:
126     MODELHIGHAPI_EXPORT
127     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
128   };
129
130   /** \class DumpStorageWeak
131    *  \ingroup CPPHighAPI
132    *  \brief Process selection attributes to be dumped by weak naming.
133    */
134   class DumpStorageWeak : public DumpStorage
135   {
136   protected:
137     MODELHIGHAPI_EXPORT
138     virtual void write(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
139   };
140
141 public:
142   /// Default constructor
143   MODELHIGHAPI_EXPORT
144   ModelHighAPI_Dumper();
145
146   /// Sets instance of a dumper
147   MODELHIGHAPI_EXPORT
148   static void setInstance(ModelHighAPI_Dumper* theDumper);
149
150   /// Returns instance of a dumper
151   MODELHIGHAPI_EXPORT
152   static ModelHighAPI_Dumper* getInstance();
153
154   /// Destructor
155   MODELHIGHAPI_EXPORT
156   virtual ~ModelHighAPI_Dumper();
157
158   /// Add custom storage to collect corresponding dump
159   MODELHIGHAPI_EXPORT
160   void addCustomStorage(const DumpStoragePtr& theStorage);
161   /// Clear custom storages list
162   MODELHIGHAPI_EXPORT
163   void clearCustomStorage();
164
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 AttributeDouble
278   MODELHIGHAPI_EXPORT
279   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal);
280   /// Dump AttributeDoubleArray
281   MODELHIGHAPI_EXPORT
282   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeDoubleArray>& theArray);
283   /// Dump AttributeString
284   MODELHIGHAPI_EXPORT
285   ModelHighAPI_Dumper& operator<<(const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr);
286   /// Dump name of entity and remember to dump "setName" if the entity has user-defined name
287   MODELHIGHAPI_EXPORT
288   ModelHighAPI_Dumper& operator<<(const FeaturePtr& theEntity);
289
290   /// Dump folder
291   MODELHIGHAPI_EXPORT
292   ModelHighAPI_Dumper& operator<<(const FolderPtr& theFolder);
293
294   /// Dump result
295   MODELHIGHAPI_EXPORT
296   ModelHighAPI_Dumper& operator<<(const ResultPtr& theResult);
297
298   /// Dump a list of results
299   MODELHIGHAPI_EXPORT
300   ModelHighAPI_Dumper& operator<<(const std::list<ResultPtr>& theResults);
301
302   /// Dump Attribute
303   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
304     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
305   /// Dump Object
306   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
307     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
308
309   /// Dump AttributeRefAttr
310   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
311     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
312   /// Dump AttributeRefAttrList as follows:
313   /// "[obj1, obj2, obj3, ...]"
314   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
315     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
316   /// Dump AttributeRefList as follows:
317   /// "[obj1, obj2, obj3, ...]"
318   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
319     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
320   /// Dump AttributeSelection
321   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
322     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
323   /// Dump AttributeSelectionList
324   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
325     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
326   /// Dump AttributeReference
327   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
328   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
329   /// Dump AttributeStringArray
330   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
331     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
332
333   /// clear list of not dumped entities
334   MODELHIGHAPI_EXPORT void clearNotDumped();
335
336   /// Check the entity is already dumped
337   MODELHIGHAPI_EXPORT
338   bool isDumped(const EntityPtr& theEntity) const;
339   /// Check theRefAttr is already dumped
340   MODELHIGHAPI_EXPORT
341   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
342   /// Check all objects in theRefList are already dumped
343   MODELHIGHAPI_EXPORT
344   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
345
346 protected:
347   /// Dump "setName" command if last entity had user-defined name
348   MODELHIGHAPI_EXPORT void dumpEntitySetName();
349
350 private:
351   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
352   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
353
354   /// Iterate all features in document and dump them into intermediate buffer
355   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
356
357   /// Dump composite feature and all it sub-features
358   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
359                bool isForce = false,
360                bool isDumpModelDo = true);
361
362   /// Iterate all features in composite feature and dump them into intermediate buffer
363   /// \param theComposite   [in] parent composite feature
364   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
365   MODELHIGHAPI_EXPORT
366   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
367                    bool theDumpModelDo = false);
368
369   /// Stores names of results for the given feature
370   void saveResultNames(const FeaturePtr& theFeature);
371
372   /// Check the result feature has default color
373   bool isDefaultColor(const ResultPtr& theResult) const;
374
375   /// Check the result feature has default deflection
376   bool isDefaultDeflection(const ResultPtr& theResult) const;
377
378   /// Check the result feature has default transparency
379   bool isDefaultTransparency(const ResultPtr& theResult) const;
380
381   /// Dump postponed entities
382   void dumpPostponed(bool theDumpFolders = false);
383
384 private:
385   struct EntityName {
386     std::string myCurrentName; ///< default name of current feature
387     std::string myUserName;    ///< user-defined name
388     bool        myIsDefault;   ///< \c true if the name is default
389     bool        myIsDumped;    ///< shows that the names of the feature are already stored
390
391     EntityName() {}
392
393     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
394       : myCurrentName(theCurName),
395         myUserName(theUserName),
396         myIsDefault(theDefault),
397         myIsDumped(false)
398     {}
399   };
400   typedef std::map<EntityPtr, EntityName> EntityNameMap;
401
402   struct LastDumpedEntity {
403     EntityPtr            myEntity;   ///< last dumped entity
404     bool                 myUserName; ///< the entity hase user-defined name
405     /// results of this entity, which has user-defined names or specific colors
406     std::list<ResultPtr> myResults;
407
408     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
409       const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
410       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
411     {}
412   };
413   typedef std::stack<LastDumpedEntity> DumpStack;
414
415   static ModelHighAPI_Dumper* mySelf;
416
417   class DumpStorageBuffer;
418   DumpStorageBuffer*  myDumpStorage;        ///< storage of dumped data
419
420   ModulesSet          myModules;            ///< modules and entities to be imported
421   EntityNameMap       myNames;              ///< names of the entities
422   DumpStack           myEntitiesStack;      ///< stack of dumped entities
423
424   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
425
426   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
427   std::set<FeaturePtr> myFeaturesToSkip;
428
429   std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
430   bool myDumpPostponedInProgress; ///< processing postponed is in progress
431
432 protected:
433   /// list of entities, used by other features but not dumped yet
434   std::set<EntityPtr> myNotDumpedEntities;
435
436   friend class SketchAPI_Sketch;
437   friend class ModelHighAPI_Folder;
438 };
439
440 #endif