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