]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Dumper.h
Salome HOME
f4d0be3eb82f3aeb07f2599e92f5fd12697ca0e1
[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 Attribute
291   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
292     operator<<(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
293   /// Dump Object
294   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
295     operator<<(const std::shared_ptr<ModelAPI_Object>& theObject);
296
297   /// Dump AttributeRefAttr
298   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
299     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr);
300   /// Dump AttributeRefAttrList as follows:
301   /// "[obj1, obj2, obj3, ...]"
302   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
303     operator<<(const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList);
304   /// Dump AttributeRefList as follows:
305   /// "[obj1, obj2, obj3, ...]"
306   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
307     operator<<(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList);
308   /// Dump AttributeSelection
309   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
310     operator<<(const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect);
311   /// Dump AttributeSelectionList
312   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
313     operator<<(const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList);
314   /// Dump AttributeReference
315   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
316   operator<<(const std::shared_ptr<ModelAPI_AttributeReference>& theReference);
317   /// Dump AttributeStringArray
318   MODELHIGHAPI_EXPORT ModelHighAPI_Dumper&
319     operator<<(const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray);
320
321   /// clear list of not dumped entities
322   MODELHIGHAPI_EXPORT void clearNotDumped();
323
324   /// Check the entity is already dumped
325   MODELHIGHAPI_EXPORT
326   bool isDumped(const EntityPtr& theEntity) const;
327   /// Check theRefAttr is already dumped
328   MODELHIGHAPI_EXPORT
329   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr) const;
330   /// Check all objects in theRefList are already dumped
331   MODELHIGHAPI_EXPORT
332   bool isDumped(const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList) const;
333
334 protected:
335   /// Dump "setName" command if last entity had user-defined name
336   MODELHIGHAPI_EXPORT void dumpEntitySetName();
337
338 private:
339   ModelHighAPI_Dumper(const ModelHighAPI_Dumper&);
340   const ModelHighAPI_Dumper& operator=(const ModelHighAPI_Dumper&);
341
342   /// Iterate all features in document and dump them into intermediate buffer
343   bool process(const std::shared_ptr<ModelAPI_Document>& theDoc);
344
345   /// Dump composite feature and all it sub-features
346   bool process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
347                bool isForce = false);
348
349   /// Iterate all features in composite feature and dump them into intermediate buffer
350   /// \param theComposite   [in] parent composite feature
351   /// \param theDumpModelDo [in] shows that command "model.do()" should be written at the end
352   MODELHIGHAPI_EXPORT
353   bool processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
354                    bool theDumpModelDo = false);
355
356   /// Stores names of results for the given feature
357   void saveResultNames(const FeaturePtr& theFeature);
358
359   /// Check the result feature has default color
360   bool isDefaultColor(const ResultPtr& theResult) const;
361
362   /// Check the result feature has default deflection
363   bool isDefaultDeflection(const ResultPtr& theResult) const;
364
365   /// Check the result feature has default transparency
366   bool isDefaultTransparency(const ResultPtr& theResult) const;
367
368   /// Dump postponed entities
369   void dumpPostponed(bool theDumpFolders = false);
370
371 private:
372   struct EntityName {
373     std::string myCurrentName; ///< default name of current feature
374     std::string myUserName;    ///< user-defined name
375     bool        myIsDefault;   ///< \c true if the name is default
376     bool        myIsDumped;    ///< shows that the names of the feature are already stored
377
378     EntityName() {}
379
380     EntityName(const std::string& theCurName, const std::string& theUserName, bool theDefault)
381       : myCurrentName(theCurName),
382         myUserName(theUserName),
383         myIsDefault(theDefault),
384         myIsDumped(false)
385     {}
386   };
387   typedef std::map<EntityPtr, EntityName> EntityNameMap;
388
389   struct LastDumpedEntity {
390     EntityPtr            myEntity;   ///< last dumped entity
391     bool                 myUserName; ///< the entity hase user-defined name
392     /// results of this entity, which has user-defined names or specific colors
393     std::list<ResultPtr> myResults;
394
395     LastDumpedEntity(EntityPtr theEntity, bool theUserName,
396       const std::list<ResultPtr>& theResults = std::list<ResultPtr>())
397       : myEntity(theEntity), myUserName(theUserName), myResults(theResults)
398     {}
399   };
400   typedef std::stack<LastDumpedEntity> DumpStack;
401
402   static ModelHighAPI_Dumper* mySelf;
403
404   class DumpStorageBuffer;
405   DumpStorageBuffer*  myDumpStorage;        ///< storage of dumped data
406
407   ModulesSet          myModules;            ///< modules and entities to be imported
408   EntityNameMap       myNames;              ///< names of the entities
409   DumpStack           myEntitiesStack;      ///< stack of dumped entities
410
411   NbFeaturesMap       myFeatureCount;       ///< number of features of each kind
412
413   /// features which should not be dumped (like coincidence and tangency created by tangent arc)
414   std::set<FeaturePtr> myFeaturesToSkip;
415
416   std::list<EntityPtr> myPostponed; ///< list of postponed entities (sketch constraints or folders)
417   bool myDumpPostponedInProgress; ///< processing postponed is in progress
418
419 protected:
420   /// list of entities, used by other features but not dumped yet
421   std::set<EntityPtr> myNotDumpedEntities;
422
423   friend class SketchAPI_Sketch;
424   friend class ModelHighAPI_Folder;
425 };
426
427 #endif